In the code given below, shouldn’t the values printed by the two printf statements (1st and 3rd) be almost the same ? I dont get the same values. Could you please spot the error.
#include <iostream>
#include <windows.h>
#include “GL/glut.h”
using namespace std ;
GLdouble objX ;
GLdouble objY ;
GLdouble objZ ;
GLdouble a ;
GLdouble b ;
GLdouble c ;
void MotionFunc(int button, int state,int x, int y)
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
GLdouble *model = new GLdouble[16] ;
glGetDoublev(GL_MODELVIEW_MATRIX,model) ;
GLdouble *proj = new GLdouble[16] ;
glGetDoublev(GL_PROJECTION_MATRIX,proj) ;
GLint *view = new GLint[4] ;
glGetIntegerv(GL_VIEWPORT,view) ;
// printf statement 1
printf("%4d,%4d
",x,view[3]-y);
gluUnProject( x, view[3]-y, 1.0, model, proj, view, &objX, &objY, &objZ) ;
//printf statement 2
printf("%4d,%4d,%4d
",objX,objY,objZ);
gluProject(objX,objY,objZ,model, proj, view,&a,&b,&c);
//printf statement 3
printf("%4d,%4d,%4d
",a,b,c);
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0) ;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT) ;
glutSwapBuffers() ;
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0) ;
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv) ;
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB) ;
glutInitWindowSize(600, 400) ;
glutInitWindowPosition(0, 0) ;
glutCreateWindow(“MouseSample”);
init() ;
glutMouseFunc(MotionFunc);
glutDisplayFunc(display);
glutIdleFunc(display) ;
glutReshapeFunc(reshape) ;
glutMainLoop();
return 0 ;
}