PDA

View Full Version : transformation of coordinates



ss122
01-28-2007, 08:53 PM
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\t");
printf("%4d,%4d\n",x,z);
gluUnProject (x,z,0.0, mvmatrix, projmatrix, viewport, &dX,&dY,&dZ);
printf("object coordinates\t");
printf("%4d,%4d,%4d\n",dX,dY,dZ);
gluProject(dX,dY,dZ,mvmatrix, projmatrix, viewport,&a,&b,&c);
printf("window coordinates\t");
printf("%4d,%4d,%4d\n",a,b,c);
printf("\n\n");
}
}

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;
}

vbovio
01-30-2007, 12:06 PM
looks like you are not changing the projection matrix to specify a 2d window before the gluUnProject/gluProject calls. I suggest that in your mouse function, you specify a 2d projection either with glOrtho or gluOrtho2D passing the window size and then do the calls, oh, and remember to push/pop your 3d projection.
also you are printing them as integers? %4d ?, try as floats like: %4.2%..
hope that helps.

ss122
01-31-2007, 08:36 PM
thankyou...the data type was causing all the problem (neeeded to be converted into float)