From 52f49ed98464e80143bd60c113719e019aaa152f Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Sat, 30 Mar 2024 00:47:14 +0200 Subject: [PATCH] Normals in camera space To keep the normal coloring consistent when rotating camera --- mogen/smpl/shaders/mesh.vert | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mogen/smpl/shaders/mesh.vert b/mogen/smpl/shaders/mesh.vert index cacafc8..c8ec92b 100644 --- a/mogen/smpl/shaders/mesh.vert +++ b/mogen/smpl/shaders/mesh.vert @@ -6,9 +6,9 @@ layout(location = NORMAL_LOC) in vec3 normal; layout(location = INST_M_LOC) in mat4 inst_m; // Uniforms -uniform mat4 M; -uniform mat4 V; -uniform mat4 P; +uniform mat4 M; // Model matrix +uniform mat4 V; // View matrix +uniform mat4 P; // Projection matrix // Outputs out vec3 frag_position; @@ -16,9 +16,10 @@ out vec3 frag_normal; void main() { - gl_Position = P * V * M * inst_m * vec4(position, 1); - frag_position = vec3(M * inst_m * vec4(position, 1.0)); + mat4 modelView = V * M * inst_m; // Compute model-view matrix + gl_Position = P * modelView * vec4(position, 1); + frag_position = vec3(modelView * vec4(position, 1.0)); // Position in camera space - mat4 N = transpose(inverse(M * inst_m)); - frag_normal = normalize(vec3(N * vec4(normal, 0.0))); + mat3 normalMatrix = transpose(inverse(mat3(modelView))); // Normal matrix in camera space + frag_normal = normalize(normalMatrix * normal); // Transform normal to camera space } \ No newline at end of file