Вы находитесь на странице: 1из 34

Graficación

UNIDAD III.- GRAFICACIÓN 3D


___________________________________________________________________
LECCIÓN 3.3.- Visualización de objetos
___________________________________________________________________

3.3.1.- Introducción

– Una forma (Shape3D) es el bloque constructor de un modelo gráfico 3D.


– Una forma está definida por su geometría y su apariencia.
– Geometría: Descripción matemática de la forma, del tamaño y de otras propiedades
estructurales.
– Apariencia: Define el color, la textura, las propiedades del material del objeto.
– La geometría de los objetos puede ser construida a través de primitivas: puntos, lineas,
triángulos, ...

– Clases de Java3D:
– Shape3D: Nodo hoja para formas geométricas tridimensionales.
– Geometry: Define la geometría
– Appearance: Define la apariencia.

3.3.2.- Puntos y Vectores

– Un punto se modela matemáticamente como un vector.


– Un vector n-dimensional es una n-tupla de números: (x 1,x2,...,xn)
– En 3D un punto es un Vector3D (x,y,z).
– En coordenadas homogéneas un punto es un Vector4D (x,y,z,w)
– Matemáticamente no hay diferencia entre punto y vector. La diferencia existe en la
interpretación.

Rafael Rivera López


1
Graficación

– La construcción y transformación geométrica depende del concepto de vector.

– 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

Rafael Rivera López


2
Graficación

package puntosyvectores;

import javax.vecmath.*;

public class PuntosYVectores {

public static void main(String[] args) {


manejaPuntos();
manejaVectores();
}

public static void manejaPuntos() {


System.out.println("Manejo de Puntos");
Point3d p1 = new Point3d(1.0, 2.3, 0.0);
Point3d p2 = new Point3d(1.0, 2.0, 0.0);
System.out.println("p1 = " + p1);
System.out.println("p2 = " + p2);
double dist = p1.distance(p2);
System.out.println("Distancia entre p1 y p2 =" + dist);
Point3d p3 = new Point3d(p1);
p3.add(p2);
System.out.println("La suma p1 + p2 = " + p3);
p3.set(p1);
p3.sub(p2);
System.out.println("La resta p1 - p2 = " + p3);
}

public static void manejaVectores() {


System.out.println("\nManejo de Vectores");
Vector3f v1 = new Vector3f(1.0f, 2.3f, 0.0f);
Vector3f v2 = new Vector3f(1.0f, 2.0f, 0.0f);
System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);
double angle = v1.angle(v2);
System.out.println("Angulo=" + Math.toDegrees(angle));
System.out.println("Producto Escalar v1*v2 = " + v1.dot(v2));
System.out.println("Longitud de v1 = " + v1.length());
Vector3f v3 = new Vector3f();
v3.cross(v1, v2);
System.out.println("Producto Vectorial v1 x v2 =" + v3);
v3.cross(v2, v1);
System.out.println("Producto Vectorial v2 x v1 =" + v3);
}
}

Rafael Rivera López


3
Graficación

3.3.3.- Prueba de Formas 3D


Clases para desplegar las formas 3D

Clase: CreacionDeFormas3D
package creaciondeformas3d;

import javax.swing.*;

public class CreacionDeFormas3D {

public static void main(String[] args) {


PanelFormas panel = new PanelFormas();
JFrame f = new JFrame("Creación de formas 3D");
f.setSize(800, 600);
f.setLocation(50, 50);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.setVisible(true);
}
}
Clase: PanelFormas
package creaciondeformas3d;

public class PanelFormas extends javax.swing.JPanel {

public PanelFormas() {
initComponents();
}

private void initComponents() {

panelCentro = new javax.swing.JTabbedPane();

setBorder(javax.swing.BorderFactory.createLineBorder(
getBackground(), 20));
setLayout(new java.awt.BorderLayout());
add(panelCentro, java.awt.BorderLayout.CENTER);
}// </editor-fold>

// Variables declaration - do not modify


private javax.swing.JTabbedPane panelCentro;
// End of variables declaration
}

Rafael Rivera López


4
Graficación

Clase PanelForma3D (se adicionará a cada etiqueta del panelCentro)


package creaciondeformas3d;

import javax.media.j3d.*;
import modelos3d.*;

public class PanelForma3D extends Panel3D{

private final Forma3D forma;

public PanelForma3D(Forma3D forma){


this.forma = forma;
addUniverso();
setOrbitBehavior(true);
}

@Override
public Node addNodoPrincipal() {
BranchGroup bg = new BranchGroup();
bg.addChild(forma);
return bg;
}
}

3.3.4.- Geometría

– Las formas geométricas 3D son modelados como puntos, líneas y superficies.


– Las superficies 3D son complejas, por lo que se utiliza una colección de polígonos
simples (triángulos y cuadriláteros o splines).
– Java3D soporta arreglos de puntos, líneas, triángulos o cuadriláteros como elementos
para construir la geometría de una forma.
– El nodo hoja Shape3D representa objetos visuales, utilizando objetos Geometry y
Appearance.

Rafael Rivera López


5
Graficación

Todo Shape3D tiene los siguientes métodos:


• setGeometry(Geometry geo);
• setAppearance(Appearance ap);

Rafael Rivera López


6
Graficación

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

Si la forma usa solo coordenadas:


int mascara = GeometryArray.COORDINATES;

Si la forma usa coordenadas y colores:


int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3 ;

Si la forma usa coordenadas, colores y normales:

int mascara = GeometryArray.COORDINATES |


GeometryArray.COLOR_3 |
GeometryArray.NORMALS;

Los métodos para asociar los elementos a la forma son:

Rafael Rivera López


7
Graficación

• setColor(int index, Color3f color)


• setColors(int startIndex, Color3f[] colors)
• setCoordinate(int index, Point3f coord)
• setCoordinates(int startIndex, Point3f[] coords)
• setNormal(int index, Vector3f normal)
• setNormals(int startIndex, Vector3f[] normals)
• setTextureCoordinate(int index, Point3f texCoord))
• setTextureCoordinates(int startIndex, Point3f[] texCoords)

Clase Forma3D
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public abstract class Forma3D extends Shape3D {

protected Point3f[] v; // VERTICES SIN REPETIRSE


protected Color3f[] c; // COLORES SIN REPETIRSE
protected Point3f[] vC; // VERTICES QUE DEFINEN LAS CARAS DEL OBJETO
protected Color3f[] cC; // COLORES DE LAS CARAS

public Forma3D() {
setAppearance(new Appearance());
crearColores();
}

public final void addComponentes(Color color) {


asignarVertices();
if (color == null) {
asignarColores();
} else {
asignarColores(color);
}
crearForma();
}

public final void crearColores() {


c = new Color3f[]{
new Color3f(Color.RED), new Color3f(Color.GREEN),
new Color3f(Color.BLUE), new Color3f(Color.CYAN),
new Color3f(Color.MAGENTA), new Color3f(Color.YELLOW),
new Color3f(Color.DARK_GRAY), new Color3f(Color.GRAY),

Rafael Rivera López


8
Graficación

new Color3f(Color.LIGHT_GRAY), new Color3f(Color.ORANGE),


new Color3f(Color.PINK), new Color3f(Color.BLACK)
};
}

public final void asignarColores(Color color) {


cC = new Color3f[vC.length];
for (int i = 0; i < cC.length; i++) {
cC[i] = new Color3f(color);
}
}

public abstract void asignarVertices();

public abstract void asignarColores();

public abstract void crearForma();


}

3.3.5.- Arreglos Básicos

Los arreglos básicos son:


PointArray LineArray TriangleArray QuadArray

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.

Rafael Rivera López


9
Graficación

Clase Lineas3D
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Lineas3D extends Forma3D {

public Lineas3D(Color color){


addComponentes(color);
}

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)
};

Rafael Rivera López


10
Graficación

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;

LineArray la = new LineArray(vC.length, mascara);


la.setCoordinates(0, vC);
la.setColors(0, cC);
setGeometry(la);
}
}
Se debe adicionar al PanelFormas una etiqueta para
...
public class PanelFormas extends javax.swing.JPanel {

public PanelFormas() {
initComponents();
panelCentro.addTab("Líneas", new PanelForma3D(new Lineas3D()));
}
...
}

Rafael Rivera López


11
Graficación

Clase XYZ: Para crear los ejes coordenados


package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class XYZ extends Forma3D{

public XYZ(Color color){


addComponentes(color);
}

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;

LineArray la = new LineArray(vC.length,mascara);


la.setCoordinates(0,vC);
la.setColors(0,cC);
setGeometry(la);
}
}

Rafael Rivera López


12
Graficación

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.

Rafael Rivera López


13
Graficación

Los triángulos serían:


0-2-1 5-3-4 5-2-3 3-2-0
3-0-4 4-0-1 4-1-5 5-1-2

Rafael Rivera López


14
Graficación

package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class PrismaTriangular extends Forma3D {


private float ancho;
private float largo;

public PrismaTriangular(float ancho, float largo) {


this(ancho,largo,null);
}

public PrismaTriangular(float ancho, float largo, Color color) {


this.ancho = ancho;
this.largo = largo;
addComponentes(color);
}

@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

Rafael Rivera López


15
Graficación

public void asignarColores() {


cC = new Color3f[]{
c[0], c[0], c[0],
c[1], c[1], c[1],
c[2], c[2], c[2], c[2], c[2], c[2],
c[3], c[3], c[3], c[3], c[3], c[3],
c[4], c[5], c[6], c[7], c[8], c[9]
};
}

@Override
public void crearForma() {
int mascara = GeometryArray.COORDINATES | GeometryArray.COLOR_3;

TriangleArray ta = new TriangleArray(vC.length, mascara);


ta.setCoordinates(0, vC);
ta.setColors(0, cC);
setGeometry(ta);
}
}

3.3.7.- QuadArray
– Define una superficie formada por cuadriláteros.
– Cuatro vértices definen un cuadrilátero.

Rafael Rivera López


16
Graficación

Clase PrismaRectangular
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class PrismaRectangular extends Forma3D {


private float ancho;
private float alto;
private float largo;

public PrismaRectangular(float ancho, float alto, float largo) {


this(ancho, alto, largo, null);
}

public PrismaRectangular(float ancho, float alto, float largo, Color color) {


this.ancho = ancho;
this.alto = alto;
this.largo = largo;
addComponentes(color);
}

@Override
public void asignarVertices() {
float x = largo/2;
float y = alto/2;
float z = ancho/2;

Rafael Rivera López


17
Graficación

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;

QuadArray geo = new QuadArray(vC.length, mascara);


geo.setCoordinates(0, vC);
geo.setColors(0, cC);
setGeometry(geo);
}
}

Rafael Rivera López


18
Graficación

3.3.8.- Arreglos con vértices compatidos (GeometryStripArray)


– En ocasiones un vértice de un arreglo es compartido por varios polígonos.
– La clase GeometryStripArray utiliza la idea de secuencia para compartir vértices.
– Se utiliza un arreglo de enteros para definir las secuencias o tiras

Rafael Rivera López


19
Graficación

Las tiras de triángulos serían:


• 8-2-9-3-10-4-11-5-6-0-7-1-8-2 (tira con 14 puntos)
• 5-4-0-3-1-2 (tira con 6 puntos)
• 11-6-10-7-9-8 (tira con 6 puntos)

Rafael Rivera López


20
Graficación

Clase: PrismaHexagonal
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class PrismaHexagonal extends Forma3D {

private float ancho;


private float largo;

public PrismaHexagonal(float ancho, float largo) {


this(ancho, largo, null);
}

public PrismaHexagonal(float ancho, float largo, Color color) {


this.ancho = ancho;
this.largo = largo;
addComponentes(color);
}

@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],

Rafael Rivera López


21
Graficación

c[3], c[2], c[2], c[3], c[3], c[2],c[2]


};
}

@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

Rafael Rivera López


22
Graficación

Clase Sombrilla
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Sombrilla extends Forma3D {


private int lados;
private float ancho;

public Sombrilla(int lados, float ancho) {


this(lados, ancho, null);
}

public Sombrilla(int lados, float ancho, Color color) {


this.lados = lados;
this.ancho = ancho;
addComponentes(color);
}

@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];
}

Rafael Rivera López


23
Graficación

@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);
}
}

3.3.9.- Arreglos de índices de puntos (IndexedGeometryArray)


• En lugar de definir los vértices a compartir, se define los índices de los vértices en un
arreglo de puntos.
• Un vértice solo se define una vez, y su índice aparece varias veces en el arreglo.
• El constructor de estos objetos necesita el número de puntos, la mascara y el número
de índices que se van a utilizar.
• Las clases que utilizan índices de puntos son IndexedPointArray, IndexedLineArray,
IndexedTriangleArray e IndexedQuadArray, IndexedGeometryStripArray

Ejemplo con IndexedTriangleArray

Rafael Rivera López


24
Graficación

Clase Octaedro
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Octaedro extends Forma3D {


private float ancho;
private int[] iV; // VECTOR DE INDICES DE VERTICES
private int[] iC; // VECTOR DE INDICES DE COLORES

public Octaedro(float ancho, Color color) {


this.ancho = ancho;
addComponentes(color);
}

public Octaedro(float ancho) {


this(ancho, null);
}

@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),

Rafael Rivera López


25
Graficación

new Point3f(0, -r, 0)


};
iV = new int[]{
4, 0, 1, 3, 0, 4, 3, 2, 0, 2, 1, 0,
3, 5, 2, 2, 5, 1, 5, 4, 1, 5, 3, 4
};
}

@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);
}
}

Rafael Rivera López


26
Graficación

3.3.10.- Arreglos de índices de puntos compartidos (IndexedGeometryStripArray)


• Se adicionan las tiras de puntos que se comparten.
• IndexedLineStripArray, IndexedTriangleStripArray y IndexedTriangleFanArray

Ejemplo de IndexedTriangleStripArray

Rafael Rivera López


27
Graficación

Clase Poliedro
package modelos3d;

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Poliedro extends Forma3D {

private float ancho;


private float alto;
private float largo;
private int[] iV;
private int[] iC;

public Poliedro(float ancho, float alto, float largo) {


this(ancho,alto,largo,null);
}

public Poliedro(float ancho, float alto, float largo, Color color) {


this.ancho = ancho;
this.alto = alto;
this.largo = largo;
addComponentes(color);
}

@Override
public void asignarVertices() {
float x1 = largo/2;
float x2 = x1 - ancho/2;
float y = alto/2;
float z = ancho/2;

Rafael Rivera López


28
Graficación

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);
}
}

Rafael Rivera López


29
Graficación

3.3.11.- Clase GeometryInfo

– Esta clase permite la definición de polígonos con caras mas generales


– Constructores:
public GeometryInfo(GeometryArray ga)
public GeometryInfo(int primitiveType)

– 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);

Rafael Rivera López


30
Graficación

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

Rafael Rivera López


31
Graficación

Clase Dodecaedro
package modelos3d;

import com.sun.j3d.utils.geometry.*;
import java.awt.*;
import javax.vecmath.*;

public class Dodecaedro extends Forma3D {

private int[] iV;


private int[] iC;
private float radio;

public Dodecaedro(float radio, Color color) {


this.radio = radio;
addComponentes(color);
}

public Dodecaedro(float radio) {


this(radio, null);
}

@Override
public void asignarVertices() {
float phi = (float) (0.5 * (Math.sqrt(5) + 1));

float r = radio;
float p = phi * r;
float q = r / phi;

Rafael Rivera López


32
Graficación

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;
}
}
}

Rafael Rivera López


33
Graficación

@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());
}

Rafael Rivera López


34

Вам также может понравиться