Converting Mouse to OpenGl 3D
I'm trying to convert the following code so that a point SX,SY,SZ can be selected and moved around.
The thing is the SZ should stay the same distance during moving around but still the point stays sync-ed with the mouse pointer.
The following code will return mouse coordinates with z=0
void MouseTo3d()
{
glFlush();
POINT pt;
GetCursorPos(&pt);
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; // OpenGL y coordinate position
GLdouble wx, wy, wz; // returned world x, y, z coords
GLdouble wx2, wy2, wz2;
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
// note viewport[3] is height of window in pixels
realy = viewport[3] - (GLint) mousey - 1;
//float depth;
//glReadPixels(pt.x,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
gluUnProject ((GLdouble) mousex, (GLdouble) realy, 0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
gluUnProject ((GLdouble) mousex, (GLdouble) realy, 1.0f, mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);
double x_AB = (wx2 - wx);
double y_AB = (wy2 - wy);
double z_AB = (wz2 - wz);
double factor_t;
if (y_AB!=0 && CW==3)
factor_t = - (wy2 / (y_AB));
if (y_AB==0 && CW==3) factor_t=0;
if (x_AB!=0 && CW==1)
factor_t = - (wx2 / (x_AB));
if (x_AB==0 && CW==1) factor_t=0;
if (z_AB!=0 && CW==2)
factor_t = - (wz2 / (z_AB));
if (z_AB==0 && CW==2) factor_t=0;
space_x = wx2 + (factor_t * x_AB);
space_y = wy2 + (factor_t * y_AB);
space_z = wz2 + (factor_t * z_AB);
}
[edited by - bgcjr on September 5, 2002 11:28:23 PM]
Game Core
i figured a solution:
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
GLint realy = viewport[3] - (GLint) mousey - 1;
glReadPixels(mousex,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
As long as the scene view depth is not altered then depth of object stays the same.
so need to update it til that happens.
then:
gluUnProject ((GLdouble) mousex, (GLdouble) realy, depth,
mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
GLint realy = viewport[3] - (GLint) mousey - 1;
glReadPixels(mousex,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
As long as the scene view depth is not altered then depth of object stays the same.
so need to update it til that happens.
then:
gluUnProject ((GLdouble) mousex, (GLdouble) realy, depth,
mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);
Game Core
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement