For those who want to take advantage of both hardware acceleration and Java’s cross-platform compatibility, Java has an OpenGL binding, known as JOGL. This binding is exactly the same as the C version – the only difference is that Java requires all methods to be ‘object-oriented’. In order to call an openGL method, we first have to acquire a GL or a GLU object (depending on which library the method would come from in C), and then call the method on that. For example, instead of the C code below:

  1. glBegin(GL_LINES);
  2. //some stuff here
  3. glEnd();

we would have the Java code:

  1. GL gl=autoDrawableObject.getGL();
  2. gl.glBegin(GL_LINES);
  3. //some stuff here
  4. gl.glEnd();

This makes it easy to convert C++ to Java code, and vice versa. In fact, if your code is longer than 100 or so lines, it’s probably best to just write a script to do the hard work for you. The packages to include are javax.media.opengl.* and javax.media.opengl.glu.*. As far as I know, there is no support for GLUT, at least with this particular Java OpenGL binding. The boilerplate code for Java is below.

  1. package edu.mbhs.sclawren.misc.funfun;
  2.  
  3. import javax.media.opengl.*;
  4. import javax.media.opengl.glu.*;
  5.  
  6. public class FFScene implements GLEventListener {
  7.  public void display(GLAutoDrawable drawable) {
  8.   GL gl = drawable.getGL();
  9.   GLU glu=new GLU();
  10.                 //now do some drawing!
  11.  }
  12.  public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
  13.   display(arg0);
  14.  }
  15.  public void init(GLAutoDrawable glad) {
  16.   final GL gl = glad.getGL();
  17.                 //its useful to put stuff here, to
  18.  }
  19.  
  20.  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  21.                 //this is configured for full screen
  22.   float h = (float)height / (float)width;
  23.   GL gl = drawable.getGL();
  24.   gl.glMatrixMode(GL.GL_PROJECTION);
  25.  
  26.   gl.glLoadIdentity();
  27.   gl.glFrustum(-1.0f, 1.0f, -h, h, 2.0f, 60.0f);
  28.   gl.glMatrixMode(GL.GL_MODELVIEW);
  29.   gl.glLoadIdentity();
  30.   gl.glTranslatef(0.0f, 0.0f, -3.0f);        
  31.  }
  32.  
  33. }
  34.  
  35.  
  36. package edu.mbhs.sclawren.misc.funfun;
  37.  
  38. import java.awt.*;
  39. import java.awt.event.*;
  40.  
  41. import javax.media.opengl.*;
  42. import javax.swing.*;
  43.  
  44. import com.sun.opengl.util.*;
  45.  
  46. public class FFFrame extends JFrame {
  47.  GLCanvas glScene;
  48.  FPSAnimator animator;
  49.  public FFFrame() {
  50.   glScene=new GLCanvas();
  51.   FFScene ff=new FFScene();
  52.      glScene.addGLEventListener(ff);
  53.      animator = new FPSAnimator(glScene, 20);
  54.  }
  55.  public void display() {
  56.   setUndecorated(true);
  57.   GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
  58.   animator.start();
  59.  }
  60. }

Now for people who are new to OpenGL. To set up a decent-looking three-dimensional scene (if you don’t want all three dimensions, don’t use opengl), you need three things – the lighting settings, the material settings, and the content (the position of the camera is taken care of in the boilerplate code above – if you want to more the camera, you can cheat and move the scene itself instead). The material itself is easiest. The four main functions are gl.glBegin(), gl.glEnd(), gl.glVertex3d(), and glu.gluSphere. Use the first three together, to draw lines, and polygons; however, our example only uses spheres. Draw a sphere with glu.gluSphere(glu.gluNewQuadric(), radius,50,50).

And now, for your coding pleasure, I present you with a complete code listing for a simple application to show the moon orbiting the earth. I’ve set it up so that the viewer rotates about both.

FFScene.java
  1. package edu.mbhs.sclawren.misc.funfun;
  2.  
  3. import javax.media.opengl.*;
  4. import javax.media.opengl.glu.*;
  5.  
  6. public class FFScene implements GLEventListener {
  7.  
  8.  double rot=0;
  9.  
  10.  public void display(GLAutoDrawable drawable) {
  11.   rot+=1;
  12.   //rot%=360;
  13.   GL gl = drawable.getGL();
  14.   GLU glu=new GLU();
  15.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  
  16.   gl.glLoadIdentity();  // Reset The Modelview Matrix
  17.   gl.glTranslatef(0.0f,0.0f,-8.0f);
  18.   gl.glRotated(rot,1.0,0.0,0.0);
  19.   gl.glTranslatef(0.0f,0.0f,8.0f);
  20.   gl.glTranslatef(0.0f,0.0f,-8.0f);
  21.   // prepare ligthsource
  22.   float ambient[] = {0.4f,0.4f,0.4f,1.0f };
  23.   float diffuse[] = {1.0f,1.0f,1.0f,1.0f };
  24.   float position[] = {-1.0f,1.0f,1.0f,0.0f };
  25.  
  26.   gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambient,0);
  27.   gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuse,0);
  28.   gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position,0);
  29.  
  30.   gl.glEnable(GL.GL_LIGHT0);
  31.   gl.glEnable(GL.GL_LIGHTING);
  32.  
  33.   float amb[]={0.2f,0.2f,0.4f,1.0f};
  34.   float diff[]={0.2f,0.2f,1.0f,1.0f};
  35.   float spec[]={0.2f,0.8f,0.8f,1.0f};
  36.   float shine=0.1f;
  37.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_AMBIENT,amb,0);
  38.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_DIFFUSE,diff,0);
  39.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_SPECULAR,spec,0);
  40.   gl.glMaterialf(GL.GL_FRONT,GL.GL_SHININESS,shine*128.0f);
  41.  
  42.   GLUquadric qd=glu.gluNewQuadric();
  43.   glu.gluSphere(qd, 1.0f,100,100);
  44.   //now draw the cube
  45.   gl.glRotated(rot*10,0.0,0.0,1.0);
  46.   gl.glTranslated(0.0,1.6,0.0);
  47.  
  48.  
  49.   float amb2[]={0.3f,0.3f,0.3f,1.0f};
  50.   float diff2[]={0.4f,0.4f,0.6f,1.0f};
  51.   float spec2[]={0.02f,0.2f,0.2f,1.0f};
  52.   float shine2=0.1f;
  53.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_AMBIENT,amb2,0);
  54.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_DIFFUSE,diff2,0);
  55.   gl.glMaterialfv(GL.GL_FRONT,GL.GL_SPECULAR,spec2,0);
  56.   gl.glMaterialf(GL.GL_FRONT,GL.GL_SHININESS,shine2*128.0f);
  57.   glu.gluSphere(qd, 0.3f,50,50);
  58.   gl.glMatrixMode(GL.GL_MATRIX_MODE);
  59.  }
  60.  
  61.  public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
  62.   display(arg0);
  63.  }
  64.  
  65.  public void init(GLAutoDrawable glad) {
  66.   final GL gl = glad.getGL();
  67.  
  68.   gl.glMatrixMode(GL.GL_PROJECTION);
  69.   gl.glLoadIdentity();
  70.   gl.glMatrixMode(GL.GL_MODELVIEW);
  71.  
  72.  
  73.  
  74.   // smooth the drawing
  75.   gl.glShadeModel(GL.GL_SMOOTH);
  76.  
  77.   // depth sorting
  78.   gl.glEnable(GL.GL_DEPTH_TEST);
  79.   gl.glDepthFunc(GL.GL_LESS);
  80.  
  81.   // set background to light gray
  82.   gl.glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
  83.  }
  84.  
  85.  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  86.   float h = (float)height / (float)width;
  87.   GL gl = drawable.getGL();
  88.   gl.glMatrixMode(GL.GL_PROJECTION);
  89.  
  90.   gl.glLoadIdentity();
  91.   gl.glFrustum(-1.0f, 1.0f, -h, h, 2.0f, 60.0f);
  92.   gl.glMatrixMode(GL.GL_MODELVIEW);
  93.   gl.glLoadIdentity();
  94.   gl.glTranslatef(0.0f, 0.0f, -3.0f);        
  95.  
  96.  }
  97.  
  98. }
FFFrame.java
  1. package edu.mbhs.sclawren.misc.funfun;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. import javax.media.opengl.*;
  7. import javax.swing.*;
  8.  
  9. import com.sun.opengl.util.*;
  10.  
  11. public class FFFrame extends JFrame {
  12.  
  13.  private static final long serialVersionUID = -5712445927536229962L;
  14.  
  15.  GLCanvas glScene;
  16.  FPSAnimator animator;
  17.  
  18.  public FFFrame() {
  19.   glScene=new GLCanvas();
  20.   FFScene ff=new FFScene();
  21.      glScene.addGLEventListener(ff);
  22.      getContentPane().add(glScene, BorderLayout.CENTER);setBackground(Color.blue);
  23.      glScene.addMouseListener(new MouseListener() {
  24.  
  25.    public void mouseClicked(MouseEvent e) {
  26.     System.exit(0);
  27.    }
  28.  
  29.    public void mouseEntered(MouseEvent e) {
  30.    }
  31.  
  32.    public void mouseExited(MouseEvent e) {
  33.    }
  34.  
  35.    public void mousePressed(MouseEvent e) {
  36.    }
  37.  
  38.    public void mouseReleased(MouseEvent e) {
  39.    }
  40.      
  41.      });
  42.      animator = new FPSAnimator(glScene, 20);
  43.  }
  44.  
  45.  public void display() {
  46.   setUndecorated(true);
  47.   GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
  48.   animator.start();
  49.  }
  50.  
  51. }

To show the FFFrame, just call new FFFrame().display().

Related posts: