Jump to content
  • Advertisement
Sign in to follow this  
polar01

OpenGL Screen to 3D space - for picking

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

Hi,

I'm currently working on a picking algorithm WITHOUT OpenGL !
The algorithm work fine, but I have problem to transform the mouse X,Y into a "3D ray" for the picking.

To test, I have try with OpenGL like this (but it doesn't work !!)

glMatrixMode(GL_PROJECTION); // Set up the projection
glLoadIdentity();

float aspectRatio = ((float)surface->GetWidth()) / surface->GetHeight();
gluPerspective(45, aspectRatio, 0.1f, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
camera->GetEye().x, camera->GetEye().y, camera->GetEye().z,
camera->GetEyeAt().x, camera->GetEyeAt().y, camera->GetEyeAt().z,
camera->GetEyeUp().x, camera->GetEyeUp().y, camera->GetEyeUp().z);

double matModel[16];
double matProj[16];
GLint viewport[4];

glGetDoublev(GL_PROJECTION_MATRIX, matProj);
glGetDoublev(GL_MODELVIEW_MATRIX, matModel);
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble o[3];
GLdouble t[3];
gluUnProject ( x, surface->GetHeight() - y, 0.0, matModel, matProj, viewport, &o[0], &o[1], &o[2]);
gluUnProject ( x, surface->GetHeight() - y, 1.0, matModel, matProj, viewport, &t[0], &t[1], &t[2]);

out->o.x = o[0];
out->o.y = o[1];
out->o.z = o[2];

out->d.x = t[0] - o[0];
out->d.y = t[1] - o[1];
out->d.z = t[2] - o[2];

out->d = Normalize(out->d);


The problem is that I cannot use OpenGL, so I have this code :


//--- Get the camera axis
VNCamera* camera = surface->GetScene()->GetActiveCamera();
Vector3 eye = camera->GetEye();
Vector3 eyeAt = camera->GetEyeAt();
Vector3 axisz = Normalize(eyeAt - eye);
Vector3 axisy = camera->GetEyeUp();

Vector3 axisx = Cross(axisz, axisy);

//---- Origin
out->o = eye;

//---- Direction
// Project mouse to space
float fx = x;
float fy = surface->GetHeight() - 1.f - y;

// Normalize to [-1 , 1]
fx /= surface->GetHeight();
fy /= surface->GetHeight();

fx -= 0.5f;
fy -= 0.5f;

fx *= 2;
fy *= 2;

// Adapt for FOV
float fovInRadians = 45.f * (M_PI / 180.0f);
fx *= tanf(fovInRadians);

// Project the mouse coordinate on the Z-far-plane
out->d.x = axisx.x * fx + axisy.x * fy + eyeAt.x;
out->d.y = axisx.y * fx + axisy.y * fy + eyeAt.x;
out->d.z = axisx.z * fx + axisy.z * fy + eyeAt.x;

// Compute the direction
out->d = out->d - eye;
out->d = Normalize(out->d);

out->mint = 0;
out->maxt = INFINITY;


I'm unable to find the problem with the last code, in order to pick in an OpenGL windows ! So, if you have an idea ?

Thx

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!