From 344bb25a8314811f6591111f10777635feb6fd36 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 8 May 2021 18:09:31 +0300 Subject: [PATCH] Add inverse view and projection matrices to camera transform uniforms --- builtin_data/_sky_backdrop.glsl | 5 ++--- shaderlib/msp_interface.glsl | 4 +++- source/render/camera.cpp | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/builtin_data/_sky_backdrop.glsl b/builtin_data/_sky_backdrop.glsl index 420f0ed5..fa28663c 100644 --- a/builtin_data/_sky_backdrop.glsl +++ b/builtin_data/_sky_backdrop.glsl @@ -7,10 +7,9 @@ uniform sampler2D distant; void main() { gl_Position = vec4(vertex.xy, 1.0, 1.0); - mat4 inv_projection = inverse(clip_eye_matrix); - out vec4 view_dir = inv_projection*vec4(vertex.xy, -1.0, 1.0); + out vec4 view_dir = eye_clip_matrix*vec4(vertex.xy, -1.0, 1.0); view_dir /= view_dir.w; - view_dir = inverse(eye_world_matrix)*vec4(view_dir.xyz, 0.0); + view_dir = world_eye_matrix*vec4(view_dir.xyz, 0.0); } #pragma MSP stage(fragment) diff --git a/shaderlib/msp_interface.glsl b/shaderlib/msp_interface.glsl index e35b9848..959f9ba5 100644 --- a/shaderlib/msp_interface.glsl +++ b/shaderlib/msp_interface.glsl @@ -11,10 +11,12 @@ struct ClipPlane uniform mat4 world_obj_matrix; uniform mat3 world_obj_normal_matrix; -uniform Transform +uniform CameraTransform { mat4 eye_world_matrix; + mat4 world_eye_matrix; mat4 clip_eye_matrix; + mat4 eye_clip_matrix; }; uniform Lighting diff --git a/source/render/camera.cpp b/source/render/camera.cpp index 9b8b0c77..8b966464 100644 --- a/source/render/camera.cpp +++ b/source/render/camera.cpp @@ -133,6 +133,7 @@ void Camera::update_projection_matrix() proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix; shdata.uniform("clip_eye_matrix", proj_matrix); + shdata.uniform("eye_clip_matrix", invert(proj_matrix)); } void Camera::update_object_matrix() @@ -146,6 +147,7 @@ void Camera::update_object_matrix() matrix = Matrix::from_columns(columns); view_matrix = invert(matrix); + shdata.uniform("world_eye_matrix", matrix); shdata.uniform("eye_world_matrix", view_matrix); } -- 2.45.2