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


gluUnProject to z=0

Recommended Posts

Ok, this game is mostly 2d. All of the spaceships stay on the z=0 plane. Now I'm trying to use the mouse to tell my little spaceship where to go. So I punch the mouse coordinates into gluUnProject like so:
  gluUnProject ( (double) mouse_x, 
                 (double) mouse_y, 0.0,
                 modelMatrix, projMatrix, viewPort,
                 &x, &y, &z );
And I get back x,y,z. But the z is 1800 (where the camera is) and not 0 like I want. So now I suppose that I need to find out where a vector coming from the camera, passing through the x,y,z from the gluUnProject above, intersects the z=0 plane. How do I do this? Unless of course there's an easier way... Thanks in advance. [edited by - JahToasted on January 17, 2004 2:34:49 AM]

Share this post

Link to post
Share on other sites
Nevermind, I figured it out.

For those who have the same problem here is the solution. This is useful if you want to convert mouse coordinates into opengl coordinates:

// Position View Up Vector
gluLookAt(mX, mY, mZoom, mX,mY, 0, 0, 1, 0);

glGetDoublev ( GL_MODELVIEW_MATRIX, modelMatrix ) ;
glGetDoublev ( GL_PROJECTION_MATRIX, projMatrix ) ;
glGetIntegerv ( GL_VIEWPORT, viewPort ) ;

gluUnProject ( mouse_x,
screen_height - mouse_y,
0.0 , modelMatrix, projMatrix, viewPort,
&gl_x, &gl_y, &z );

gl_x = mX + (mZoom * (gl_x - mX) / (mZoom - z))) ;
gl_y = mY + (mZoom * (gl_y - mY) / (mZoom - z))) ;

gl_x and gl_y will be the mouse''s coordinates at z=0. For different values of z I''m guessing this would be what you''d need to do:

GLdouble gl_z = 100.0 ;
gl_x = mX + ((mZoom - gl_z) * (gl_x - mX) / (mZoom - gl_z - z))) ;
gl_y = mY + ((mZoom - gl_z) * (gl_y - mY) / (mZoom - gl_z - z))) ;

Of course that''s only a guess, I haven''t tried it, and I''m not going to since all I needed was the z=0 case.

I hope this helps someone who was struggling like I was.

Share this post

Link to post
Share on other sites