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?