JOGL 3D Basic shape

JOGL 3D Basic Shape 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 Basic shape

JOGL 3D Basic shape

In three dimensional object, z-axis is also exist with x-axis and y-axis. These axis represents height, width and depth of an object. Thus, it represents the real view of an object.

JOGL 3D Example

In this example, we are creating a simple 3D object.

J3DBasic.java

  1. package com.tpoint.jogl;  
  2. import javax.media.opengl.*;  
  3. import javax.media.opengl.awt.GLCanvas;  
  4. import javax.media.opengl.glu.GLU;  
  5.   
  6. import javax.swing.JFrame;  
  7.   
  8. public class J3DBasic implements GLEventListener {  
  9. private GLU glu = new GLU();  
  10.       
  11.    @Override  
  12. public void display(GLAutoDrawable drawable) {  
  13.   
  14. final GL2 gl = drawable.getGL().getGL2();  
  15. gl.glTranslatef(0f, 0f,-3f);  
  16. gl.glBegin(GL2.GL_LINES);  
  17. gl.glVertex3f(-1f,0f,0);  
  18. gl.glVertex3f(0f,1f, 0);  
  19. gl.glEnd();  
  20.   
  21.       //3D  
  22. gl.glBegin(GL2.GL_LINES);  
  23. gl.glVertex3f(-1f,0f,-2f);  
  24. gl.glVertex3f(0f,1f,-2f);  
  25. gl.glEnd();  
  26.   
  27.       //top  
  28. gl.glBegin(GL2.GL_LINES);  
  29. gl.glVertex3f(-1f,0f,0);  
  30. gl.glVertex3f(-1f,0f,-2f);  
  31. gl.glEnd();  
  32.   
  33.       //bottom  
  34. gl.glBegin(GL2.GL_LINES);  
  35. gl.glVertex3f(0f,1f, 0);  
  36. gl.glVertex3f(0f,1f,-2f);  
  37. gl.glEnd();  
  38.    }  
  39.   
  40.    @Override  
  41. public void dispose(GLAutoDrawable arg0) {  
  42.       //method body  
  43.    }  
  44.       
  45.    @Override  
  46. public void init(GLAutoDrawable arg0) {  
  47.       // method body  
  48.    }  
  49.   
  50.    @Override  
  51. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {  
  52.       
  53.   
  54.       GL2 gl = drawable.getGL().getGL2();  
  55. if(height <= 0)  
  56. height = 1;  
  57.               
  58. final float h = (float) width / (float) height;  
  59. gl.glViewport(00, width, height);  
  60. gl.glMatrixMode(GL2.GL_PROJECTION);  
  61. gl.glLoadIdentity();  
  62.           
  63. glu.gluPerspective(40.0f, h, 1.518.0);  
  64. gl.glMatrixMode(GL2.GL_MODELVIEW);  
  65. gl.glLoadIdentity();  
  66.   
  67.    }  
  68.   
  69. public static void main(String[] args) {  
  70.   
  71.   
  72. final GLProfile gp = GLProfile.get(GLProfile.GL2);  
  73. GLCapabilities cap = new GLCapabilities(gp);  
  74.   
  75.   
  76. final GLCanvas gc = new GLCanvas(cap);  
  77.       J3DBasic b = new J3DBasic();  
  78. gc.addGLEventListener(b);  
  79. gc.setSize(600400);  
  80.   
  81.   
  82. final JFrame frame = new JFrame("JOGL 3D");  
  83.   
  84.   
  85. frame.getContentPane().add(gc);  
  86. frame.setSize(frame.getContentPane().getPreferredSize());  
  87. frame.setVisible(true);  
  88.    }  
  89.     }