Search code examples
openglglutpicking

OpenGL Glut Picking not processing hits


I can't figure out why I keep getting 0 hits when I click on anything. I had already gotten the main robot working and have it responding to keyboard commands great, but I can't seem to get it to register a hit for some reason.
Been trying to follow this tutorial: Lighthouse Tutorial
Full Code Here: My Git Repo

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();
}

Solution

  • Looks like you are not setting your view matrix (glortho,gluperspective) or rendering your robot to pick. Take a look at the following which works. I am sure it will help, as much as it is part of a class.

    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;
            }
        }
    }