I am getting very strange results when I run this program. Most of the time the program works as it should, other times a white bar is displayed across the bottom of the screen (where the XP taskbar goes). Is this just a JOGL glitch? The main problem I am having is trying to add an Animator object. When I do, the screen goes blank white, and the init and display methods do not get called. I have noticed the NEHE tutorials use static classes and objects whereas my code does not. Is this necessary to use JOGL properly?
import java.nio.*;
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class Cube extends Frame implements GLEventListener, KeyListener
{
private Animator animator;
private FloatBuffer vertices;
private IntBuffer indices;
public Cube (GraphicsConfiguration gc)
{
super(gc);
// create an OpenGL canvas
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
// add a GLEventListener
canvas.addGLEventListener(this);
// now add the canvas to the frame
add(canvas, BorderLayout.CENTER);
// add a key listener to the frame and to the canvas
addKeyListener(this);
canvas.addKeyListener(this);
// animator = new Animator(canvas);
}
public static void main (String[] args)
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
// create the frame with 800 x 600 resolution
Cube frame = new Cube(gc);
frame.setSize(640, 480);
frame.setUndecorated(true);
// set to full screen mode
if( gd.isFullScreenSupported() )
gd.setFullScreenWindow(frame);
//frame.setVisible(true);
frame.show();
}
public void display (GLDrawable drawable)
{
System.out.println("display()");
// retrieve the GL object
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
// set the camera up
glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// clear the screen
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// set the draw color to white
gl.glColor3f(1.0f, 1.0f, 1.0f);
// draw the cube
gl.glDrawElements(GL.GL_QUADS, 24, GL.GL_UNSIGNED_INT, indices);
// flush the buffer
gl.glFlush();
}
public void displayChanged (GLDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
}
public void init (GLDrawable drawable)
{
System.out.println("init()");
// retrieve the GL object
GL gl = drawable.getGL();
// set the clear color to black
gl.glClearColor(0, 0, 0, 0);
// clear the screen
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// set the polygon mode
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
// disable culling
gl.glDisable(GL.GL_CULL_FACE);
// create the cube object
vertices = BufferUtils.newFloatBuffer(24);
indices = BufferUtils.newIntBuffer(24);
// load cube vertices
vertices.put
(
new float[]
{
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f
}
);
// load cube indices
indices.put
(
new int[]
{
0, 1, 2, 3, // top face
4, 5, 6, 7, // bottom face
0, 3, 7, 4, // front face
1, 2, 6, 5, // back face
0, 1, 5, 4, // right face
2, 3, 7, 6 // left face
}
);
// enable the open GL vertex array
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
// fill up the vertex array with the cube vertices
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices);
// animator.start();
}
public void reshape (GLDrawable drawable, int x, int y, int width, int height)
{
// retrieve the GL object
GL gl = drawable.getGL();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
}
public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();
if(key == KeyEvent.VK_ESCAPE)
{
// animator.stop();
System.exit(0);
}
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
}