OpenGL Normal Mapping

Handling discontinuity vs continuity

You are thinking about this the wrong way.

Depending on the use case your normal map may be continous or discontinous. For example in your cube, imagine if each face had a different surface type, then the normals would be different depending on which face you are currently in.

Which normal you use is determined by the texture itself and not by any blending in the fragment.

The actual algorithm is

  • Load rgb values of normal
  • Convert to -1 to 1 range
  • Rotate by the model matrix
  • Use new value in shading calculations

If you want continous normals, then you need to make sure that the charts in the texture space that you use obey that the limits of the texture coordinates agree.

Mathematically that means that if U and V are regions of R^2 that map to the normal field N of your Shape then if the function of the mapping is f it should be that:

If lim S(x_1, x_2) = lim S(y_1, y_2) where {x1,x2} \subset U and {y_1, y_2} \subset V then lim f(x_1, x_2) = lim f(y_1, y_2).

In plain English, if the cooridnates in your chart map to positions that are close in the shape, then the normals they map to should also be close in the normal space.

TL;DR do not belnd in the fragment. This is something that should be done by the normal map itself when its baked, not’by you when rendering.

Handling the tangent space

You have 2 options. Option n1, you pass the tangent T and the normal N to the shader. In which case the binormal B is T X N and the basis {T, N, B} gives you the true space where normals need to be expressed.

Assume that in tangent space, x is side, y is forward z is up. Your transformed normal becomes (xB, yT, zN).

If you do not pass the tangent, you must first create a random vector that is orthogonal to the normal, then use this as the tangent.

(Note N is the model normal, where (x,y,z) is the normal map normal)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top