0
\$\begingroup\$

Vertex shader:

#version 450

layout(push_constant) uniform MVPConst{
    mat4 m;
    mat4 n;
} mvp;

layout(binding = 0) uniform UBO{
    mat4  vM;
    mat4  cM;
    mat4  pM;
    vec4  lP;
    float time;
    uvec2 res;
    float varA;
} ubo;

layout(location = 0) in vec3 vert_pos;
layout(location = 1) in vec2 vert_uvco;
layout(location = 2) in vec3 vert_norm;
layout(location = 3) in vec4 vert_tangent;
layout(location = 4) in vec3 vert_bitangent;

layout(location = 0) out vec2 frag_uvco;
layout(location = 1) out vec3 frag_wpos;
layout(location = 2) out vec3 frag_light_dir;
layout(location = 3) out mat3 frag_TBN;

void main(){
    vec4     wP = mvp.m * vec4(vert_pos,1.0f);
    mat4    mat = ubo.pM * ubo.vM;
    gl_Position = mat * wP;

    frag_uvco = vert_uvco;
    frag_wpos = wP.xyz;

    frag_light_dir = ubo.lP.xyz - wP.xyz;

    vec3 T = normalize(vec3(mvp.n * vert_tangent));
    vec3 N = normalize(vec3(mvp.n * vec4(vert_norm,1.)));
    vec3 B = normalize(cross(N,T) * vert_tangent.w);

    frag_TBN = mat3(T,B,N);
}

Fragment shader:

#version 450

#define PI 3.14159265359

layout(binding = 0) uniform UBO{
    mat4  vM;
    mat4  cM;
    mat4  pM;
    vec4  lP;
    float time;
    uvec2 res;
    float varA;
} ubo;

layout(binding = 1) uniform sampler2DArray tex_samplr;

layout(location = 0) in vec2 frag_uvco;
layout(location = 1) in vec3 frag_wpos;
layout(location = 2) in vec3 frag_light_dir;
layout(location = 3) in mat3 frag_TBN;

layout(location = 0) out vec4 out_frag;

uvec2 res  = ubo.res;
float time = ubo.time;

vec3 specular(){
    vec3  diffuse_light = vec3(1.);
    vec3 specular_light = vec3(.0);
    vec3      tex_color = texture(tex_samplr,vec3(frag_uvco * 10,0)).rgb;
    vec3       tex_norm = texture(tex_samplr,vec3(frag_uvco * 10,1)).rgb;
               tex_norm = tex_norm * 2.0 - 1.0;
               tex_norm = normalize(tex_norm * frag_TBN);

    vec3 surf_norm;

    if(ubo.varA > 1.)
    surf_norm = normalize(tex_norm);
    else
    surf_norm = normalize(frag_TBN[2]);

    return surf_norm;
}

void main(){
    out_frag.rgb = specular();
}

Vertex Normal

Normal Map * TBN

My up vector is Z (Blue)

How to fix it?

\$\endgroup\$
1
  • \$\begingroup\$ Is mvp.n the normal matrix of the Model-View-Project matrix? I see you use that to calculate TBN. This means that your normals are transformed from Tangent Space to Projection Space. Usually lighting calculations are done in View Space or World Space, could that be what's tripping you up? \$\endgroup\$ Commented Jan 2 at 16:30

0

You must log in to answer this question.

Browse other questions tagged .