From d0f739c6032bcb58e81ebe61c3fb69fb7a8bc441 Mon Sep 17 00:00:00 2001 From: Roz K Date: Tue, 20 Dec 2022 16:10:09 +0100 Subject: [PATCH] Rework camera. --- game/camera.py | 27 +++++++++++++++++++++++---- game/game.py | 14 ++------------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/game/camera.py b/game/camera.py index d06c3fc..3892855 100644 --- a/game/camera.py +++ b/game/camera.py @@ -13,7 +13,13 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from engine import mat4, mat4_projection, mat4_lookat, resolve_input, set_input_mat4 +from engine import ( + vec3, vec3_mul_vec3, + mat3, mat3_rotation, mat3_mul_vec3, + mat4, mat4_projection, mat4_lookat, + resolve_input, set_input_mat4) + +_m2km = vec3(0.001, 0.001, 0.001) class _Inputs: __slots__ = 'projection', 'view' @@ -23,17 +29,30 @@ class _Inputs: self.view = resolve_input(shader, view) class Camera: - __slots__ = 'projection', 'view' + __slots__ = 'origin', 'lookat', 'rotation', 'projection', 'view' def __init__(self): + self.origin = vec3() + self.lookat = vec3() + self.rotation = mat3() self.projection = mat4() self.view = mat4() def set_projection(self, half_fov, ratio, near_z, far_z): mat4_projection(self.projection, half_fov, ratio, near_z, far_z) - def set_view(self, origin, lookat): - mat4_lookat(self.view, origin, lookat) + def set_view(self, origin, lookat, axis, angle): + self.origin.set(*origin) + self.lookat.set(*lookat) + mat3_rotation(self.rotation, axis, angle) + mat3_mul_vec3(self.origin, self.rotation, self.origin) + mat3_mul_vec3(self.lookat, self.rotation, self.lookat) + mat4_lookat(self.view, self.origin, self.lookat) + + def to_km(self): + vec3_mul_vec3(self.origin, _m2km, self.origin) + vec3_mul_vec3(self.lookat, _m2km, self.lookat) + mat4_lookat(self.view, self.origin, self.lookat) def resolve_inputs(self, shader, projection = b'u_projection', view = b'u_view'): return _Inputs(shader, projection, view) diff --git a/game/game.py b/game/game.py index b917848..4365db7 100644 --- a/game/game.py +++ b/game/game.py @@ -177,12 +177,7 @@ def main(): vec3_rotate(cube_orientation, vec3_forward, vec3_up, (current_time * 0.43) % tau) vec3_rotate(clouds_orientation, vec3_forward, vec3_up, (current_time * -0.037) % tau) - origin = vec3() - lookat = vec3() - mat3_rotation(rotation, vec3_up, (current_time * 0.05) % tau) - mat3_mul_vec3(origin, rotation, vec3(*camera_origin)) - mat3_mul_vec3(lookat, rotation, vec3(*camera_lookat)) - camera.set_view(origin, lookat) + camera.set_view(camera_origin, camera_lookat, vec3_up, (current_time * 0.05) % tau) environment.from_sun(camera.view, sun_direction, sun_power) select_shader(terrain_shader) @@ -215,12 +210,7 @@ def main(): unselect_texture(2, normalmap) unselect_shader(tests_shader) - m2km = vec3(0.001, 0.001, 0.001) - origin_km = vec3() - lookat_km = vec3() - vec3_mul_vec3(origin_km, origin, m2km) - vec3_mul_vec3(lookat_km, lookat, m2km) - camera.set_view(origin_km, lookat_km) + camera.to_km() select_shader(sky_shader) camera.update_inputs(sky_camera_inputs)