Jump to content
  • Advertisement


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


Correct useage of gluUnProject.

This topic is 5970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
isn''t searching wonderful?

I have the same problem!

nobody answered this in 2000 so anybody 2 years later got an answer?

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.

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!