I decided last night to learn som java and JOGL, i am originally a c++ programmer so I havent really put much effort in reading about java, i guessed that I’d learn as I play along.
Anyways, so I made my first JOGL applications, it’s a simple triangle that rotates around it’s Y-axis. And I use the Animator class to handle the animation.
The problem is that the animation is really jerky and the CPU is up at around 98% of use. Err… So what am I doing wrong?
edit: I just noticed that the animation gets even more jerky if I move my mouse over the window alot
Here’s the source:
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
class GLFrame extends Frame implements GLEventListener
{
protected Animator glAnimator;
private int yrot = 0;
public GLFrame()
{
super("JOGL Frame");
setSize(320, 240);
GLCanvas glCanvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
glCanvas.addGLEventListener(this);
add(glCanvas);
glAnimator = new Animator(glCanvas);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
glAnimator.stop();
System.exit(0);
}
});
setVisible(true);
glAnimator.start();
}
public void init(GLDrawable drawable)
{
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_FUNC);
}
public void reshape(GLDrawable drawable, int i, int x, int width, int height)
{
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 1000.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
}
public void display(GLDrawable drawable)
{
yrot++;
if( yrot > 360 ){ yrot = 0; }
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_TRIANGLES);
gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f,-1.0f, 0.0f);
gl.glVertex3f( 1.0f,-1.0f, 0.0f);
gl.glEnd();
gl.glFlush();
gl.glFinish();
}
public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
}
public static void main(String[] args)
{
new GLFrame();
}
}