diff --git a/game/camera.py b/game/camera.py index 030148f..d06c3fc 100644 --- a/game/camera.py +++ b/game/camera.py @@ -15,33 +15,29 @@ from engine import mat4, mat4_projection, mat4_lookat, resolve_input, set_input_mat4 +class _Inputs: + __slots__ = 'projection', 'view' + + def __init__(self, shader, projection, view): + self.projection = resolve_input(shader, projection) + self.view = resolve_input(shader, view) + class Camera: - __slots__ = '_projection_input', '_projection', '_view_input', '_view' + __slots__ = 'projection', 'view' def __init__(self): - self._projection_input = None - self._projection = mat4() - self._view_input = None - self._view = mat4() - - @property - def projection(self): - return self._projection - - @property - def view(self): - return self._view + 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) + mat4_projection(self.projection, half_fov, ratio, near_z, far_z) def set_view(self, origin, lookat): - mat4_lookat(self._view, origin, lookat) + mat4_lookat(self.view, origin, lookat) - def resolve_inputs(self, shader, projection_input = b'u_projection', view_input = b'u_view'): - self._projection_input = resolve_input(shader, projection_input) - self._view_input = resolve_input(shader, view_input) + def resolve_inputs(self, shader, projection = b'u_projection', view = b'u_view'): + return _Inputs(shader, projection, view) - def update_inputs(self): - set_input_mat4(self._projection_input, self._projection) - set_input_mat4(self._view_input, self._view) + def update_inputs(self, inputs): + set_input_mat4(inputs.projection, self.projection) + set_input_mat4(inputs.view, self.view) diff --git a/game/game.py b/game/game.py index 359467d..4af776d 100644 --- a/game/game.py +++ b/game/game.py @@ -18,8 +18,25 @@ from math import pi, tau, dist from engine import * -from game import ( - math, resources, shader, camera, batch, triangles, generator, environment, sea) +from game import math +from game import generator +from game import shader +from game import resources +from game import batch +from game import triangles +from game import environment +from game.camera import Camera +from game import sea + +proj_hfov = pi * 0.25 +proj_ratio = 16.0 / 9.0 +proj_near_z = 8.0 +proj_far_z = 3000.0 + +sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 1.0))) +sun_power = 1.0 +camera_origin = vec3((0.0, -1200.0, 500.0)) +camera_lookat = vec3((0.0, 500.0, -500.0)) def main(): print("Generating terrain...") @@ -108,36 +125,22 @@ def main(): cube_id = cube.spawn(tests_batch, cube_translation, cube_orientation) clouds_id = clouds.spawn(tests_batch, (0.0, 0.0, 32.0), clouds_orientation) - proj_hfov = pi * 0.25 - proj_ratio = 16.0 / 9.0 - proj_near_z = 8.0 - proj_far_z = 3000.0 - - terrain_environment_inputs = environment.resolve_inputs(terrain_shader) - terrain_camera = camera.Camera() - terrain_camera.set_projection(proj_hfov, proj_ratio, proj_near_z, proj_far_z) - terrain_camera.resolve_inputs(terrain_shader) - - tests_environment_inputs = environment.resolve_inputs(tests_shader) - tests_camera = camera.Camera() - tests_camera.set_projection(proj_hfov, proj_ratio, proj_near_z, proj_far_z) - tests_camera.resolve_inputs(tests_shader) - - sky_environment_inputs = environment.resolve_inputs(sky_shader) sea_phase = resolve_input(sky_shader, b'u_sea_phase') sea_polar_textures = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png')) sea_polar_sampler = resolve_input(sky_shader, b'u_sea_polar_sampler') sea_detail_texture = sea.load_detail_texture('data/sea_bump.png') sea_detail_sampler = resolve_input(sky_shader, b'u_sea_detail_sampler') sky_triangles = create_triangles(triangles.sky_triangles(64, proj_far_z - 0.1, proj_ratio)) - sky_camera = camera.Camera() - sky_camera.set_projection(proj_hfov, proj_ratio, proj_near_z, proj_far_z) - sky_camera.resolve_inputs(sky_shader) - sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 1.0))) - sun_power = 1.0 - origin = vec3((0.0, -1200.0, 500.0)) - lookat = vec3((0.0, 500.0, -500.0)) + camera = Camera() + camera.set_projection(proj_hfov, proj_ratio, proj_near_z, proj_far_z) + terrain_camera_inputs = camera.resolve_inputs(terrain_shader) + tests_camera_inputs = camera.resolve_inputs(tests_shader) + sky_camera_inputs = camera.resolve_inputs(sky_shader) + + terrain_environment_inputs = environment.resolve_inputs(terrain_shader) + tests_environment_inputs = environment.resolve_inputs(tests_shader) + sky_environment_inputs = environment.resolve_inputs(sky_shader) up = vec3(vec3_up) _rotation = mat3() @@ -165,15 +168,6 @@ def main(): begin_frame() frame_begin = time.thread_time() - mat3_rotation(_rotation, up, (current_time * 0.05) % tau) - mat3_mul_vec3(_origin, _rotation, origin) - mat3_mul_vec3(_lookat, _rotation, lookat) - - terrain_camera.set_view(_origin, _lookat) - tests_camera.set_view(_origin, _lookat) - sky_camera.set_view(vec3(math.vec3_scale(_origin, 0.001)), vec3(math.vec3_scale(_lookat, 0.001))) - environment_values = environment.from_sun(terrain_camera.view, sun_direction, sun_power) - mat3_rotation(_rotation, up, (current_time * 0.21) % tau) mat3_mul_vec3(_blob_translation, _rotation, blob_translation) tests_batch.set_param(0, blob_id, _blob_translation) @@ -187,8 +181,15 @@ def main(): mat3_mul_vec3(_clouds_orientation, _rotation, clouds_orientation) tests_batch.set_param(1, clouds_id, _clouds_orientation) + mat3_rotation(_rotation, up, (current_time * 0.05) % tau) + mat3_mul_vec3(_origin, _rotation, camera_origin) + mat3_mul_vec3(_lookat, _rotation, camera_lookat) + + camera.set_view(_origin, _lookat) + environment_values = environment.from_sun(camera.view, sun_direction, sun_power) + select_shader(terrain_shader) - terrain_camera.update_inputs() + camera.update_inputs(terrain_camera_inputs) environment.update_inputs(terrain_environment_inputs, environment_values) select_texture(tiles_texture, tiles_sampler) select_texture(heightmap, terrain_heightmap_sampler) @@ -204,7 +205,7 @@ def main(): unselect_shader(terrain_shader) select_shader(tests_shader) - tests_camera.update_inputs() + camera.update_inputs(tests_camera_inputs) environment.update_inputs(tests_environment_inputs, environment_values) select_texture(tests_texture, tests_sampler) select_texture(heightmap, tests_heightmap_sampler) @@ -217,8 +218,10 @@ def main(): unselect_texture(heightmap) unselect_shader(tests_shader) + camera.set_view(vec3(math.vec3_scale(_origin, 0.001)), vec3(math.vec3_scale(_lookat, 0.001))) + select_shader(sky_shader) - sky_camera.update_inputs() + camera.update_inputs(sky_camera_inputs) environment.update_inputs(sky_environment_inputs, environment_values) set_input_float(sea_phase, (current_time * 0.023) % 1.0) select_texture(sea_polar_textures, sea_polar_sampler) @@ -255,8 +258,8 @@ def main(): ", avg =", round((frame_avg / perf_count) * 1000.0, 2), "ms") # seed 666 - # origin = vec3((0.0, -1200.0, 500.0)) - # lookat = vec3((0.0, 500.0, -500.0)) + # camera_origin = vec3((0.0, -1200.0, 500.0)) + # camera_lookat = vec3((0.0, 500.0, -500.0)) # for x in range(10000) # current_time = 0 # Draw * 9999 : min = 0.2 , max = 2.16 , avg = 0.26 ms