OpenGL Glut Picking不处理命中

时间:2014-11-04 05:12:35

标签: opengl glut picking

我无法弄清楚当我点击任何内容时为什么我会持续0次点击。我已经让主要的机器人工作并且对键盘命令做出了很好的响应,但我似乎无法通过某种原因来记录它。一直试图遵循这个教程:{{3 }}
完整代码:Lighthouse Tutorial

int handlePicking(int x, int y)
{
    int hits;

    GLint viewport[4];

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

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

    glGetIntegerv(GL_VIEWPORT, viewport);
    gluPickMatrix(x, viewport[3] - y, 5, 5, viewport);
    glMatrixMode(GL_MODELVIEW);
    glInitNames();

    // restoring the original projection matrix
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glFlush();

    // returning to normal rendering mode
    hits = glRenderMode(GL_RENDER);

    std::cout << hits << std::endl;

    return -1;
}

void robot()
{
    glInitNames();

    glPushName(ROBOT_HEAD);
    robotHead();
    glPopName();

    glPushName(ROBOT_EYES);
    robotLeftEye();
    robotRightEye();
    glPopName();

    glPushName(ROBOT_BODY);
    robotBody();
    glPopName();

    glPushName(ROBOT_ARMS);
    robotLeftArm();
    robotRightArm();
    glPopName();

    glPushName(ROBOT_LEGS);
    robotLeftLeg();
    robotRightLeg();
    glPopName();
}

1 个答案:

答案 0 :(得分:0)

看起来您没有设置视图矩阵(glortho,gluperspective)或渲染您的机器人。看看以下哪个有效。我相信它会有所帮助,就像它是课堂的一部分一样。

GLvoid CGame::Selection(GLvoid)
{
    GLuint  buffer[512];
    GLint   hits;
    GLint   viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glSelectBuffer(512, buffer);
    (GLvoid) glRenderMode(GL_SELECT);
    glInitNames();
    glPushName(0);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y-4), 8.0f, 8.0f, viewport);
    gluPerspective(70.0f,(GLfloat)width/(GLfloat)height,0.001f,100.0f);
    glMatrixMode(GL_MODELVIEW);

    renderTargetAnswers(); //YOUR ROBOT() SHOULD GO HERE
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    hits=glRenderMode(GL_RENDER);
    if (hits>0)
    {
        score+=1;
        int choose = buffer[3];
        int depth = buffer[1];
        for (int loop = 1; loop<hits;loop++)
        {
            if (buffer[loop*4+1] < GLuint(depth))
            {
                choose = buffer[loop*4+3];
                depth = buffer[loop*4+1];
            }       
        }
        if (!targetanswers[choose].hit)
        {
            targetanswers[choose].hit=GL_TRUE;
        }
    }
}