wheni use gluUnProject and gluproject for transfering from window to world coordinates, and vice versa using the following code, i get errors in the coordinates displayed.
Could someone please help me out as i have been struggling with this for a long time now.
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include “glui.h”
float xy_aspect;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDepthRange(0.0, 1.0);
}
void pickRects(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
GLdouble dX, dY, dZ,a,b,c;
GLint *viewport = new GLint[4] ;
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble *mvmatrix = new GLdouble[16] ;
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
GLdouble *projmatrix = new GLdouble[16] ;
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
int z=viewport[3] -y;
printf("window coordinates “);
printf(”%4d,%4d
",x,z);
gluUnProject (x,z,0.0, mvmatrix, projmatrix, viewport, &dX,&dY,&dZ);
printf("object coordinates “);
printf(”%4d,%4d,%4d
",dX,dY,dZ);
gluProject(dX,dY,dZ,mvmatrix, projmatrix, viewport,&a,&b,&c);
printf("window coordinates “);
printf(”%4d,%4d,%4d
“,a,b,c);
printf(”
");
}
}
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 15.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glFlush();
}
void myGlutReshape( int x, int y )
{
int tx, ty, tw, th;
GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
glViewport( tx, ty, tw, th );
xy_aspect = (float)tw / (float)th;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (200, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutMouseFunc(pickRects);
glutDisplayFunc(display);
GLUI_Master.set_glutReshapeFunc( myGlutReshape );
glutMainLoop();
return 0;
}