Ejemplos de Java3D
Oscar A. Ch´
avez Bosquez
Contenido
1
Introducci´
on
Conceptos b´
asicos
2
Ejemplos
Hola mundo en 3D
Rotaci´
on simple
Rotaci´
on compuesta
Animaci´
on
Rotaci´
on y animaci´
on
Color de fondo
¿Que es Java3D?
API de Java
Aplicaciones y applets con gr´
aficos en tres dimensiones
Paradigma de mundos virtuales
¿Que es Java3D?
API de Java
Aplicaciones y applets con gr´
aficos en tres dimensiones
Paradigma de mundos virtuales
¿Que es Java3D?
API de Java
Aplicaciones y applets con gr´
aficos en tres dimensiones
Paradigma de mundos virtuales
¿Que es Java3D?
API de Java
Aplicaciones y applets con gr´
aficos en tres dimensiones
Paradigma de mundos virtuales
Ventajas y desventajas
Ventajas
Software libre
Multiplataforma
Creaci´
on de animaciones y juegos
No utiliza demasiados recursos de c´
omputo
Se pueden explotar todas las capacidades del lenguaje
Desventajas
Es m´
as lento que otras alternativas
Solamente se pueden crear applets y aplicaciones
Ventajas y desventajas
Ventajas
Software libre
Multiplataforma
Creaci´
on de animaciones y juegos
No utiliza demasiados recursos de c´
omputo
Se pueden explotar todas las capacidades del lenguaje
Desventajas
Es m´
as lento que otras alternativas
Solamente se pueden crear applets y aplicaciones
Conceptos clave
Universo virtual
: Representa el
entorno 3D que contiene las
escenas
Escena
: Contiene un elemento
gr´
afico (que puede tener
comportamiento)
Sistema de coordenadas espacial
Sistema de referencia
Formado por tres rectas
perpendiculares entre s´ı
(X, Y, Z)
se cortan en el
origen
(0, 0, 0)
Coordenadas de cada punto en
Universo conceptual
Al sistema de coordenadas se le
conoce como
universo virtual
La pantalla se encuentra viendo
hacia abajo del eje Z
Los valores positivos del eje X a
la derecha
Los valores positivos del eje Y
hacia arriba
El punto
(0,0,0)
se encuentra
al centro
Unidad de ´
angulo
Unidades en
radianes
:
´
Unidad de ´
angulo
Unidades en
radianes
:
´
HolaUniverso.java
1 i m p o r t j a v a . awt . G r a p h i c s C o n f i g u r a t i o n ; 2 i m p o r t com . s u n . j 3 d . u t i l s . u n i v e r s e .∗; 3 i m p o r t com . s u n . j 3 d . u t i l s . g e o m e t r y .C o l o r C u b e; 4 i m p o r t j a v a x . m e d i a . j 3 d .∗;
5 i m p o r t j a v a x . v e c m a t h .∗; 6 i m p o r t j a v a x . s w i n g . J A p p l e t ; 7 i m p o r t j a v a x . s w i n g . JFrame ;
8 i m p o r t com . s u n . j 3 d . u t i l s . a p p l e t . JMainFrame ; 9 p u b l i c c l a s s H o l a U n i v e r s o e x t e n d s J A p p l e t { 10 p u b l i c H o l a U n i v e r s o ( ) {
11 G r a p h i c s C o n f i g u r a t i o n c o n f i g=S i m p l e U n i v e r s e. g e t P r e f e r r e d C o n f i g u r a t i o n ( ) ; 12 Canvas3D c a n v a s 3 D =new Canvas3D ( c o n f i g ) ;
13 t h i s. add ( c a n v a s 3 D ) ;
14 S i m p l e U n i v e r s e s i m p l e U =new S i m p l e U n i v e r s e( c a n v a s 3 D ) ; 15 B r a n c h G r o u p e s c e n a = c r e a r E s c e n a G r a p h ( ) ;
16 e s c e n a . c o m p i l e ( ) ;
17 s i m p l e U . g e t V i e w i n g P l a t f o r m ( ) . s e t N o m i n a l V i e w i n g T r a n s f o r m ( ) ; 18 s i m p l e U . a d d B r a n c h G r a p h ( e s c e n a ) ;
19 }
20 p u b l i c B r a n c h G r o u p c r e a r E s c e n a G r a p h ( ) { 21 B r a n c h G r o u p e s c e n a = new B r a n c h G r o u p( ) ; 22 C o l o r C u b e c u b o = new C o l o r C u b e( 0 . 5 ) ; 23 e s c e n a . a d d C h i l d ( c u b o ) ;
24 r e t u r n e s c e n a ; 25 }
26 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { 27 H o l a U n i v e r s o u n i v e r s o =new H o l a U n i v e r s o ( ) ; 28 JFrame f o r m a =new JMainFrame ( u n i v e r s o , 2 5 6 , 2 5 6 ) ; 29 }
RotacionSimple.java
1
p u b l i c
B r a n c h G r o u p
c r e a r E s c e n a G r a p h ( )
{
2 // C r e a un o b j e t o de t i p o e s c e n a3
B r a n c h G r o u p
e s c e n a =
new
B r a n c h G r o u p
( ) ;
4 // C o l o r C u b e e s una c l a s e u t i l i t a r i a5
C o l o r C u b e
cubo =
new
C o l o r C u b e
( 0 . 5 ) ;
6 // E l o b j e t o r o t a c i o n t i e n e una m a t r i z t r a n s f o r m a d a c o m p u e s t a 7T r a n s f o r m 3 D
r o t a c i o n =
new
T r a n s f o r m 3 D
( ) ;
8 // Rota 45 g r a d o s9
r o t a c i o n . r o t X ( Math . P I / 4 . 0 ) ;
10 // C r e a un o b j e t o r o t a d o11
T r a n s f o r m G r o u p
o b j R o t a d o =
new
T r a n s f o r m G r o u p
( r o t a c i o n ) ;
12 // A p l i c a l a r o t a c i ´o n a l c u b o13
o b j R o t a d o . a d d C h i l d ( cubo ) ;
14 // A g r e g a e l e l e m e n t o a l a e s c e n a 15e s c e n a . a d d C h i l d ( o b j R o t a d o ) ;
16r e t u r n
e s c e n a ;
RotacionCompuesta.java
1
p u b l i c
B r a n c h G r o u p
c r e a r E s c e n a G r a p h ( )
{
2B r a n c h G r o u p
e s c e n a =
new
B r a n c h G r o u p
( ) ;
3C o l o r C u b e
cubo =
new
C o l o r C u b e
( 0 . 5 ) ;
4T r a n s f o r m 3 D
r o t a c i o n =
new
T r a n s f o r m 3 D
( ) ;
5T r a n s f o r m 3 D
o t r a R o t a c i o n =
new
T r a n s f o r m 3 D
( ) ;
6r o t a c i o n . r o t X ( Math . P I / 4 . 0 ) ;
7
o t r a R o t a c i o n . r o t Y ( Math . P I / 4 . 0 ) ;
8 // A g r e g a l a o t r a R o t a c i o n9
r o t a c i o n . mul ( o t r a R o t a c i o n ) ;
10
T r a n s f o r m G r o u p
o b j R o t a d o =
new
T r a n s f o r m G r o u p
( r o t a c i o n ) ;
11o b j R o t a d o . a d d C h i l d ( cubo ) ;
12
e s c e n a . a d d C h i l d ( o b j R o t a d o ) ;
13r e t u r n
e s c e n a ;
Animacion.java
1
p u b l i c
B r a n c h G r o u p
c r e a r E s c e n a G r a p h ( )
{
2B r a n c h G r o u p
e s c e n a =
new
B r a n c h G r o u p
( ) ;
3C o l o r C u b e
cubo =
new
C o l o r C u b e
( 0 . 5 ) ;
4
T r a n s f o r m G r o u p
o b j G i r a d o =
new
T r a n s f o r m G r o u p
( ) ;
5 // P e r m i t e m o d i f i c a r l a s p r o p i e d a d e s de un o b j e t o6
o b j G i r a d o . s e t C a p a b i l i t y (
7
T r a n s f o r m G r o u p
. ALLOW TRANSFORM WRITE ) ;
8o b j G i r a d o . a d d C h i l d ( cubo ) ;
9
e s c e n a . a d d C h i l d ( o b j G i r a d o ) ;
10 // C r e a l a f u n c i o n que v a r i a c o n e l t i e m p o p a r a g e n e r a r l a a n i m a c i o n 11
A l p h a
r o t a c i o n A l p h a =
new
A l p h a
(
−
1 , 4 0 0 0 ) ;
12 // C r e a un n u e v o o b j e t o de c o m p o r t a m i e n t o13
R o t a t i o n I n t e r p o l a t o r
r o t a d o r=
new
R o t a t i o n I n t e r p o l a t o r
(
14r o t a c i o n A l p h a ,
o b j G i r a d o ) ;
15 // C r e a e l o b j e t o que d e l i m i t a l a r e g i o n d e l c o m p o r t a m i e n t o16
B o u n d i n g S p h e r e
l i m i t e s =
new
B o u n d i n g S p h e r e
( ) ;
17r o t a d o r . s e t S c h e d u l i n g B o u n d s ( l i m i t e s ) ;
18
o b j G i r a d o . a d d C h i l d ( r o t a d o r ) ;
19r e t u r n
e s c e n a ;
RotaAnima.java
1 p u b l i c B r a n c h G r o u p c r e a r E s c e n a G r a p h ( ) { 2 B r a n c h G r o u p e s c e n a = new B r a n c h G r o u p( ) ; 3 C o l o r C u b e c u b o =new C o l o r C u b e( 0 . 5 ) ; 4 T r a n s f o r m 3 D r o t a c i o n = new T r a n s f o r m 3 D( ) ; 5 T r a n s f o r m 3 D o t r a R o t a c i o n = new T r a n s f o r m 3 D( ) ; 6 r o t a c i o n . r o t X ( Math . P I / 4 . 0 ) ;
7 o t r a R o t a c i o n . r o t Y ( Math . P I / 5 . 0 ) ; 8 r o t a c i o n . mul ( o t r a R o t a c i o n ) ;
9 T r a n s f o r m G r o u p o b j R o t a d o =new T r a n s f o r m G r o u p( r o t a c i o n ) ; 10 T r a n s f o r m G r o u p o b j G i r a d o =new T r a n s f o r m G r o u p( ) ;
11 o b j G i r a d o . s e t C a p a b i l i t y (T r a n s f o r m G r o u p. ALLOW TRANSFORM WRITE ) ; 12 o b j G i r a d o . a d d C h i l d ( c u b o ) ;
13 e s c e n a . a d d C h i l d ( o b j R o t a d o ) ; 14 o b j R o t a d o . a d d C h i l d ( o b j G i r a d o ) ; 15 T r a n s f o r m 3 D e j e Y =new T r a n s f o r m 3 D( ) ;
16 // C r e a l a f u n c i o n que v a r i a c o n e l t i e m p o p a r a g e n e r a r l a a n i m a c i o n 17 A l p h a r o t a c i o n A l p h a =new A l p h a(−1 , 4 0 0 0 ) ;
18 // C r e a un n u e v o o b j e t o de c o m p o r t a m i e n t o que e j e c u t a l a o p e r a c i o n en e l 19 // o b j e t o de t r a s n f o r m a c i o n e s p e c i f i c a d o y l o a g r e g a a l g r a f i c o de l a e s c e n a 20 R o t a t i o n I n t e r p o l a t o r r o t a d o r = new R o t a t i o n I n t e r p o l a t o r( r o t a c i o n A l p h a ,
21 o b j G i r a d o ,
22 e j e Y ,
23 0 . 0 f ,
24 (f l o a t) Math . P I∗2 . 0 f ) ;
25 B o u n d i n g S p h e r e l i m i t e s = new B o u n d i n g S p h e r e( ) ; 26 r o t a d o r . s e t S c h e d u l i n g B o u n d s ( l i m i t e s ) ;
27 o b j G i r a d o . a d d C h i l d ( r o t a d o r ) ; 28 r e t u r n e s c e n a ;
Fondo.java
1 i m p o r t j a v a . awt . G r a p h i c s C o n f i g u r a t i o n ; 2 i m p o r t com . s u n . j 3 d . u t i l s . u n i v e r s e .∗; 3 i m p o r t j a v a x . m e d i a . j 3 d .∗;
4 i m p o r t j a v a x . s w i n g . J A p p l e t ; 5 i m p o r t j a v a x . s w i n g . JFrame ;
6 i m p o r t com . s u n . j 3 d . u t i l s . a p p l e t . JMainFrame ; 7 p u b l i c c l a s s Fondo e x t e n d s J A p p l e t { 8 p u b l i c Fondo ( ) {
9 G r a p h i c s C o n f i g u r a t i o n c o n f i g = S i m p l e U n i v e r s e. g e t P r e f e r r e d C o n f i g u r a t i o n ( ) ; 10 Canvas3D c a n v a s 3 D =new Canvas3D ( c o n f i g ) ;
11 t h i s. add ( c a n v a s 3 D ) ;
12 S i m p l e U n i v e r s e s i m p l e U =new S i m p l e U n i v e r s e( c a n v a s 3 D ) ; 13 B r a n c h G r o u p e s c e n a F o n d o = c r e a F o n d o ( ) ;
14 s i m p l e U . a d d B r a n c h G r a p h ( e s c e n a F o n d o ) ; 15 }
16 p u b l i c B r a n c h G r o u p c r e a F o n d o ( ) {
17 B r a n c h G r o u p e s c e n a = new B r a n c h G r o u p( ) ;
18 B a c k g r o u n d f o n d o =new B a c k g r o u n d ( 1 . 0 f , 1 . 0 f , 1 . 0 f ) ; 19 B o u n d i n g S p h e r e l i m i t e s =new B o u n d i n g S p h e r e( ) ; 20 f o n d o . s e t A p p l i c a t i o n B o u n d s ( l i m i t e s ) ;
21 e s c e n a . a d d C h i l d ( f o n d o ) ; 22 r e t u r n e s c e n a ;
23 }
24 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { 25 Fondo u n i v e r s o =new Fondo ( ) ;
26 JFrame f o r m a =new JMainFrame ( u n i v e r s o , 2 5 6 , 2 5 6 ) ; 27 }