I’ve just written a simple animation in JOGL : a 6-colored cube spins around y axis. But it seems my animation has a little speed workaround : sometimes it accelerate, and sometimes it slows though i just call glRotate() with a float variable that I increment on each display call (my code is below).
- My processor is a pentium dual core t3400
- My operating system is Xubuntu 11.04 32 bits
- My graphic card is ATI Radeon HD4330 and i’ve installed a recent proprietary driver : 11.6 (downloaded on AMD website, not on ubuntu software manager)
In Eclipse EDI (I’m using Indigo 3.7.0), this is how i’ve set my user library :
- I’ve downloaded the JOGL 2.0 beta 23 (from 2011, march 03) for i586 linux
- In the user library I imported the following jars : newt.all.jar, nativewindow.all.jar, jogl.all.jar, gluegen-rt.jar
- All of the imported jars link to the lib subfolder of uncompressed jogl archive : where are all the *.so files
Here my three classes.
ArcballExperiment.java
package com.gmail.bernabe.laurent.jogl.arcball_experiment;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import com.jogamp.opengl.util.Animator;
public class ArcBallExperiment extends JFrame {
public ArcBallExperiment(){
setTitle("ArcBall Experimentation");
setSize(500, 500);
setLocationRelativeTo(null);
GLCanvas glCanvas = new GLCanvas();
TheGLEventListener glEventListener = new TheGLEventListener();
glCanvas.addGLEventListener(glEventListener);
add(glCanvas);
final Animator animator = new Animator(glCanvas);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
animator.stop();
System.exit(0);
}
});
animator.start();
}
public static void main(String[] args) {
new ArcBallExperiment().setVisible(true);
}
private static final long serialVersionUID = 1L;
}
Cube.java :
package com.gmail.bernabe.laurent.jogl.arcball_experiment;
import javax.media.opengl.GL2;
public class Cube {
/**
* Draws a cube in a GL2 context
* @param gl - GL2 - GL2 context
*/
public static void draw(GL2 gl){
gl.glBegin(GL2.GL_QUADS);
// front : blue
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex3f(-0.5f, +0.5f, +0.5f);
gl.glVertex3f(-0.5f, -0.5f, +0.5f);
gl.glVertex3f(+0.5f, -0.5f, +0.5f);
gl.glVertex3f(+0.5f, +0.5f, +0.5f);
// back : green
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(+0.5f, -0.5f, -0.5f);
gl.glVertex3f(+0.5f, +0.5f, -0.5f);
gl.glVertex3f(-0.5f, +0.5f, -0.5f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
// left : red
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glVertex3f(-0.5f, +0.5f, +0.5f);
gl.glVertex3f(-0.5f, +0.5f, -0.5f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(-0.5f, -0.5f, +0.5f);
// right : orange
gl.glColor3f(1.0f, 0.4f, 0.0f);
gl.glVertex3f(+0.5f, +0.5f, -0.5f);
gl.glVertex3f(+0.5f, +0.5f, +0.5f);
gl.glVertex3f(+0.5f, -0.5f, +0.5f);
gl.glVertex3f(+0.5f, -0.5f, -0.5f);
// top : white
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(+0.5f, +0.5f, +0.5f);
gl.glVertex3f(-0.5f, +0.5f, +0.5f);
gl.glVertex3f(-0.5f, +0.5f, -0.5f);
gl.glVertex3f(+0.5f, +0.5f, -0.5f);
// bottom : yellow
gl.glColor3f(1.0f, 1.0f, 0.0f);
gl.glVertex3f(+0.5f, -0.5f, +0.5f);
gl.glVertex3f(+0.5f, -0.5f, -0.5f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(-0.5f, -0.5f, +0.5f);
gl.glEnd();
}
}
TheGLEventListener.java :
package com.gmail.bernabe.laurent.jogl.arcball_experiment;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.gl2.GLUgl2;
public class TheGLEventListener implements GLEventListener {
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(
0f, 0f, 3f,
0f, 0f, 0f,
0f, 1f, 0f
);
gl.glRotatef(rotationAngle, 0f, 1f, 0f);
Cube.draw(gl);
rotationAngle += 0.3f;
if (rotationAngle >= 360f)
rotationAngle %= 360f;
}
@Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glClearDepthf(1.0f);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (float) width/height, 1f, 10f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
private GLUgl2 glu = new GLUgl2();
private float rotationAngle = 0f;
}
I’m thinking it could be because I imported incompatible jars in the user library made for my JOGL eclipse projects, but I’m not sure.
Thanks in advance
(The title ArcBall experiment seems inapropriated, and it’s right : this cube spinning is juste a phase for me to remember again the JOGL programming before going to arcball implementation.)
Finally I solved the problem 🙂
I changed the animator instance to FPSAnimator instead of Animator, in order to keep a constant FPS. There is just one class to update :
ArcBallExperiment.java