Jump to content
  • Advertisement
Sign in to follow this  
Golden Dragon

OpenGL Help with gluUnProject

This topic is 4857 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 currently working on an editor, and need to be able to find out where on the xz-plane that the user clicks on, so that they could add a new object at that location. From what I've read, this can be achieved with gluUnProject, but I can't get it to work right. Here is the code I have at the moment:
GLdouble mModel[16], mProj[16];
GLint    viewport[4];
GLdouble objXn, objYn, objZn;
objXn=OpenGL.Camera().GetPosition()[VEC_X];
objYn=OpenGL.Camera().GetPosition()[VEC_Y];
objZn=OpenGL.Camera().GetPosition()[VEC_Z];
GLdouble objXf, objYf, objZf;
objXf=0.0f;
objYf=0.0f;
objZf=0.0f;

glGetDoublev(GL_MODELVIEW_MATRIX, mModel);
glGetDoublev(GL_PROJECTION_MATRIX, mProj);
glGetIntegerv(GL_VIEWPORT, viewport);

nY = viewport[3] - nY;

// get the points
//gluUnProject( nX, nY, 0.0f, mModel, mProj, viewport, &objXn, &objYn, &objZn);
gluUnProject( nX, nY, 1.0f, mModel, mProj, viewport, &objXf, &objYf, &objZf);

tVec3D vPoint;

double dist = -objYn/(objYn-objYf);

vPoint[VEC_X] = objXn + dist*(objXn-objXf);
vPoint[VEC_Y] = objYn + dist*(objYn-objYf);
vPoint[VEC_Z] = objZn + dist*(objZn-objZf);

However, vPoint does not have the correct values in it. For example, if I click on -1,0,0 vPoint will have something strange like -36, 0, -360, which is definitely not right. What am I doing wrong? Thanks Golden Dragon

Share this post


Link to post
Share on other sites
Advertisement
I was having a similar problem a while back, and it turned out that my modelview and proj matricies were incorrect. You need to handle your mouse clicks after you call gluLookAt() (or after all of your translations/rotations needed to orient your world).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!