List of usage examples for javax.media.j3d Shape3D Shape3D
public Shape3D(Geometry geometry, Appearance appearance)
From source file:SimpleIndexedQuadSmooth.java
/** * Build a cube from an IndexedQuadArray. This method creates the vertices * as a set of eight points and the normals as a set of six vectors (one for * each face). The data is then defined such that each vertex has a * different normal associated with it when it is being used for a different * face./*from ww w. j a v a2s . c o m*/ * * @return Node that is the shape. */ protected Node buildShape() { //The shape. The constructor specifies 8 vertices, that both //vertices and normals are to be defined and that there are //24 normals to be specified (4 for each of the 6 faces). IndexedQuadArray indexedCube = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24); //The vertex coordinates defined as an array of points. Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f), new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; //The vertex normals defined as an array of vectors Vector3f[] normals = { new Vector3f(1.0f, 1.0f, 1.0f), new Vector3f(-1.0f, 1.0f, 1.0f), new Vector3f(-1.0f, -1.0f, 1.0f), new Vector3f(1.0f, -1.0f, 1.0f), new Vector3f(1.0f, 1.0f, -1.0f), new Vector3f(-1.0f, 1.0f, -1.0f), new Vector3f(-1.0f, -1.0f, -1.0f), new Vector3f(1.0f, -1.0f, -1.0f) }; //Define the indices used to reference vertex array int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; //Define the indices used to reference normal array int normalIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; //Set the data indexedCube.setCoordinates(0, cubeCoordinates); indexedCube.setNormals(0, normals); indexedCube.setCoordinateIndices(0, coordIndices); indexedCube.setNormalIndices(0, normalIndices); //Define an appearance for the shape Appearance app = new Appearance(); Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f); Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f); float shininess = 20.0f; app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Create and return the shape return new Shape3D(indexedCube, app); }
From source file:GeometryByReferenceTest.java
BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); // Set up attributes to render lines app = new Appearance(); transp = new TransparencyAttributes(); transp.setTransparency(0.5f);//w ww .j av a 2s . c o m transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); transp.setTransparencyMode(TransparencyAttributes.NONE); app.setTransparencyAttributes(transp); tetraRegular = createGeometry(1); tetraStrip = createGeometry(2); tetraIndexed = createGeometry(3); tetraIndexedStrip = createGeometry(4); geoArrays[0] = tetraRegular; geoArrays[1] = tetraStrip; geoArrays[2] = tetraIndexed; geoArrays[3] = tetraIndexedStrip; shape = new Shape3D(tetraRegular, app); shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ); Transform3D t = new Transform3D(); // move the object upwards t.set(new Vector3f(0.0f, 0.3f, 0.0f)); // rotate the shape Transform3D temp = new Transform3D(); temp.rotX(Math.PI / 4.0d); t.mul(temp); temp.rotY(Math.PI / 4.0d); t.mul(temp); // Shrink the object t.setScale(0.6); TransformGroup trans = new TransformGroup(t); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(trans); trans.addChild(shape); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Set up the global lights 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(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:TextureTest.java
protected Shape3D createTextureGeometry(String szFile, boolean bWireframe) { // load all the texture data from the file and create the geometry // coordinates TextureGeometryInfo texInfo = createTextureCoordinates(szFile); if (texInfo == null) { System.err.println("Could not load texture info for file:" + szFile); return null; }/*from ww w. ja v a 2s .c om*/ // print some stats on the loaded file System.out.println("Loaded File: " + szFile); System.out.println(" Texture image: " + texInfo.m_szImage); System.out.println(" Texture coordinates: " + texInfo.m_TexCoordArray.length); // create an Appearance and assign a Material Appearance app = new Appearance(); PolygonAttributes polyAttribs = null; // create the PolygonAttributes and attach to the Appearance, // note that we use CULL_NONE so that the "rear" side of the geometry // is visible with the applied texture image if (bWireframe == false) polyAttribs = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0); else polyAttribs = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0); app.setPolygonAttributes(polyAttribs); // load the texture image and assign to the appearance TextureLoader texLoader = new TextureLoader(texInfo.m_szImage, Texture.RGB, this); Texture tex = texLoader.getTexture(); app.setTexture(tex); // create a GeometryInfo for the QuadArray that was populated. GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); gi.setCoordinates(texInfo.m_CoordArray); gi.setTextureCoordinates(texInfo.m_TexCoordArray); // use the triangulator utility to triangulate the polygon int[] stripCountArray = { texInfo.m_CoordArray.length }; int[] countourCountArray = { stripCountArray.length }; gi.setContourCounts(countourCountArray); gi.setStripCounts(stripCountArray); Triangulator triangulator = new Triangulator(); triangulator.triangulate(gi); // generate normal vectors for the triangles, not // strictly necessary as we are not lighting the scene // but generally useful NormalGenerator normalGenerator = new NormalGenerator(); normalGenerator.generateNormals(gi); // wrap the GeometryArray in a Shape3D and assign appearance return new Shape3D(gi.getGeometryArray(), app); }
From source file:SimpleTextureGen.java
/** * Build a cube from an IndexedQuadArray. This method creates the vertices * as a set of eight points and the normals as a set of six vectors (one for * each face). The data is then defined such that each vertex has a * different normal associated with it when it is being used for a different * face. The shape doesn't have texture coordinates or nornmals defined * since the texture coordinate generator will define the necessary data. * //from w w w . j a v a 2 s. c o m * @return Node that is the shape. */ protected Node buildShape() { IndexedQuadArray indexedCube = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES, 24); Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f), new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; int textIndices[] = { 0, 1, 2, 3, 3, 0, 1, 2, 1, 2, 3, 0, 1, 2, 3, 0, 3, 0, 1, 2, 1, 2, 3, 0 }; indexedCube.setCoordinates(0, cubeCoordinates); indexedCube.setCoordinateIndices(0, coordIndices); return new Shape3D(indexedCube, DefineAppearance()); }
From source file:GeometryByReferenceNIOBuffer.java
BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); // Set up attributes to render lines app = new Appearance(); transp = new TransparencyAttributes(); transp.setTransparency(0.5f);/*from www . j a va 2s .c o m*/ transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); transp.setTransparencyMode(TransparencyAttributes.NONE); app.setTransparencyAttributes(transp); //create the direct nio buffer createJ3DBuffers(); tetraRegular = createGeometry(1); tetraStrip = createGeometry(2); tetraIndexed = createGeometry(3); tetraIndexedStrip = createGeometry(4); geoArrays[0] = tetraRegular; geoArrays[1] = tetraStrip; geoArrays[2] = tetraIndexed; geoArrays[3] = tetraIndexedStrip; shape = new Shape3D(tetraRegular, app); shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ); Transform3D t = new Transform3D(); // move the object upwards t.set(new Vector3f(0.0f, 0.3f, 0.0f)); // rotate the shape Transform3D temp = new Transform3D(); temp.rotX(Math.PI / 4.0d); t.mul(temp); temp.rotY(Math.PI / 4.0d); t.mul(temp); // Shrink the object t.setScale(0.6); TransformGroup trans = new TransformGroup(t); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(trans); trans.addChild(shape); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Set up the global lights 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(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:SimpleTexture.java
/** * Build a cube from an IndexedQuadArray. This method creates the vertices * as a set of eight points and the normals as a set of six vectors (one for * each face). The data is then defined such that each vertex has a * different normal associated with it when it is being used for a different * face. The shape is created with texture coordinates so that when the * appearance is set it will use the appearance texture on the surface. * /*from www . ja v a2s .co m*/ * @return Node that is the shape. */ protected Node buildShape() { IndexedQuadArray indexedCube = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS | IndexedQuadArray.TEXTURE_COORDINATE_2, 24); Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f), new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) }; //Define the texture coordinates. These are defined //as floating point pairs of values that are used to //map the corners of the texture image onto the vertices //of the face. We then define the indices into this //array of values in a similar way to that used for //the vertices and normals. TexCoord2f[] textCoord = { new TexCoord2f(1.0f, 1.0f), new TexCoord2f(0.0f, 1.0f), new TexCoord2f(0.0f, 0.0f), new TexCoord2f(1.0f, 0.0f) }; int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; int textIndices[] = { 0, 1, 2, 3, 3, 0, 1, 2, 1, 2, 3, 0, 1, 2, 3, 0, 3, 0, 1, 2, 1, 2, 3, 0 }; indexedCube.setCoordinates(0, cubeCoordinates); indexedCube.setCoordinateIndices(0, coordIndices); indexedCube.setNormals(0, normals); indexedCube.setNormalIndices(0, normalIndices); indexedCube.setTextureCoordinates(0, 0, textCoord); indexedCube.setTextureCoordinateIndices(0, 0, textIndices); return new Shape3D(indexedCube, DefineAppearance()); }
From source file:InterleavedTest.java
BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); // Set up attributes to render lines app = new Appearance(); app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE); transp = new TransparencyAttributes(); transp.setTransparency(0.5f);//from w w w.j a v a 2 s .c o m transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); transp.setTransparencyMode(TransparencyAttributes.NONE); app.setTransparencyAttributes(transp); // load textures TextureAttributes texAttr1 = new TextureAttributes(); texAttr1.setTextureMode(TextureAttributes.DECAL); TextureAttributes texAttr2 = new TextureAttributes(); texAttr2.setTextureMode(TextureAttributes.MODULATE); TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this); if (tex == null) return null; tex1 = tex.getTexture(); tex = new TextureLoader(texImage2, new String("RGB"), this); if (tex == null) return null; tex2 = tex.getTexture(); textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null); textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null); tetraRegular = createGeometry(1); tetraStrip = createGeometry(2); tetraIndexed = createGeometry(3); tetraIndexedStrip = createGeometry(4); geoArrays[0] = tetraRegular; geoArrays[1] = tetraStrip; geoArrays[2] = tetraIndexed; geoArrays[3] = tetraIndexedStrip; shape = new Shape3D(tetraRegular, app); shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); Transform3D t = new Transform3D(); // move the object upwards t.set(new Vector3f(0.0f, 0.3f, 0.0f)); // rotate the shape Transform3D temp = new Transform3D(); temp.rotX(Math.PI / 4.0d); t.mul(temp); temp.rotY(Math.PI / 4.0d); t.mul(temp); // Shrink the object t.setScale(0.6); TransformGroup trans = new TransformGroup(t); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(trans); trans.addChild(shape); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Set up the global lights 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(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:InterleavedNIOBuffer.java
BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); // Set up attributes to render lines app = new Appearance(); app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE); transp = new TransparencyAttributes(); transp.setTransparency(0.5f);//from w ww.ja v a 2 s . co m transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); transp.setTransparencyMode(TransparencyAttributes.NONE); app.setTransparencyAttributes(transp); // load textures TextureAttributes texAttr1 = new TextureAttributes(); texAttr1.setTextureMode(TextureAttributes.DECAL); TextureAttributes texAttr2 = new TextureAttributes(); texAttr2.setTextureMode(TextureAttributes.MODULATE); TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this); if (tex == null) return null; tex1 = tex.getTexture(); tex = new TextureLoader(texImage2, new String("RGB"), this); if (tex == null) return null; tex2 = tex.getTexture(); textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null); textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null); createInterleavedBuffers(); tetraRegular = createGeometry(1); tetraStrip = createGeometry(2); tetraIndexed = createGeometry(3); tetraIndexedStrip = createGeometry(4); geoArrays[0] = tetraRegular; geoArrays[1] = tetraStrip; geoArrays[2] = tetraIndexed; geoArrays[3] = tetraIndexedStrip; shape = new Shape3D(tetraRegular, app); shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); Transform3D t = new Transform3D(); // move the object upwards t.set(new Vector3f(0.0f, 0.3f, 0.0f)); // rotate the shape Transform3D temp = new Transform3D(); temp.rotX(Math.PI / 4.0d); t.mul(temp); temp.rotY(Math.PI / 4.0d); t.mul(temp); // Shrink the object t.setScale(0.6); TransformGroup trans = new TransformGroup(t); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(trans); trans.addChild(shape); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Set up the global lights 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(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:LightTest.java
protected BranchGroup createFloor() { final int LAND_WIDTH = 12; final float LAND_HEIGHT = -4.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]; Color3f[] colorArray = new Color3f[nVertexCount]; // create an Appearance Appearance app = new Appearance(); // create the parent BranchGroup BranchGroup bg = new BranchGroup(); int nItem = 0; Color3f whiteColor = new Color3f(1, 1, 1); Color3f blackColor = new Color3f(0, 0, 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 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); colorArray[nItem++] = blackColor; coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z + nTileSize); colorArray[nItem++] = whiteColor; coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z + nTileSize); colorArray[nItem++] = blackColor; coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z); colorArray[nItem++] = whiteColor; }//from ww w .ja v a2s. c o m } } // create a GeometryInfo and generate Normal vectors // for the QuadArray that was populated. GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); gi.setCoordinates(coordArray); gi.setColors(colorArray); 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); return bg; }
From source file:ImageComponentByReferenceTest.java
void createRaster(BranchGroup scene) { // Create raster geometries and shapes Vector3f trans = new Vector3f(); Transform3D tr = new Transform3D(); TransformGroup tg;/* w ww . j av a 2 s . c om*/ // Left raster = new javax.media.j3d.Raster(); raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE); raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE); raster.setPosition(new Point3f(-0.9f, 0.75f, 0.0f)); raster.setType(javax.media.j3d.Raster.RASTER_COLOR); raster.setOffset(0, 0); raster.setSize(image[2].getWidth(), image[2].getHeight()); raster.setImage(image[2]); Shape3D sh = new Shape3D(raster, new Appearance()); scene.addChild(sh); }