gluProject not returning correct values - usually

Started by freezway, March 06, 2013, 23:49:23

Previous topic - Next topic

freezway

I want to use gluProject to get the screen space coordinates of a 3d point. My goal is to get the location that a vertex rendered to. I load the Modelview, Projection and Viewport matrices into Float/IntBuffers then call gluProject. Here's where it gets weird. If the difference between the x, y, and z of the camera and the point I'm trying to are equal (e.g. camera at 4,3,5 and point at 2,1,3, the the differences are 2,2,2) Then it actually give the correct values, but anywhere else and I get incorrect values. Changing where I look (the angle) has no impact on the correctness. I have confirmed that the matrices I used to render and the matrices I use with gluProject are identical.

Here is my code for rendering:
glEnable(GL_LIGHTING);
glEnable(GL_NORMAL_ARRAY);
glViewport(0, 0, Display.getHeight(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 1, .1f, 1000f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
cam.look();
for (Cube cube : cubes) {
    cube.draw();
}
grid.draw();


and for getting the result:
glViewport(0, 0, Display.getHeight(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 1, .1f, 1000f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
cam.look();

modelview = BufferUtils.createFloatBuffer(16); //floatbuffers, defined earlier
projection = BufferUtils.createFloatBuffer(16);
viewport = BufferUtils.createIntBuffer(16);
FloatBuffer result = BufferUtils.createFloatBuffer(3);

glGetFloat(GL_MODELVIEW_MATRIX, modelview);
glGetFloat(GL_PROJECTION_MATRIX, projection);
glGetInteger(GL_VIEWPORT, viewport);

gluProject(1f, 0f, 0f, modelview, projection, viewport, result);
//Utils.printFloatBuffer(projection);
//Utils.printFloatBuffer(modelview);
System.out.println(result.get(0) + " " + result.get(1) + " " + result.get(2));


and finally, Camera.look() :
void look() {
    calcPoints(xAng, yAng);
    gluLookAt(x, y, z, x + dx, y + dy, z + dz, 0, 1, 0);
}


I've been facing this issue for weeks and can't find any information on it.

quew8

Is it just me or are you only giving (1, 0, 0) coords into gluProject?

freezway

Quote from: quew8 on March 07, 2013, 18:42:35
Is it just me or are you only giving (1, 0, 0) coords into gluProject?

Yes, I have a grid being drawn which allows me to see where that point is then compare what gluProject gives me. Example: I can see that that vertex on my grid is at about (0,200) screen coords (by eyeballing or screenshotting and measuring distances in gimp) then I run the gluProject and compare to that. When the difference in x y and z are the same, gluProject will return the same coords as where that vertex is on my screen. I can post a screenshot if it will help.

quew8

Ok. I've just had a looks at the glu specs:
QuoteA return value of GLU_TRUE indicates success, a return value of GLU_FALSE indicates failure
Although there is no info on what could cause a failure, still best to check this.
When you say the the differences are the same, are you talking about dx = dy = dz as in (1, 1, 1), (2, 2, 2), (3, 3, 3) etc.? The only thing I can think of right now is perhaps something in the draw() method of Cube. Perhaps we could see that.