Last Updated:

Using OpenGL in Java

OpenGL in Java
 

Principles of implementing OpenGL in Java

Nowadays, Java is very widespread and more and more different technologies are being ported to this language. OpenGL is no exception. OpenGL complements Java in such a way that promising 3D graphics technologies become a reality when writing applets for WEB using Java. OpenGL is implemented in Java through additional extensions and libraries. There are several types of these libraries, all of them produced by various small companies. I will not consider commercial libraries, but I will consider the most common and most importantly free (GNU license) gl4Java library (It can be taken at http://www.jausoft.com). In addition, this library has the least drawbacks and in addition to this exists for almost all operating systems. Therefore, next we will consider this library.

Although Java and OpenGL are multi-platform, GL4Java is installed on different platforms in different ways. GL4Java is the glue between Java and a particular OpenGL implementation on that platform. For example, in Windows, OpenGL is implemented through the opengl32 dynamic library.dll and GL4Java allows you to access the functions of this library from Java applets. Of course, for each specific operating system, there are GL4Java libraries compiled specifically for this operating system.

But there's a little trick. Since Java applets do not allow the use of local user files, the question arises, how does a Java program access Windows dynamic libraries? This is done in such a way that GL4Java has special libraries (called wrapper libraries, i.e. located between Java and OpenGL libraries), which can be accessed from Java applets. Thus, through these libraries there is already a direct access to the OpenGL libraries. Another feature of this implementation is that the speed of the Java applet is comparable to the speed of standard OpenGL programs written, for example, in C / C ++, since OpenGL commands are processed not in the Java interpreter, but are executed by the system, referring to local libraries.

2. Building a Java Applet Using OpenGL

The construction of the applet is not much different from the standard one. Let's look at the example below.

The MainApp class is the main applet class, inherited from the Applet class. The MainGL class inherits from the special GLAnimCanvas class, which is defined in the GL4Java library. Classes that are responsible for drawing on the surface of the OpenGL applet should inherit from this class. GLAnimCanvas inherits from the GLCanvas class and is an extension of it, adding threading support for animation. Basically, most OpenGL applets use this class.

Let's now consider what basic methods (which are necessary for the operation of an OpenGL program) can be overridden in their class and what they are needed for.

public void preInit() - Called before the OpenGL context is created. Typically used to set OpenGL parameters, such as using double buffering.

public void init() - Called after the OpenGL context is created. Here you can call OpenGL projection installation commands and other initialization commands.

public void display() - This is the method that is called to redraw the content on the drawing surface. In this example, it is called automatically, but you can call it yourself. This method calls the OpenGL commands responsible for drawing.

That's basically all you need to know. For more information and additional methods, see the GL4Java documentation.

The process of writing an OpenGL program in Java is not much different from a program in C/C++. The only thing I want to note is that the OpenGL command should be preceded by "gl." (gl with a dot), respectively before the command from the GLU library "glu." etc.

Next, I give an example (skeleton) of a Java applet using OpenGL.

  // Include GL4Java libraries
import gl4java.GLContext;
import gl4java.awt.GLCanvas;
import gl4java.awt.GLAnimCanvas;

// Include standard Java libraries
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.applet.*;

// Our main GL class
class MainGL extends GLAnimCanvas
{
 // Constructor
 public MainGL(int w, int h)
 {
  super(w, h);
 }

 // Preinitialization
 public void preInit()
 {
  // Set up double buffering
  doubleBuffer = true;
 }
 
 // Initialization. Installing OpenGL projections
 public void init()
 {
  gl.glMatrixMode(GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glOrtho(-5.0, 5.0, -5.0, 5.0, 2.0, 50.0);
  gl.glMatrixMode(GL_MODELVIEW);
  gl.glTranslatef(0, 0, -5);
  glj.gljCheckGL();
  glj.gljMakeCurrent(false);
  gl.glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 }
 
 // Drawing function.
 public void display()
 {
  // If there are any problems with OpenGL, then exit
  if (glj.gljMakeCurrent() == false) return;
 
  // Here comes the standard OpenGL command stream
 
  gl.glClear(GL_COLOR_BUFFER_BIT);
 
  // ... OpenGL commands ...
  // Display the buffer on the screen
  // (when double buffered)
  glj.gljSwap();
 }
}
 
// Main applet class
public class MainApp extends Applet
{
 glmain=null;
 // Initialize the applet
 public void init()
 {
  setLayout(null);
  setSize(640,480); // Applet Size

  // Create Our GL Object
  glmain = new MainGL(640, 480);
  // Set options for it
  glmain.setBounds(0,0,640,480);
  // And add it to our applet
  add(glmain);
 }

 // This function is called when
 // when the applet starts
 public void start()
 {
  glmain.start();
 }

 // This function is called when
 // when the applet stops
 public void stop()
 {
  glmain.stop();
 }

 // This function is called when
 // when the applet is destroyed
 public void destroy()
 {
  glmain.stop();
  glmain.cvsDispose();
 }
}