1
我成立了一個場景中使用:雷與gluUnProject()進入世界採摘座標
glViewport(0,0,screen->w,screen->h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const float wh = (float)screen->w/(float)screen->h;
gluPerspective(zoom,wh,1,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-3); // puts model inside z-range
後來,當用戶點擊,我要計算在世界射線座標我相交測試(不使用glReadPixels()
獲得Z):
double mv[16], p[16], a, b, c, d, e, f;
glGetDoublev(GL_MODELVIEW_MATRIX,mv);
glGetDoublev(GL_PROJECTION_MATRIX,p);
matrix_t _mv, _p;
glGetFloatv(GL_MODELVIEW_MATRIX,_mv.f);
glGetFloatv(GL_PROJECTION_MATRIX,_p.f);
const matrix_t inv = (_p*_mv).inverse();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
gluUnProject(x,viewport[3]-y,0,mv,p,viewport,&a,&b,&c);
const vec_t origin(vec_t(a,b,c)*inv);
gluUnProject(x,viewport[3]-y,1,mv,p,viewport,&d,&e,&f);
const vec_t dest(vec_t(d,e,f)*inv);
ray_t ray(origin,dest-origin);
不幸的是我誤解了如何將光線轉換成從相機座標到世界座標 - 我的射線失誤。
如何正確地將光線轉換爲世界座標?