List of usage examples for javax.media.j3d Shape3D Shape3D
public Shape3D(Geometry geometry, Appearance appearance)
From source file:Drag.java
public Cube(Appearance appearance) { QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES | QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2); quadArray.setCoordinates(0, verts);/*from ww w . ja va2 s. c o m*/ quadArray.setNormals(0, normals); quadArray.setTextureCoordinates(0, textCoords); shape3D = new Shape3D(quadArray, appearance); }
From source file:PlatformTest.java
public BranchGroup createSceneGraph() { final int LAND_WIDTH = 12; final float LAND_HEIGHT = -1.0f; final int LAND_LENGTH = 12; final int nTileSize = 2; // calculate how many vertices we need to store all the "tiles" // that compose the QuadArray. final int nNumTiles = ((LAND_LENGTH / nTileSize) * 2) * ((LAND_WIDTH / nTileSize) * 2); final int nVertexCount = 4 * nNumTiles; Point3f[] coordArray = new Point3f[nVertexCount]; Point2f[] texCoordArray = new Point2f[nVertexCount]; // create an Appearance and load a texture Appearance app = new Appearance(); Texture tex = new TextureLoader("land.jpg", this).getTexture(); app.setTexture(tex);/*from w w w . j a v a 2 s .c om*/ // create the parent BranchGroup BranchGroup bg = new BranchGroup(); int nItem = 0; // loop over all the tiles in the environment for (int x = -LAND_WIDTH; x <= LAND_WIDTH; x += nTileSize) { for (int z = -LAND_LENGTH; z <= LAND_LENGTH; z += nTileSize) { // if we are on the border of the environment create a // TransformGroup to position a ColorCube to create a "wall" if (x == -LAND_WIDTH || x == LAND_WIDTH || z == -LAND_LENGTH || z == LAND_LENGTH) { TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(x, 0, z)); tg.setTransform(t3d); tg.addChild(new ColorCube(nTileSize / 2)); bg.addChild(tg); } // if we are not on the last row or column create a "tile" // and add to the QuadArray. Use CCW winding and assign texture // coordinates. if (z < LAND_LENGTH && x < LAND_WIDTH) { coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z); texCoordArray[nItem++] = new Point2f(0, 0); coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z + nTileSize); texCoordArray[nItem++] = new Point2f(1, 0); coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z + nTileSize); texCoordArray[nItem++] = new Point2f(1, 1); coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z); texCoordArray[nItem++] = new Point2f(0, 1); } } } // create a GeometryInfo and generate Normal vectors // for the QuadArray that was populated. GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); gi.setCoordinates(coordArray); gi.setTextureCoordinates(texCoordArray); NormalGenerator normalGenerator = new NormalGenerator(); normalGenerator.generateNormals(gi); // wrap the GeometryArray in a Shape3D Shape3D shape = new Shape3D(gi.getGeometryArray(), app); // add the Shape3D to the parent BranchGroup bg.addChild(shape); // create some lights for the scene Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(m_Bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(m_Bounds); // add the lights to the parent BranchGroup bg.addChild(aLgt); bg.addChild(lgt1); // create a light gray background Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f)); back.setApplicationBounds(m_Bounds); bg.addChild(back); // compile the whole scene //bg.compile(); return bg; }
From source file:ExDepthCue.java
public Shape3D buildSurface(double freqAlpha, double freqTheta, double radius, float red, float green, float blue) { int nAngles = 64; double amp = radius / 4.0; int nAlpha = nAngles / 2; double theta, alpha; double x, y, z, rprime, r; double deltaTheta, deltaAlpha; int i, j;/* w ww .j a va 2 s . c o m*/ int i1, i2, i3, i4; deltaTheta = 360.0 / (nAngles - 1.0); deltaAlpha = 180.0 / (nAlpha - 1.0); // Build an appearance Appearance app = new Appearance(); LineAttributes latt = new LineAttributes(); latt.setLineWidth(1.0f); app.setLineAttributes(latt); ColoringAttributes catt = new ColoringAttributes(); catt.setColor(red, green, blue); app.setColoringAttributes(catt); PolygonAttributes patt = new PolygonAttributes(); patt.setCullFace(PolygonAttributes.CULL_NONE); patt.setPolygonMode(PolygonAttributes.POLYGON_LINE); app.setPolygonAttributes(patt); // Compute coordinates double[] coordinates = new double[nAlpha * nAngles * 3]; alpha = 90.0; int n = 0; for (i = 0; i < nAlpha; i++) { theta = 0.0; for (j = 0; j < nAngles; j++) { r = radius + amp * Math.sin((freqAlpha * ((double) i / (double) (nAlpha - 1)) + freqTheta * ((double) j / (double) (nAngles - 1))) * 2.0 * Math.PI); y = r * Math.sin(alpha / 180.0 * Math.PI); rprime = y / Math.tan(alpha / 180.0 * Math.PI); x = rprime * Math.cos(theta / 180.0 * Math.PI); z = rprime * Math.sin(theta / 180.0 * Math.PI); coordinates[n + 0] = x; coordinates[n + 1] = y; coordinates[n + 2] = z; n += 3; theta += deltaTheta; } alpha -= deltaAlpha; } // Compute coordinate indexes int[] indexes = new int[(nAlpha - 1) * nAngles * 4]; n = 0; for (i = 0; i < nAlpha - 1; i++) { for (j = 0; j < nAngles; j++) { i1 = i * nAngles + j; if (j == nAngles - 1) { i2 = i1 - j; i3 = (i + 1) * nAngles; } else { i2 = i1 + 1; i3 = (i + 1) * nAngles + j + 1; } i4 = (i + 1) * nAngles + j; indexes[n + 0] = i1; indexes[n + 1] = i2; indexes[n + 2] = i3; indexes[n + 3] = i4; n += 4; } } // Build the shape IndexedQuadArray lines = new IndexedQuadArray(coordinates.length / 3, // Number // of // coordinates GeometryArray.COORDINATES, // coordinates only indexes.length); // Number of indexes lines.setCoordinates(0, coordinates); lines.setCoordinateIndices(0, indexes); Shape3D shape = new Shape3D(lines, app); return shape; }
From source file:PickTest.java
private Group createObject(int index, double scale, double xpos, double ypos) { Shape3D shape = null;//from w w w . j a v a2 s .c o m Geometry geom = null; // Create a transform group node to scale and position the object. Transform3D t = new Transform3D(); t.set(scale, new Vector3d(xpos, ypos, 0.0)); TransformGroup objTrans = new TransformGroup(t); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); // Create a second transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. TransformGroup spinTg = new TransformGroup(); spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING); Appearance appearance = new Appearance(); switch (index) { case 0: geom = new GullCG(); break; case 1: geom = new TetrahedronTA(); break; case 2: geom = new OctahedronTFA(); break; case 3: geom = new IcosahedronTSA(); break; case 4: geom = new CubeIQA(); break; case 5: geom = new TetrahedronITA(); break; case 6: geom = new OctahedronITFA(); break; case 7: geom = new IcosahedronITSA(); break; case 8: geomMorph[0] = new ColorPyramidUp(); geomMorph[1] = new ColorCube(); geomMorph[2] = new ColorPyramidDown(); break; case 9: geom = new TetrahedronLA(); break; case 10: geom = new TetrahedronILA(); break; case 11: geom = new TetrahedronLSA(); break; case 12: geom = new TetrahedronILSA(); break; case 13: geom = new TetrahedronPA(); break; case 14: geom = new TetrahedronIPA(); break; // TODO: other geo types, Text3D? case 15: geom = new TetrahedronTA(); break; } Material m = new Material(); if (index == 8) { m.setLightingEnable(false); appearance.setMaterial(m); morph = new Morph((GeometryArray[]) geomMorph, appearance); morph.setCapability(Morph.ALLOW_WEIGHTS_READ); morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE); PickTool.setCapabilities(morph, PickTool.INTERSECT_FULL); spinTg.addChild(morph); } else { // Geometry picking require this to be set. if (index == 0) m.setLightingEnable(true); else m.setLightingEnable(false); appearance.setMaterial(m); if ((index == 13) || (index == 14)) { PointAttributes pa = new PointAttributes(); pa.setPointSize(4.0f); appearance.setPointAttributes(pa); } shape = new Shape3D(geom, appearance); shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ); shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); shape.setCapability(Shape3D.ENABLE_PICK_REPORTING); PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL); spinTg.addChild(shape); } // add it to the scene graph. objTrans.addChild(spinTg); return objTrans; }
From source file:AvatarTest.java
protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile, String szSoundFile) {//w w w . j a va 2 s . co m QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2); float[] coordArray = { -LAND_WIDTH, LAND_HEIGHT, 0, LAND_WIDTH, LAND_HEIGHT, 0, LAND_WIDTH, LAND_HEIGHT, LAND_LENGTH, -LAND_WIDTH, LAND_HEIGHT, LAND_LENGTH }; float[] texArray = { 0, 0, 1, 0, 1, 1, 0, 1 }; quadArray.setCoordinates(0, coordArray, 0, 4); if ((m_nFlags & TEXTURE) == TEXTURE) { quadArray.setTextureCoordinates(0, 0, texArray, 0, 4); setTexture(app, szTextureFile); } Shape3D sh = new Shape3D(quadArray, app); BranchGroup bg = new BranchGroup(); bg.addChild(sh); return bg; }
From source file:ViewProj.java
public BranchGroup createVWorldViewSG() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); objRoot.setCapability(BranchGroup.ALLOW_DETACH); // setup a transform group to hold the scaled scene TransformGroup objTrans = new TransformGroup(); objRoot.addChild(objTrans);/*from www .j a v a2 s . c o m*/ // get the eye point, field of view and clip distances float fov = (float) view.getFieldOfView(); // figure out the angle factors to find points along the edges // of the FOV // X = fovSpreadX * (Y - eyeVW.y) + eyeVW.x; float fovSpreadX = (float) Math.tan(fov / 2); // Z = fovSpreadZ * (X - eyeVW.x) + eyeVW.z; float fovSpreadZ = 1.0f / fovSpreadX; //System.out.println("fovSpreadX = " + fovSpreadX); //System.out.println("fovSpreadZ = " + fovSpreadZ); Transform3D vpTransform = new Transform3D(); viewingPlatform.getViewPlatformTransform().getTransform(vpTransform); Vector3f vpTranslation = new Vector3f(); vpTransform.get(vpTranslation); eyePtVW.set(vpTranslation); eyePtVW.negate(); // get the eye point in our 2D coord system. Point3f eyePt = new Point3f(0.0f, eyePtVW.z, 0.1f); float frontClipDist = (float) view.getFrontClipDistance(); float backClipDist = (float) view.getBackClipDistance(); // set up the clip plane lines Point3f[] cpPoints = new Point3f[5]; cpPoints[0] = new Point3f(frontClipDist * fovSpreadX, eyePtVW.z + frontClipDist, 0.1f); cpPoints[1] = new Point3f(cpPoints[0]); cpPoints[1].x *= -1; Point3f backLeft = new Point3f(-backClipDist * fovSpreadX, eyePtVW.z + backClipDist, 0.1f); cpPoints[2] = backLeft; Point3f backRight = new Point3f(backLeft); backRight.x *= -1; cpPoints[3] = backRight; cpPoints[4] = cpPoints[0]; //for (int i = 0; i < 4; i++) { // System.out.println("cpPoints[" + i + "] = " + cpPoints[i]); //} int[] cpLength = new int[1]; cpLength[0] = 5; LineStripArray cpLines = new LineStripArray(5, LineArray.COORDINATES, cpLength); cpLines.setCoordinates(0, cpPoints); Appearance cpApp = new Appearance(); ColoringAttributes cpCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT); cpApp.setColoringAttributes(cpCa); Shape3D cpShape = new Shape3D(cpLines, cpApp); objTrans.addChild(cpShape); // get the limits of the space float minY = eyePt.y; float maxY = backLeft.y; float minX = backLeft.x; float maxX = backRight.x; // figure out the X and Y extents and offsets float deltaX = maxX - minX; float deltaY = maxY - minY; float offsetX = -(maxX + minX) / 2.0f; float offsetY = -(maxY + minY) / 2.0f; float gridSize = Math.max(deltaX, deltaY); // scale the grid slightly to give a border around the edge gridSize *= 1.1f; //System.out.println("offsetX = " + offsetX); //System.out.println("offsetY = " + offsetY); // Scale the view to fit -1 to 1 Transform3D trans = new Transform3D(); trans.set(new Vector3f(offsetX, offsetY, 0.0f), 2.0f / gridSize); objTrans.setTransform(trans); // figure out a grid step that is a multiple of 10 which keeps the // number of steps less than 30. float gridStep = 1.0f; while ((gridSize / gridStep) > 30.0) { gridStep *= 10; } int gridNumSteps = (int) Math.ceil(gridSize / gridStep) + 1; // allocate the grid points array, four points for each step (x and y) // with a couple extra points for the extra grid points added // below int gridNumPoints = 4 * (gridNumSteps + 4); Point3f[] gridPts = new Point3f[gridNumPoints]; for (int i = 0; i < gridNumPoints; i++) { gridPts[i] = new Point3f(); } // find the grid limits. Add a step on each side to make sure // the grid is larger than the view float gridMinY = gridStepFloor(minY, gridStep) - gridStep; float gridMaxY = gridStepCeil(maxY, gridStep) + gridStep; float gridMinX = gridStepFloor(minX, gridStep) - gridStep; float gridMaxX = gridStepCeil(maxX, gridStep) + gridStep; //System.out.println("gridMinY = " + gridMinY); //System.out.println("gridMaxY = " + gridMaxY); //System.out.println("gridMinX = " + gridMinX); //System.out.println("gridMaxX = " + gridMaxX); // set up the background grid Appearance bgApp = new Appearance(); ColoringAttributes bgCa = new ColoringAttributes(); bgCa.setColor(grey); LineAttributes bgLa = new LineAttributes(); bgApp.setColoringAttributes(bgCa); // clear out the clip grid point list numClipGridPts = 0; // set up the vertical lines int numPts = 0; for (float x = gridMinX; x <= gridMaxX; x += gridStep) { gridPts[numPts].x = x; gridPts[numPts].y = gridMinY; gridPts[numPts].z = -0.2f; gridPts[numPts + 1].x = x; gridPts[numPts + 1].y = gridMaxY; gridPts[numPts + 1].z = -0.2f; numPts += 2; // try to add a line to the clipped grid // find the intersection of the clipped line with the FOV sides // this is a distance relative to the eye float clipZ = fovSpreadZ * Math.abs(x - eyePtVW.x); if (clipZ < frontClipDist) { // clip to front clip plane clipZ = frontClipDist; } if (clipZ < backClipDist) { // clip to back clip plane // line is not clipped clipGridPtsVW[numClipGridPts].x = x; clipGridPtsVW[numClipGridPts].y = clipZ + eyePtVW.z; clipGridPtsVW[numClipGridPts].z = -0.1f; clipGridPtsVW[numClipGridPts + 1].x = x; clipGridPtsVW[numClipGridPts + 1].y = backClipDist + eyePtVW.z; clipGridPtsVW[numClipGridPts + 1].z = -0.1f; numClipGridPts += 2; } } LineArray vertLa = new LineArray(numPts, LineArray.COORDINATES); vertLa.setCoordinates(0, gridPts, 0, numPts); Shape3D vertShape = new Shape3D(vertLa, bgApp); objTrans.addChild(vertShape); // set up the horizontal lines numPts = 0; for (float y = gridMinY; y <= gridMaxY; y += gridStep) { gridPts[numPts].x = gridMinX; gridPts[numPts].y = y; gridPts[numPts++].z = -0.2f; gridPts[numPts].x = gridMaxX; gridPts[numPts].y = y; gridPts[numPts++].z = -0.2f; // try to add a line to the clipped grid // find the intersection of the clipped line with the FOV sides // this is a distance relative to the eye float clipDist = (y - eyePtVW.z); if ((clipDist > frontClipDist) && (clipDist < backClipDist)) { float clipX = fovSpreadX * clipDist; clipGridPtsVW[numClipGridPts].x = -clipX; clipGridPtsVW[numClipGridPts].y = y; clipGridPtsVW[numClipGridPts].z = -0.1f; clipGridPtsVW[numClipGridPts + 1].x = clipX; clipGridPtsVW[numClipGridPts + 1].y = y; clipGridPtsVW[numClipGridPts + 1].z = -0.1f; numClipGridPts += 2; } } LineArray horizLa = new LineArray(numPts, LineArray.COORDINATES); horizLa.setCoordinates(0, gridPts, 0, numPts); Shape3D horizShape = new Shape3D(horizLa, bgApp); objTrans.addChild(horizShape); // draw the clipped grid. if (numClipGridPts > 0) { LineArray clipLa = new LineArray(numClipGridPts, LineArray.COORDINATES); clipLa.setCoordinates(0, clipGridPtsVW, 0, numClipGridPts); Appearance clipGridApp = new Appearance(); ColoringAttributes clipCa = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT); clipGridApp.setColoringAttributes(clipCa); LineAttributes clipGridLa = new LineAttributes(); Shape3D clipShape = new Shape3D(clipLa, clipGridApp); objTrans.addChild(clipShape); } // set up the coordinate system Appearance coordSysApp = new Appearance(); LineAttributes coordSysLa = new LineAttributes(); coordSysLa.setLineWidth(3.0f); coordSysApp.setLineAttributes(coordSysLa); ColoringAttributes coordSysCa = new ColoringAttributes(grey, ColoringAttributes.SHADE_FLAT); coordSysApp.setColoringAttributes(coordSysCa); Point3f[] coordSysPts = new Point3f[4]; coordSysPts[0] = new Point3f(gridMinX, 0, -0.5f); coordSysPts[1] = new Point3f(gridMaxX, 0, -0.5f); coordSysPts[2] = new Point3f(0, gridMinY, -0.5f); coordSysPts[3] = new Point3f(0, gridMaxY, -0.5f); LineArray coordSysLines = new LineArray(4, LineArray.COORDINATES); coordSysLines.setCoordinates(0, coordSysPts); Shape3D coordSysShape = new Shape3D(coordSysLines, coordSysApp); objTrans.addChild(coordSysShape); // set up the circle Appearance circleApp = new Appearance(); ColoringAttributes circleCa = new ColoringAttributes(); circleCa.setColor(red); circleApp.setColoringAttributes(circleCa); PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace(PolygonAttributes.CULL_NONE); circleApp.setPolygonAttributes(pa); int step = 360 / (numCirclePts - 1); for (int deg = 0; deg < 360; deg += step) { double angle = Math.toRadians(deg); circlePtsVW[deg / 10].x = sphereRadius * (float) Math.sin(angle); circlePtsVW[deg / 10].y = sphereRadius * (float) Math.cos(angle); circlePtsVW[deg / 10].z = -0.3f; } circlePtsVW[numCirclePts - 1].set(circlePtsVW[0]); int[] lineStripLength = new int[1]; lineStripLength[0] = numCirclePts; //LineStripArray circleLineStrip = new LineStripArray(numCirclePts, // LineArray.COORDINATES, lineStripLength); TriangleFanArray circleLineStrip = new TriangleFanArray(numCirclePts, LineArray.COORDINATES, lineStripLength); circleLineStrip.setCoordinates(0, circlePtsVW); Shape3D circleShape = new Shape3D(circleLineStrip, circleApp); objTrans.addChild(circleShape); return objRoot; }
From source file:GearTest.java
void addBodyDisks(float shaftRadius, float bodyOuterRadius, float thickness, Appearance look) { int gearBodySegmentVertexCount; // #(segments) per tooth-unit int gearBodyTotalVertexCount; // #(vertices) in a gear face int gearBodyStripCount[] = new int[1]; // per strip (1) vertex count // A ray from the gear center, used in normal calculations float xDirection, yDirection; // The x and y coordinates at each point of a facet and at each // point on the gear: at the shaft, the root of the teeth, and // the outer point of the teeth float xRoot0, yRoot0, xShaft0, yShaft0; float xRoot3, yRoot3, xShaft3, yShaft3; float xRoot4, yRoot4, xShaft4, yShaft4; // Temporary variables for storing coordinates and vectors Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f); // Gear start differential angle. All gears are constructed with the // center of a tooth at Z-axis angle = 0. double gearStartAngle = -1.0 * toothTopCenterAngle; // Temporaries that store start angle for each portion of tooth facet double toothStartAngle, toothTopStartAngle, toothDeclineStartAngle, toothValleyStartAngle, nextToothStartAngle;// w ww . ja v a 2 s. c o m Shape3D newShape; int index; // The z coordinates for the body disks final float frontZ = -0.5f * thickness; final float rearZ = 0.5f * thickness; /* * Construct the gear's front body (front facing torus disk) __2__ - | - * 4 - /| /- / / | /| \ 0\ / | / / > \ / | / | > \ / | / / | \ / ____|/ | > * \-- --__/ | 1 3 5 * */ gearBodySegmentVertexCount = 4; gearBodyTotalVertexCount = 2 + gearBodySegmentVertexCount * toothCount; gearBodyStripCount[0] = gearBodyTotalVertexCount; TriangleStripArray frontGearBody = new TriangleStripArray(gearBodyTotalVertexCount, GeometryArray.COORDINATES | GeometryArray.NORMALS, gearBodyStripCount); xDirection = (float) Math.cos(gearStartAngle); yDirection = (float) Math.sin(gearStartAngle); xShaft0 = shaftRadius * xDirection; yShaft0 = shaftRadius * yDirection; xRoot0 = bodyOuterRadius * xDirection; yRoot0 = bodyOuterRadius * yDirection; coordinate.set(xRoot0, yRoot0, frontZ); frontGearBody.setCoordinate(0, coordinate); frontGearBody.setNormal(0, frontNormal); coordinate.set(xShaft0, yShaft0, frontZ); frontGearBody.setCoordinate(1, coordinate); frontGearBody.setNormal(1, frontNormal); for (int count = 0; count < toothCount; count++) { index = 2 + count * 4; toothStartAngle = gearStartAngle + circularPitchAngle * (double) count; toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement; nextToothStartAngle = toothStartAngle + circularPitchAngle; xDirection = (float) Math.cos(toothValleyStartAngle); yDirection = (float) Math.sin(toothValleyStartAngle); xShaft3 = shaftRadius * xDirection; yShaft3 = shaftRadius * yDirection; xRoot3 = bodyOuterRadius * xDirection; yRoot3 = bodyOuterRadius * yDirection; xDirection = (float) Math.cos(nextToothStartAngle); yDirection = (float) Math.sin(nextToothStartAngle); xShaft4 = shaftRadius * xDirection; yShaft4 = shaftRadius * yDirection; xRoot4 = bodyOuterRadius * xDirection; yRoot4 = bodyOuterRadius * yDirection; coordinate.set(xRoot3, yRoot3, frontZ); frontGearBody.setCoordinate(index, coordinate); frontGearBody.setNormal(index, frontNormal); coordinate.set(xShaft3, yShaft3, frontZ); frontGearBody.setCoordinate(index + 1, coordinate); frontGearBody.setNormal(index + 1, frontNormal); coordinate.set(xRoot4, yRoot4, frontZ); frontGearBody.setCoordinate(index + 2, coordinate); frontGearBody.setNormal(index + 2, frontNormal); coordinate.set(xShaft4, yShaft4, frontZ); frontGearBody.setCoordinate(index + 3, coordinate); frontGearBody.setNormal(index + 3, frontNormal); } newShape = new Shape3D(frontGearBody, look); this.addChild(newShape); // Construct the gear's rear body (rear facing torus disc) TriangleStripArray rearGearBody = new TriangleStripArray(gearBodyTotalVertexCount, GeometryArray.COORDINATES | GeometryArray.NORMALS, gearBodyStripCount); xDirection = (float) Math.cos(gearStartAngle); yDirection = (float) Math.sin(gearStartAngle); xShaft0 = shaftRadius * xDirection; yShaft0 = shaftRadius * yDirection; xRoot0 = bodyOuterRadius * xDirection; yRoot0 = bodyOuterRadius * yDirection; coordinate.set(xShaft0, yShaft0, rearZ); rearGearBody.setCoordinate(0, coordinate); rearGearBody.setNormal(0, rearNormal); coordinate.set(xRoot0, yRoot0, rearZ); rearGearBody.setCoordinate(1, coordinate); rearGearBody.setNormal(1, rearNormal); for (int count = 0; count < toothCount; count++) { index = 2 + count * 4; toothStartAngle = gearStartAngle + circularPitchAngle * (double) count; toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement; nextToothStartAngle = toothStartAngle + circularPitchAngle; xDirection = (float) Math.cos(toothValleyStartAngle); yDirection = (float) Math.sin(toothValleyStartAngle); xShaft3 = shaftRadius * xDirection; yShaft3 = shaftRadius * yDirection; xRoot3 = bodyOuterRadius * xDirection; yRoot3 = bodyOuterRadius * yDirection; xDirection = (float) Math.cos(nextToothStartAngle); yDirection = (float) Math.sin(nextToothStartAngle); xShaft4 = shaftRadius * xDirection; yShaft4 = shaftRadius * yDirection; xRoot4 = bodyOuterRadius * xDirection; yRoot4 = bodyOuterRadius * yDirection; coordinate.set(xShaft3, yShaft3, rearZ); rearGearBody.setCoordinate(index, coordinate); rearGearBody.setNormal(index, rearNormal); coordinate.set(xRoot3, yRoot3, rearZ); rearGearBody.setCoordinate(index + 1, coordinate); rearGearBody.setNormal(index + 1, rearNormal); coordinate.set(xShaft4, yShaft4, rearZ); rearGearBody.setCoordinate(index + 2, coordinate); rearGearBody.setNormal(index + 2, rearNormal); coordinate.set(xRoot4, yRoot4, rearZ); rearGearBody.setCoordinate(index + 3, coordinate); rearGearBody.setNormal(index + 3, rearNormal); } newShape = new Shape3D(rearGearBody, look); this.addChild(newShape); }
From source file:TexCoordTest.java
BranchGroup createDemLandscape() { final double LAND_WIDTH = 200; final double LAND_LENGTH = 200; final double nTileSize = 10; final double yMaxHeight = LAND_WIDTH / 8; // calculate how many vertices we need to store all the "tiles" that // compose the QuadArray. final int nNumTiles = (int) (((LAND_LENGTH / nTileSize) * 2) * ((LAND_WIDTH / nTileSize) * 2)); final int nVertexCount = 4 * nNumTiles; Point3f[] coordArray = new Point3f[nVertexCount]; Point2f[] texCoordArray = new Point2f[nVertexCount]; // create the Appearance for the landscape and initialize all // the texture coordinate generation parameters createAppearance(yMaxHeight);/*from ww w . j a va 2 s . c o m*/ // create the parent BranchGroup BranchGroup bg = new BranchGroup(); // create the geometry and populate a QuadArray // we use a simple sin/cos function to create an undulating surface // in the X/Z dimensions. Y dimension is the distance "above sea-level". int nItem = 0; double yValue0 = 0; double yValue1 = 0; double yValue2 = 0; double yValue3 = 0; final double xFactor = LAND_WIDTH / 5; final double zFactor = LAND_LENGTH / 3; // loop over all the tiles in the environment for (double x = -LAND_WIDTH; x <= LAND_WIDTH; x += nTileSize) { for (double z = -LAND_LENGTH; z <= LAND_LENGTH; z += nTileSize) { // if we are not on the last row or column create a "tile" // and add to the QuadArray. Use CCW winding if (z < LAND_LENGTH && x < LAND_WIDTH) { yValue0 = yMaxHeight * Math.sin(x / xFactor) * Math.cos(z / zFactor); yValue1 = yMaxHeight * Math.sin(x / xFactor) * Math.cos((z + nTileSize) / zFactor); yValue2 = yMaxHeight * Math.sin((x + nTileSize) / xFactor) * Math.cos((z + nTileSize) / zFactor); yValue3 = yMaxHeight * Math.sin((x + nTileSize) / xFactor) * Math.cos(z / zFactor); // note, we do not assign any texture coordinates! coordArray[nItem++] = new Point3f((float) x, (float) yValue0, (float) z); coordArray[nItem++] = new Point3f((float) x, (float) yValue1, (float) (z + nTileSize)); coordArray[nItem++] = new Point3f((float) (x + nTileSize), (float) yValue2, (float) (z + nTileSize)); coordArray[nItem++] = new Point3f((float) (x + nTileSize), (float) yValue3, (float) z); } } } // create a GeometryInfo and assign the coordinates GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); gi.setCoordinates(coordArray); // generate Normal vectors for the QuadArray that was populated. NormalGenerator normalGenerator = new NormalGenerator(); normalGenerator.generateNormals(gi); // wrap the GeometryArray in a Shape3D Shape3D shape = new Shape3D(gi.getGeometryArray(), m_Appearance); // add the Shape3D to a BranchGroup and return bg.addChild(shape); return bg; }
From source file:ffx.potential.MolecularAssembly.java
/** * <p>/*from w w w . ja v a2 s . c o m*/ * createBox</p> */ public void createBox() { int vertices = 8; LineArray la = new LineArray(4 * vertices, GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS); la.setCapability(LineArray.ALLOW_COORDINATE_WRITE); la.setCapability(LineArray.ALLOW_COORDINATE_READ); la.setCapability(LineArray.ALLOW_COLOR_WRITE); la.setCapability(LineArray.ALLOW_COUNT_READ); la.setCapability(LineArray.ALLOW_INTERSECT); la.setCapability(LineArray.ALLOW_FORMAT_READ); // Create a normal // for (ListIterator li = bondlist.listIterator(); li.hasNext(); ){ // la.setCoordinate(i, a1); // la.setColor(i, col); // la.setNormal(i++, a1); // } ColoringAttributes cola = new ColoringAttributes(new Color3f(), ColoringAttributes.SHADE_GOURAUD); Appearance app = new Appearance(); lineAttributes = new LineAttributes(); lineAttributes.setLineWidth(RendererCache.bondwidth); lineAttributes.setCapability(LineAttributes.ALLOW_WIDTH_WRITE); lineAttributes.setLineAntialiasingEnable(true); app.setLineAttributes(lineAttributes); app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ); app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE); RenderingAttributes ra = new RenderingAttributes(); ra.setAlphaTestValue(0.1f); ra.setAlphaTestFunction(RenderingAttributes.GREATER); ra.setDepthBufferEnable(true); ra.setDepthBufferWriteEnable(true); app.setRenderingAttributes(ra); app.setColoringAttributes(cola); Shape3D wireframe = new Shape3D(la, app); // PickTool.setCapabilities(wire, PickTool.INTERSECT_COORD); wireframe.setUserData(this); wireframe.setBounds(new BoundingSphere(new Point3d(0, 0, 0), 10.0)); try { wireframe.setBoundsAutoCompute(false); } catch (Exception e) { e.printStackTrace(); } wireframe.setCapability(Shape3D.ALLOW_GEOMETRY_READ); wireframe.setCapability(Shape3D.ALLOW_APPEARANCE_READ); // return wire; }
From source file:ExSound.java
private Shape3D buildBox(float width, float height, float depth, Appearance app) { float w2 = width / 2.0f; float h2 = height / 2.0f; float d2 = depth / 2.0f; float[] coordinates = new float[8 * 3]; int n = 0;//from w w w.j a va2 s. c o m // Around the bottom of the box coordinates[n + 0] = -w2; coordinates[n + 1] = -h2; coordinates[n + 2] = d2; n += 3; coordinates[n + 0] = w2; coordinates[n + 1] = -h2; coordinates[n + 2] = d2; n += 3; coordinates[n + 0] = w2; coordinates[n + 1] = -h2; coordinates[n + 2] = -d2; n += 3; coordinates[n + 0] = -w2; coordinates[n + 1] = -h2; coordinates[n + 2] = -d2; n += 3; // Around the top of the box coordinates[n + 0] = -w2; coordinates[n + 1] = h2; coordinates[n + 2] = d2; n += 3; coordinates[n + 0] = w2; coordinates[n + 1] = h2; coordinates[n + 2] = d2; n += 3; coordinates[n + 0] = w2; coordinates[n + 1] = h2; coordinates[n + 2] = -d2; n += 3; coordinates[n + 0] = -w2; coordinates[n + 1] = h2; coordinates[n + 2] = -d2; n += 3; IndexedQuadArray quads = new IndexedQuadArray(coordinates.length, // vertex // count GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2, coordinateIndices.length); quads.setCoordinates(0, coordinates); quads.setCoordinateIndices(0, coordinateIndices); quads.setNormals(0, normals); quads.setNormalIndices(0, normalIndices); quads.setTextureCoordinates(0, textureCoordinates); quads.setTextureCoordinateIndices(0, textureCoordinateIndices); Shape3D shape = new Shape3D(quads, app); return shape; }