Jump to content
  • Advertisement
Sign in to follow this  

Scrolling camera and gluUnproject

This topic is 4145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

	gluPerspective(_fov, _aspect, _zNear, _zFar);

	// Setup the view matrix

	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]

Share this post

Link to post
Share on other sites
I think I am getting closer to sorting out my problem but I'm not quite there yet. I was thinking that because my camera is moving when the character moves, the modelview matrix is changing and that's what is making it 'seem' as if gluUnproject isn't updating fast enough. I tried reloading the modelview matrix after drawing the character but that resulted in the mouse positions not being updated unless I moved the mouse. At this point the mouse coordinates seemed to be all over the place. I tried playing around with it some more but I still can't seem to figure it out. Here's a couple of images to try and clarify what's happening.

I have drawn a simple line from the character (solid quad) to the desired position (wired quad) which is the position of the mouse.

Ok this image is before I move the character anywhere and everything is fine.


Now here I have moved the character up a tiny bit, notice how the actual mouse position (wired quad) and the position (end of line) given by gluUnproject differ.


Any ideas?

[Edited by - 0beSt0 on June 8, 2008 12:15:44 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!