[osg-users] TBN Matrix for Normal Mapping - OSG and GLSL

Rômulo Cerqueira romulogcerqueira at gmail.com
Tue Nov 7 12:48:57 PST 2017


I have used normal mapping using GLSL and OSG for my application (an imaging sonar simulation) and I got problems by calculating the TBN matrix on shaders. The normal vectors contain lower resolution on border in comparison with the center of image.

Follows my vertex code:


#version 130

out vec3 pos;
out vec3 normal;
out mat3 TBN;

void main() {
    pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
    normal = gl_NormalMatrix * gl_Normal.xyz;

    vec3 n = normalize(normal);             
    vec3 t = normalize(cross(normal, vec3(-1,0,0)));
    vec3 b = cross(t, n) + cross(n, t);     
    TBN = transpose(mat3(t,b,n));

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;

Part of my fragment code:


#version 130

in vec3 pos;
in vec3 normal;
in mat3 TBN;
uniform sampler2D normalTexture;

void main() {
    vec3 newNormal = (texture2D(normalTexture, gl_TexCoord[0].st).rgb * 2.0 - 1) * TBN;
    newNormal = normalize(newNormal);

The resulting figure is attached.

How can I calculate the TBN matrix to compute the normal mapping properly?

Thanks in advance,


Thank you!


Read this topic online here:


More information about the osg-users mailing list