Here’s a test case:
GLEventListener impl:
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
public class TestRenderer implements GLEventListener {
private static final float MARGIN = 0.05f;
private static final int NR_OF_FRAGMENTS_X = 10;
private static final int NR_OF_FRAGMENTS_Y = 5;
private float leftMargin, rightMargin, bottomMargin, topMargin;
private float[] xPos;
private float[] yPos;
public void display(GLAutoDrawable drawable) {
long startTime = System.nanoTime();
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// border:
gl.glColor3f(0.8f, 0.8f, 0.8f);
gl.glBegin(GL.GL_LINE_LOOP);
gl.glVertex2f(leftMargin, bottomMargin);
gl.glVertex2f(rightMargin, bottomMargin);
gl.glVertex2f(rightMargin, topMargin);
gl.glVertex2f(leftMargin, topMargin);
gl.glEnd();
// grid:
gl.glBegin(GL.GL_LINES);
// vertical lines (using xPos):
for(float f : xPos) {
gl.glVertex2f(leftMargin + f, bottomMargin);
gl.glVertex2f(leftMargin + f, topMargin);
}
// horizontal lines (using yPos):
for(float f : yPos) {
gl.glVertex2f(leftMargin, bottomMargin + f);
gl.glVertex2f(rightMargin, bottomMargin + f);
}
gl.glEnd();
gl.glFinish();
System.out.println("TestRenderer.display(): " +
((System.nanoTime() - startTime) / 1000000.0f) + " ms");
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged) {}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(x, width, y, height, 0.0, 1.0);
leftMargin = MARGIN * width;
rightMargin = width - leftMargin;
bottomMargin = MARGIN * height;
topMargin = height - bottomMargin;
xPos = getFragmentPositions(leftMargin, rightMargin, NR_OF_FRAGMENTS_X);
yPos = getFragmentPositions(bottomMargin, topMargin, NR_OF_FRAGMENTS_Y);
}
public static float[] getFragmentPositions(float min, float max, int frags) {
float[] res = new float[frags + 1];
float spacing = (max - min) / frags;
for(int i = 0; i < res.length; ++i) {
res[i] = i * spacing;
}
return res;
}
}
and a simple starter class (uncomment/comment to switch between the canvas and panel impl):
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLJPanel;
import javax.swing.JFrame;
import com.sun.opengl.util.FPSAnimator;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Weird GL problem test case");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(768, 512));
GLCapabilities caps = new GLCapabilities();
System.out.println(caps);
GLCanvas canvas = new GLCanvas(caps);
canvas.addGLEventListener(new TestRenderer());
frame.add(canvas);
final FPSAnimator anim = new FPSAnimator(canvas, 10);
// GLJPanel panel = new GLJPanel(caps);
// panel.addGLEventListener(new TestRenderer());
// frame.add(panel);
// final FPSAnimator anim = new FPSAnimator(panel, 10);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing() {
anim.stop();
}
});
frame.pack();
anim.start();
frame.setVisible(true);
}
}
Test results:
Now even the GLCanvas version runs perfectly on my nVIDIA based laptop! Needs more research regarding my “final” application…
On the linux machine it’s like posted yesterday. sysout tells me that GLJPanel needs around 6-8 ms to finish and the CPU utilization is around 35% (X server). GLCanvas finishes the rendering in around 1 ms using no CPU…
I also got access to another WinXP machine. It’s an “older” one, P4 2.8 GHz with an Intel 82865G onboard graphics card. This machine behaves like the linux machine (both use onboard graphics), GLJPanel results in high CPU usage.
Is there any possiblity to see if GLJPanel is hardware accelerated or uses the image copy mechanism?
Conclusion:
I guess, I will stick with the GLCanvas after taking a deeper look at the GLCanvas on my laptop. It seems to be the most “compatible” for me, especially as I have no option to rely on nVIDIA/ATI graphics cards on every computer :(.