can't get gluUnProject to work with mouse events
Hi Graphics gurus,
I'm totally new to OpenGL and I'm trying to convert mouse events into my world coordinates. I've drawn 4 lines making a rectangle on the XY plane and I'm looking at the origin with the camera. The points of the rectangle are (-5,0),(5,0),(-5,5),(5,5)... However, the corresponding translations I'm getting from gluUnProject are (-50,0),(50,0),(-50,50),(50,50). It sure looks like I just need to divide by 10 to get the correct results... Could be a coincidence, so of course I need to know what I'm doing wrong or missing.
Here's my world setup code:
glfwGetWindowSize( &width, &height );
// Make sure that height is non-zero to avoid division by zero
height = height < 1 ? 1 : height;
glViewport( 0, 0, width, height );
// Clear color and depht buffers
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Set up projection matrix
glMatrixMode( GL_PROJECTION ); // Select projection matrix
glLoadIdentity(); // Start with an identity matrix
gluPerspective( // Set perspective view
65.0, // Field of view = 65 degrees
(double)width/(double)height, // Window aspect (square pixels)
1.0, // Near Z clipping plane
100.0 // Far Z clippling plane
);
// Set up modelview matrix
glMatrixMode( GL_MODELVIEW ); // Select modelview matrix
glLoadIdentity(); // Start with an identity matrix
gluLookAt( // Set camera position and orientation
0.0, 0.0, 10.0, // Camera position (x,y,z)
0.0, 0.0, 0.0, // View point (x,y,z)
0.0, 1.0, 0.0 // Up-vector (x,y,z)
);
drawOneLine(-5,0, 5, 0);
drawOneLine(-5,0, -5, 5);
drawOneLine(-5,5,5,5);
drawOneLine(5,5, 5,0);
...
and then here's where I call gluUnProject from within my mouse moved callback:
void CGLFWTestProjectDlg::MouseMoved(int x, int y)
{
static GLdouble modelmatrix[16];
static GLdouble projmatrix[16];
static GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
static GLdouble x1, y1, z1, x2, y2, z2, rx, ry;
gluUnProject(x,viewport[3]-y,1,modelmatrix,projmatrix,viewport,&x2,&y2,&z2);
CString S;
S.Format("Mouvemoved(%d,%d) --> %lf, %lf, %lf\r\n", x,y, x2,y2, z2);
ATLTRACE(S);
}
Any ideas what I'm doing wrong? Thanks a lot for any help!
Mike
in your line of code
your winZ value shouldnt be 1. it needs to be something between 0 & 1. 0 being your far plane and 1 being the near plane. im not sure as to why you are getting a value that large.
gluUnProject(x,viewport[3]-y,1,modelmatrix,projmatrix,viewport,&x2,&y2,&z2);
your winZ value shouldnt be 1. it needs to be something between 0 & 1. 0 being your far plane and 1 being the near plane. im not sure as to why you are getting a value that large.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement