|
最近做了三维拾取的程序,比如对一个立方体做面的拾取,是否用深度值来挑选,也就是对点击记录
select buffer 比较窗口坐标,取具有最小窗口坐标的名称的面作为拾取的面? 请指教,我自己感觉不太多,
一般的做法是怎么样的? 谢谢!
我主要思路如下:
void pickFace(int button, int state, int x, int y) //鼠标点击函数
{
glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(512,selBuffer);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)x,(GLdouble)(viewport[3] - y),5.0,5.0,viewport);
glOrtho(-3.0,3.0, -3.0,3.0, -3.0,3.0);
glMatrixMode (GL_MODELVIEW);
Cube(GL_SELECT); //画立方体
glMatrixMode(GL_PROJECTION);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
processPick(hits,selBuffer); //处理点击记录
glutSwapBuffers();
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void processPick(GLint hits,GLuint selBuffer[])
{
......
std::vector<float> depth;
float temp;
for (int i=0;i<hits;++i)
{
printf(" number of names for hit = %d\n", *ptr);
ptr++;
temp = (float) *ptr/0x7fffffff; //最小窗口坐标
depth.push_back(temp);
ptr++;
temp = (float) *ptr/0x7fffffff; //最大窗口坐标
depth.push_back(temp);
ptr++;
ptr++;
}
.......... //找出depth中最小的窗口坐标,对应的名称即为拾取面的名称
}
void Cube(GLenum mode)
{
//Front face
if (mode == GL_SELECT)
{
glLoadName(1);
}
else
{
//画立方体的面
}
//Back face
.....
.....
}
|
|