JOGL 3D Cube

JOGL 3D Cube 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 Cube

JOGL 3D Cube

In this section, we will learn how to create a cube. So for that firstly create six quadrilaterals of same size and positioned them in such a way that by connecting them we will get a proper cube. We will also rotate this cube to see all its sides.

To create a rotating cube, go through the below example: -

J3DCube.java

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