Mouse coords to 3d coords

Started by
5 comments, last by Charleh 20 years, 1 month ago
I''ve used glunProject to get the convert the mouse coords to gl world coords, the problem is I''m looking at a 2d scene with x,y (width, height) in perspective and every quad laid on the scene on a z coord specified by a zoom variable. (for zooming the 2d scene in and out) How do I make sure that glunProject is giving me the mouse coords to the right z depth? At the moment its using the wrong z - the mouse cursor and the object drawn under the cursor in perspective mode dont meet up, the object meets up in the top left corner but as I move the mouse to the right the object moves faster than the mouse - same for downwards, so glunProject is giving me the positional coords from a z depth thats too deep! I''ve tried changing the z mouse coord passed to the func but it just seems to totally screw up unless its set to 1.
Advertisement
I think I didn''t explain that very well, and I''ve also had another look -

The mouse position is fine in ortho mode, gluUnProject gives me the right coordinates for displaying the object in ortho mode, but I want to position the object as if it was being drawn in ortho mode but in a perspective view

If my mouse x/y coords stay the same but the screen zoom (mouse z) coord changes gluUnProject should return the same x/y coords and a different z coord - I don''t want this to happen, I want to somehow scale/translate the x and y coords as if the object had moved from whatever z it was to z = 0.

Thinking about it all I need to do is convert the 3d world coordinates to 2d ortho coords - is there a gl command which does this easily?
Since the mouse is correct in the top left corner, you are most likely not scaling the mouse x and y to fit your OpenGL projection matrix. First you need to figure out the Z value at the position where you want the mouse cursor to appear. Then factor that Z value in when you figure out the screen position of the mouse.

Posting more details about your project would help.
Yeah thats what I need to do, scale the x,y coords - but what do I use to scale the x/y coords?

POINT m_GetMousePos(bool useGLCoords){	POINT mpos;	GetCursorPos(&mpos);					// Gets The Current Cursor Coordinates (Mouse Coordinates)	ScreenToClient(hWnd, &mpos);	if(useGLCoords == 1)	{		double mposx, mposy, mposz;		GLint viewport[4];		GLdouble modelview[16];		GLdouble projection[16];		GLfloat winX, winY;		glGetDoublev( GL_MODELVIEW_MATRIX, modelview );		glGetDoublev( GL_PROJECTION_MATRIX, projection );		glGetIntegerv( GL_VIEWPORT, viewport );		winX = (float)mpos.x;		winY = (float)viewport[3] - (float)mpos.y;		gluUnProject(winX, winY, 0, modelview, projection, viewport, &mposx, &mposy, &mposz);		mpos.x = -mposx;		mpos.y = -mposy;		return mpos;	}	else	{		return mpos;	}} 


Basically I''m returning the x/y coords, assuming I have a global called ''screenzoom'' which holds the Z offset for objects in the scene, how do I scale x/y to incorporate z?
Off topic: Why do you do if(useGlCoords == 1) instead of
if(useGlCoords)
Sorry I just think thats the beauty of booleans to keep the statement short.
Try google for "glRenderMode(GL_SELECT)" and "gluPickMatrix" I''m sorry, cant help you anymore than that - im still learning about the subject as well

This topic is closed to new replies.

Advertisement