PDA

View Full Version : Need help with glm::project & glm::unproject



caaso
05-26-2012, 10:10 AM
Hello,

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::project 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?

projection matrix:


P = glm::perspective(fov, ratio, 0.1f, farPlane); //fov 45 ratio winW/winH farplane 100000


model matrix:


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


view matrix


V = glm::lookAt( glm::vec3( posX, posY, posZ ),glm::vec3( tarX, tarY, tarZ ),glm::vec3( 0.0f, 1.0f, 0.0f ) );


viewport:


glViewport(0, 0, wWidth, wHeight);
viewport=glm::vec4(0, 0, wWidth, wHeight);


screen to world:


glm::project(inPos, V*M, P, viewport);


world to screen:


glm::vec3 screen=glm::vec3(inVert.X(),wHeight-inVert.Y(),inVert.Z());
glm::vec3 world=glm::unProject(screen, V*M, P, viewport);


output:
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...:sorrow:

greetz caaso

caaso
05-29-2012, 12:32 PM
No one who can help?

tonyo_au
05-29-2012, 07:46 PM
Now as I thought z=0.99 would mean that the camera target point would nearly on the farplane

This will be a long way short of the far plane with near 0.1f and farPlane 100000.
If you want better results from project/unproject you will need to go to double precision or keep near/far much closer together.
You might want to do some hand (ie with calculator) calulation for z with different near/far values. You will be surprised how much of you view is in the z-depth range of 0.999 - 1.0