Démonstration de java 3d

Description

C'est une démonstration de JAVA 3D pour la création de quelques formes mathématiques et comment faire pour spécifier la transformation euclidienne avec les "TransformGroup" ....

Source / Exemple :


/*

  • @Author : Scupper
  • /
import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.GraphicsConfiguration; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; public class FormArrayFromMathEquation extends Applet implements ColorInterface{ private TransformGroup tg ; private Transform3D axis; private BranchGroup objRoot; public static enum EQ{ LINES, POINTS, QUADS, TRIANGLES } public FormArrayFromMathEquation(EQ equationType) { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); BranchGroup scene = null; Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); switch(equationType){ case LINES : scene= createLinesSceneGraph(); break; case POINTS : scene= createPointsSceneGraph(); break; case TRIANGLES : scene= createTrianglesSceneGraph(); break; case QUADS : scene= createQuadsSceneGraph(); break; } // SimpleUniverse is a Convenience Utility class SimpleUniverse simpleU = new SimpleUniverse(canvas3D); // This will move the ViewPlatform back a bit so the // objects in the scene can be viewed. simpleU.getViewingPlatform().setNominalViewingTransform(); simpleU.addBranchGraph(scene); } // end of CanvasJava3DSample (constructor) public LineArray constructLineArray(int N, float r,float z){ Point3f[] ps = new Point3f[N]; Color3f[] cs = new Color3f[N]; LineArray la = new LineArray(N,LineArray.COORDINATES | LineArray.COLOR_3 ); for(int i = 0; i< N; i++){ // positions of the points which defines the lines array // these points are defined by : M[ rSin(t) , rCos(t), zSin(2t) ] ps[i] = new Point3f((float)(r*Math.sin(i)), (float)(r*Math.cos(i)), (float)(z*Math.sin(2*i)));//*Math.asin(1/(i+1)) // This array is used to define the color by which each line should be colored cs[i] = new Color3f((float)(r*Math.sin(i)),(float)(r*Math.cos(i)),(float)(z*Math.sin(2*i)*Math.acos(1/(i+1))));//*Math.acos(1/(i+1)) // Prepare the line array and store the color of each line la.setCoordinate(i, ps[i]); la.setColor(i, cs[i]); } return la; } //Triangle Array is defined by 3 vertexes, so by 3 points public TriangleArray constructTriangleArray(int N, float r,float z,boolean signum){ TriangleArray ta = new TriangleArray(3*N,TriangleArray.COORDINATES | TriangleArray.COLOR_3 ); for(int i = 0; i< N ; i++){ for(int j = 0 ; j < 3 ; j++){ if(signum){ // Initialize the top part of the Triangle array ta.setCoordinate(3*i+j, new Point3f(i*(float)Math.cos(2*j*Math.PI/3)/N, i*(float)Math.sin(2*j*Math.PI/3)/N, i*1.0f/N ) ); }else{ // Initialize the bottom part of the Triangle array ta.setCoordinate((3*i+j), new Point3f(i*(float)Math.cos((2*j-1)*Math.PI/3)/N, i*(float)Math.sin((2*j-1)*Math.PI/3)/N, -i*1.0f/N ) ); } // Again set the color of the color of each point of the Triangle array ta.setColor(3*i+j, new Color3f(r*i*(float)Math.cos(2*j*Math.PI/3)/N,r*i*(float)Math.sin(2*j*Math.PI/3)/N,(float)(z*Math.sin(2*j)))); } } return ta; } public QuadArray constructQuadArray(int N, float r,float z,boolean signum){ QuadArray ta = new QuadArray(4*N,QuadArray.COORDINATES | QuadArray.COLOR_3 ); for(int i = 0; i< N ; i++){ for(int j = 0 ; j < 4 ; j++){ if(signum){ ta.setCoordinate(4*i+j, new Point3f(i*(float)Math.cos((j-1)*Math.PI/2)/N, i*(float)Math.sin((j-1)*Math.PI/2)/N, i*1.0f/N ) ); }else{ ta.setCoordinate((4*i+j), new Point3f(i*(float)Math.cos((j)*Math.PI/2)/N, i*(float)Math.sin((j)*Math.PI/2)/N, -i*1.0f/N ) ); } ta.setColor(4*i+j, new Color3f(r*i*(float)Math.cos(3*j*Math.PI/4)/N,r*i*(float)Math.sin(3*j*Math.PI/4)/N,(float)(z*Math.sin(3*j)))); } } return ta; } public PointArray constructPointArray(int N1 ,int N2, int N3,float r, float theta, float phi){ PointArray la = new PointArray(N1*N2*N3,LineArray.COORDINATES | LineArray.COLOR_3 ); for(int i = 0; i< N1; i++){ for(int j = 0; j < N2;j++ ){ for(int k = 0; k < N3; k++){ la.setCoordinate(i+j*N1+k*N1*N2, new Point3f( (float)(( (i*r/N1) )*Math.cos(( (j*theta/N2) ))*Math.cos(( (k*phi/N3) ))), (float)((float)(( (i*r/N1) )*Math.cos(( (j*theta /N2)))*Math.sin(( (k*phi/N3) )))), (float)(( (i*r/N1) )*Math.cos(( (j*theta /N2)))))); la.setColor(i+j*N1+k*N1*N2, new Color3f( (float)(( (i*r/N1) )*Math.cos(( (j*theta/N2) ))*Math.cos(( (k*phi/N3) ))), (float)((float)(( (i*r/N1) )*Math.cos(( (j*theta /N2)))*Math.sin(( (k*phi/N3) )))), (float)(( (i*r/N1) )*Math.cos(( (j*theta /N2)))))); } } } return la; } // Create the transform group and then initiate the rotation's axis and the Value of the Alpha (speed of rotation) public TransformGroup createTransformGroup(Shape3D shape, int alpha, float theta){ axis = new Transform3D(); Transform3D axistemp = new Transform3D(); Transform3D axistemp2 = new Transform3D(); axistemp.rotY(Math.PI/4); axistemp2.rotZ(Math.PI/2); axis.rotX(2*Math.PI/theta); axis.mul(axistemp, axistemp2); tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE ); tg.addChild(shape); Alpha rotationAlpha = new Alpha(-1, alpha); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, tg ); rotator.setTransformAxis(axis); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); tg.addChild(rotator); return tg; } public TransformGroup createPointsTransformGroup(Shape3D shape,int alpha, float theta){ axis = new Transform3D(); axis.rotX(2*Math.PI/theta); tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE ); tg.addChild(shape); Alpha rotationAlpha = new Alpha(-1, alpha); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, tg ); rotator.setTransformAxis(axis); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); tg.addChild(rotator); return tg; } public TransformGroup createTrianglesTransformGroup(Shape3D shape,int alpha, float theta){ axis = new Transform3D(); Transform3D axistemp = new Transform3D(); Transform3D axistemp2 = new Transform3D(); axistemp.rotY(Math.PI/4); axistemp2.rotZ(Math.PI/2); axis.rotX(theta*Math.PI/180); axis.mul(axistemp, axistemp2); tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE ); tg.addChild(shape); Alpha rotationAlpha = new Alpha(-1, alpha); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, tg ); rotator.setTransformAxis(axis); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); tg.addChild(rotator); return tg; } public TransformGroup createQuadsTransformGroup(Shape3D shape,int alpha, float theta){ axis = new Transform3D(); Transform3D axistemp = new Transform3D(); Transform3D axistemp2 = new Transform3D(); axistemp.rotY(Math.PI/4); axistemp2.rotZ(Math.PI/2); axis.rotX(theta*Math.PI/180); axis.mul(axistemp, axistemp2); tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE ); tg.addChild(shape); Alpha rotationAlpha = new Alpha(-1, alpha); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, tg ); rotator.setTransformAxis(axis); BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); tg.addChild(rotator); return tg; } public BranchGroup createLinesSceneGraph() { objRoot = new BranchGroup(); Shape3D[] lines = new Shape3D[20]; // Draw 20 Circle defined by a lineArray and set it's diameter and alpha ... for(int i = 0 ; i < lines.length ; i++){ lines[i] = new Shape3D(constructLineArray(1000,(i/(float)lines.length ),0.2f)); objRoot.addChild(createTransformGroup(lines[i],i*1000,(i%2==0)?2*i:-2*i)); } return objRoot; } public BranchGroup createPointsSceneGraph() { objRoot = new BranchGroup(); Shape3D points ; // for(int i = 0 ; i < lines.length ; i++){ points = new Shape3D(constructPointArray(10,100,100,10f,(float)(2*Math.PI),(float)(2*Math.PI))); objRoot.addChild(createPointsTransformGroup(points,4000,100)); // } return objRoot; } public BranchGroup createTrianglesSceneGraph() { objRoot = new BranchGroup(); Shape3D trianglesp ; Shape3D trianglesn ; // for(int i = 0 ; i < lines.length ; i++){ trianglesp = new Shape3D(constructTriangleArray(10,0.8f,0.4f,true)); trianglesn = new Shape3D(constructTriangleArray(10,0.8f,0.4f,false)); System.out.println("Triangle Contructed ..."); objRoot.addChild(createTrianglesTransformGroup(trianglesp,4000,10)); objRoot.addChild(createTrianglesTransformGroup(trianglesn,4000,10)); // } return objRoot; } public BranchGroup createQuadsSceneGraph() { objRoot = new BranchGroup(); Shape3D Quadsp ; Shape3D Quadsn ; // for(int i = 0 ; i < lines.length ; i++){ Quadsp = new Shape3D(constructQuadArray(10,0.8f,0.4f,true)); Quadsn = new Shape3D(constructQuadArray(10,0.8f,1.4f,false)); System.out.println("Quads Contructed ..."); objRoot.addChild(createQuadsTransformGroup(Quadsp,4000,10)); objRoot.addChild(createQuadsTransformGroup(Quadsn,4000,10)); // } return objRoot; } public static void main(String[] args) { Frame[] frame = new Frame[FormArrayFromMathEquation.EQ.values().length]; for(EQ q : EQ.values()){ frame[q.ordinal()] = new MainFrame(new FormArrayFromMathEquation(q),600, 600); frame[q.ordinal()].setTitle("Java3D Test "+(q.ordinal()+1)); } } } /////////////////////////////////////////////////////////////////////// // // l'interface contenant les couleurs prédéfinies // ///////////////////////////////////////////////////////////////////// import javax.vecmath.Color3f; /** *
  • @author Scupper
  • /
public interface ColorInterface { public static final Color3f RED = new Color3f(1.0f,0.0f,0.0f); public static final Color3f GREEN = new Color3f(0.0f,1.0f,0.0f); public static final Color3f BLUE = new Color3f(0.0f,0.0f,1.0f); public static final Color3f YELLOW = new Color3f(1.0f,1.0f,0.0f); public static final Color3f CYAN = new Color3f(0.0f,1.0f,1.0f); public static final Color3f MAGENTA = new Color3f(1.0f,0.0f,1.0f); public static final Color3f WHITE = new Color3f(1.0f,1.0f,1.0f); public static final Color3f BLACK = new Color3f(0.0f,0.0f,0.0f); }

Conclusion :


Comme résultat vous aurez 4 fenêtres ou les figure font des rotations suivant un axe prédéfini ...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.