List of usage examples for javax.media.j3d Appearance setTextureAttributes
public void setTextureAttributes(TextureAttributes textureAttributes)
From source file:PictureBall.java
public PictureBall() { // Create the universe SimpleUniverse universe = new SimpleUniverse(); // Create a structure to contain objects BranchGroup group = new BranchGroup(); // Set up colors Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Color3f red = new Color3f(0.7f, .15f, .15f); // Set up the texture map TextureLoader loader = new TextureLoader("K:\\3d\\Arizona.jpg", "LUMINANCE", new Container()); Texture texture = loader.getTexture(); texture.setBoundaryModeS(Texture.WRAP); texture.setBoundaryModeT(Texture.WRAP); texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f)); // Set up the texture attributes //could be REPLACE, BLEND or DECAL instead of MODULATE TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); Appearance ap = new Appearance(); ap.setTexture(texture);//from w ww .j a v a 2 s .c om ap.setTextureAttributes(texAttr); //set up the material ap.setMaterial(new Material(red, black, red, black, 1.0f)); // Create a ball to demonstrate textures int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS; Sphere sphere = new Sphere(0.5f, primflags, ap); group.addChild(sphere); // Create lights Color3f light1Color = new Color3f(1f, 1f, 1f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); group.addChild(light1); AmbientLight ambientLight = new AmbientLight(new Color3f(.5f, .5f, .5f)); ambientLight.setInfluencingBounds(bounds); group.addChild(ambientLight); // look towards the ball universe.getViewingPlatform().setNominalViewingTransform(); // add the group of objects to the Universe universe.addBranchGraph(group); }
From source file:TextureImage.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. Add it to the // root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans);//from www .j a v a2 s .c om // Create appearance object for textured cube Appearance app = new Appearance(); Texture tex = new TextureLoader(texImage, this).getTexture(); app.setTexture(tex); TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); app.setTextureAttributes(texAttr); // Create textured cube and add it to the scene graph. Box textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS, app); objTrans.addChild(textureCube); // Create a new Behavior object that will perform the desired // operation on the specified transform object and add it into // the scene graph. Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:SimpleTexture.java
/** * This defines the appearance with a texture. The texture is loaded from an * external file./*from w ww . java 2 s .c o m*/ * * @return Appearance that uses the texture. */ protected Appearance DefineAppearance() { //Load the texture from the external image file TextureLoader textLoad = new TextureLoader("housebrick.jpg", this); //Access the image from the loaded texture ImageComponent2D textImage = textLoad.getImage(); //Create a two dimensional texture Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB, textImage.getWidth(), textImage.getHeight()); //Set the texture from the image loaded texture.setImage(0, textImage); //Create the appearance that will use the texture Appearance app = new Appearance(); app.setTexture(texture); //Define how the texture will be mapped onto the surface //by creating the appropriate texture attributes TextureAttributes textAttr = new TextureAttributes(); textAttr.setTextureMode(TextureAttributes.REPLACE); app.setTextureAttributes(textAttr); app.setMaterial(new Material()); return app; }
From source file:SimpleTextureGen.java
/** * This defines the appearance for the shape using a texture. It uses a * TextureLoader to load the texture image from an external file and a * TexCoordGeneration to define the texture coordinates. * /*from ww w .jav a2s. c o m*/ * @return Appearance that uses a texture. */ protected Appearance DefineAppearance() { //This is used to automatically define the texture coordinates. //The coordinates are generated in object coordinates, but by //commented out the line with 'OBJECT_LINEAR' and uncommenting //the line 'EYE_LINEAR' the program will use eye coordinates. TexCoordGeneration textCoorder = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, //TexCoordGeneration.EYE_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2); //Load the texture from the external image file TextureLoader textLoad = new TextureLoader("housebrick.jpg", this); //Access the image from the loaded texture ImageComponent2D textImage = textLoad.getImage(); //Create a two dimensional texture Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB, textImage.getWidth(), textImage.getHeight()); //Set the texture from the image loaded texture.setImage(0, textImage); //Create the appearance that will use the texture Appearance app = new Appearance(); app.setTexture(texture); //Pass the coordinate generator to the appearance app.setTexCoordGeneration(textCoorder); //Define how the texture will be mapped onto the surface //by creating the appropriate texture attributes TextureAttributes textAttr = new TextureAttributes(); textAttr.setTextureMode(TextureAttributes.REPLACE); app.setTextureAttributes(textAttr); app.setMaterial(new Material()); return app; }
From source file:BooksDemo.java
private Appearance createTexture(String fileName) { Image sourceImage = UIHelper.readImage(fileName); if (sourceImage == null) System.out.println("Image could not be loaded from " + fileName); TextureLoader loader = new TextureLoader(sourceImage, this); ImageComponent2D image = loader.getImage(); if (image == null) System.out.println("Texture could not be loaded from " + fileName); Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight()); texture.setImage(0, image);/* w ww . j a v a 2 s. c o m*/ texture.setEnable(true); texture.setMagFilter(Texture.BASE_LEVEL_LINEAR); texture.setMinFilter(Texture.BASE_LEVEL_LINEAR); Appearance appearance = new Appearance(); PolygonAttributes polyAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0f); appearance.setPolygonAttributes(polyAttributes); appearance.setTexture(texture); TextureAttributes textureAttributes = new TextureAttributes(); appearance.setTextureAttributes(textureAttributes); return appearance; }
From source file:TextureTransformTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); // create the rotation interpolator to rotate the scene RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f); rotator.setSchedulingBounds(createApplicationBounds()); objTrans.addChild(rotator);//from w ww . ja va 2 s . co m // create the box final int nScale = 50; Appearance app = new Appearance(); Box box = new Box(nScale, nScale, nScale, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS, app); // load the texture image TextureLoader texLoader = new TextureLoader("texture.gif", this); app.setTexture(texLoader.getTexture()); // set the texture attributes and ensure we // can write to the Transform for the texture attributes m_TextureAttributes = new TextureAttributes(); m_TextureAttributes.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE); app.setTextureAttributes(m_TextureAttributes); // connect all the elements objTrans.addChild(box); objRoot.addChild(objTrans); objRoot.addChild(createRotator()); return objRoot; }
From source file:OrientedTest.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D textMat = new Transform3D(); // Assuming uniform size chars, set scale to fit string in view textMat.setScale(1.2 / sl);/* w w w .j av a2 s . com*/ objScale.setTransform(textMat); // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. Add it to the // root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); Appearance apText = new Appearance(); Material m = new Material(); m.setLightingEnable(true); apText.setMaterial(m); Appearance apEarth = new Appearance(); Material mm = new Material(); mm.setLightingEnable(true); apEarth.setMaterial(mm); Appearance apStone = new Appearance(); apStone.setMaterial(mm); // create 3D text Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2), new FontExtrusion()); Text3D txt = new Text3D(f3d, textString, new Point3f(-sl / 2.0f, 3.0f, 0.0f)); OrientedShape3D textShape = new OrientedShape3D(); textShape.setGeometry(txt); textShape.setAppearance(apText); textShape.setAlignmentAxis(0.0f, 1.0f, 0.0f); objScale.addChild(textShape); // Create a simple shape leaf node, add it to the scene graph. Transform3D cubeMat = new Transform3D(); TransformGroup cubeTrans = new TransformGroup(cubeMat); cubeMat.set(new Vector3d(0.9, 0.0, -1.0)); cubeTrans.setTransform(cubeMat); cubeTrans.addChild(new ColorCube(0.3)); objTrans.addChild(cubeTrans); TextureLoader stoneTex = new TextureLoader(stoneImage, new String("RGB"), this); if (stoneTex != null) apStone.setTexture(stoneTex.getTexture()); TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); apStone.setTextureAttributes(texAttr); Transform3D coneMat = new Transform3D(); TransformGroup coneTrans = new TransformGroup(coneMat); coneMat.set(new Vector3d(0.0, 0.0, 0.0)); coneTrans.setTransform(coneMat); coneTrans.addChild(new Cone(.2f, 0.8f, Cone.GENERATE_NORMALS | Cone.GENERATE_TEXTURE_COORDS, apStone)); objTrans.addChild(coneTrans); TextureLoader earthTex = new TextureLoader(earthImage, new String("RGB"), this); if (earthTex != null) apEarth.setTexture(earthTex.getTexture()); apEarth.setTextureAttributes(texAttr); Transform3D cylinderMat = new Transform3D(); TransformGroup cylinderTrans = new TransformGroup(cylinderMat); cylinderMat.set(new Vector3d(-0.9, 0.5, -1.0)); cylinderTrans.setTransform(cylinderMat); cylinderTrans.addChild( new Cylinder(.35f, 2.0f, Cylinder.GENERATE_NORMALS | Cylinder.GENERATE_TEXTURE_COORDS, apEarth)); objTrans.addChild(cylinderTrans); objTrans.addChild(objScale); // Set up the background Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f); Background bgNode = new Background(bgColor); bgNode.setApplicationBounds(bounds); objRoot.addChild(bgNode); // Set up the ambient light Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); // Set up the directional lights Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); objRoot.addChild(light2); apText.setMaterial(mm); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:ConicWorld.java
private Appearance createAppearance(int idx) { Appearance app = new Appearance(); // Globally used colors Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); idx = idx % 5;/*from www.j av a2s. c o m*/ switch (idx) { // Lit solid case 0: { // Set up the material properties Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, white, 80.0f)); break; } // Lit solid, no specular case 1: { // Set up the material properties Color3f objColor = new Color3f(0.0f, 0.8f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, white, 80.0f)); break; } // Lit solid, specular only case 2: { // Set up the material properties Color3f objColor = new Color3f(0.0f, 0.8f, 0.8f); app.setMaterial(new Material(black, black, objColor, white, 80.0f)); break; } // Texture mapped, lit solid case 3: { // Set up the texture map TextureLoader tex = new TextureLoader(texImage, this); app.setTexture(tex.getTexture()); // Set up the material properties app.setMaterial(new Material(white, black, white, black, 1.0f)); TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); app.setTextureAttributes(texAttr); break; } // Another lit solid with a different color case 4: { // Set up the material properties Color3f objColor = new Color3f(1.0f, 1.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, white, 80.0f)); break; } default: { ColoringAttributes ca = new ColoringAttributes(); ca.setColor(new Color3f(0.0f, 1.0f, 0.0f)); app.setColoringAttributes(ca); } } return app; }
From source file:OrientedPtTest.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D textMat = new Transform3D(); // Assuming uniform size chars, set scale to fit string in view textMat.setScale(1.2 / sl);/*from w w w . j a va 2 s .c o m*/ objScale.setTransform(textMat); // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. Add it to the // root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); Appearance apText = new Appearance(); Material m = new Material(); m.setLightingEnable(true); apText.setMaterial(m); Appearance apEarth = new Appearance(); Material mm = new Material(); mm.setLightingEnable(true); apEarth.setMaterial(mm); Appearance apStone = new Appearance(); apStone.setMaterial(mm); // create 3D text Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2), new FontExtrusion()); Point3f textPt = new Point3f(-sl / 2.0f, 3.0f, 0.0f); Text3D txt = new Text3D(f3d, textString, textPt); OrientedShape3D textShape = new OrientedShape3D(); textShape.setGeometry(txt); textShape.setAppearance(apText); textShape.setAlignmentMode(OrientedShape3D.ROTATE_ABOUT_POINT); // text is centered around 0, 3, 0. Make it rotate around 0,5,0 Point3f rotationPt = new Point3f(0.0f, 5.0f, 0.0f); textShape.setRotationPoint(rotationPt); objScale.addChild(textShape); // also add a small Sphere at the rotation point to // show that we are rotating around the right point Sphere sphere = new Sphere(0.2f); TransformGroup sphereGroup = new TransformGroup(); Transform3D sphereXform = new Transform3D(); sphereXform.set(new Vector3f(rotationPt)); sphereGroup.setTransform(sphereXform); sphereGroup.addChild(sphere); objScale.addChild(sphereGroup); // Create a simple shape leaf node, add it to the scene graph. Transform3D cubeMat = new Transform3D(); TransformGroup cubeTrans = new TransformGroup(cubeMat); cubeMat.set(new Vector3d(0.9, 0.0, -1.0)); cubeTrans.setTransform(cubeMat); cubeTrans.addChild(new ColorCube(0.3)); objTrans.addChild(cubeTrans); TextureLoader stoneTex = new TextureLoader(stoneImage, new String("RGB"), this); if (stoneTex != null) apStone.setTexture(stoneTex.getTexture()); TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.REPLACE); apStone.setTextureAttributes(texAttr); Transform3D coneMat = new Transform3D(); TransformGroup coneTrans = new TransformGroup(coneMat); coneMat.set(new Vector3d(0.0, 0.0, 0.0)); coneTrans.setTransform(coneMat); coneTrans.addChild(new Cone(.2f, 0.8f, Cone.GENERATE_NORMALS | Cone.GENERATE_TEXTURE_COORDS, apStone)); objTrans.addChild(coneTrans); TextureLoader earthTex = new TextureLoader(earthImage, new String("RGB"), this); if (earthTex != null) apEarth.setTexture(earthTex.getTexture()); apEarth.setTextureAttributes(texAttr); Transform3D cylinderMat = new Transform3D(); TransformGroup cylinderTrans = new TransformGroup(cylinderMat); cylinderMat.set(new Vector3d(-0.9, 0.5, -1.0)); cylinderTrans.setTransform(cylinderMat); cylinderTrans.addChild( new Cylinder(.35f, 2.0f, Cylinder.GENERATE_NORMALS | Cylinder.GENERATE_TEXTURE_COORDS, apEarth)); objTrans.addChild(cylinderTrans); objTrans.addChild(objScale); // Set up the background Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f); Background bgNode = new Background(bgColor); bgNode.setApplicationBounds(bounds); objRoot.addChild(bgNode); // Set up the ambient light Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); // Set up the directional lights Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); objRoot.addChild(light2); apText.setMaterial(mm); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:KeyNavigateTest.java
protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile, String szSoundFile) {/*w w w. java 2 s .c o m*/ Group g = new Group(); app.setPolygonAttributes( new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0, false)); app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f)); m_TextureAttributes = new TextureAttributes(TextureAttributes.REPLACE, new Transform3D(), new Color4f(0, 0, 0, 1), TextureAttributes.FASTEST); app.setTextureAttributes(m_TextureAttributes); if ((m_nFlags & ComplexObject.TEXTURE) == ComplexObject.TEXTURE) setTexture(app, szTextureFile); Cone cone = new Cone(1, 1, Primitive.GENERATE_TEXTURE_COORDS, app); g.addChild(cone); attachBehavior(new TextureAnimationBehavior(m_TextureAttributes)); return g; }