1个回答
展开全部
2D向3D转换。逆变换
void mouse(int button, int state, int x, int y)
{
GLint viewport[4];
GLdouble mvmatrix[16],projmatrix[16];
GLint realy;
GLdouble wx,wy,wz;
switch(button) {
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN) {
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
realy = viewport[3] - y -1;
printf("Coordinate at curosr are (%4d, %4d\n",x,realy);
gluUnProject(x,realy,0,mvmatrix,projmatrix,viewport,&wx,
&wy,&wz);
printf("World coords at z=0 are (%f, %f, %f)\n",wx,wy,wz);
gluUnProject(x,realy,1,mvmatrix,projmatrix,viewport,&wx,
&wy,&wz);
printf("World coords at z=1 are (%f, %f, %f)\n",wx,wy,wz);
}break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN)
exit(0);break;
default: break;
}
}
void mouse(int button, int state, int x, int y)
{
GLint viewport[4];
GLdouble mvmatrix[16],projmatrix[16];
GLint realy;
GLdouble wx,wy,wz;
switch(button) {
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN) {
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
realy = viewport[3] - y -1;
printf("Coordinate at curosr are (%4d, %4d\n",x,realy);
gluUnProject(x,realy,0,mvmatrix,projmatrix,viewport,&wx,
&wy,&wz);
printf("World coords at z=0 are (%f, %f, %f)\n",wx,wy,wz);
gluUnProject(x,realy,1,mvmatrix,projmatrix,viewport,&wx,
&wy,&wz);
printf("World coords at z=1 are (%f, %f, %f)\n",wx,wy,wz);
}break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN)
exit(0);break;
default: break;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询