Correct useage of gluUnProject.
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
------------------------------
isn''t searching wonderful?
I have the same problem!
nobody answered this in 2000 so anybody 2 years later got an answer?
I have the same problem!
nobody answered this in 2000 so anybody 2 years later got an answer?
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]);
}
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]);
}
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.
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.
hehe i tried that and it doesnt exactly work for me, it moves the object to some random place or something (definately does not move it to where i clicked =P )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement