JOGL 3D Cuboid

JOGL 3D Cuboid with Introduction, Installation, Interfaces, Classes, Frame using AWT, Frame using Swing, 2D Objects, Primitives Shapes, 3D Cube, 3D Triangle, 3D Basic Shape, 3D Objects etc.

JOGL 3D Cuboid

JOGL 3D Cuboid

In the previous section, we create a cube which is hollow from inside. Now, we will create a solid cuboid and rotate it. While rotating, the cuboid will also move on the whole screen.

To create a solid graphical 3D object it is required to enable depth test. The gl.glEnable( GL2.GL_DEPTH_TEST ) method is used to enable it.

JCuboid.java

  1. package com.tpoint.jogl;  
  2.   
  3. import javax.media.opengl.*;  
  4. import javax.media.opengl.awt.GLCanvas;  
  5. import javax.media.opengl.glu.GLU;  
  6.   
  7. import javax.swing.JFrame;  
  8.   
  9. import com.jogamp.opengl.util.FPSAnimator;  
  10.   
  11. public class JCuboid implements GLEventListener {  
  12.   
  13.     
  14.    private GLU glu = new GLU();  
  15.    private float rotation = 0.0f;  
  16.         
  17.    @Override  
  18.    public void display( GLAutoDrawable drawable ) {  
  19.       
  20.       final GL2 gl = drawable.getGL().getGL2();  
  21.       gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );  
  22.       gl.glLoadIdentity();  
  23.       gl.glTranslatef( 0f, 0f, -5.0f );   
  24.   
  25.         
  26.       gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);   
  27.    
  28.       
  29.         
  30.         
  31.       gl.glBegin(GL2.GL_QUADS);   
  32.       gl.glColor3f(1f,0f,0f); //red color  
  33.         
  34.       //Top  
  35.       gl.glVertex3f(-1f, 0.8f, 0.5f);   
  36.       gl.glVertex3f(-0.5f, 0.8f, 0.5f);   
  37.       gl.glVertex3f(-1f,0.5f,0.5f );   
  38.       gl.glVertex3f(-0.5f, 0.5f,0.5f );   
  39.           
  40.       //Bottom  
  41.       gl.glVertex3f( -1f,0.8f, 0.9f );   
  42.       gl.glVertex3f( -1f, 0.5f, 0.9f );   
  43.       gl.glVertex3f( -0.5f, 0.5f, 0.9f );   
  44.       gl.glVertex3f(-0.5f, 0.8f, 0.9f );   
  45.   
  46.       //Front  
  47.       gl.glVertex3f(-0.5f, 0.8f, 0.5f );   
  48.       gl.glVertex3f( -1f, 0.8f, 0.5f );   
  49.       gl.glVertex3f( -1f, 0.8f, 0.9f );  
  50.       gl.glVertex3f( -0.5f, 0.8f, 0.9f);  
  51.       //Back  
  52.    
  53.       gl.glVertex3f(-0.5f,0.5f, 0.5f);   
  54.       gl.glVertex3f( -1f, 0.5f, 0.5f);   
  55.       gl.glVertex3f( -1f, 0.5f, 0.9f );   
  56.       gl.glVertex3f( -0.5f, 0.5f, 0.9f );   
  57.         
  58.       //Left  
  59.       gl.glVertex3f(-0.5f,0.8f, 0.9f   );   
  60.       gl.glVertex3f(  -0.5f, 0.8f, 0.5f );   
  61.       gl.glVertex3f(-0.5f,0.5f, 0.9f);  
  62.       gl.glVertex3f( -0.5f, 0.5f, 0.5f);  
  63.   
  64.       //Right  
  65.       gl.glVertex3f( -1f, 0.8f, 0.9f);   
  66.       gl.glVertex3f( -1f, 0.8f, 0.5f );   
  67.       gl.glVertex3f( -1f, 0.5f, 0.5f);   
  68.       gl.glVertex3f(-1f, 0.5f, 0.9f);   
  69.       gl.glEnd();   
  70.       gl.glFlush();  
  71.           
  72.       rotation -= 0.15f;  
  73.    }  
  74.      
  75.    @Override  
  76.    public void dispose( GLAutoDrawable drawable ) {  
  77.         
  78.    }  
  79.      
  80.    @Override  
  81.    public void init( GLAutoDrawable drawable ) {  
  82.       
  83.       final GL2 gl = drawable.getGL().getGL2();  
  84.       gl.glEnable( GL2.GL_DEPTH_TEST );  
  85.       
  86.    }  
  87.         
  88.    @Override  
  89.    public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {  
  90.       
  91.   
  92.       final GL2 gl = drawable.getGL().getGL2();  
  93.       if( height<=0 )  
  94.          height = 1;  
  95.               
  96.       final float h = ( float ) width / ( float ) height;  
  97.       gl.glViewport( 00, width, height );  
  98.       gl.glMatrixMode( GL2.GL_PROJECTION );  
  99.       gl.glLoadIdentity();  
  100.           
  101.       glu.gluPerspective( 45.0f, h, 1.020.0 );  
  102.       gl.glMatrixMode( GL2.GL_MODELVIEW );  
  103.       gl.glLoadIdentity();  
  104.    }  
  105.         
  106.    public static void main( String[] args ) {  
  107.       
  108.       final GLProfile gp = GLProfile.get( GLProfile.GL2 );  
  109.       GLCapabilities cap = new GLCapabilities( gp );  
  110.         
  111.         
  112.       final GLCanvas gc = new GLCanvas( cap );  
  113.       JCuboid cuboid = new JCuboid();  
  114.           
  115.       gc.addGLEventListener( cuboid );  
  116.       gc.setSize( 100100 );  
  117.           
  118.       final JFrame frame = new JFrame ( " JOGL Cuboid" );  
  119.       frame.add(gc);    
  120.       frame.setSize(500,400);    
  121.       frame.setVisible(true);   
  122.       final FPSAnimator animator = new FPSAnimator(gc, 300,true);  
  123.           
  124.       animator.start();  
  125.    }}