View Full Version : shading setup
05-12-2007, 09:24 AM
i have been using one single lighting shader for all geometry. however, for some meshes one might not want a specific feature to be applied, e.g. no normal mapping, no specular mapping etc. how should i deal with this? i can think of 3 solutions:
1) i stick with one shader and use if-statements. that's slow, but works.
2) i use #define's and compile several shaders. i then switch to the appropiate shader for every mesh, but have to re-set all the uniforms for every mesh.
3) i stick with one shader and create dummy textures which i then use instead of real ones, if those are not present (e.g. a plain blue 2x2 texture if no normal map is provided)
what would you suggest? thanks!
05-12-2007, 10:30 AM
If you can think of n solutions then you actually have 2^n-1 solutions - always consider using a hybrid :)
In your list my choice would be a hybrid of #2 and #3, but I would add one more solution to this list:
4) Create multiple small shader objects. Each shader represents one sngle effect. Link in different combinations.
This is similar to #2 but I like the idea more than putting lots of #ifdef in the code.
So main shader would look like this:
vec3 normal = GetNormal(); /* constant or from texture */
vec2 texCoord = GetTexCoord(); /* no offset, parallax or displacement */
vec4 diffuse = GetDiffuse(normal, texCoord); /* constant, texture, lightmap, subsurface scatering */
vec4 specular = GetSpecular(normal, texCoord); /* none, constant, masked, environment reflection */
vec4 color = MixDiffuseAndSpecular(diffuse, specular, normnal, texCoord); /* add, blend, fresnel term */
gl_FragColor = color;
}Now you only have to link it with different implementations of used functions.
Of course you can also use different set of main funcitons or both approaches at the same time.
05-13-2007, 05:12 AM
hey that sounds like a great idea. i'll do that, thanks!
05-17-2007, 12:04 PM
mh i've decided against your solution in the end - having to re-set all the uniforms for every shader switch turned out to be more expensive than simply using one shader and providing dummy textures.
05-23-2007, 07:04 AM
One offtopic question:
When you have defines for example:
code for param 1
#endifHow do you set the PARAM_1 to be defined? The only method I can think of is to add the #define PARAM_1 line at the top of the shader but this looks bad.
Sorry for the off and for the English,
05-23-2007, 09:09 AM
You can add that in glShaderSource call. You can pass several shader source strings.
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.