View Full Version : Cannot pick object using ray casting method

03-09-2011, 12:28 AM
Hi all,
I tried to solve the riddle myself but still cant solve it. Basically i am trying to pick object by casting ray into the scene. I have read a couple of threads in this forum like this one http://www.opengl.org/discussion_boards/...true#Post279322 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=279322&Searchpa ge=1&Main=53954&Words=glunproject&Search=true#Post 279322) but still it aint working for me.
I m using opengl3.3 core profile. I define my own UnProject function as follows,

//invMVP is inverse of MVP matrix
//win[x,y] range from -[win_width/2,win_height/2] to [win_width/2,win_height/2]
winz is obtained using
glReadPixels( x, height-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

void UnProject(float winx, float winy, float winz, float* objXYZ) {
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

M3DVector3f tmp;

tmp[0] = ((2*winx-viewport[0])/float(viewport[2])) -1;
tmp[1] = ((2*winy-viewport[1])/float(viewport[3])) -1;
tmp[2] = 2*winz-1;


After these calls, i generate a ray from the cam position (viewRay.o) in the ray direction. So here is the whole thing can anyone tell me if I am missing something.

int window_x = x - width/2.0;
int window_y = (height - y) - height/2.0;
glReadPixels( x, height-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
viewRay.d.x = objPt[0]-viewRay.o.x;
viewRay.d.y = objPt[1]-viewRay.o.y;
viewRay.d.z = objPt[2]-viewRay.o.z;

Note that i have checked my winZ values and they are correct.

03-09-2011, 05:45 AM
colour picking for the win ..
You are making life hard for yourself. Inverse projection isn't really that accurate anyway.

03-09-2011, 06:24 AM

you can use this page, I think it's very helpful:


03-09-2011, 03:36 PM
Hi all,
I managed to solve the problem. I was not dividing the os coordinates (after multiplication with the inverse MVP) by os.w now it is perfectly fine. I found this wiki page http://www.opengl.org/wiki/GluProject_and_gluUnProject_code to be helpful. Here is my UnProject code

void UnProject(float winx, float winy, float winz, GLint* viewport, float* objXYZ) {
M3DVector4f tmp;

tmp[0] = (2.0f*((winx-viewport[0])/float(viewport[2]))) -1;
tmp[1] = (2.0f*((winy-viewport[1])/float(viewport[3]))) -1;
tmp[2] = (2.0f*winz)-1.0;
tmp[3] = 1.0;