Jump to content
  • Advertisement
Sign in to follow this  
EngineCoder

Two-sided stencil test problems

This topic is 3211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My z-fail shadow volume rendering works fine without two-sided stencil test, but I can't figure out how to make it work with it. The scene renders as if there are no shadows at all. Here's the code:
void render3d()
{
    int stencil = 128;

    glClearStencil( stencil );
    glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );

    scene->getActiveCamera()->view();

    // Renders the ambient lighting.

    glShadeModel( GL_FLAT );
    GLfloat globalAmbient[] = { 0.6f, 0.6f, 0.6f, 1.0f };
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
    glEnable( GL_CULL_FACE );
    glCullFace( GL_BACK );

    renderAmbient = true;    
    renderOpaque = true;
    renderShadowVolume = false;
    glPushMatrix();
    renderNode( scene->root ); // Opaque objects.
    glPopMatrix();

    // If the sky exists, render it.
    if (scene->skyTextureBottom != 0) {
        renderSky();
    }

    renderOpaque = false;
    glDepthMask( GL_FALSE );
    glPushMatrix();
    renderNode( scene->root ); // Translucent objects.
    glPopMatrix();

    // Renders shadow volumes.
    glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );     
    glDepthMask( GL_FALSE );
    glDepthFunc( GL_LEQUAL );
    glDisable( GL_TEXTURE_2D );
    glDisable( GL_LIGHTING );

    glEnable( GL_POLYGON_OFFSET_FILL );
    glPolygonOffset( 0.0f, 100.0f );

    glDisable( GL_CULL_FACE );

    glEnable( GL_STENCIL_TEST );
    glEnable( GL_STENCIL_TEST_TWO_SIDE_EXT );

    GLHelper::aglActiveStencilFaceEXT( GL_FRONT );
    glStencilFunc( GL_ALWAYS, 0, ~0 );
    glStencilMask( ~0 );
    glStencilOp( GL_KEEP, GL_DECR_WRAP_EXT, GL_KEEP );

    GLHelper::aglActiveStencilFaceEXT( GL_BACK );
    glStencilFunc( GL_ALWAYS, 0, ~0 );
    glStencilMask( ~0 );
    glStencilOp( GL_KEEP, GL_INCR_WRAP_EXT, GL_KEEP );
           
    renderAmbient = false;
    renderOpaque = true;
    renderShadowVolume = true;

    glPushMatrix();
    renderNode( scene->root ); // Opaque objects.
    glPopMatrix();

    renderOpaque = false;    
    glPushMatrix();
    renderNode( scene->root ); // Translucent objects.
    glPopMatrix();

    glDisable( GL_POLYGON_OFFSET_FILL );
    glEnable( GL_CULL_FACE );
    
    // Renders the scene normally.
    float black[4] = { 0, 0, 0, 1 };
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );

    glShadeModel( GL_SMOOTH );
    glDepthFunc( GL_EQUAL );
    glDisable( GL_STENCIL_TEST_TWO_SIDE_EXT );
    glEnable( GL_STENCIL_TEST );
    glStencilFunc( GL_EQUAL, stencil, ~0 );
    glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
    glEnable( GL_BLEND );
    glBlendFunc( GL_ONE, GL_ONE );
    glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); 
    glEnable( GL_TEXTURE_2D );
    glEnable( GL_LIGHTING );

    renderAmbient = false;    
    renderOpaque = true;
    renderShadowVolume = false;
    glPushMatrix();
    renderNode( scene->root ); // Opaque objects.
    glPopMatrix();

    renderOpaque = false;
    glPushMatrix();
    renderNode( scene->root ); // Translucent objects.
    glPopMatrix();

    glDisable( GL_STENCIL_TEST );
    glDisable( GL_BLEND );
    glDepthMask( GL_TRUE );
    glDepthFunc( GL_LESS );
}
[/SOURCE]
Any help, comments or questions would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Check the NVIDIA developer site. Seems they've revamped the older SSV demo to leverage the new core features of GL3 (2-sided stencil, depth clamp, etc.).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!