用glRenderMode挑选

时间:2015-02-03 10:46:48

标签: c++ opengl qglwidget picking

我尝试进行OpenGL选择,所以我按照这篇帖子(OpenGL Picking on Selection Mode)进行了操作,这似乎有一个可行的解决方案,但我不能让我的工作。我的场景中有一个3D纹理多边形,我想知道何时点击它。

这是我的代码:

右键单击时启动拾取的鼠标方法:

void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button()==Qt::RightButton){
        pickObjects(event->pos().x(),event->pos().y());
    }
}

pickObjects方法:

#define BUFSIZE 512
void pickObjects(int x, int y)
{
    GLint viewport[4];
    GLint hits;

    GLuint selectBuf[BUFSIZE];
    glSelectBuffer (BUFSIZE, selectBuf);

    glRenderMode (GL_SELECT);

    glMatrixMode (GL_PROJECTION);
    glPushMatrix ();
    glLoadIdentity ();

    glGetIntegerv (GL_VIEWPORT, viewport);

    gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport);
    gluPerspective(fov, this->width() / this->height(), 0.1, 1000);

    glMatrixMode(GL_MODELVIEW);

    glInitNames();

    makeCustomAnnot(GL_SELECT);

    int hits=0;

    glMatrixMode (GL_PROJECTION);
    glPopMatrix ();

    glMatrixMode(GL_MODELVIEW);

    hits = glRenderMode (GL_RENDER);
    if (hits != 0)
    {
        cout<<"FOUND " << hits << " hit(s)"<<endl; //ALWAYS GIVES 0 HITS
        processHits(hits,selectBuf);
    }

}

processHits方法:

void processHits(GLint inHits, GLuint buffer[])
{
    unsigned int i, j;
    GLuint names, *ptr, minZ,*ptrNames, numberOfNames;

    ptr = (GLuint *) buffer;
    minZ = 0xffffffff;
    for (i = 0; i < inHits; i++) {
        names = *ptr;
        ptr++;
        if (*ptr < minZ) {
            numberOfNames = names;
            minZ = *ptr;
            ptrNames = ptr+2;
        }
        ptr += names+2;
    }

    cout << "Nearest: ";
    ptr = ptrNames;
    for (j = 0; j < numberOfNames; j++,ptr++) {
        cout<< *ptr ;
    }
}

绘图obejct方法:

void makeCustomAnnot(GLenum mode){

    glEnable(GL_TEXTURE_2D);
    glColor3f(1,1,1);
    GLuint j=0;

    QImage img("img.jpg");
    img=QGLWidget::convertToGLFormat(img);

    GLuint texturesAnnot[1];
    glBindTexture( GL_TEXTURE_2D, texturesAnnot[0] );
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

    if(mode == GL_SELECT){
        glPushName (j);
    }

    glBegin(GL_QUADS);
        glTexCoord2d(0,0);glVertex3d(x4,y4,z4);
        glTexCoord2d(1,0);glVertex3d(x3,y3,z3);
        glTexCoord2d(1,1);glVertex3d(x2,y2,z2);
    glTexCoord2d(0,1);glVertex3d(x1,y1,z1);
    glEnd();

    if(mode == GL_SELECT)
        glPopName ();
    }

    glDisable(GL_TEXTURE_2D);
}

渲染方法:

void render()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0,0,this->width() , this->height());

    gluPerspective(fov, this->width() / this->height(), 0.1, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRotatef(xRot / 5.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot / 5.0f, 0.0f, 1.0f, 0.0f);
    glRotatef(zRot / 5.0f, 0.0f, 0.0f, 1.0f);

    glPushMatrix();
        makeCustomAnnot(GL_RENDER);
    glPopMatrix();

    glDisable(GL_DEPTH_TEST);
}

所以hits = glRenderMode(GL_RENDER);总是返回0次点击。我做错了什么?

1 个答案:

答案 0 :(得分:1)

所以这就是我所做的让它完美运作。

processHits方法:

void processHits(GLint inHits, GLuint buffer[])
{
    unsigned int i, j;
    GLuint names, *ptr, minZ,*ptrNames, numberOfNames;

    ptr = (GLuint *) buffer;
    minZ = 0xffffffff;
    for (i = 0; i < inHits; i++) {
        names = *ptr;
        ptr++;
        if (*ptr < minZ) {
            numberOfNames = names;
            minZ = *ptr;
            ptrNames = ptr+2;
        }
        ptr += names+2;
    }

    cout << "Nearest: ";
    ptr = ptrNames;
    for (j = 0; j < numberOfNames; j++,ptr++) {
        cout<< *ptr ;
    }
}

绘图对象方法创建了我需要选择的对象:

void makeCustomAnnot(GLenum mode){

    glEnable(GL_TEXTURE_2D);
    glColor3f(1,1,1);
    GLuint j=0;

    QImage img("img.jpg");
    img=QGLWidget::convertToGLFormat(img);

    GLuint texturesAnnot[1];
    glBindTexture( GL_TEXTURE_2D, texturesAnnot[0] );
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

    if(mode == GL_SELECT)
        glLoadName (j);

    glBegin(GL_QUADS);
        glTexCoord2d(0,0);glVertex3d(x4,y4,z4);
        glTexCoord2d(1,0);glVertex3d(x3,y3,z3);
        glTexCoord2d(1,1);glVertex3d(x2,y2,z2);
    glTexCoord2d(0,1);glVertex3d(x1,y1,z1);
    glEnd();

    glDisable(GL_TEXTURE_2D);
}

pickObjects方法,与渲染功能相同,但有选择说明:

#define BUFSIZE 512
unsigned int selectBuf[BUFSIZE];

void pickObjects(int x, int y)
{
    GLint viewport[4];
    GLint hits;

    glSelectBuffer (BUFSIZE, selectBuf);
    glRenderMode (GL_SELECT);

    glMatrixMode (GL_PROJECTION);
    glPushMatrix ();
    glLoadIdentity ();

    glGetIntegerv (GL_VIEWPORT, viewport);

    gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport);
    gluPerspective(fov, this->width() / this->height(), 0.1, 1000);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glRotatef(xRot / 5.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot / 5.0f, 0.0f, 1.0f, 0.0f);
    glRotatef(zRot / 5.0f, 0.0f, 0.0f, 1.0f);

    glRotatef((float)angleV, 1.0f, 0.0f, 0.0f);

    glInitNames();
    glPushName( 10000 );

    glPushMatrix();
        makeCustomAnnot(GL_SELECT);//draw scene in GL_SELECT mode to create the names
    glPopMatrix();

    hits = glRenderMode (GL_RENDER);//get the hits in GL_RENDER mode
    if (hits != 0)
    {
        processHits(hits,selectBuf);
    }

    glPopMatrix();
    glDisable(GL_DEPTH_TEST);
}

渲染方法:

void render()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0,0,this->width() , this->height());

    gluPerspective(fov, this->width() / this->height(), 0.1, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRotatef(xRot / 5.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot / 5.0f, 0.0f, 1.0f, 0.0f);
    glRotatef(zRot / 5.0f, 0.0f, 0.0f, 1.0f);

    glRenderMode (GL_RENDER);

    glPushMatrix();
        makeCustomAnnot(GL_RENDER);
    glPopMatrix();

    glDisable(GL_DEPTH_TEST);
}

最后命中= glRenderMode(GL_RENDER);在pickObjects方法中,然后processHits方法完全返回拾取的对象。