简体   繁体   中英

Why does gluProject not give the correct screen space coordinates with LWJGL

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

也许尝试使up矢量与摄像机的方向和位置成直角,而不是始终保持(0,1,0)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM