Академический Документы
Профессиональный Документы
Культура Документы
3.3.1.- Introducción
– Clases de Java3D:
– Shape3D: Nodo hoja para formas geométricas tridimensionales.
– Geometry: Define la geometría
– Appearance: Define la apariencia.
– La clase Point3d:
– void distance(Point3d p1): Distancia a otro punto.
– La clase Vector3d:
– double dot(Vector3d v1): Producto escalar.
– double cross(Vector3d v1, Vector3d v2): Producto vectorial.
– double length(): Longitud
– double angle(Vector3d v1): Ángulo entre vectores
package puntosyvectores;
import javax.vecmath.*;
Clase: CreacionDeFormas3D
package creaciondeformas3d;
import javax.swing.*;
public PanelFormas() {
initComponents();
}
setBorder(javax.swing.BorderFactory.createLineBorder(
getBackground(), 20));
setLayout(new java.awt.BorderLayout());
add(panelCentro, java.awt.BorderLayout.CENTER);
}// </editor-fold>
import javax.media.j3d.*;
import modelos3d.*;
@Override
public Node addNodoPrincipal() {
BranchGroup bg = new BranchGroup();
bg.addChild(forma);
return bg;
}
}
3.3.4.- Geometría
GeometryArray
– Permite construir objetos con arreglos de puntos, líneas y polígonos.
– Define los vértices y especifica las relaciones estructurales entre los vértices.
– Define si la forma usan coordenadas, colores, texturas o normales a la superficie.
– Las coordenadas especifican la ubicación del vértice.
– Los colores pueden ser utilizados para determinar el color de los objetos.
– Las normales a las superficie son necesarias para calcular las reflexiones de luz.
– La textura define las coordenadas en el espacio de texturas.
Se utiliza una mascara para definir elementos de la forma gráfica, los valores permitidos son:
– COORDINATES – coordenadas de vértices.
– NORMALS – normal de superficie de vértices.
– COLOR_3 – color del vértice.
– COLOR_4 – color del vértice
– TEXTURE_COORDINATE_2 – Textura 2D
– TEXTURE_COORDINATE_3 – Textura 3D
– TEXTURE_COORDINATE_4 – Textura 4D
Clase Forma3D
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public Forma3D() {
setAppearance(new Appearance());
crearColores();
}
PointArray
– Define una geometría consistente de un conjunto de puntos
– new PointArray(int numPuntos,int mascara);
LineArray
– Define una geometría consistente de segmentos de línea.
– Dos vértices contiguos definen un segmento de línea.
Clase Lineas3D
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public Lineas3D(){
this(null);
}
@Override
public void asignarVertices() {
v = new Point3f[]{
new Point3f(0.5f, 0.0f, 0.5f),
new Point3f(0.5f, 0.5f, 0.0f),
new Point3f(0.5f, 0.0f, 0.0f),
new Point3f(-0.5f, 0.0f, 0.5f),
new Point3f(-0.5f, 0.5f, 0.0f),
new Point3f(-0.5f, 0.0f, 0.0f)
};
vC = new Point3f[]{
v[0], v[1], v[1], v[2], v[2], v[0],
v[3], v[4], v[4], v[5], v[5], v[3],
v[0], v[3],
v[1], v[4],
v[2], v[5]
};
}
@Override
public void asignarColores() {
cC = new Color3f[]{
c[3], c[3], c[3], c[3], c[3], c[3],
c[4], c[4], c[4], c[4], c[4], c[4],
c[3], c[4], c[3], c[4], c[3], c[4]
};
}
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
public PanelFormas() {
initComponents();
panelCentro.addTab("Líneas", new PanelForma3D(new Lineas3D()));
}
...
}
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public XYZ(){
this(null);
}
@Override
public void asignarVertices() {
float f = 10;
vC = new Point3f[]{
new Point3f(-f, 0, 0), new Point3f( f, 0, 0),
new Point3f( 0, f, 0), new Point3f( 0,-f, 0),
new Point3f( 0, 0, f), new Point3f( 0, 0,-f)
};
}
@Override
public void asignarColores() {
cC = new Color3f[]{
c[0],c[0], c[1],c[1], c[2],c[2]
};
}
@Override
public void crearForma(){
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
3.3.6.- TriangleArray
– Define una superficie formada por triángulos.
– Cada tres vértices definen un triangulo.
– Cada triángulo tiene un frente y un revés, el único que se puede ver es el frente.
– El sentido de para unir los vértices del frente de un triángulo es contrario a las manecillas
del reloj.
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
double rad = Math.toRadians(30);
float x = largo/2;
float z = ancho/2;
float y = (float)(z * Math.tan(rad));
float r = (float)(z / Math.cos(rad));
v = new Point3f[]{
new Point3f( x,-y, z),
new Point3f( x, r, 0),
new Point3f( x,-y,-z),
new Point3f(-x,-y, z),
new Point3f(-x, r, 0),
new Point3f(-x,-y,-z)
};
vC = new Point3f[]{
v[0], v[2], v[1],
v[5], v[3], v[4],
v[5], v[2], v[3],
v[3], v[2], v[0],
v[3], v[0], v[4],
v[4], v[0], v[1],
v[4], v[1], v[5],
v[5], v[1], v[2]
};
}
@Override
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
3.3.7.- QuadArray
– Define una superficie formada por cuadriláteros.
– Cuatro vértices definen un cuadrilátero.
Clase PrismaRectangular
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
float x = largo/2;
float y = alto/2;
float z = ancho/2;
v = new Point3f[]{
new Point3f( x, y, z),
new Point3f( x, y, -z),
new Point3f( x, -y, -z),
new Point3f( x, -y, z),
new Point3f(-x, y, z),
new Point3f(-x, y, -z),
new Point3f(-x, -y, -z),
new Point3f(-x, -y, z)
};
vC = new Point3f[]{
v[3], v[2], v[1], v[0],
v[6], v[7], v[4], v[5],
v[6], v[2], v[3], v[7],
v[7], v[3], v[0], v[4],
v[4], v[0], v[1], v[5],
v[5], v[1], v[2], v[6]
};
}
@Override
public void asignarColores() {
cC = new Color3f[]{
c[0], c[0], c[0], c[0],
c[1], c[1], c[1], c[1],
c[2], c[2], c[2], c[2],
c[3], c[3], c[3], c[3],
c[4], c[4], c[4], c[4],
c[5], c[5], c[5], c[5]
};
}
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
Clase: PrismaHexagonal
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
int lados = 6;
double alfa = Math.toRadians(360f / lados);
double beta = (Math.PI - alfa) / 2;
float radio = (float) (ancho * Math.sin(beta) / Math.sin(alfa));
float x = largo / 2;
v = new Point3f[lados * 2];
for (int i = 0; i < lados; i++) {
float y = (float) (radio * Math.sin(alfa * i));
float z = (float) (radio * Math.cos(alfa * i));
v[i] = new Point3f(x, y, z);
v[i + lados] = new Point3f(-x, y, z);
}
vC = new Point3f[]{
v[ 5], v[ 4], v[ 0], v[ 3], v[ 1], v[ 2],
v[11], v[ 6], v[10], v[ 7], v[ 9], v[ 8],
v[ 8], v[ 2], v[ 9], v[ 3], v[10], v[ 4], v[11],
v[ 5], v[ 6], v[ 0], v[ 7], v[ 1], v[ 8], v[ 2]
};
}
@Override
public void asignarColores() {
cC = new Color3f[]{
c[0], c[0], c[0], c[0], c[0], c[0],
c[1], c[1], c[1], c[1], c[1], c[1],
c[2], c[2], c[3], c[3], c[2], c[2],c[3],
@Override
public void crearForma() {
int[] tira = {6, 6, 14};
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
TriangleStripArray ta = new TriangleStripArray(vC.length, mascara, tira);
ta.setCoordinates(0, vC);
ta.setColors(0, cC);
setGeometry(ta);
}
}
TriangleFanArray
En este caso, se comparte siempre el primer vértice
Clase Sombrilla
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
vC = new Point3f[lados + 2];
double angulo = -Math.toRadians(360f / lados);
vC[0] = new Point3f(0, ancho, 0);
for (int i = 1; i <= lados; i++) {
float x = (float) (ancho * Math.cos(angulo * i));
float z = (float) (ancho * Math.sin(angulo * i));
vC[i] = new Point3f(x, -0.5f * ancho, z);
}
vC[lados + 1] = vC[1];
}
@Override
public void asignarColores() {
cC = new Color3f[lados + 2];
cC[0] = c[5];
for (int i = 1; i < cC.length; i++) {
cC[i] = c[i % 3];
}
cC[lados+1] = cC[1];
}
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
int[] tira = {lados + 2};
TriangleFanArray fa
= new TriangleFanArray(vC.length, mascara, tira);
fa.setCoordinates(0, vC);
fa.setColors(0, cC);
setGeometry(fa);
}
}
Clase Octaedro
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
float r = ancho/2;
v = new Point3f[]{
new Point3f( 0, r, 0),
new Point3f( r, 0, 0),
new Point3f( 0, 0, r),
new Point3f(-r, 0, 0),
new Point3f( 0, 0,-r),
@Override
public void asignarColores() {
iC = new int[]{
0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7
};
}
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
int max = Math.max(v.length,c.length);
IndexedTriangleArray ta =
new IndexedTriangleArray(max, mascara, iV.length);
ta.setCoordinates(0, v);
ta.setColors(0, c);
ta.setCoordinateIndices(0, iV);
ta.setColorIndices(0, iC);
setGeometry(ta);
}
}
Ejemplo de IndexedTriangleStripArray
Clase Poliedro
package modelos3d;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
float x1 = largo/2;
float x2 = x1 - ancho/2;
float y = alto/2;
float z = ancho/2;
v = new Point3f[]{
new Point3f( x2, y, z),
new Point3f( x1, y, 0),
new Point3f( x2, y,-z),
new Point3f(-x2, y,-z),
new Point3f(-x1, y, 0),
new Point3f(-x2, y, z),
new Point3f( x2,-y, z),
new Point3f( x1,-y, 0),
new Point3f( x2,-y,-z),
new Point3f(-x2,-y,-z),
new Point3f(-x1,-y, 0),
new Point3f(-x2,-y, z)
};
iV = new int[]{
1, 2, 0, 3, 5, 4,
7, 6, 8,11, 9,10,
9, 3, 8, 2, 7, 1, 6, 0,11, 5,10, 4, 9, 3
};
}
@Override
public void asignarColores() {
iC = new int[]{
0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1,
2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2
};
}
@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
int tiras[] = new int[]{6,6,14};
IndexedTriangleStripArray geo =
new IndexedTriangleStripArray(v.length,mascara,iV.length,tiras);
geo.setCoordinates(0, v);
geo.setCoordinateIndices(0,iV);
geo.setColors(0, c);
geo.setColorIndices(0, iC);
setGeometry(geo);
}
}
– Tipos primitivos:
– TRIANGLE_ARRAY
– TRIANGLE_FAN_ARRAY
– TRIANGLE_STRIP_ARRAY
– QUAD_ARRAY
– POLYGON_ARRAY
– Conjunto de vértices:
– void setCoordinates(Point3f[] coords)
– Conjuntos de índices:
– void setCoordinateIndices(int[] indices)
– void setNormalIndices(int[] indices)
– void setColorIndices(int[] indices)
– void setTextureCoordinateIndices(int[] indices)
Clase NormalGenerator
– Generar automáticamente normales:
– public void generateNormals(GeometryInfo gi);
Construcción de un dodecaedro
– Tiene 20 vértices y 12 caras
– Se crea un objeto GeometryInfo para crear las caras
– Los 20 vértices son:
(1, 1, 1),
(0, 1/f, f), (f, 0, 1/f), (1/f, f, 0),
(-1, 1, 1), (0, -1/f, f), (1, -1, 1),
(f, 0, -1/f), (1, 1, -1), (-1/f, f, 0),
(-f, 0, 1/f), (-1, -1, 1), (1/f, -f, 0),
(1, -1, -1), (0, 1/f, -f), (-1, 1, -1),
(-1/f, -f, 0), (-f, 0, -1/f), (0, -1/f, -f),
(-1, -1, -1)
donde f =
√ 5+1
2
Clase Dodecaedro
package modelos3d;
import com.sun.j3d.utils.geometry.*;
import java.awt.*;
import javax.vecmath.*;
@Override
public void asignarVertices() {
float phi = (float) (0.5 * (Math.sqrt(5) + 1));
float r = radio;
float p = phi * r;
float q = r / phi;
v = new Point3f[]{
new Point3f( r, r, r),
new Point3f( p, 0, q),
new Point3f( r,-r, r),
new Point3f( 0,-q, p),
new Point3f( 0, q, p),
new Point3f( q, p, 0),
new Point3f( r, r,-r),
new Point3f( p, 0,-q),
new Point3f( r,-r,-r),
new Point3f( q,-p, 0),
new Point3f(-q,-p, 0),
new Point3f(-r,-r, r),
new Point3f(-p, 0, q),
new Point3f(-r, r, r),
new Point3f(-q, p, 0),
new Point3f(-r, r,-r),
new Point3f( 0, q,-p),
new Point3f( 0,-q,-p),
new Point3f(-r,-r,-r),
new Point3f(-p, 0,-q)
};
iV = new int[]{
4, 3, 2, 1, 0,
5, 0, 1, 7, 6,
1, 2, 9, 8, 7,
3,11,10, 9, 2,
13,12,11, 3, 4,
14,13, 4, 0, 5,
16,15,14, 5, 6,
7, 8,17,16, 6,
8, 9,10,18,17,
18,10,11,12,19,
15,19,12,13,14,
17,18,19,15,16
};
}
@Override
public void asignarColores() {
iC = new int[60];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 5; j++) {
iC[i * 5 + j] = i;
}
}
}
@Override
public void crearForma() {
GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
int[] tira = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
gi.setCoordinates(v);
gi.setCoordinateIndices(iV);
gi.setColors(c);
gi.setColorIndices(iC);
gi.setStripCounts(tira);
setGeometry(gi.getGeometryArray());
}