Rework camera.

This commit is contained in:
Roz K 2022-12-20 16:10:09 +01:00
parent 6eb05b27ab
commit d0f739c603
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
2 changed files with 25 additions and 16 deletions

View File

@ -13,7 +13,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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)

View File

@ -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)