Archived

This topic is now archived and is closed to further replies.

griffenjam

Correct useage of gluUnProject.

Recommended Posts

I am trying to use gluUnProject to get the location of the mouse cursor, but the results I''m getting arn''t quite what I want. The cursor is moving in the correct direction but it is staying close to the origin (or not even on the screen). Not matter what I set windZ to. Am I missing something? I searched on google and didn''t see a good example useage. Jason Mickela ICQ : 873518 E-Mail: jmickela@sbcglobal.net ------------------------------ "Evil attacks from all sides but the greatest evil attacks from within." Me ------------------------------

Share this post


Link to post
Share on other sites
Hi there.
I understand your question as follows:
You have your mousePosition (x,y) and want to get the position in 3D space, depending on what you were drawing in the 3D Scene.

Here''s some code that does this. Note: The z-coordinate used by gluUnProject is always between 0 and 1.

posX, posY - mousePosition in 2D Screenspace relative to current viewport

GLdouble projection[16], modelview[16];
GLint viewport[4];
GLdouble result[3];

glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetIntegerv(GL_VIEWPORT, viewport);

if(posY >= viewport[1] && posY < viewport[3] &&
posX >= viewport[0] && posX < viewport[2])
{
posY = viewport[3] - posY;

GLfloat depth;
glReadBuffer(GL_BACK); // when using double buffer
glReadPixels(posX, posY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);

gluUnProject(posX, posY, depth, modelview, projection, viewport, &result[0], &result[1], &result[2]);

mPosition3D = Vector3f(result[0], result[1], result[2]);
}

Share this post


Link to post
Share on other sites
Thanks NextS.

You understood correctly and it works now.. and in conjunction with my 3d terrain and ray-collision checking i can now see where the mouse clicks in exact 3d co-ords on the terrain.

BTW:
Anyone know how to implement this using ray tracing from the eye through the mousex and mousey? That was my approach when i couldn''t get gluUnProject to work and i think it might be faster than calling gluUnProject.

Share this post


Link to post
Share on other sites