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 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;
m3dTransformVector3(objXYZ,tmp,invMVP);
}
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 );
UnProject(window_x,window_y,winZ,objPt);
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.