Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shadowing and shaders

Started by StockwellLadSteve Mar 14, 2007 at 6:52 PM 4 replies 1.4k views
Original Post
StockwellLadSteve
StockwellLadSteve
Hey guys, I have a question regarding shadowing and shaders. I have an application which renders streetlamps and projects a spotlight into the scene onto my flat plane ground and onto the sides of houses. To do this I first of all filter out which polygons are visible to each streetlamp in its spotlights' immediate area of influencem then I pass those polygons to my cg shader which does the spotlight calculations. Now, because i dont yet have shadowing the ground is also lit underneath the house which of course shouldn't be happening. I want to implement shadows and have had some problem with shadow mapping already, and also because I already have sorted out the polygons visible to the light, I thought I should try shadow volumes as I can simply extrude out past the polygons visible to the light. However one part of the problem is confusing me and I want to clarify it before i start coding it. If i set up the shadow volume, how will my cg shader be able to perform the relevant stencil tests? i'm sorry if the question is a little vague but i'm really confused on how I'll do the actual testing against the stencil buffer in my shader. Can anyone assist me please? Thanks
Kalidor
Kalidor
Quote:
Original post by StockwellLadSteve
If i set up the shadow volume, how will my cg shader be able to perform the relevant stencil tests? i'm sorry if the question is a little vague but i'm really confused on how I'll do the actual testing against the stencil buffer in my shader.
Stencil testing is separate from shaders so you don't perform the stencil test in your shader; it's done the same as it would be if you weren't using shaders.
StockwellLadSteve
StockwellLadSteve
Hi thanks for the reply. I really appreciate it.

So just to clarify

1) Render the shadow volumes
2) Do my lighting through the shader
3) The stencil test is then performed and the correct fragments will appear in shadow?


If you can confirm it'd make my day

Thanks



Steve
stanlo
stanlo
That's about right, though if a pixel fails a stencil test it won't be going through a pixel shader (because why bother?).
StockwellLadSteve
StockwellLadSteve
Some further help please guys if possible.

I've got my algorithm working to correctly render my shadow volume. At present its not extruded to infinity - just by about two times the length from the light to the object.

The shadow volume is correctly rendered. I have checked this by placing my viewpoint inside the volume with backface culling enabled and the volume is of course invisible. It is capped on both ends of the volume.

Yet despite this and using (and even in desperation copying directly a couple of) zfail/pass rendering algorithms, I'm still not getting anything in shadow.

I am using cg shaders to perform my lighting.

I am first of all turning the shader off and rendering the scene with ambient light only, then rendering the shadow volumes, then turning the shaders back on to render the scene with lighting enabled. Areas which were inside my shadow volume are still remaining lit and i can't figure out why. Are there some common pitfalls I should be looking for.

For those interested here is the code from the render method.


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);	glPushAttrib(GL_ENABLE_BIT);	cgOff = true;	render();	glPopAttrib();		glPushAttrib(GL_ALL_ATTRIB_BITS);	glColorMask(0, 0, 0, 0);	glShadeModel(GL_FLAT);	glDepthMask(0);	glDepthFunc(GL_LESS);	glEnable(GL_STENCIL_TEST);    glStencilFunc( GL_ALWAYS, 0, ~0 );	glStencilOp( GL_KEEP, GL_INCR, GL_KEEP );    glCullFace( GL_FRONT );	scene.RenderShadowVolumes();	glStencilOp( GL_KEEP, GL_DECR, GL_KEEP );    glCullFace( GL_BACK );	scene.RenderShadowVolumes();	glPopAttrib();	glPushAttrib(GL_ENABLE_BIT);	glEnable(GL_COLOR_MATERIAL);	glStencilFunc(GL_EQUAL, 0, ~0);	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);	glEnable(GL_STENCIL_TEST);	cgOff = false;	render();	glPopAttrib();//the render methodvoid render(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);	glLoadIdentity();													glTranslatef(0.0f, 0.0f, -1.0f);		totalfps +=fps;	totalcountfps++;		glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glMultMatrixf(&camera.projMatrix[0][0]);	    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    glMultMatrixf(&camera.viewMatrix[0][0]);			skybox.Draw(0,0,0,2500, 2500, 2500);	scene.cgOff = cgOff;		glViewport(0,0, WIDTH, HEIGHT);	scene.DrawScene();}
stanlo
stanlo
You're clearing your stencil buffer inside your render function. Are you sure you want to do that? Pretty much everything before your final render call will have no effect.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.