I'm using the glm library for my project and now i want to calculate the screen and world coordinates. But somehow something went totally wrong.
For example if I want to get the screen coords from the camera target point (x 3575675.000000 y 0.000000 z 5674475.000000) with glm:roject I get the right x and y coordinates of the screen, but the z value seems not correct in this example i get x 149.0 y 121.5 z 0.999. Now as I thought z=0.99 would mean that the camera target point would nearly on the farplane of the perspective, but the farplane is defined as 100000.f. Now using this screen coords and unproject them with glm::unproject gives the world coordinates x 3575667.750000 y 29989.830000 z 5674465.000000. The new y value is nearly the distance between camera position and target position. What am I doing wrong?
Code :P = glm::perspective(fov, ratio, 0.1f, farPlane); //fov 45 ratio winW/winH farplane 100000
Code :glm::mat4 T = glm::translate(glm::mat4(1.0f),glm::vec3(X, Y, Z)); glm::mat4 Rx = glm::rotate(T, aX, glm::vec3(1.0f, 0.0f, 0.0f)); glm::mat4 Ry = glm::rotate(Rx, aX, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 Rz = glm::rotate(Ry, aZ, glm::vec3(0.0f, 0.0f, 1.0f)); M = glm::scale(Rz, glm::vec3(scaleXYZ));
Code :V = glm::lookAt( glm::vec3( posX, posY, posZ ),glm::vec3( tarX, tarY, tarZ ),glm::vec3( 0.0f, 1.0f, 0.0f ) );
Code :glViewport(0, 0, wWidth, wHeight); viewport=glm::vec4(0, 0, wWidth, wHeight);
screen to world:
Code :glm::project(inPos, V*M, P, viewport);
world to screen:
Code :glm::vec3 screen=glm::vec3(inVert.X(),wHeight-inVert.Y(),inVert.Z()); glm::vec3 world=glm::unProject(screen, V*M, P, viewport);
point: 3575675.00 0.00 5674475.000
point to screen: 149.00 121.50 0.99
screen to world: 3575667.75 29989.83 5674465.00
Hope anyone can help me...