0

I've managed to successfully use OpenGL's stencil buffer for a single instance in a scene. However, I'm unsure of how to use it in two different places in the same scene. Defining two stencil shapes in sequence prevents either from working, and my attempts to nest one use inside of the other didn't work either. I've seen examples of multiple uses of stencil buffer in the same scene, but I was not able to understand or adapt the code. Here is what I've been able to get working so far.

void display(void) {
    // store floor shape in stencil buffer
    glClearStencil(0);
    glClear(GL_STENCIL_BUFFER_BIT);
    glEnable(GL_STENCIL_TEST);
    glStencilMask(1);
    glStencilFunc(GL_ALWAYS, 1, 1);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    floor->draw();
            glBegin(GL_QUADS);                   //
                glVertex3f(0.0, 0.0, 0.0);       //
                glVertex3f(0.0, 100.0, 0.0);     //
                glVertex3f(0.0, 100.0, 100.0);   //
                glVertex3f(0.0, 0.0, 100.0);     //
            glEnd();                             //

    // draw scene outside floor
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    scene();

    // draw reflection of scene in floor
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    glPushMatrix();
    glScalef(1.0, 1.0, -1.0);
    scene();
    glPopMatrix();
    glDisable(GL_STENCIL_TEST);

    // draw translucent floor
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(1.0, 1.0, 1.0, 0.7);
    floor->draw();
    //windowHole();
    glDisable(GL_BLEND);

    glFlush();
    glutSwapBuffers();
    glutPostRedisplay();
}

screenshot of program working

user1986358
  • 81
  • 2
  • 9
  • 1
    What is the code that doesn't work? – yiding Jan 24 '13 at 05:12
  • Linking to some screenshots would be very helpful as well. – Tim Jan 24 '13 at 07:17
  • Sorry, I should have been more clear. The five lines of code I have marked show one attempt to have multiple areas selected by the stencil buffer. This doesn't work, but I'm looking for a way that does. The screenshot is of the program working, those extra lines not added. The scene is reflected in the ground because floor->draw() defines that polygon for the stencil buffer, but I need to be able to maintain this effect while making another reflective surface out of some other polygon, such as the one defined in the new lines. – user1986358 Jan 24 '13 at 09:15
  • 1
    is the polygon you defined in the same plane as the floor? If not, you have to add another render pass with different transformation matrices. – Nico Schertler Jan 24 '13 at 10:39

0 Answers0