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. To draw objects in JOGL first of all we have to construct a basic JOGL frame. Below given are the classes required to construct a basic frame.
To make your program capable of using JOGL graphical API, you need to implement GLEventListener interface. You can find the GLEventListener interface in the javax.media.opengl package.
The following table provides the 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 drawable) It is called by the object of GLAutoDrawable interface immediately after the OpenGL context is initialized. |
4 | Void reshape(GLAutoDrawble drawble, int x, 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.
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.
The following table provides the details of various methods and descriptions of GLAutoDrawable interface −
Sr.No | Methods and Descriptions |
---|---|
1 | GL getGL() Returns the GL pipeline object that is used by the current object of GLAutoDrawable interface. |
2 | void addGLEventListener(GLEventListener Listener) Adds the given listener to the end of current drawable queue. |
3 | void addGLEventListener(int index, GLEventListener listener) Adds the given listener at the given index of this drawable queue. |
4 | void destroy() Destroys all resources associated with this object of GLAutoDrawable interface, including the GLContext. |
Note − There are other methods in this package. Only a few important methods pertaining to template are discussed in this interface.
GLCanvas and GLJpanel are the two main classes of JOGL GUI that implement GLAutoDrawable interface, which can 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.
The display() method of GLEventListener contains the code for rendering 3D scene. It is invoked whenever display() method of GLCanvas is invoked.
Below given are the constructors required to instantiate GLCanvas class.
Sr.No | Constructor and Description |
---|---|
1 | GLCanvas() It creates a new GLCanvas component with a default set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism, on the default screen device. |
2 | 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. |
Below given are the methods used for event handling of GLCanvas class.
Sr. No. | Methods and Description |
---|---|
1 | void addGLEventListener(GLEventListener listener) Adds the given listener to the end of this drawable queue. |
2 | void addGLEventListener(int indexGLEventListener listener) Adds the given listener at the given index of this drawable queue. |
To instantiate GLCanvas class, you need the 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.
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.
Below given is a constructor to instantiate GLCapabilities class
Sr. No. | Methods and Description |
---|---|
1 | GLCapabilities(GLProfile glprofile) It creates a GLCapabilities object. |
To instantiate GLCanvas class, you need an 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. The instance of GLCapabilities class can be used to serve the purpose.
The GLCapabilities class in turn requires a GLProfile object.
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 the 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, it gives you singleton GLProfile objects for each available JOGL profile.
Below given is the prototype of the get method of GLProfile class.
Sr.No. | Method and Description |
---|---|
1 | Static GLProfile get(String profile) Uses the default device. |
As this is a static method, you need to invoke it using the 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);
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 This interface contains 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). |
It is a lightweight Swing component which provides OpenGL rendering support. It is provided for compatibility with Swing.
Below given is diagram represents the class hierarchey of GLJPanel class.
Below given are various constructors GLJPanel class.
Sr. No. | Constructors and Description |
---|---|
1 | GJPanel() It creates a new GLJPanel component with a default set of OpenGL capabilities. |
2 | (GLCapabilitiesImmutable) It creates a new GLJPanel component with the requested set of OpenGL capabilities. |
3 | GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser chooser) It creates a new GLJPanel component. |
Below given are the methods of GLJPanel class.
Sr.No. | Methods and Description |
---|---|
1 | void addGLEventListener(GLEventListener listener) This method adds the given listener to the end of this drawable queue. |
2 | void addGLEventListener(int indexGLEventListener listener) This method adds the given listener at the given index of this drawable queue. |