PDA

View Full Version : gluUnproject and gluProject



ss122
01-28-2007, 07:31 PM
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\n",x,view[3]-y);

gluUnProject( x, view[3]-y, 1.0, model, proj, view, &objX, &objY, &objZ) ;

//printf statement 2
printf("%4d,%4d,%4d\n",objX,objY,objZ);


gluProject(objX,objY,objZ,model, proj, view,&a,&b,&c);

//printf statement 3
printf("%4d,%4d,%4d\n",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 ;
}

Debugger
01-29-2007, 05:55 AM
// printf statement 1
printf("%4d,%4d\n",x,view[3]-y);

gluUnProject( x, view[3]-y, 1.0, model, proj, view, &objX, &objY, &objZ) ;

//printf statement 2
printf("%4d,%4d,%4d\n",objX,objY,objZ);


gluProject(objX,objY,objZ,model, proj, view,&a,&b,&c);

//printf statement 3
printf("%4d,%4d,%4d\n",a,b,c);

1. Why do you use view[3]-y?, x and y is the window coordinates relative to the upper-left corner of the screen when you release your mouse, it’s not the object’s coordinate, use y please.
2. You define objX, objY, objZ and a, b, c as double float variable, but you use %d when printing, this will lost some data, try using %f instead

The following will works

// printf statement 1
printf("%4d,%4d\n",x, y);

gluUnProject( x, y, 1.0, model, proj, view, &amp;objX, &amp;objY, &amp;objZ) ;

//printf statement 2
printf("%f,%f,%f\n",objX,objY,objZ);


gluProject(objX,objY,objZ,model, proj, view,&amp;a,&amp;b,&amp;c);

//printf statement 3
printf("%f,%f,%f\n",a,b,c);

ss122
01-31-2007, 08:26 PM
thanks a lot! it did work when i changed it to float variable type. it has to be viewport[3] - y because the screen coordinates have top left as the origin

Jeff1000
01-31-2007, 09:19 PM
Hi ss122,

Which was changed to float variable type, could you please post your working code? I am trying to use them also, thanks!

ss122
02-04-2007, 08:38 PM
the code which is displayed above in the first post works.. only thar you wil have to use %f (float variable) instead of integer while using the printf statement. hope u get it.