   Help: Shadow mapping objects that depend on a shader to render.
  How do I apply a shadow map to objects that rely on a shader to display it initially? For example; a building that is normal mapped, the first pass I can render the building and apply a normal map shader to it. The second pass, I render the building with no normal maps, but apply the shadow map to it to get the correct shadow (I can do this using a shader or a non-shader method). That works because on the second pass, no shaders are required when rendering the building. I can just render the vertices and turn off all bells and whistles.Now for somthing like grass, where the grass polygons depend on the shader for size, rotate to face, etc, I cannot render a second pass and apply a shadow to it. If I use a shader to apply my shadow maps, I cannot apply it to the grass because the grass uses another shader to display it. If I use a non-shader method for shadow maps, I am still not able to get the grass to shade because the grass shader handles the color and will not apply the shadow map.I don't want to add the shadow map code to my grass shader or any other shader that objects depend on. I would rather come up with a second pass to shadow things. How do I do this for objects that depend on a shader to render? Thanks for any suggestions.   
  Well you can't really apply a shadow to something after you've rendered it, since you only want to apply the shadow occlusion factor to the contribution from a single light.  So for instance, if your normal mapping shader sums the contribution from 4 lights you wouldn't want to apply the occlusion to all 4, just one.A better approach would be to render your shadow occlusion to a screen-sized texture ahead of time, and then when you're doing your main pass for objects you can sample the occlusion texture and use it to attenuate the appropriate light source (in fact you can pack in an occlusion factor for 4 light sources by sticking one in each channel).I made a sample in XNA that demonstrates this approach, if you're interested.   ;  Thanks for the example. I'm using GLSL, but I can follow what you are doing.Our engine is a bit antiquated and not using deferred lighting. But for this example with shadow maps, I think it could be implemented. The problem we have is that not every object is passed through a shader (only normal/spec mapped objects). Using the deferred method you mentioned would require that every shadow receiver go through a shader(???). And without deferred lighting, calculating the light for every light source may be a bit expensive.Quote:Original post by MJPWell you can't really apply a shadow to something after you've rendered itI got this from a game tutorial and was able to shadow objects this way (Terrain, buildings, ect., as long as the object was not solely dependent on a shader (like grass));Quote:Original post by Duel93Quote:Original post by MJPWell you can't really apply a shadow to something after you've rendered itI got this from a game tutorial and was able to shadow objects this way (Terrain, buildings, ect., as long as the object was not solely dependent on a shader (like grass))Affecting shadow means the pixel doesn't have specular and diffuse light from the point light, but has only ambient light. You may be subtract the two values ( specular and diffuse value ) at the second pass, after rendering everything at the first pass.[Edited by - Jae Hyuk Kwak on May 28, 2009 3:47:45 PM];  If I understand MJP's example, the first pass is the scene-to-depth texture (where you also create a shadow map and from that a shadow occlusion map), the second pass renders normally and the shadow occlusion map is used to attenuate the light. So all lights in the scene would have to be calculated for the fragment then multiplied against the occlusion shadow map.That implies to me that ALL shadow receivers are sent through a shader (terrain, vehicles, buildings, etc) and one of the input textures is the occlusion map. This cannot be done to an object which is not rendered with a shader (or can it???)Our problem is that not all shadow receivers go through a shader currently (Only objects normal/spec mapped). If I understand correctly, I would have to have to have a shader for all shadow receivers with the occlusion map as a texture input.;  Yes, you would need to use a shader for all receivers since there's no way to multiply a texture lookup with the contribution from a single light source using fixed-function stuff (or at least, not that I know of).You could use your shadow occlusion texture to do *something* with the result of your fixed-function output pass, but you won't be able to obtain "correct" results.  That's because the results of your fixed-function pass will typically contain at least your ambient contribution + the contribution from a single light source, and you would only want to multiply the occlusion factor the the light's contribution.  You could still multiply anyway and probably get fairly good results, depending on your scene.  However if you have more than one light source, things will start too look wrong very quickly.   ;  And to get objects with transparency to shadow? When doing the render-scene-to-depth-texture, I assume that these are rendered to the depth buffer also?   ;  I actually just implemented something similar for my day job.  I had the problem of trying to tie together a few third party plugins to our rendering engine and add shadows on top of them.  I originally tried a shader generator based on rendering state, but could not work it into one of the third party plugins due to them being explicitly fixed function pipeline or non-modifiable shaders, so I ended up going with a multi-pass shadowing technique.First, I render my scene without shadows.  Then I render my shadow map, then I re-render the scene using only the shadow map as input.  My fragment shader for the shadow pass discards any fragment that isn't shadowed (not affecting the existing framebuffer).  In openGL for the shadow pass, I set the depth function to LEQUAL and the blend function to src=DST_COLOR and dst=ZERO, which multiply the incoming fragment by the existing fragment in the frame buffer.  The blend function is what is key here since it dictates what to do with the fragment out of the fragment shader and how to put it into the frame buffer with the existing color (from the first render).This works pretty well when you don't get to control every aspect of your rendering pipeline at the expense of rendering your scene several times, which is really not as bad as it seems since the shadow map has to happen either way and the shadow pass is a very simple shader and discards a lot of fragments as it renders.Cheers, Bob   