Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Question Tut 27(stencil shadows)

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

Hi all, i have a question, in that tut there is a function "cast shadow" :
void castShadow( ShadowedObject& object, GLfloat *lightPosition )
	// Determine Which Faces Are Visible By The Light.

	for ( int i = 0; i < object.nFaces; i++ )
		const Plane& plane = object.pFaces[i].planeEquation;

		GLfloat side = plane.a*lightPosition[0]+

		if ( side > 0 )
			object.pFaces[i].visible = true;
			object.pFaces[i].visible = false;

	glDisable( GL_LIGHTING );					// Turn Off Lighting

	glDepthMask( GL_FALSE );					// Turn Off Writing To The Depth-Buffer

	glDepthFunc( GL_LEQUAL );
	glEnable( GL_STENCIL_TEST );					// Turn On Stencil Buffer Testing

	glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );		// Don''t Draw Into The Colour Buffer

	glStencilFunc( GL_ALWAYS, 1, 0xFFFFFFFFL );
// First Pass. Increase Stencil Value In The Shadow

	glFrontFace( GL_CCW );
	glStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
	doShadowPass( object, lightPosition );
	// Second Pass. Decrease Stencil Value In The Shadow

	glFrontFace( GL_CW );
	glStencilOp( GL_KEEP, GL_KEEP, GL_DECR );
	doShadowPass( object, lightPosition );
glFrontFace( GL_CCW );
	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );	// Enable Rendering To Colour Buffer For All Components

	// Draw A Shadowing Rectangle Covering The Entire Screen

	glColor4f( 0.0f, 0.0f, 0.0f, 0.4f );
	glEnable( GL_BLEND );
	glStencilFunc( GL_NOTEQUAL, 0, 0xFFFFFFFFL );
	glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
		glVertex3f(-0.1f, 0.1f,-0.10f);
		glVertex3f( 0.1f, 0.1f,-0.10f);
		glVertex3f( 0.1f,-0.1f,-0.10f);
my question is: in this way you have to do all passes for ALL objects present in the scene, is it possible to create a single stencil map? i mean for example project all shadows and then do the last pass? in the tutorial this is not done for making simple the explanation or because it wouldn''t work?
There aren''''t problems that can''''t be solved with a gun...

Share this post

Link to post
Share on other sites
It does work (am doing so). So I guess he''s doing it for the sake of simplicity.

You could possibly run into problems if the number of objects with overlapping shadows is too great. For each shadow the stencil buffer gets increased (Don''t ask me why NeHe is doing it in reverse). If you have one shadow stencil buffer will be zero, except for where that shadow is, there it''ll be one. If a second shadow is cast onto the same position, stencil will be two where the shadows overlap.
If you have 8 bit stencil, you can count up to 255, if you have one more shadows overlapping then that, the last one will cause an overflow (stencil back to zero) and there''ll be a gap in your shadow. But, how likely is that to happen?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!