List of usage examples for javax.media.j3d Transform3D setTranslation
public final void setTranslation(Vector3d trans)
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 va2s.com // 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:IntersectTest.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // 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);//w w w. j av a 2s. c o m // Set up the directional lights Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f); Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f); Vector3f light2Direction = new Vector3f(-6.0f, -2.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); Transform3D t3 = new Transform3D(); // Shapes for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { for (int z = 0; z < 3; z++) { t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y * 4.0, -20 - z * 4.0)); TransformGroup objTrans = new TransformGroup(t3); objRoot.addChild(objTrans); // Create a simple shape leaf node, add it to the scene // graph. GeometryArray geom = null; if (((x + y + z) % 2) == 0) { geom = new RandomColorCube(); } else { geom = new RandomColorTetrahedron(); } Shape3D shape = new Shape3D(geom); // use the utility method to set the capabilities PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL); objTrans.addChild(shape); } } } // Lines Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f), new Point3f(2.0f, 0.0f, 0.0f) }; Color3f grey = new Color3f(0.7f, 0.7f, 0.7f); Color3f[] colors = { grey, grey }; for (int y = 0; y < 5; y++) { for (int z = 0; z < 5; z++) { t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0 - z * 2.0)); TransformGroup objTrans = new TransformGroup(t3); objRoot.addChild(objTrans); LineArray la = new LineArray(verts.length, LineArray.COORDINATES | LineArray.COLOR_3); la.setCoordinates(0, verts); la.setColors(0, colors); Shape3D shape = new Shape3D(); shape.setGeometry(la); // use the utility method to set the capabilities PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL); objTrans.addChild(shape); } } // Points for (double x = -2.0; x <= 2.0; x += 1.0) { for (double y = -2.0; y <= 2.0; y += 1.0) { for (double z = -2.0; z <= 2.0; z += 1.0) { t3.setTranslation(new Vector3d(-10.0 + 2.0 * x, 0.0 + 2.0 * y, -20.0 + 2.0 * z)); TransformGroup objTrans = new TransformGroup(t3); objRoot.addChild(objTrans); PointArray pa = new PointArray(1, PointArray.COORDINATES | PointArray.COLOR_3); pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0)); pa.setColor(0, grey); Shape3D shape = new Shape3D(); shape.setGeometry(pa); // use the utility method to set the capabilities PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL); objTrans.addChild(shape); } } } return objRoot; }
From source file:HiResCoordTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); Transform3D t3dTilt = new Transform3D(); t3dTilt.rotX(0.3);/* w w w . j av a 2 s. co m*/ TransformGroup objTrans = new TransformGroup(t3dTilt); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); TransformGroup objTransPlanets = new TransformGroup(); objTransPlanets.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 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, objTransPlanets, yAxis, 0.0f, (float) Math.PI * 2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), m_TranslateSunZ); rotator.setSchedulingBounds(bounds); objTransPlanets.addChild(rotator); // create the sun TransformGroup sunTg = createSun(); // create Earth Transform3D t3dEarth = new Transform3D(); t3dEarth.setScale(m_EarthRadius); t3dEarth.setTranslation(new Vector3d(m_EarthOrbit, 0, 0)); objTransPlanets.addChild(createPlanet("Earth", new Color3f(0, 0.1f, 1.0f), t3dEarth, null)); // create Mars Transform3D t3dMars = new Transform3D(); t3dMars.setTranslation( new Vector3d(Math.sin(Math.PI * 1.5) * m_MarsOrbit, 0, Math.cos(Math.PI * 0.5) * m_MarsOrbit)); t3dMars.setScale(m_MarsRadius); objTransPlanets.addChild(createPlanet("Mars", new Color3f(1, 0, 0), t3dMars, null)); // create Mercury Transform3D t3dMercury = new Transform3D(); t3dMercury.setTranslation( new Vector3d(Math.sin(Math.PI) * m_MercuryOrbit, 0, Math.cos(Math.PI) * m_MercuryOrbit)); t3dMercury.setScale(m_MercuryRadius); objTransPlanets.addChild(createPlanet("Mercury", new Color3f(0.5f, 0.5f, 0.5f), t3dMercury, null)); sunTg.addChild(objTransPlanets); objTrans.addChild(sunTg); objRoot.addChild(objTrans); return objRoot; }
From source file:Text2DTest.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create a Transformgroup to scale all objects so they // appear in the scene. TransformGroup objScale = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(0.4);/*from ww w.jav a 2 s .c om*/ objScale.setTransform(t3d); objRoot.addChild(objScale); // 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); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); TransformGroup textTranslationGroup; Transform3D textTranslation; float yPos = -.5f; Shape3D textObject = new Text2D("Rotating Yellow Text", new Color3f(1f, 1f, 0f), "Serif", 60, Font.BOLD); Appearance app = textObject.getAppearance(); PolygonAttributes pa = app.getPolygonAttributes(); if (pa == null) pa = new PolygonAttributes(); pa.setCullFace(PolygonAttributes.CULL_NONE); if (app.getPolygonAttributes() == null) app.setPolygonAttributes(pa); objTrans.addChild(textObject); textTranslation = new Transform3D(); textTranslation.setTranslation(new Vector3f(0f, yPos, 0f)); textTranslationGroup = new TransformGroup(textTranslation); textTranslationGroup.addChild(objTrans); objScale.addChild(textTranslationGroup); yPos += .5f; /* Blue 40point text */ textObject = new Text2D("Blue 40point Text", new Color3f(0f, 0f, 1f), "Serif", 40, Font.BOLD); textTranslation = new Transform3D(); textTranslation.setTranslation(new Vector3f(0f, yPos, 0f)); textTranslationGroup = new TransformGroup(textTranslation); textTranslationGroup.addChild(textObject); objScale.addChild(textTranslationGroup); yPos += .5f; /* Green italic text */ textObject = new Text2D("Green Italic Text", new Color3f(0f, 1f, 0f), "Serif", 70, Font.ITALIC); textTranslation = new Transform3D(); textTranslation.setTranslation(new Vector3f(0f, yPos, 0f)); textTranslationGroup = new TransformGroup(textTranslation); textTranslationGroup.addChild(textObject); objScale.addChild(textTranslationGroup); yPos += .5f; // 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); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator); return objRoot; }
From source file:LightTest.java
protected Group createSphere(float x, float y, float z, float radius) { TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(x, y, z)); tg.setTransform(t3d);/*from w ww . j a v a 2 s . c o m*/ // create an Appearance and Material Appearance app = new Appearance(); Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 80.0f)); tg.addChild(new Sphere(radius, Primitive.GENERATE_NORMALS, app)); return tg; }
From source file:LightTest.java
public Group createGeometry() { Point3f pos = new Point3f(); ((PointLight) m_Light).getPosition(pos); m_TransformGroup = new TransformGroup(); m_TransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); m_TransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3f(pos.x, pos.y, pos.z)); m_TransformGroup.setTransform(t3d);// w ww . ja v a 2 s . c om m_Sphere = new Sphere(0.2f, Primitive.ENABLE_APPEARANCE_MODIFY | Primitive.GENERATE_NORMALS, 16); m_TransformGroup.addChild(m_Sphere); m_TransformGroup.addChild(super.createGeometry()); return (Group) m_TransformGroup; }
From source file:Text2DTest.java
/** * Process a keyboard event to move or rotate the viewer. *///w ww . ja v a2s. c o m void processManualEvent(AWTEvent[] events) { for (int i = 0; i < events.length; ++i) { if (events[i] instanceof KeyEvent) { KeyEvent event = (KeyEvent) events[i]; if (event.getKeyCode() == KeyEvent.VK_EQUALS) { continue; } Transform3D t = new Transform3D(); viewTransformGroup.getTransform(t); Vector3f viewDir = new Vector3f(0f, 0f, -1f); Vector3f translation = new Vector3f(); t.get(translation); t.transform(viewDir); if (event.getKeyCode() == KeyEvent.VK_UP) { translation.x += viewDir.x; translation.y += viewDir.y; translation.z += viewDir.z; } else if (event.getKeyCode() == KeyEvent.VK_DOWN) { translation.x -= viewDir.x; translation.y -= viewDir.y; translation.z -= viewDir.z; } else if (event.getKeyCode() == KeyEvent.VK_RIGHT) { rotation += -.1; } else if (event.getKeyCode() == KeyEvent.VK_LEFT) { rotation += .1; } t.rotY(rotation); t.setTranslation(translation); viewTransformGroup.setTransform(t); } } }
From source file:ffx.potential.bonded.RestraintBond.java
/** * <p>// w w w . j av a 2 s.c om * setBondTransform3d</p> * * @param t3d a {@link javax.media.j3d.Transform3D} object. * @param pos an array of double. * @param orient an array of double. * @param len a double. * @param newRot a boolean. */ public void setBondTransform3d(Transform3D t3d, double[] pos, double[] orient, double len, boolean newRot) { // Bond Orientation if (newRot) { angle = angle(orient, y); cross(y, orient, bcross); bcross[3] = angle - Math.PI; axisAngle.set(bcross); } // Scale the orientation vector to be a fourth the bond length // and add it to the position vector of the of the first atom scalar(orient, len / 4.0d, cstart); sum(cstart, pos, cstart); pos3d.set(cstart); t3d.setTranslation(pos3d); t3d.setRotation(axisAngle); t3d.setScale(scale); }
From source file:LightTest.java
public void synchLightToUi() { super.synchLightToUi(); // set some defaults if things go wrong... double x = 0; double y = 0; double z = 0; double constant = 0.01; double linear = 0; double quadratic = 0; try {/* ww w . j a va 2 s .co m*/ x = Double.valueOf(m_XPositionTextField.getText()).doubleValue(); y = Double.valueOf(m_YPositionTextField.getText()).doubleValue(); z = Double.valueOf(m_ZPositionTextField.getText()).doubleValue(); constant = Double.valueOf(m_ConstantAttenuationTextField.getText()).doubleValue(); linear = Double.valueOf(m_LinearAttenuationTextField.getText()).doubleValue(); quadratic = Double.valueOf(m_QuadraticAttenuationTextField.getText()).doubleValue(); } catch (java.lang.NumberFormatException e) { // invalid numeric input - just ignore. } ((PointLight) m_Light).setPosition((float) x, (float) y, (float) z); ((PointLight) m_Light).setAttenuation((float) constant, (float) linear, (float) quadratic); if (m_TransformGroup != null) { Transform3D t3d = new Transform3D(); m_TransformGroup.getTransform(t3d); t3d.setTranslation(new Vector3d(x, y, z)); m_TransformGroup.setTransform(t3d); } if (m_Sphere != null) { Appearance app = new Appearance(); Color3f objColor = new Color3f(); m_Light.getColor(objColor); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 80.0f)); m_Sphere.getShape(Sphere.BODY).setAppearance(app); } }
From source file:AvatarTest.java
/** * Process a keyboard event/* w w w .j a v a2 s .c o m*/ */ private void processAWTEvent(AWTEvent[] events) { for (int n = 0; n < events.length; n++) { if (events[n] instanceof KeyEvent) { KeyEvent eventKey = (KeyEvent) events[n]; if (eventKey.getID() == KeyEvent.KEY_PRESSED) { int keyCode = eventKey.getKeyCode(); int keyChar = eventKey.getKeyChar(); Vector3f translate = new Vector3f(); Transform3D t3d = new Transform3D(); m_TransformGroup.getTransform(t3d); t3d.get(translate); switch (keyCode) { case KeyEvent.VK_LEFT: translate.x += TRANSLATE_LEFT; break; case KeyEvent.VK_RIGHT: translate.x += TRANSLATE_RIGHT; break; } // System.out.println( "Steering: " + translate.x ); translate.y = 0.5f; t3d.setTranslation(translate); m_TransformGroup.setTransform(t3d); } } } }