opengl 鼠标拾取
代码
void GLWidget::HitGet(QPoint pos)
{
GLuint NameBuffer[27];
GLint viewport[4];
GLint hits;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer(27,NameBuffer);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)pos.x(),(GLdouble)(viewport[3]-pos.y()),5,5,viewport);
glOrtho(0, 600, 0, 600, 4.0, 15.0);
qDebug()<<"viewport"<<viewport[0]<<" "<<pos<<(GLdouble)pos.x()<<viewport[3]-(GLdouble)(pos.y());
glMatrixMode(GL_MODELVIEW);
for(int i=0;i<10;i++){
glLoadName(i);
Data::nodes[i].Draw();
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
hits=glRenderMode(GL_RENDER);
qDebug()<<hits;
if(hits>0)
{
qDebug()<<NameBuffer[0]<<NameBuffer[1]<<NameBuffer[3];
}
}
代码
void GLWidget::MousePick(QPoint pos)
{
#ifndef SIZE
#define SIZE 30
GLuint NameBuffer[SIZE]; //名称缓存
GLint viewport[4]; //视点
GLint hits;
//设置选择模式参数
glSelectBuffer(SIZE,NameBuffer);
glGetIntegerv (GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glRenderMode(GL_SELECT);
//保存矩阵
glPushMatrix();
glLoadIdentity();
//拾取对象
gluPickMatrix((GLdouble)pos.x(),(GLdouble)(viewport[3]-pos.y()),5,5,viewport);
glOrtho(0, 600, 0, 600, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
glInitNames();
glPushName(0);
Paint(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
hits=glRenderMode(GL_RENDER);
qDebug()<<hits;
if(hits>0)
{
qDebug()<<NameBuffer[0]<<NameBuffer[1]<<NameBuffer[2]<<NameBuffer[3]<<NameBuffer[4]<<NameBuffer[5];
}
#endif
}