Jump to content
  • Advertisement
Sign in to follow this  
Amablue

OpenGL OpenGL problem with shadows: I can't seem to disable writing to the color buffer

This topic is 3884 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

I'm trying to get some shadows to be cast off of this cube in a scene. I've got GL_QUADS being extruded off the cube to the ground, but I don't want them being written to the color buffer, just the stencil buffer. I tried google and looking at NeHe's tutorials, but I can't figure out where I'm going wrong. I end up with some GL_QUADS appearing in the scene coming off the cube, when they should be invisible and only used for stcencil buffer calculations.
void drawShadows(Light* light)
{  
    // Most of this stuff is straight from the NeHe tutorial on shadows

    glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT );
	
    // Prepare the GL stuff
	glDisable( GL_LIGHTING ); // No drawing the shadow volume quads
	glDepthMask( GL_FALSE ); // Don't keep track of their depth either
	glDepthFunc( GL_LEQUAL ); // Not sure what this is, but it was in the tutorial
    glEnable( GL_STENCIL_TEST ); // Prepare the stencil buffer
	glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
	glStencilFunc( GL_ALWAYS, 1, 0xFFFFFFFFL );

    // First Pass. Increase Stencil Value In The Shadow
	glCullFace(GL_BACK);
	glStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
	doShadowPass(light);
	// Second Pass. Decrease Stencil Value In The Shadow
    glCullFace(GL_FRONT);
	glStencilOp( GL_KEEP, GL_KEEP, GL_DECR );
	doShadowPass(light);

	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );	// Enable Rendering To Colour Buffer For All Components
	glPopAttrib();
}

void doShadowPass(Light* light)
{
    float vector1[3];
    float vector2[3];
    float normalizedVector1[3];
    float normalizedVector2[3];

    for (int i = 0; i < 4; i++)
    {
        // Code ommited here
        // It's just where I get the edges for the edges[][] array
        
        glBegin(GL_QUADS);
            glVertex3f(edges[0][0], edges[0][1], edges[0][2]);
            glVertex3f(edges[1][0], edges[1][1], edges[1][2]);
            glVertex3f(edges[1][0] + (normalizedVector2[0] * INFINITY), 
                       edges[1][1] + (normalizedVector2[1] * INFINITY), 
                       edges[1][2] + (normalizedVector2[2] * INFINITY));
            glVertex3f(edges[0][0] + (normalizedVector1[0] * INFINITY), 
                       edges[0][1] + (normalizedVector1[1] * INFINITY), 
                       edges[0][2] + (normalizedVector1[2] * INFINITY));
        glEnd();
    }
}
EDIT: Didn't realize there was a forum for OpenGL specifically. Can someone move this? [Edited by - Amablue on April 2, 2008 2:29:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
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!