Archived

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

try_catch_this

Need Help Using gluUnProject

Recommended Posts

bool Reshape( long width, long height )
{
   if( height == 0 ) // Prevent A Divide By Zero By

      height=1;
   ::glViewport( 0, 0, (GLsizei)(width), (GLsizei)(height) );
   ::glMatrixMode( GL_PROJECTION );
   ::glLoadIdentity();                                          // Reset The Projection Matrix

   ::gluPerspective( 45.0f, (GLfloat)(width)/(GLfloat)(height), // Calculate The Aspect Ratio Of The Window

					 0.010f, 1000.0f);
   ::glMatrixMode( GL_MODELVIEW );                              // Select The Modelview Matrix

   ::glLoadIdentity();
   return false;
}


C_Vector2D WindowToGLCoordinates( GLdouble x, GLdouble y )
{
   GLdouble model[16] = {0}, proj[16] = {0}, obj[3] = { 0, 0, 0 };
   GLint    view[4]   = {0};
   double   z = -40;

   ::glGetDoublev(  GL_MATRIX_MODE,       model );
   ::glGetDoublev(  GL_PROJECTION_MATRIX, proj  );
   ::glGetIntegerv( GL_VIEWPORT,          view  );

   y = (double)view[3] - y;
   ::glReadPixels( (int)(x), (int)(y), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z );
   ::gluUnProject( x, y, z, model, proj, view, &obj[0], &obj[1], &obj[2] );
   return C_Vector2D( (double)(obj[0]), (double)(obj[1]) );
}
Most of this code is from NeHe. The first is how I set up the perspective. Next is how I try to get the glcoordinates from mouse coodrinates. Anyone see anything wrong here? C_Vector2D ends up having 0, 0; I am sure they are not called between glbegin and slend.
gdipong oglpong

Share this post


Link to post
Share on other sites
I have been having trouble with this for a few days now.

I really need help with this.


bool Reshape( long width, long height )
{
if( height == 0 ) // Prevent A Divide By Zero By

height=1;
::glViewport( 0, 0, (GLsizei)(width), (GLsizei)(height) );
::glMatrixMode( GL_PROJECTION );
::glLoadIdentity(); // Reset The Projection Matrix

::gluPerspective( 45.0f, (GLfloat)(width)/(GLfloat)(height), // Calculate The Aspect Ratio Of The Window

0.010f, 1000.0f);
::glMatrixMode( GL_MODELVIEW ); // Select The Modelview Matrix

::glLoadIdentity();
return false;
}

C_Vector3D MainWindow::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 );
}


This is how I call it.
POINT mouse;
GetCursorPos( &mouse );
ScreenToClient( mw.m_WinBase, &mouse );
C_Vector3D t = mw.WindowToGLCoordinates( mouse.x, mouse.y );

Share this post


Link to post
Share on other sites
::glClearDepth( 1.0f );
::glDepthFunc( GL_LEQUAL ); // The Type Of Depth Testing (Less Or Equal)
::glEnable( GL_DEPTH_TEST );
::glShadeModel( GL_SMOOTH ); // Select Smooth Shading
::glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
::glBlendFunc( GL_ONE, GL_SRC_ALPHA ); // Set Blending Mode (Cheap / Quick)
::glEnable( GL_BLEND );
::glEnable( GL_TEXTURE_2D );
::glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
::glColor3f( 1, 1, 1 );

After some help on the IRC it looks like my values are off by half now. But the Z coordinate is correct though

Share this post


Link to post
Share on other sites