Advertisement Jump to content
Sign in to follow this  

Replacement for gluUnproject

This topic is 4958 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

C_Vector3D WindowToGLCoordinates( int x, int y )
   GLint    viewport[4];
   GLdouble modelview[16];
   GLdouble projection[16];
   GLfloat  winX, winY, winZ;
   GLdouble posX, posY, posZ;

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

   winX = (float)x;
   winY = (float)viewport[3] - (float)y;
   ::glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

   ::gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

   return C_Vector3D( posX, posY, posZ );

C_Vector2D GLToWindowCoordinates( double x, double y, double z )
   GLdouble *model = NULL, *proj = NULL, *win[3];
   GLint    *view  = NULL;
   ::glGetDoublev(  GL_MATRIX_MODE,       model );
   ::glGetDoublev(  GL_PROJECTION_MATRIX, proj  );
   ::glGetIntegerv( GL_VIEWPORT,          view  );

   ::gluProject( (GLdouble)x, (GLdouble)y, z, model, proj, view, win[0], win[1], win[2] );
   return C_Vector2D( (double)*(win[0]), (double)*(win[1]) );

I recently disabled depth buffer writes in my game and got a massive fps increase. From 60-90. I dont want to turn it back on. Problm is that certain things dont quite work any more. the above code is one of them. I was using those functions to tell if the mouse was over my buttons and stuff but they nolonger work cause the z value returned is nolonger accurate. Anyone know how to replace these functions when the depth buffer is off? Or host to only enable the depth buffer at certain times? thanks

Share this post

Link to post
Share on other sites
So you're using it for selection, basically? If so, why not use the selection mode? Check out glSelectBuffer and glRenderMode. To identify an object at a certain pixel, you can also use the stencil buffer. Draw the object to the stencil buffer, writing a unique number for each selectable object.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!