I have read Jogl - User’s Guide, which says
It is generally recommended that applications perform as little work as possible inside their mouse and keyboard handlers to keep the GUI responsive. However, since OpenGL commands can not be run from directly within the mouse or keyboard event listener, the best practice is to store off state when the listener is entered and retrieve this state during the next call to GLEventListener.display().
I’m wondering what is the disadvantage if I write code like following. What is the proper way to render in “repaint only on demand” mode? I also want to know is there any boost in performance if I turn on switch “-DJOGL_SINGLE_THREADED_WORKAROUND=true”?
Thanks
///////////////////////// code ////////////////////////
import static net.java.games.jogl.GL.;
import net.java.games.jogl.;
import net.java.games.jogl.util.BufferUtils;
import javax.swing.;
import java.awt.;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.nio.IntBuffer;
public class Test extends JFrame implements GLEventListener, MouseListener {
GLCanvas canvas;
GL gl;
GLU glu;
public Test() throws HeadlessException {
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
gl = canvas.getGL();
glu = canvas.getGLU();
canvas.addGLEventListener(Test.this);
canvas.addMouseListener(Test.this);
add(canvas);
addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
System.out.println(Thread.currentThread());
canvas.setRenderingThread(Thread.currentThread());
}
});
}
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(640, 480);
t.setLocationRelativeTo(null);
t.setVisible(true);
}
private void processHits(int hits, IntBuffer buffer) {
System.out.println(“hits = " + hits);
int ptr = 0;
for(int i=0; i<hits; i++) {
int names = buffer.get(ptr);
System.out.println(” number of names for hit = " + names); ptr++;
System.out.println(" z1 is " + (float)buffer.get(ptr)/0x7fffffff + “;”); ptr++;
System.out.println(" z2 is " + (float)buffer.get(ptr)/0x7fffffff + “;”); ptr++;
System.out.print(" names are ");
int ii = 0, jj = 0;
for(int j=0; j<names; j++) {
System.out.print(buffer.get(ptr));
if(j==0) ii = buffer.get(ptr);
else if(j==1) jj = buffer.get(ptr);
ptr++;
}
System.out.println();
board[ii][jj] = (board[ii][jj]+1)%3;
}
}
final int BUFSIZE = 512;
private int[][] board = new int[3][3];
public void init(GLDrawable glDrawable) {
gl.glClearColor(0f, 0f, 0f, 0f);
}
private void drawSquares(int mode) {
for(int i=0; i<3; i++) {
if(mode==GL_SELECT) gl.glLoadName(i);
for(int j=0; j<3; j++) {
if(mode==GL_SELECT) gl.glPushName(j);
gl.glColor3f((float)i/3, (float)j/3, (float)board[i][j]/3);
gl.glRecti(i, j, i+1, j+1);
if(mode==GL_SELECT) gl.glPopName();
}
}
}
public void display(GLDrawable glDrawable) {
gl.glClear(GL_COLOR_BUFFER_BIT);
drawSquares(GL_RENDER);
gl.glFlush();
}
public void reshape(GLDrawable glDrawable, int x, int y, int w, int h) {
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0, 3, 0, 3);
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLDrawable glDrawable, boolean b, boolean b1) {}
public void mousePressed(MouseEvent e) {
IntBuffer selectBuf = BufferUtils.newIntBuffer(BUFSIZE);
int[] viewport = new int[4];
gl.glGetIntegerv(GL_VIEWPORT, viewport);
gl.glSelectBuffer(BUFSIZE, selectBuf);
gl.glRenderMode(GL_SELECT);
gl.glInitNames();
gl.glPushName(0);
gl.glMatrixMode(GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
glu.gluPickMatrix(e.getX(), viewport[3]-e.getY(), 5, 5, viewport);
glu.gluOrtho2D(0, 3, 0, 3);
drawSquares(GL_SELECT);
gl.glMatrixMode(GL_PROJECTION);
gl.glPopMatrix();
gl.glFlush();
int hits = gl.glRenderMode(GL_RENDER);
processHits(hits, selectBuf);
canvas.repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}