Jump to content
  • Advertisement
Sign in to follow this  

SFML and OpenGL ray picking in debug not working

This topic is 2365 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 have no clue why, but when I use window mode, I get incorrect values for my ray picking tests, if I run fullscreen I am good to go.

Any ideas why that would be?

I am not sure if it has to do with the way SFML makes a RC for a window mode vs. fullscreen, but glReadPixels under a window mode return 1 and with the fullscreen I get a value that is less than 1 e.g. .7 and I get correct results for my picking...

Thanks Edited by MARS_999

Share this post

Link to post
Share on other sites
No one here has seen this issue before?

I tried the same code now with FreeGLUT in a window mode and works fine, but not SFML. Anything with RC in the window creation cause this to happen vs. fullscreen setup?


Share this post

Link to post
Share on other sites
I wrote picking not long ago for my game engine school task like this:

(I kind of left out some unrelated bits of pieces at beginning and end of this function so..)

void Controller::tryPick(int mouseX, int mouseY) {

Camera_ptr camera = model->getSceneManager().getCamera();

if (!camera) {

GLdouble x = mouseX;
GLdouble y = mouseY;

//Set Projection

//Set ModelView
gluLookAt(camera->getEye().x, camera->getEye().y, camera->getEye().z,
camera->getLookAt().x, camera->getLookAt().y, camera->getLookAt().z,
camera->getUp().x, camera->getUp().y, camera->getUp().z);

//Get Viewport
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);

//Fix Y coord
y = (double)vp[3] - y;

//Get Matrices
GLdouble proj[16], movi[16];
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetDoublev(GL_MODELVIEW_MATRIX, movi);

//Compute hit points on near and far plane of mouse coordinate
GLdouble nearx, neary, nearz;
gluUnProject(x, y, 0, movi, proj, vp, &nearx, &neary, &nearz);
GLdouble farx, fary, farz;
gluUnProject(x, y, 1, movi, proj, vp, &farx, &fary, &farz);

//Direction of ray is hit point of far plane minus point on near plane
Vector4 direction(farx - nearx, fary - neary, farz - nearz, 0.0f);

//Create Inverse Matrix of ModelView Matrix
float movit[16];
for (int i = 0; i < 16; i++) {
movit = movi; //Double to Float
Matrix4 matrix;
MatrixInverse(movit, matrix.m); //1st param is IN, 2nd OUT

//Base Position of Camera is 0
Vector4 position(0.0f, 0.0f, 0.0f, 1.0f);

//Position * Inversed Matrix
position = matrix * position;

//Create Ray
Ray ray(position, direction);
Edited by ProgrammerDX

Share this post

Link to post
Share on other sites
This is what I am using at works as I said in FreeGLUT window mode and SFML fullscreen but not SFML window mode....

static inline void GetScreenPosition(double mouseX, double mouseY,
double& x, double& y, double& z)
float mouseZ = 0.0f;
GLint vp[4];
GLdouble projection[16] = {0.0};
GLdouble modelview[16] = {0.0};
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetIntegerv(GL_VIEWPORT, vp);
double yInverted = double(vp[3]) - mouseY;
glReadPixels(int(mouseX), int(yInverted), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouseZ);
gluUnProject(mouseX, yInverted, mouseZ, modelview, projection, vp, &x, &y, &z);

Share this post

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