Sign in to follow this  
zerothrillz

Getting 3D mouse position

Recommended Posts

Hi! I'm working on a level editor for my project engine. The user draw a bunch of polygons that are extruded and textured. Basically, taking a 2D level and making it 3D. The code can obtain the mouse coordinates in in orthogonic projection just fine, as they pertain to the 2D plain that the polygons exist on using code derived from the NeHe tutorial on the subject. As is, the code reads whatever the pixel coordinate the mouse is over and returns that pixel's projected x,y and z value. However, I want to be able to edit in the 3D projection mode as well. Is there a way I can unproject only to coordinates located on the X/Y plane, i.e. clamping to z == 0? Or is the solution to this to do line-plane intersection test using the line made from the camera position to the projected mouse coordinates and the X/Y plane on the 0 axis? Thanks for any help, I hope I described the problem well enough.
	int x = mouse_screen_x;
	int y = mouse_screen_y;
	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;
	winY = (float)viewport[3] - (float)y;
	glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

	mouse_x = posX;
	mouse_y = posY;
	mouse_z = posZ;
[Edited by - zerothrillz on May 3, 2010 12:10:56 PM]

Share this post


Link to post
Share on other sites
Yes, and I tried playing around with the settings to get the desired effect to no avail. I would like for someone to specify how to read only coordinates with z value of 0, or inform me that that is not possible and that I need to get the intersection point on that plain.

Share this post


Link to post
Share on other sites
Using depth (aka z) = 0 is easy: winZ = 0.0f;

Unprojecting this would result in a position relatively close to the camera (depending on your near plane setting).

If you want the position at world space z = 0 (i.e. in the xy-plane), I'd say there's no sensible way around ray-plane intersection tests (not a general test though, since this could be easily simplified).

Share this post


Link to post
Share on other sites
Thanks for the responses, guys.

Lord_Evil: The tip on using gluUnProject with a z of zero helped, I hadn't thought about using that to get the camera positon, as I was having trouble doing the transformation my self. Working great now!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this