Jump to content

Java Bindings for OpenGL

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by M4gnum0n (talk | contribs) at 15:09, 2 August 2010 (maintenance tags). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Java Binding for the OpenGL API is a JSR API specification (JSR 231) for the Java SE platform which allows to use OpenGL [1] on the Java Platform. There is also Java Binding for the OpenGL ES API (JSR 239) for the Java ME platform.

Programming concepts

Core OpenGL API and GLU library calls are available from Java through a thin wrapper looking very much as the original OpenGL C API [2].

All platform specific libraries (available from the CGL API for Mac OS X, GLX for X Window System, and WGL for Microsoft Windows) are also abstracted out to create a platform independent way of selecting Framebuffer attributes and performing platform specific Framebuffer operations[3].

Example

This examples shows how to draw a polygon (without initialization or repaint code) [4]. Here is the reference C implementation:

 int DrawGLScene(GLvoid) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units
    glBegin(GL_TRIANGLES); //Drawing Using Triangles
    glVertex3f( 0.0f, 1.0f, 0.0f); // Top
    glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
    glEnd();	
    glTranslatef(3.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS); // Draw A Quad
    glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
    glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
    glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
    glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
    glEnd();
    glFlush();
    return TRUE;
 }

Which translates to the following Java implementation:

 public void display(GLAutoDrawable gLDrawable) {
   final GL gl = gLDrawable.getGL();
   gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
   gl.glLoadIdentity();
   gl.glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units
   gl.glBegin(GL.GL_TRIANGLES); // Drawing Using Triangles
   gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top
   gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
   gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
   gl.glEnd();
   gl.glTranslatef(3.0f, 0.0f, 0.0f);
   gl.glBegin(GL.GL_QUADS); // Draw A Quad
   gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
   gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
   gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
   gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
   gl.glEnd();
   gl.glFlush();
 }

Implementations

References

  1. ^ All functions in core OpenGL 2.0 have been included.
  2. ^ Except GLU NURBS routines which are not exposed through the public API.
  3. ^ Platform-specific extensions are not included in the public API. Each implementation can choose to export some of these APIs via the GL.getPlatformGLExtensions() and GL.getExtension(String) method calls which return Objects whose data types are specific to the given implementation.
  4. ^ Borrowed from the Nehe tutorial, whose code is free to use elsewhere.

See also