Hi,
don't know if it helps, but this is a method for the class Mouse, class =
that I use to deal with ecerything concerned to the mouse, of course :-) =
Given the mouse position on the screen (x and y) it returns an =
sdgLineSegment3* ... I use it 'cause I'm developing a scenario creation =
program=20
for my flight simulator, and I want it to look like Sim City or stuff =
like that, so that I can put houses and trees and facilities on the
terrain map just clicking with the mouse... this is the sgdLineSegment3* =
that you will test against the terrain scenegraph to get the triangles =
that
collided 'under' your mouse arrow... Ok, weird explanation, sorry :-P =
but it performs the unproject stuff from 2D to 3D and of course you get =
a line
cause you are moving from 2D space to 3D space...
=20
sgdLineSegment3* Mouse::unproject(int x, int y)
{
GLint viewport[4];
GLdouble projmatrix[16],mvmatrix[16];
GLint realy; /* OpenGL y coordinate position */
GLdouble wx, wy, wz; /* returned world x, y, z coords */
GLdouble wxw, wyw, wzw;
/* Load the OpenGL matrices (biewport and projection are correct, =
modelview is Ones) */
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
// Get the current context
ssgContext *curr_context =3D ssgGetCurrentContext();
/* Create a support sgMat4 for MODELVIEW */
sgMat4 modelview;
curr_context->getModelviewMatrix(modelview);
/* This matrix should be of doubles!!! */
sgdMat4 d_modelview;
makeSgMat4double(modelview,d_modelview);
/* Create a support matrix for PROJECTION */
sgMat4 projection;
curr_context->getProjectionMatrix(projection);
/* This matrix shoul be of doubles!!! */
sgdMat4 d_projection;
makeSgMat4double(projection,d_projection);
/* note viewport[3] is height of window in pixels */
realy =3D viewport[3] - (GLint) y - 1;
gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0, (GLdouble *) =
d_modelview, (GLdouble *)d_projection, viewport, &wx, &wy, &wz);=20
gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0, (GLdouble *) =
d_modelview, (GLdouble *)d_projection, viewport, &wxw, &wyw, &wzw);=20
// Create the sgVec3* line (used for SSG_ISECT!!!)
sgdLineSegment3 *lineS =3D =
(sgdLineSegment3*)malloc(sizeof(sgdLineSegment3));
sgdSetVec3(lineS->a,wxw, wyw, wzw);
sgdSetVec3(lineS->b,wx, wy, wz);
curr_context->setNearFar(CAMERA_NEAR,CAMERA_FAR);
return lineS;
} |