Jump to content
  • Advertisement

Archived

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

vicviper

Projecting screen cursor coordinates over a 3D plane (D3DXVec3Unproject)

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

Hi, I'm trying to do a table game that can be played with a cursor. What I want is to convert the cursor XY screen coordinates to XYZ worldspace coordinates. I found that DirectX has D3DXVec3Unproject function that is what I'm looking for, the problem is that I need to use it for openGL too... is there any sourcecode that does the same as that D3DX function? [edited by - vicviper on September 4, 2003 4:31:20 AM]

Share this post


Link to post
Share on other sites
Advertisement
The function you need is "gluUnproject".
There is an example with glut.
Bye.

#include <GL/glut.h>

void Display();
void Reshape(int w,int h);
void Mouse(int button,int state,int x,int y);

int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMouseFunc(Mouse);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

glutMainLoop();
return 0;
}

GLdouble ox=0.0,oy=0.0,oz=0.0;
void Display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(10.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);

glPushMatrix();
glRotatef(45,1.0,0.0,0.0);
glutSolidTorus(4.0,5.0,20,20);
glPopMatrix();

glPushMatrix();
glDepthMask(GL_FALSE);
glTranslated(ox,oy,oz);
glutSolidSphere(0.5,15,15);
glDepthMask(GL_TRUE);
glPopMatrix();

glFlush();
}

void Reshape(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(float)w/(float)h,5.0,30.0);
glMatrixMode(GL_MODELVIEW);
}

void Mouse(int button,int state,int x,int y) {
GLint viewport[4];
GLdouble modelview[16],projection[16];
GLfloat wx=x,wy,wz;

if(state!=GLUT_DOWN)
return;
if(button==GLUT_RIGHT_BUTTON)
exit(0);
glGetIntegerv(GL_VIEWPORT,viewport);
y=viewport[3]-y;
wy=y;
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);
glutPostRedisplay();
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!