Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Charleh

Mouse coords to 3d coords

This topic is 5406 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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • 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!