718122, 1107 JOGL - Cubo 30
JOGL - Cubo 3D
En los capitulos anteriores hemos visto cémo dibujar un triangulo 3d y rotarlo. Ahora, en este
capitulo, puede aprender cémo hacer un cubo 3D, cémo girarlo, cémo adjuntar una imagen en él
De la misma manera, este capitulo proporciona ejemplos para dibujar un cubo 3D y aplicarle
colores y adjuntarle una imagen
A continuacién se muestra el programa para dibujar un cubo 3D y aplicarle colores.
import java. awt.DisplayMode;
import. javax.media.opengl.GL2;
import javax.media. opengl .GLautoDrawable;
import. javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLeventListener;
import. javax.media.opengl.GLProfile;
import javax.media.opengl.awt .GLCanvas;
import javax.media.opengl.glu.GLU;
import javax. swing. JFrame;
import com. jogamp.opengl.util.FPSAnimator;
public class Cube implements GLEventListener {
public static DisplayMode dm, dm_old;
private GLU glu = new GLU();
private float rquad = 0.¢f;
@0verride
public void display( GLautoDrawable drawable ) {
final GL2 gl = drawable.getGL() .getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER BIT | GL2.GL_DEPTH_BUFFER_BIT );
gl.glLoadIdentity();
gl.glTranslatef( Of, ef, -5.ef )5
// Rotate The Cube On X, Y &Z
gl.glRotatef(rquad, 1.0f, 1.0f, 1.0);
//giving different colors to different sides
gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
hitpsstwwitutorialspoint comjoglogl_4_eube him 19‘71522, 1107 JOG -Cubo 30
gl.glColor3f(1f,0f,f); //red color
gl.glvertex3F(1.0f, 1.0f, -1.0F); // Top Right Of The Quad (Top)
gl.glvertex3f( -1.0f, 1.0f, -1.0F); // Top Left Of The Quad (Top)
gl.glvertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top)
gl.glvertex3f( 1.0, 1.6f, 1.0f ); // Bottom Right Of The Quad (Top)
gl.glColor3f( @f,1f,0F ); //green color
gl.glvertex3( 1.0, -1.0f, 1.0F ); // Top Right Of The Quad
gl.glvertex3f( -1.0f, -1.0f, 1.6F ); // Top Left Of The Quad
gl.glvertex3f( -1.0f, -1.0f, -1.0F ); // Bottom Left Of The Quad
gl.glvertex3f( 1.0f, -1.0f, -1.0 ); // Bottom Right Of The Quad
gl.glcolor3t( ef, 0f,1f ); //bLue color
gl.glvertex3f( 1.0, 1.0f, 1.0 ); // Top Right Of The Quad (Front)
gl.glvertex3F( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front)
gl.glVertex3f( -1.0f, -1.0f, 1.¢f ); // Bottom Left Of The Quad
gl.glvertex3f( 1.0, -1.0f, 1.0f ); // Bottom Right Of The Quad
gl.glColor3f( 1f,1f,ef ); //yellow (red + green)
gl.glvertex3f( 1.0f, -1.0f, -1.0F ); // Bottom Left Of The Quad
gl.glvertex3f( -1.0f, -1.0f, -1.0F ); // Bottom Right Of The Quad
gl.glvertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back)
gl.glvertex3f( 1.0, 1.0f, -1.0f ); // Top Left Of The Quad (Back)
gl.glColor3f( 1f,0f,1f ); //purple (red + green)
gl.glvertex3f( -1.0f, 1.0f, 1.0F ); // Top Right Of The Quad (Left)
gl.glvertex3F( -1.0f, 1.0f, -1.0F ); // Top Left Of The Quad (Left)
gl.glvertex3#( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
gl.glvertex3f( -1.0f, -1.0f, 1.0 ); // Bottom Right Of The Quad
gl.glColor3f( @f,1f, 1f ); //sky blue (blue +green)
gl.glvertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right)
gl.glvertex3F( 1.0f, 1.6F, 1.0f ); // Top Left Of The Quad
gl.glvertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
gl.glvertex3f( 1.0f, -1.0f, -1.6F ); // Bottom Right Of The Quad
gl.gl€nd(); // Done Drawing The Quad
gl.glFlush();
rquad -= 0.155
@override
public void dispose( GlautoDrawable drawable ) {
// TobO Auto-generated method stub
+
htps:wwuterilspoint comjoglfogl_34_cube him 219718122, 1107 JOGL - Cubo 30
@override
public void init( GLAutoDrawable drawable ) {
final GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel( GL2.GL_SMOOTH );
gl.glClearColor( of, of, Of, OF );
gl.glClearbepth( 1.0f );
gl.gl€nable( GL2.GL_DEPTH_TEST );
gl.glDepthFunc( GL2.GL_LEQUAL );
gl.glHint( GL2.GL_PERSPECTIVE_CORRECTION HINT, GL2.GL_NICEST );
@0verride
public void reshape( GLautoDrawable drawable, int x, int y, int width, int height
// TODO Auto-generated method stub
final GL2 gl = drawable. getGL().getL2();
if( height It;= @ )
height = 1;
final float h = ( float ) width / ( float ) height;
gl.glViewport( @, 2, width, height );
gl.giMatrixMode( GL2.GL_PROJECTION );
gl.glloadidentity();
glu.gluPerspective( 45.6F, h, 1.0, 20.0 );
gl.glMatrixMode( GL2.GL_MODELVIEW );
gl.glloadidentity();
public static void main( String[] args ) {
final GLProfile profile = GLProfile.get( GLProfile.GL2 );
GLCapabilities capabilities = new GLCapabilities( profile );
// The canvas
final GLCanvas glcanvas = new GLCanvas( capabilities );
Cube cube = new Cube();
glcanvas.addGLeventListener( cube );
glcanvas.setSize( 490, 490 );
final JFrame frame = new JFrame ("Multicolored cube" );
frame.getContentPane().add( glcanvas );
hips tutorialspoint comvjoglogl_34_eube him 319718122, 1107 JOGL - Cubo 30
frame.setSize( frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);
animator. start();
Cuando compila y ejecuta el programa anterior, se genera el siguiente resultado, Muestra un cubo
3D coloreado.
Aplicar textura al cubo
Se dan los siguientes pasos para aplicar textura a un cubo:
* Puede vincular la textura requerida al cubo usando el método
gl.glBindTexture(GL2.GL_TEXTURE_2D.texture) de la interfaz Drawable.
* Este método requiere un argumento de textura (int) junto con GL2.GL_TEXTURE_2D(int)
* Antes de ejecutar Display() , debe crear una variable de textura
* En el método init() 0 en las lineas iniciales del método glDisplay() , habilite la textura
usando el método gl.glEnable(GL2.GL_TEXTURE_2D)
hitpshwwtoriaspoint comjoalag|_3d_cube him 49718122, 1107
File f:
Texturs
texturs
JOGL - Cabo 30
Cree el objeto de textura, que necesita un objeto de archivo como parametro, que a su vez
necesita la ruta de la imagen utilizada como textura para el objeto.
‘ile = new File(“c:\\pictures\\boy. jpg”);
e t = texturelO.newTexture(file, true);
e = t.getTextureObject(gl);
* Manejar la excepcién ‘archivo no encontrado’
A contin
import
import
import
import
import
import
import
import
import
import
import
import
import
import
public
pub:
pri
pri
priy
@ov
pub’
wacién se muestra el programa para adjuntar una imagen en un cubo.
java.awt.DisplayMode;
java.io.File;
java.io. IOException;
Javax.media.opengl.GL2;
javax.media. opengl .GLautoDrawable;
javax.media.opengl.GLCapabilities;
javax.media.opengl.GLeventListener;
javax.media.opengl.GLProfile;
javax.media.opengl.awt .GLCanvas;
Javax.media.opengl.glu.GLU;
javax. swing. JFrame;
com, jogamp.opengl.util.FPSAnimator;
jogamp.opengl.util.texture. Texture;
com. jogamp. opengl. util. texture. TextureI0;
com.
class CubeTexture implements GLEventListener {
lic static DisplayMode dm, dm_old;
vate GLU glu = new GLU();
vate float xrot,yrot, zrot;
vate int texture;
erride
lic void display(GLautoDrawable drawable) {
// TODO Auto-generated method stub
final GL2 gl = drawable.getGL().get6L2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glloadidentity(); // Reset The View
el.elTranslatef(@f. Of. -5.0F):
hitpsshwwtorilepoint comjoalagl_3d_cube.him 59718122, 1107
JOGL - Cubo 30
gl.glRotatef(xrot, 1.0f, 1.0f, 1.0F);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zrot, 0.0, 8.0f, 1.0f);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
gl.glBegin(GL2.GL_QUADS) ;
// Front Face
gl.glTexCoord2f(@.0f, 0.0F);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glTexCoord2#(1.0f, 1.0F);
gl.glTexcoord2f(@.0f, 1.0F);
// Back Face
gl.glTexCoord2#(1.0f, 0.0F);
gl.glTexCoord2f (1.0, 1.0F);
gl.glTexCoord2#(@.0f, 1.0F);
gl.glTexCoord2f(0.0f, @.0F);
// Top Face
gl.glTexcoord2# (0.0, 1.0F);
gl.glTexCoord2# (8.0, 2.0F);
gl.glTexCoord2#(1.0f, 2.0F);
gl.glTexCoord2#(1.0f, 1.0F);
// Bottom Face
gl.glTexCoord2#(1.0f, 1.0F);
gl.glTexCoord2f(9.0f, 1.0f);
gl.glTexCoord2#(@.0f, 0.0F);
gl.glTexCoord2f(1.0f, 0.0F);
// Right face
gl.glTexCoord2f(1.0f, 0.0F);
gl.glTexCoord2f(1.0f, 1.0);
gl.glTexCoord2f(@.0f, 1.0F);
gl.glTexCoord2f(@.0f, 0.0F);
// Left Face
gl.glTexCoord2f(0.0f, 0.0f);
gl.glTexCoord2#(1.0f, 0.0F);
gl.glTexCoord2f(1.0F, 1.0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glend();
el.elFlush():
hitpshwwcttorialspoint comjoalog|_
{cube.him
gl.glVertex3#(-1.0F,
gl.glvertex3#( 1.0F,
gl.glVertex3F( 1.0f,
gl.glVertex3#(-1.0F,
gl.glVertex3#(-1.0F,
gl.glvertex3#(-1.0F,
gl.glvertex3F( 1.0f,
gl.glVertex3( 1.0f,
gl.glVertex3#(-1.0F,
gl.glvertex3#(-1.0F,
gl.glVertex3F( 1.0f,
gl.glVertex3( 1.0f,
gl
gl
gl
gl
gl
gl
gl
gl
-glvertex3#(-1.
-glvertex3#( 1.
sglvertex3#( 1.
-glVertex3#(-1.
-glvertex3#( 1.
-glvertex3#( 1.
-glvertex3#( 1.
-glvertex3#( 1.
oF,
oF,
oF,
oF,
oF,
oF,
oF,
oF,
gl.glVertex3#(-1.0F,
gl.glVertex3#(-1.0F,
gl.glVertex3#(-1.0F,
gl.glVertex3#(-1.0f,
-1.0f, 1.0f);
-1.0F, 1.0f);
1.0f, 1.0F);
1.0F, 1.0F)5
-1.0f, -1.0F);
1.0f, -1.0F)5
1.0f, -1.0F);
-1.0f, -1.0F);
1.0F,
1.0f,
1.0f,
1.0f,
-1.0f, -1.6F);
-1.0f, -1.6F)5
-1.0f, 1.0f);
-1.0f, 1.0f);
-1.0f, -1.6F);
1.0f, -1.0F);
1.0f, 1.0F);
-1.0F, 1.0f);
-1.0f, -1.0F)5
-1.0f, 1.0f);
1.0f, 1.0F);
1.0f, -1.0F);
69718122, 1107 JOGL - Cubo 30
//change the speeds here
xrot += .1;
yrot += .1f;
zrot += .1f;
+
@override
public void dispose(GLAutoDrawable drawable) {
// method body
@override
public void init(GLautoDrawable drawable) {
final GL2 gl = drawable. getGL().get6L2();
gl.glShadeModel (GL2.GL_SMOOTH) ;
gl.glClearColor(of, ef, Of, OF);
gl.glClearDepth(1.0F) 5
gl.gl€nable(GL2.GL_DEPTH_TEST) ;
gl.glDepthFunc(GL2.GL_LEQUAL) ;
gl.glHint (6L2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST) ;
MW
gl.glénable(GL2.GL_TEXTURE_2D)
try
File im = new File("E:\\office\\boy. jpg ");
Texture t = TextureI0.newTexture(im, true);
textures t.getTexturedbject (gl);
}eatch(TOException e){
e.printStackTrace();
@override
public void reshape(GLautoDrawable drawable, int x, int y, int width, int height) -
// TODO Auto-generated method stub
final GL2 gl = drawable.getGL().get6L2();
if(height 1t;= @)
height = 1;
hitpssiwwctoriaepoint comjoalagl_3d_cube him 719718122, 1107 JOGL - Cubo 30
final float h = (float) width / (float) height;
gl.glViewport(@, 2, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION) ;
gl.glloadidentity();
glu.gluPerspective(45.af, h, 1.0, 20.0);
gl.glMatrixMode(GL2.GL_MODELVIEW) ;
gl.glloadidentity();
public static void main(String[] args) {
// TODO Auto-generated method stub
final GLProfile profile = GLProfile.get(GLProfile.GL2)
GLCapabilities capabilities - new GLCapabilities (profile) ;
// The canvas
final GLCanvas glcanvas = new GLCanvas(capabilities) ;
CubeTexture r = new CubeTexture();
glcanvas.addGLeventListener(r);
glcanvas.setSize(400, 400);
final JFrame frame = new JFrame (" Textured Cube");
frame. getContentPane() .add(glcanvas) ;
frame. setSize (frame. getContentPane().getPreferredSize());
frame. setVisible(true) ;
final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);
animator. start();
Cuando compila y ejecuta el programa anterior, se genera el siguiente resultado, Puede ver un
cubo 3D con la textura deseada aplicada sobre él.
hitpsshwwtorialepoint comjoglagl_Sd_cube him 89718122, 1107
(B Tetured Cube
hitpshwwtoriaspoint comjoalag|_3d_cube him
sos.
uo 30
99