opengl 鼠标拾取

 

 

ContractedBlock.gifExpandedBlockStart.gif代码
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];
}
}

 

 

 

ContractedBlock.gifExpandedBlockStart.gif代码

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
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值