Está en la página 1de 8

Lib de Códigos

Libro d Códi
DemoTriangleStripArray

Prof. Andrés Eloy Castillo R.
Clases
• ParaboloideGeometria
Crea la geometria básica de un paraboloide.

• ParaboloideSimple
Se define como uan extensión de BranchGroup.
Se apropia de una clase Shape3D, la cual se relaciona con una geometria
y una apariencia. La geometria es la geometria de un paraboloide y la
apariencia, se definen a través de un ColoringAttributes y un Poly‐
gonAttributes.

• EjeXYZ
Define los ejes X, Y y Z para su visualización

• EscenaGrafica
Define un punto de vista y carga el objeto ParaboloideSimple.

• DemoTriangleStripArray
Una ventana tipo swing, que aloja un objeto Canva3D y por la tanto
permite visualizar la escena y tiene el método main(), por lo que es el
punto de arranque de todos los procesos.
ParaboloideGeometria.java 23/02/2009 03:51 a.m.

1 import javax.media.j3d.*;
2 import javax.vecmath.*;
3
4 public class ParaboloideGeometria
5 {
6 TriangleStripArray estructuraParaboloide;
7 public ParaboloideGeometria(double diametro, double rfd, int n , int m)
8 {
9 int[] verticesxTiras = new int[n];
10 int formato = TriangleStripArray.COORDINATES ;
11 Point3d punto = new Point3d();
12 double x = 0.0,y= 0.0,z= 0.0;
13 double deltaPhi = 360.0/(n-1);
14 double deltaRho = diametro/(2*(m-1));
15 double foco = rfd*diametro;
16 System.out.println("deltaPHI: " + deltaPhi);
17 System.out.println("deltaRho: " + deltaRho);
18 System.out.println("foco: " + foco);
19
20 for (int i = 0;i < n ;i++ )
21 {
22 verticesxTiras[i] = 2*m;
23 }
24 estructuraParaboloide = new TriangleStripArray(2*m*n,formato,verticesxTiras);
25
26 for (int j = 0; j<m ;j++ )
27 for (int i = 0; i < n ;i++ )
28 for(int k = 0; k<2;k++)
29 {
30 x = (j*deltaRho)*Math.cos(Math.toRadians((i+k)*deltaPhi));
31 y = (j*deltaRho)*Math.sin(Math.toRadians((i+k)*deltaPhi));
32 z =(Math.pow(x,2)+Math.pow(y,2))/(4*foco);
33
34 estructuraParaboloide.setCoordinate(i*2*m+2*j+k,new Point3d(x,y,z));
35 }
36 }
37 public TriangleStripArray getGeometria()
38 {
39 return estructuraParaboloide;
40 }
41
42 }
43

Page 1 of 1
ParaboloideSimple.java 23/02/2009 03:50 a.m.

1 import javax.media.j3d.*;
2 import javax.vecmath.*;
3 class ParaboloideSimple extends BranchGroup
4 {
5 ParaboloideSimple()
6 {
7 Appearance ap01 = new Appearance();
8 Appearance ap02 = new Appearance();
9 PolygonAttributes poligono01 =
10 new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
11 PolygonAttributes.CULL_FRONT,0.0f);
12 PolygonAttributes poligono02 =
13 new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
14 PolygonAttributes.CULL_BACK,0.0f);
15 PointAttributes pointAttributes = new PointAttributes(3.0f,true);
16 ColoringAttributes amarillo =
17 new ColoringAttributes(1.0f,1.0f,0.0f,
18 ColoringAttributes.SHADE_GOURAUD);
19 ColoringAttributes amarilloObscuro =
20 new ColoringAttributes(0.85f,0.85f,
21 0.0f,ColoringAttributes.SHADE_GOURAUD);
22 ap01.setPolygonAttributes(poligono01);
23 ap01.setColoringAttributes(amarilloObscuro);
24 ap01.setPointAttributes(pointAttributes);
25 ap02.setPolygonAttributes(poligono02);
26 ap02.setColoringAttributes(amarillo);
27 ap02.setPointAttributes(pointAttributes);
28 ParaboloideGeometria paraboloideGeometria =
29 new ParaboloideGeometria(10.0,0.15,50,50);
30 Shape3D parabolaCaraInterna = new Shape3D();
31 Shape3D parabolaCaraExterna = new Shape3D();
32 parabolaCaraExterna.setGeometry(paraboloideGeometria.getGeometria());
33 parabolaCaraExterna.setAppearance(ap01);
34 parabolaCaraInterna.setGeometry(paraboloideGeometria.getGeometria());
35 parabolaCaraInterna.setAppearance(ap02);
36 addChild(parabolaCaraInterna);
37 addChild(parabolaCaraExterna);
38 compile();
39 }
40 }
41

Page 1 of 1
EjeXYZ.java 23/02/2009 03:55 a.m.

1 import javax.media.j3d.*;
2 import javax.vecmath.*;
3
4 class EjeXYZ extends BranchGroup
5 {
6 public EjeXYZ()
7 {
8 super();
9 // Creando las geometrias.
10 int formatoVertices,nroVertices;
11 LineArray geoEjeX,geoEjeY,geoEjeZ;
12 nroVertices = 2;
13
14 formatoVertices = GeometryArray.COORDINATES |GeometryArray.COLOR_3;
15 geoEjeX = new LineArray(nroVertices,formatoVertices);
16 geoEjeY = new LineArray(nroVertices,formatoVertices);
17 geoEjeZ = new LineArray(nroVertices,formatoVertices);
18 // ****************************************
19 //*****************************************
20
21 //Creando los elementos coordenadas y color.
22 //******************************************
23 Color3f rojo, verde,azul;
24 rojo = new Color3f(1.0f,0.0f,0.0f);
25 verde = new Color3f(0.0f,1.0f,0.0f);
26 azul = new Color3f(0.0f,0.0f,1.0f);
27
28 Point3f punto1,punto2,punto3,punto4,punto5,punto6;
29
30 punto1 = new Point3f(-15.0f,0.0f,0.0f);
31 punto2 = new Point3f(15.0f,0.0f,0.0f);
32 punto3 = new Point3f(0.0f,-15.0f,0.0f);
33 punto4 = new Point3f(0.0f,15.0f,0.0f);
34 punto5 = new Point3f(0.0f,0.0f,-15.0f);
35 punto6 = new Point3f(0.0f,0.0f,15.0f);
36
37 //***********************************************
38
39 // Agregando coordenadas y color.
40 geoEjeX.setCoordinate(0,punto1);
41 geoEjeX.setCoordinate(1,punto2);
42 geoEjeX.setColor(0,rojo);
43 geoEjeX.setColor(1,rojo);
44
45 geoEjeY.setCoordinate(0,punto3);
46 geoEjeY.setCoordinate(1,punto4);
47 geoEjeY.setColor(0,verde);
48 geoEjeY.setColor(1,verde);
49
50 geoEjeZ.setCoordinate(0,punto5);
51 geoEjeZ.setCoordinate(1,punto6);
52 geoEjeZ.setColor(0,azul);
53 geoEjeZ.setColor(1,azul);
54
55 // **************************************************
56 // Creando los elementos Shape3D
57 Shape3D ejeX,ejeY,ejeZ;
58 ejeX = new Shape3D();
59 ejeY = new Shape3D();
60 ejeZ = new Shape3D();
61
62 // Agregando la geometria a los Shape3D
63 ejeX.setGeometry(geoEjeX);
64 ejeY.setGeometry(geoEjeY);

Page 1 of 2
EjeXYZ.java 23/02/2009 03:55 a.m.

65 ejeZ.setGeometry(geoEjeZ);
66
67 // Agregando los Shape3D, al brazo
68 addChild(ejeX);
69 addChild(ejeY);
70 addChild(ejeZ);
71
72 // Compilando el brazo de volumenes.
73 compile();
74 }
75 }
76

Page 2 of 2
EscenaGrafica.java 23/02/2009 03:52 a.m.

1 import javax.media.j3d.*;
2 import com.sun.j3d.utils.universe.*;
3 import com.sun.j3d.utils.geometry.*;
4 import javax.vecmath.*;
5 import java.awt.*;
6
7
8 class EscenaGrafica
9 {
10 Canvas3D canvas3D;
11 EscenaGrafica()
12 {
13 GraphicsConfiguration config =
14 SimpleUniverse.getPreferredConfiguration();
15 canvas3D = new Canvas3D(config);
16 SimpleUniverse su = new SimpleUniverse(canvas3D);
17 double x,y,z;
18 double r,phi,tetha;
19 r = 30.0;
20 phi = 60.0;
21 tetha =35.0;
22 x = r*Math.cos(Math.toRadians(tetha))*Math.sin(Math.toRadians(phi));
23 y = r*Math.cos(Math.toRadians(tetha))*Math.cos(Math.toRadians(phi));
24 z = r*Math.sin(Math.toRadians(tetha));
25 Transform3D t3d = new Transform3D();
26 t3d.lookAt( new Point3d(x,y,z),new Point3d(0.0,0.0,0.0),
27 new Vector3d(0.0,0.0,1.0));
28 t3d.invert();
29 su.getViewingPlatform().getViewPlatformTransform().setTransform(t3d);
30 BranchGroup bg = new BranchGroup();
31
32 ParaboloideSimple paraboloide = new ParaboloideSimple();
33 bg.addChild(paraboloide);
34 EjeXYZ ejeXYZ = new EjeXYZ();
35 bg.addChild(ejeXYZ);
36 bg.compile();
37
38 //Agregando el brazo al universo virtual.
39 su.addBranchGraph(bg);
40 }
41 public Canvas3D getCanvas3D()
42 {
43 return canvas3D;
44 }
45 }

Page 1 of 1
DemoTriangleStripArray.java 23/02/2009 03:52 a.m.

1 import javax.swing.*;
2 class DemoTriangleStripArray extends JFrame
3 {
4 DemoTriangleStripArray()
5 {
6 super("Demo Seno");
7 EscenaGrafica eg = new EscenaGrafica();
8 setSize(700,500);
9 setLocation(200,200);
10 getContentPane().add("Center",eg.getCanvas3D());
11 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12 setVisible(true);
13 }
14 public static void main(String[] args)
15 {
16 DemoTriangleStripArray demo = new DemoTriangleStripArray();
17 }
18 }

Page 1 of 1

También podría gustarte