using gluUnProject

Greetings, once more. I’m trying to use gluUnProject to get world coordinates for my mouse. However the calls to glGetIntegerv, glGetDoublev to get the viewport, modelview and projection matrices return 0 values.
My questions are:
Can I do this inside the Mouse Listener methods:
I still need a gl, glu object. How do I pass these to:
public void mouseClicked(MouseEvent e)

right now I’ve created a helper class:


class TransformMouseCoords {
    GLDrawable glDrawable;
    
    public void initClass(GLDrawable glDrawable) {
      this.glDrawable = glDrawable;
      
    }
    public void makeTransform(int x, int y, double worldX, double worldY) {
      GL gl = glDrawable.getGL();
      GLU glu = glDrawable.getGLU();
      
      int[] viewport = new int[4];
      double[] mvmatrix = new double[16];
      double[] projmatrix = new double[16];
      int realy;
      double[] worldCoords = new double[3];
      /*double[] wy = new double[1];
      double[] wz = new double[1];*/
      
      gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
      gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix);
      gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix);
      realy = viewport[3] - y  - 1;
      System.out.println("Coordinates at cursor: (" + x + ", " + realy + ")");
      for (int i = 0; i < 4; i++) 
          System.out.println(viewport[i]);
      
      for (int i = 0; i < 16; i++) {
          System.out.println(projmatrix[i]);
      }
      for (int i = 0; i < 16; i++) {
          System.out.println(mvmatrix[i]);
      }
      glu.gluUnProject((double)x, (double)realy, 0.0, 
            mvmatrix, projmatrix, viewport,
            worldCoords);
      worldX = worldCoords[0];
      worldY = worldCoords[1];
      System.out.println("Coordinates at world: (" + worldCoords[0] + ", " + worldCoords[1] + ")");
    }
}

which I call in mouseClicked.


public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        double worldX = 0, worldY = 0;
        tmc.makeTransform(x, y, worldX, worldY);
        System.out.println("we got coordinates: (" + worldX + ", " + worldY + ")");
    }

Am I missing something in passing variables in Java? Why are my viewport, modelview and projection matrices filled with zeros?
PS. I call initClass inside my display callback, just to be sure I get a valid glDrawable each time. Is this correct?

Bluntly, no.

OpenGL calls are only valid from the thread that the OpenGL context was created from, which is what the GLEventListener display callbacks are attempting to enforce. In general, its a bad idea to maintain a reference to the GL object, instead just keep passing it down as a method arg and you can’t make calls from an invalid thread.

The mouse events come from an AWT event thread, so GL calls won’t be valid. A half-assed solution is to set a flag and queue up calls to gluUnProject etc. and do them later. A more general method that I’ve used is to queue up all your mouse events when they actually happen, and then flush this queue (performing any actual handling logic) during a display callback. This is fairly easy to do with just a simple wrapper around where you add your mouse event listeners to your GL display.