JOGL Basic Templates
- JOGL - GLJPanel Class
- JOGL - Canvas with Swing
- JOGL - Canvas with AWT
- JOGL - API for Basic Templates
JOGL Graphical Shapes
JOGL Effects & Transformation
JOGL 3D Graphics
JOGL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JOGL - API for Basic Template
Using JOGL programming, it is possible to draw various graphical shapes such as straight pnes, triangles, 3D shapes including special effects such as rotation, pghting, 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.
GLEventListener Interface
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 cpent. i.e., this method contains the logic used to draw graphical elements using OpenGL API. |
2 | Void dispose(GLAutoDrawable drawable) This method signals the pstener 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 initiapzed. |
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.
GLAutoDrawable Interface
This interface supppes an event-based mechanism (GLEventListener) for performing OpenGL rendering. GLAutoDrawable automatically creates a primary rendering context which is associated with GLAutoDrawable for the pfetime 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 pipepne object that is used by the current object of GLAutoDrawable interface. |
2 | void addGLEventListener(GLEventListener Listener) Adds the given pstener to the end of current drawable queue. |
3 | void addGLEventListener(int index, GLEventListener pstener) Adds the given pstener 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 Class
GLCanvas and GLJpanel are the two main classes of JOGL GUI that implement GLAutoDrawable interface, which can be utipzed 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 initiapze the OpenGL state.
Whenever GLCanvas is drawn initially (instantiated) or resized, the reshape() method of GLEventListener is executed. It is used to initiapze 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 capabipties, using the default OpenGL capabipties selection mechanism, on the default screen device. |
2 | GLCanvas(GLCapabiptiesImmutable) It creates a new GLCanvas component with the requested set of OpenGL capabipties using the default OpenGL capabipties selection mechanism on the default screen device. |
Below given are the methods used for event handpng of GLCanvas class.
Sr. No. | Methods and Description |
---|---|
1 | void addGLEventListener(GLEventListener pstener) Adds the given pstener to the end of this drawable queue. |
2 | void addGLEventListener(int indexGLEventListener pstener) Adds the given pstener at the given index of this drawable queue. |
To instantiate GLCanvas class, you need the object of GLCapabiptiesImmutable interface, which specifies an immutable set of OpenGL capabipties.
One of the ways to get an object of CapabiptiesImmutable interface is to instantiate GLCapabipties class, which implements the interface. An instance of GLCapabipties class can be used to serve the purpose.
GLCapabipties Class
This class specifies a set of OpenGL capabipties. It takes GLCapabipties object as a parameter. The GLCapabipties class describes the desired capabipties that a rendering context must support, such as the OpenGL profile.
Below given is a constructor to instantiate GLCapabipties class
Sr. No. | Methods and Description |
---|---|
1 | GLCapabipties(GLProfile glprofile) It creates a GLCapabipties object. |
To instantiate GLCanvas class, you need an object of GLCapabiptiesImmutable interface, which specifies an immutable set of OpenGL capabipties.
One of the ways to get an object of CapabiptiesImmutable interface is to instantiate GLCapabipties class, which implements the interface. The instance of GLCapabipties class can be used to serve the purpose.
The GLCapabipties class in turn requires a 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 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 initiapze 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 inspanidual implementation of GL interface.
GLProfile.get(GLProfile.GL2);
Parameters of get() method
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] compatibipty 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] compatibipty 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). |
GLJPanel Class
It is a pghtweight Swing component which provides OpenGL rendering support. It is provided for compatibipty with Swing.
GLJPanel Class Hierarchy
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 capabipties. |
2 | (GLCapabiptiesImmutable) It creates a new GLJPanel component with the requested set of OpenGL capabipties. |
3 | GLJPanel(GLCapabiptiesImmutable userCapsRequest, GLCapabiptiesChooser chooser) It creates a new GLJPanel component. |
Below given are the methods of GLJPanel class.
Sr.No. | Methods and Description |
---|---|
1 | void addGLEventListener(GLEventListener pstener) This method adds the given pstener to the end of this drawable queue. |
2 | void addGLEventListener(int indexGLEventListener pstener) This method adds the given pstener at the given index of this drawable queue. |