Question

I am trying to reflect a bunch of trees from a forest into a nearby lake.I followed all the standard steps to draw a reflection using the Stencil Buffer.But the "water" doesn't seem to allow the objects below to be seen.This is my function: draw_sea() - draws the plan on which to reflect and drawFigTree(fig_tree) - draws the trees below. Where's the problem?

void draw_mirror() 
{
    glEnable(GL_STENCIL_TEST);
    //glColorMask(0, 0, 0, 0);
    glDisable(GL_DEPTH_TEST);
    glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    glDisable(GL_LIGHTING);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    draw_sea();
    glDisable(GL_BLEND);
    glEnable(GL_LIGHTING);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    glPushMatrix();
    glTranslatef(0.0, SEA_LEVEL, 0.0);
    glEnable(GL_CLIP_PLANE0);
    glClipPlane(GL_CLIP_PLANE0, clip_plane0 );
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-25.0, 2*(-1), 0.0);
    glScalef(1.0, -1.0, 1.0);

    drawFigTree(fig_tree);
    glPopMatrix();
    glDisable(GL_CLIP_PLANE0);
    glDisable(GL_STENCIL_TEST);
}
Was it helpful?

Solution

Blending doesn't work together with depth testing. For blending to work you must sort visible surfaces far-to-near. In case of the water you must draw the reflection first (it's like looking "through" the water surface at the reflected scene) and then the water.

OTHER TIPS

    glDisable(GL_DEPTH_TEST);

You are correct in turning off depth tests. But you must also turn off depth writes; otherwise, you'll have polluted the depth buffer when it comes time to render the reflected geometry.

glDepthMask(GL_FALSE);

Note: Don't forget to turn the depth writes back on after rendering the lake.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top