Advertisement

Question Tut 27(stencil shadows)

Started by August 10, 2003 08:36 AM
0 comments, last by Thor82 21 years, 6 months ago
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]+
			plane.b*lightPosition[1]+
			plane.c*lightPosition[2]+
			plane.d;

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

glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT );
	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 );
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	glStencilFunc( GL_NOTEQUAL, 0, 0xFFFFFFFFL );
	glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
	glPushMatrix();
	glLoadIdentity();
	glBegin( GL_TRIANGLE_STRIP );
		glVertex3f(-0.1f, 0.1f,-0.10f);
		glVertex3f(-0.1f,-0.1f,-0.10f);
		glVertex3f( 0.1f, 0.1f,-0.10f);
		glVertex3f( 0.1f,-0.1f,-0.10f);
	glEnd();
	glPopMatrix();
	glPopAttrib();
}
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...
There aren''t problems that can''t be solved with a gun...
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?
How do I set my laser printer on stun?

This topic is closed to new replies.

Advertisement