Hey guys I need a little help as this problem is giving me some trouble. What I'm doing is calculating the mouse world coordinates using gluUnproject and that works fine. I'm trying to implement a point and click method of movement for my game character which also works, until the camera starts to follow them. All movement is in 2D but the game world is in 3D. The perspective camera basically follows the position of the character so they never go out of focus.
Now the actual problem is when I'm moving the character and the camera is following and I click a new position, the position given by gluUnproject seems to be 'off' by some offset value which I can't figure out. This 'offset' value seems to increase as well the further the mouse is away from the character while moving.
I can't figure out what is causing this issue and any help would be appreciated, thanks.
Here is the code I'm using for gluUnproject taken from Nehe article #13.
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x; // Mouse x coordinate
winY = (float)viewport[3] - (float)y; // Mouse y coordinate
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
return CVector3(posX, posY, posZ);
Here is the code I use for the camera to follow the player which is basically passing the character's position to gluLookAt()
// Reset the current viewport
glViewport(0, 0, w-1, h);
// Setup the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(_fov, _aspect, _zNear, _zFar);
// Setup the view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(_target.X(), _target.Y(), _eye.Z(), // Eye
_target.X(), _target.Y(), _target.Z(), // Target
_up.X(), _up.Y(), _up.Z() ); // Up
[Edited by - 0beSt0 on June 8, 2008 12:35:59 AM]