- JOGL Tutorial
- JOGL - Home
- JOGL - Overview
- JOGL - Installation
- JOGL Basic Templates
- JOGL - API for Basic Templates
- JOGL - Canvas with AWT
- JOGL - Canvas with Swing
- JOGL - GLJPanel Class
- JOGL Graphical Shapes
- JOGL - Drawing Basics
- JOGL - Drawing with GL_Lines
- JOGL - Pre-defined shapes
- JOGL Effects & Transformation
- JOGL - Transformation
- JOGL - Coloring
- JOGL - Scaling
- JOGL - Rotation
- JOGL - Lighting
- JOGL 3D Graphics
- JOGL - 3D Basics
- JOGL - 3D Triangle
- JOGL - 3D Cube
- JOGL - Appendix
- JOGL Useful Resources
- JOGL - Quick Guide
- JOGL - Useful Resources
- JOGL - Discussion
JOGL Basic Template
This chapter introduces you to the idea of writing a JOGL basic template.
Important interfaces and classes
To make your program capable of using JOGL graphical API, you need to implement GLEventListener interface.
GLEventListener interface
You can find GLEventListener interface in javax.media.opengl package.
Interface: GLEventListener
Package: javax.media.opengl
The following table gives details of various methods and descriptions of GLEventListener interface:
Sr. No. | Methods and Descriptions |
---|---|
1 |
Void display(GLAutoDrawable drawable) It is called by the object of GLAutoDrawable interface to initiate OpenGL rendering by the client. i.e., this method contains the logic used to draw graphical elements using OpenGL API. |
2 |
Void dispose(GLAutoDrawable drawable) This method signals the listener to perform the release of all OpenGL resources per each GLContext, such as memory buffers and GLSL programs. |
3 |
Void init(GLAutoDrawble drawble) It is called by the object of GLAutoDrawable interface immediately after the OpenGL context is initialized. |
4 |
Void reshape(GLAutoDrawble drawble,in tx,int y,int width ,int height) It is called by the object of GLAutoDrawable interface during the first repaint after the component has been resized. It is also called whenever the position of the component on the window, is changed. |
All the methods of GLEventListener require object of GLAutoDrawable interface as a parameter.
GLAutoDrawable interface
This interface supplies an event based mechanism (GLEventListener) for performing OpenGL rendering. GLAutoDrawable automatically creates a primary rendering context which is associated with GLAutoDrawable for the lifetime of the object.
Interface: GLAutoDrawble
Package: javax.media.opengl
Sr. No. | Methods and Descriptions |
---|---|
1 |
GL getGL() This method Returns the GL pipeline object that is used by the current object of GLAutoDrawable interface. |
2 |
Void addGLEventListener(GLEventListener Listener) This method adds the given listener to the end of current drawable queue. |
3 |
Void addGLEventListener(int index,GLEventListener listener) This method adds the given listener at the given index of this drawable queue. |
4 |
Void destroy() This method destroys all resources associated with this object of GLAutoDrawable inteface, including the GLContext. |
Note: There are other methods in this package. Only few important methods pertaining to template are discussed in this interface.
GLCanvas class
GLCanvas and GLJpanel are the two main classes of JOGL GUI that implement GLAutoDrawable interface, which allow them to be utilized as drawing surfaces for OpenGL commands.
GLCanvas is a heavyweight AWT component which provides OpenGL rendering support. This is the primary implementation of an AWTAutoGLDrawable interface. It also inherits java.awt.Canvas class. Since it is a heavyweight component, in certain cases, GLJCanvas may not integrate with swing component correctly. Therefore, care must be taken while using it with Swing. Whenever you face problems with GLJCanvas, then you must use GLJPanel class.
The hierarchical diagram of class GLCanvas can be as shown below:
GLEventistener interface works along with GLCanvas class, it responds to the changes in GLCanvas class and to the drawing requests made by them.
Whenever GLCanvas class is instantiated, the init() method of GLEventListener is invoked. You can override this method to initialize the OpenGL state.
Whenever GLCanvas is drawn initially (instantiated), or resized, the reshape() method of GLEventListener is executed. It is used to initialize the OpenGL viewport and projection matrix. It is also called whenever the component's location is changed.
Display() method of GLEventListener contains the code for rendering 3D scene. It is invoked whenever display() method of GLCanvas is invoked .
class: GLCanvas
package: javax.media.opengl.awt
Contructor
GLCanvas()
It creates a new GLCanvas component with a default set of OpenGL capabilities, us-ing the default OpenGL capabilities selection mechanism, on the default screen device.
GLCanvas(GLCapabilitiesImmutable)
It creates a new GLCanvas component with the requested set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism, on the default screen device.
Sr. No. | Methods and Description |
---|---|
1 |
Void addGLEventListener(GLEventListener listener) It adds the given listener to the end of this drawable queue |
2 |
Void addGLEventListener(int indexGLEventListener listener) It adds the given listener at the given index of this drawable queue. |
To instantiate GLCanvas class, you need object of GLCapabilitiesImmutable interface, which specifies an immutable set of OpenGL capabilities.
One of the ways to get an object of CapabilitiesImmutable interface is to instantiate GLCapabilities class, which implements the interface. An instance of GLCapabilities class can be used to serve the purpose.
GLCapabilities class
This class specifies a set of OpenGL capabilities. It takes GLCapabilities object as a parameter. The GLCapabilities class describes the desired capabilities that a rendering context must support, such as the OpenGL profile.
class: GLCapabilities
package: javax.media.opengl
Constructor
GLCapabilities(GLProfile glprofile)
Creates a GLCapabilities object.
To instantiate GLCanvas class, you need object of GLCapabilitiesImmutable interface, which specifies an immutable set of OpenGL capabilities.
One of the ways to get an object of CapabilitiesImmutable interface is to instantiate GLCapabilities class, which implements the interface. An instance of GLCapabilities class can be used to serve the purpose.
GLCapabilities class
This class specifies a set of OpenGL capabilities. It takes GLCapabilities object as a parameter. The GLCapabilities class describes the desired capabilities that a rendering context must support, such as the OpenGL profile.
class: GLCapabilities
package: javax.media.opengl
Constructor
GLCapabilities(GLProfile glprofile)
The GLCapabilities class in turn requires GLProfile object.
GLProfile class
Since several versions of OpenGL API were released, you need to specify the exact version of OpenGL API being used in your program to your Java Virtual Machine (JVM). This is done using GLProfile class. The get() method of this class accepts different predefined String objects as parameters. Each String object is a name of an interface and each interface supports certain versions of OpenGL. If you initialize this class as static and singleton, this class gives you singleton GLProfile objects for each available JOGL profile.
class: GLProfile
package: javax.media.opengl
Method and Description |
---|
Static GLProfile get(String profile) Uses the default device. |
As this is a static method, you need to invoke it using class name, and it requires a predefined static string variable as parameter. There are 12 such variables in this class, each represents an individual implementation of GL interface.
GLProfile.get(GLProfile.GL2);
The following table shows String parameters of get() method of GLProfile class:
Sr. No. | Predefined String value (Interface name) and Description |
---|---|
1 |
GL2 This interface contains all OpenGL [1.0 … 3.0] methods, as well as most of its extensions defined at the time of this specification. |
2 |
GLES1 This interface contains all OpenGL ES [1.0 ... 1.1] methods ,as well as most of its extensions defined at the time of this specification |
3 |
GLES2 This interface contains all OpenGL ES 2.0 methods, as well as most of its extensions defined at the time of this specification. |
4 |
GLES3 This interface contains all OpenGL ES 3.0 methods, as well as most of its extensions defined at the time of this specification. |
5 |
GL2ES1 This Interface contains the common subset of GL2 and GLES1. |
6 |
GL2ES2 This Interface contains the common subset of GL3, GL2 and GLES2. |
7 |
GL2GL3 This Interface contains the common subset of core GL3 (OpenGL 3.1+) and GL2. |
8 |
GL3 This interface contains all OpenGL [3.1 ... 3.3] core methods, as well as most of its extensions defined at the time of this specification. |
9 |
GL3bc This interface contains all OpenGL [3.1 ... 3.3] compatibility methods, as well as most of its extensions defined at the time of this specification. |
10 |
GL3ES3 Interface containing the common subset of core GL3 (OpenGL 3.1+) and GLES3 (OpenGL ES 3.0). |
11 |
GL4 This interface contains all OpenGL [4.0 ... 4.3] core methods, as well as most of its extensions defined at the time of this specification. |
12 |
GL4bc This interface contains all OpenGL [4.0 ... 4.3] compatibility profile, as well as most of its extensions defined at the time of this specification. |
13 |
GL4ES3 Interface containing the common subset of core GL4 (OpenGL 4.0+) and GLES3 (OpenGL ES 3.0). |
Now everything is set for our first program using JOGL.
Basic template using canvas with AWT
Using JOGL programming, it is possible to draw various graphical shapes such as straight
lines, triangles, 3D shapes including special effects such as rotation, lighting, colors etc.
The basic template of JOGL programming is given below:
Step1: Creating the class
Initially create a class that implements GlEventListener interface and import the package javax.media.opengl. Implement all four methods display(), dispose(), reshape(), init(). Since this is the basic frame, primitive tasks such as creating canvas class, adding it to frame were discussed. All the GLEVentListener interface methods were left unimplemented.
Step2: Preparing the canvas
(a)Constructing the GLCanvas class object
final GLCanvas glcanvas = new GLCanvas( xxxxxxx ); //here capabilities obj should be passed as parameter
(b)Instantiating the GLCapabilities class
GLCapabilities capabilities = new GLCapabilities( xxxxx ); //here profile obj should be passed as parameter
(c)Generating GLProfile object
As it is the static method, it is invoked using class name. Since this tutorial is about JOGL2, let us generate GL2 interface object.
final GLProfile profile = GLProfile.get( GLProfile.GL2 ); // both, variable and method are static hence both are called using class name.
Let us see the code snippet for canvas
//getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities);
(d)Now add GLEventListener to the canvas using the method addGLEventListener(). This method needs object of GLEventListener interface as parameter. Hence, pass object of a class that implements GLEventListener.
BasicFrame basicframe=newBasic Frame( );// class which implements GLEventListener interface glcanvas.addGLEventListener( basicframe );
(e) Set size of the frame using setSize() method inherited by GLCanvas from javax.media.opengl.awt.AWTGLAutoDrawable.
glcanvas.setSize( 400, 400 );
Now you are ready with GLCanvas.
Step3: Creating the Frame
Create the frame by instantiating the Frame class Object of JSE AWT frame component.
Add canvas to it and make the frame visible.
//creating frame final Frame frame = new frame( " Basic Frame" ); //adding canvas to frame frame.add( glcanvas ); frame.setVisible( true );
Step 4: Viewing frame in full screen
For full screen view of frame, get the default screen size using java.awt.Toolkit class. Now, using those default screen size dimensions, set the frame size using setSize() method.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(screenSize.width, screenSize.height);
Let us go through the program to generate basic frame using AWT:
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.swing.JFrame; public class BasicFrame implements GLEventListener{ @Override public void display(GLAutoDrawable arg0) { // method body } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); BasicFrame b = new BasicFrame(); glcanvas.addGLEventListener(b); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (" Basic Frame"); //adding canvas to frame frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); } }
If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLCanvas class with AWT:
Using canvas with Swing
Using Canvas with AWT gives you a graphical frame with heavy weight features. For having lightweight graphical frame, you need to use GLCanvas with Swing. While using GLCanvas with Swing, you can place GLCanvas in the JFrame window directly, or you can add it to Jpanel.
The following program generates basic Frame using GLCanvas with Swing window:
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.swing.JFrame; public class BasicFrame implements GLEventListener{ @Override public void display(GLAutoDrawable arg0) { // method body } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); BasicFrame b = new BasicFrame(); glcanvas.addGLEventListener(b); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (" Basic Frame"); //adding canvas to it frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport
If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLCanvas with Swing window.
GLJPanel class
It is a lightweight Swing component which provides OpenGL rendering support. It is provided for compatibility with Swing.
GLJPanel class hierarchy
class: GLJPanel
package: javax.media.opengl.awt
Contructors
GJPanel()
Creates a new GLJPanel component with a default set of OpenGL capabilities.
(GLCapabilitiesImmutable)
Creates a new GLJPanel component with the requested set of OpenGL capabilities.
GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser chooser)
Creates a new GLJPanel component.
Methods and Description |
---|
Void addGLEventListener(GLEventListener listener) This method Adds the given listener to the end of this drawable queue. |
Void addGLEventListener(int indexGLEventListener listener) This method Adds the given listener at the given index of this drawable queue. |
Using GLJPanel with Swing window
Let us see the program that generates basic frame using GLJPanel with Swing window:
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.swing.JFrame; public class BasicFrame implements GLEventListener{ @Override public void display(GLAutoDrawable arg0) { // method body } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); BasicFrame b = new BasicFrame(); glcanvas.addGLEventListener(b); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (" Basic Frame"); //adding canvas to it frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport
If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLJPanel with swing window:
To Continue Learning Please Login
Login with Google