GLCapabilities caps = new GLCapabilities();
caps.setDoubleBuffered( true );
caps.setHardwareAccelerated( true );
canvas = new GLCanvas( caps );
We have to ask this GLCapabilities object for a component that is tuned to the characteristics of the current display, such as its color depth.
The next thing we have to do with our canvas is to register it as a GLEventListener:
canvas.addGLEventListener(this);
The GLEventListener interface defines four methods:
In the sample application, init() is used for debug and to set the color for erasing the canvas (to white or black):
GL gl = drawable.getGL(); gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
The GL class is "the basic interface to OpenGL."
Think of it as a handle for making openGL (gl.h)
method calls.
glu.-type calls to glu.h are made via the
GLU object also retrieved from the GLDrawable.
The reshape() method sets or re-sets its size and "viewport," which represents what part of the component is being drawn into:
GL gl = drawable.getGL(); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum( -0.1f, 0.1f, -0.1f, 0.1f, 0.2f, 2.0f );or
GL gl = drawable.getGL(); GLU glu = drawable.getGLU(); gl.glViewport( 0, 0, width, height ); gl.glMatrixMode( GL.GL_PROJECTION ); gl.glLoadIdentity(); glu.gluOrtho2D( 0.0, 450.0, 0.0, 375.0);
The display() method performs the drawing after init() and reshape() at startup. It could also be called by JOGL's Animator class, which exists to call display() repeatedly from a loop. This has obvious use for games, media, and other applications -- they perform animation by slightly changing the component on each successive call to display().
display() also erases the component with:
gl.glClear(GL.GL_COLOR_BUFFER_BIT)