Search code examples
openglglslshadernormals

Optional normal mapping in GLSL


I am trying to figure out how to deal with materials that may or may not have a normal map and if not tell the shader to use the vertex normal. The code right now looks like this:

// retrieve the normal from the normal map
gNormal = texture(normalMap, uv);
gNormal = normalize(gNormal * 2.0 - 1.0);
gNormal = vec4(normalize(TBN * gNormal.xyz), 1.0);

// TODO: figure out a way to toggle normal mapping
//gNormal = vec4(normalize(normal), 1.0);

The most common solution is procedurally generating shaders and switching on the fly but that is a complex topic in itself. Are there any other options besides passing in a uniform bool ?


Solution

  • Another option is to always use a normal map. The simplest normal map is a 1x1 texture with 1 normal vector - for instance (0, 0, 1).
    With this solution, you do not need branching in the shader.