Sign in to follow this  
famous

Shader usage

Recommended Posts

hi, this may sound like a noob question but I'm not quite sure how to use shaders in practice. Somehow all tutorials I've read about shaders kinda suggested that there will only be one big shader responsible for drawing the entire scene but can't you set different shaders for different objects? That said, what if we wanna draw into a texture using a shader? For simplicity lets assume we have a directional light with it's according shadow map and we wanna sample the shadow map per pixel and just copy it into a new texture. How do we get access to the shadow map if we only apply the shader to our texture?

Share this post


Link to post
Share on other sites
You're free to do whatever you want with shaders. You could have one big "uber" shader that does everything, or you could split it up by material, e.g. wood, metal, etc, and post processing effect, e.g. blur, etc.

Shaders are generally split up by material rather than by object. That way, if you have two different objects that have the same look, you can just use the same shader.

It also depends on how you'd like your effect to look - some "effects" require different components, like smooth wood may only require a specular map, whereas gnarly bark-like wood may require a normal and/or bump map. In this case, you could set up a general 'wood' shader and include code for specular mapping or normal/bump mapping. So when you draw your gnarly bark, you set the wood shader and switch on/pass in normal/bump mapping, and when you draw smooth wood, you set the wood shader and switch on/pass in specular mapping. It's not even really necessary to break this down as much - metal/concrete/sand/carpet, etc are essentially drawn in the same way, only perhaps the bump/specular/textures maps are different.

You can draw to a texture using a shader by creating a texture as a renderTarget and using something like SetRenderTarget (DirectX, but probably similar for OpenGL) before you draw your scene/objects. Once you've drawn to this texture, you can then use it (sample it) in subsequent draw calls by calling something like SetTexture on the next shader passing in that texture. Depending on the hardware you're using (the last time I looked), you can set up to 16 textures per pass (may be more now).

For your shadow mapping, you'd setup a texture for the shadow map as a render target, then draw your shadow map to this texture, then pass this texture in to your shader(s) when you draw your final scene. This is also similar to how post-processing works. You usually draw your final scene to a render target (instead of the back buffer) and then pass that texture into a post-process shader which is used to draw a simple screen-sized quad. Once the post-process filter chain is complete, the screen-aligned quad is drawn to the back buffer.

Hope that helps.

Share this post


Link to post
Share on other sites
If you use more than one pass in your renderer (i.e. to generate shadow map, reflection map, post processing, etc...) then you are already using more than one shader. Honestly I can't think of any interesting situation where you could only use a single shader for an entire frame...

Also, keep in mind that there are several programmable pipeline stages (D3D11 has six!) so the term 'shaders' may not be what you are really intending - rather you might mean the entire pipeline configuration (more along the lines of a *.fx file or something).

Share this post


Link to post
Share on other sites
Thanks, this helps me a lot.

So what I wanna do is to sample an existing shadow map and approximate a new one with the use of parallax occlusion mapping.
The vertex shader in the std POM algorithm just calculates and normalizes the eye vector (I don't need that because I use a parallel projection so the eye vector is the same for the entire map, I can set it in in the application) and the vertex normal. But is there a texture normal? The POM algorithm needs one so how do I set it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this