🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

gluUnProject and gluPerspective

Started by
3 comments, last by MProg_25 18 years ago
I found a code for transform viewport coordinates in opengl coordinates (article 13 by Luke Benstead on nehe site), this code works really well but only if i disable the perspective. this is the code : float GetOGLPos(int x,int y,float*Vecteur) { 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 Vecteur[0]=posX,Vecteur[1]=posY,Vecteur[2]=posZ; } If you know how put the perspective in this code (or maybe i use it wrong) help me please. MProg_25
Advertisement
Yes , this code is very popular , my looks like this:
Vector3D GetOGLPos(int x, int y){	GLint viewport[4];	GLdouble modelview[16];	GLdouble projection[16];	GLint winX, winY;	GLfloat winZ;	GLdouble posX, posY, posZ;	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );	glGetDoublev( GL_PROJECTION_MATRIX, projection );	glGetIntegerv( GL_VIEWPORT, viewport );	winX = x;	winY = viewport[3] - y;	glReadPixels( winX,winY, 1,1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);	return Vector3D((float)posX, (float)posY, (float)posZ);}


I dont know what do you mean by perspective, do you mean ortho view??

Anyways , i use this code for ray casting , my origin is my camera point, and my direction is caculated:
 Vector3D vDir=GetOGLPos(...)-vCamEye; vDir.normalize();


If you use ortho projection, i dont understand why do you bother by calculating with OGLPos ?!
That's my problem, this code works well only in ortho projection and i would use it with gluperspective (not ortho view).

I would convert the viewport coordinates in openGL coordinates with perspective (gluPerspective (45,(float)width/(float)height,1,100)) (not ortho mode).

It should definitely work with perspective view as well.

Porbably matrix states for GL_PROJECTION has changed just before your GetOGLPos() call.
So that mean i don't use correctly this code ... ok.

I call the projection matrix only in the resize code in WM_SIZE and in the picking code.

I put this code in the draw part of my code after

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

and before

SwapBuffers (DC);

if someone know how to use GetOGLPos plz help me

This topic is closed to new replies.

Advertisement