1

So I'm using gluProject to take the 3d coordinates and tell me where on the screen they rendered. The problem is I don't get the same coordinates with gluProject and where they actually ended up rendering. The weird part is that if the difference between the x, y, and z of the camera and the point i'm testing are equal (e.g. camera at 5,3,4 and point at 2,0,1) it gives the correct values. I'm using gluLookAt() for camera transforms. I have confirmed that the same matrices that I use for rendering are being passed in to gluProject. Below is my rendering code, camera.look(), and my part with gluProject:

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();

Camera.look():

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

part with gluProject:

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));
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Not sure if this would matter but you don't need 16 entries for the viewport, 4 is all it returns (starting x and y then width and height). – james82345 Mar 07 '13 at 00:03
  • Oh man , we are in the year 2013 and you are still using fixed pipeline?Use the modern approach and then it is up to you how your projection matrix looks like... – Michael IV Mar 07 '13 at 08:25

1 Answers1

0

Maybe try having the up vector at a right angle to the camera direction and position instead of (0, 1, 0) constantly.

james82345
  • 530
  • 4
  • 13