From 6d9429546c79ce7ba3ef82a32a226068a6de965e Mon Sep 17 00:00:00 2001 From: Roz K Date: Sat, 17 Dec 2022 05:03:08 +0100 Subject: [PATCH] Bump engine submodule and switch to custom instance parameters. --- engine | 2 +- game/batch.py | 34 ++++++++++++------------------ game/game.py | 53 ++++++++++++++++++++++++----------------------- game/resources.py | 4 ++-- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/engine b/engine index 8f3612b..3df976c 160000 --- a/engine +++ b/engine @@ -1 +1 @@ -Subproject commit 8f3612bed26d4bdb9f6c62541bf6068285f760b3 +Subproject commit 3df976c154f76351702fe9f512f09c072dd67f60 diff --git a/game/batch.py b/game/batch.py index 3fe4c6f..0966230 100644 --- a/game/batch.py +++ b/game/batch.py @@ -16,43 +16,35 @@ from array import array from engine import ( - INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, BATCH_ORIENTATION_FORMAT_NONE, create_batch, draw_batch, destroy_batch) + INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, create_batch, draw_batch, destroy_batch) class Batch: - __slots__ = '_batch', 'max_size', 'flags', 'meshes', 'translations', 'orientations' + __slots__ = '_batch', 'max_size', 'flags', 'meshes', 'params' - def __init__(self, max_size, translation_format, orientation_format): + def __init__(self, max_size, params_format): assert max_size <= BATCH_MAX_SIZE - self._batch = create_batch(max_size, translation_format, orientation_format) + self._batch = create_batch(max_size, params_format) self.max_size = max_size self.flags = array('B') self.meshes = array('I') - self.translations = array('f') - if orientation_format != BATCH_ORIENTATION_FORMAT_NONE: - self.orientations = array('f') - else: - self.orientations = None + self.params = [array('f') for _ in params_format] def __del__(self): destroy_batch(self._batch) - def append(self, flags, mesh, translation, orientation): - assert len(translation) == 3 - assert orientation is None or len(orientation) == 3 + def append(self, flags, mesh, params): + assert len(params) == len(self.params) index = len(self.flags) assert index < self.max_size self.flags.append(flags | INSTANCE_FLAG_SPAWNED) self.meshes.append(mesh) - self.translations.extend(translation) - if self.orientations is not None: - self.orientations.extend(orientation) + for _params, param in zip(self.params, params): + _params.extend(param) return index - def set_translation(self, index, translation): - self.translations[index * 3 : index * 3 + 3] = translation - - def set_orientation(self, index, orientation): - self.orientations[index * 3 : index * 3 + 3] = orientation + def set_param(self, param, index, value): + size = len(value) + self.params[param][index * size : index * size + size] = value def draw(self): - draw_batch(self._batch, self.flags, self.meshes, self.translations, self.orientations) + draw_batch(self._batch, self.flags, self.meshes, self.params) diff --git a/game/game.py b/game/game.py index 0a5389e..aafce7a 100644 --- a/game/game.py +++ b/game/game.py @@ -33,8 +33,10 @@ def main(): terrain_shader = shader.load('terrain') sky_shader = shader.load('sky') - print("Loading resources...") select_shader(terrain_shader) + terrain_environment_inputs = environment.resolve_inputs() + + print("Loading resources...") archive = resources.RuntimeArchive.load('data/rk_island.rkar') tiles_texture = archive.get_texture('tiles') tiles_vertices = archive.get_vertices('tiles') @@ -45,28 +47,19 @@ def main(): rock = archive.get_model('rock') mud = archive.get_model('mud') lava = archive.get_model('lava') + heightmap = create_texture(1, b'u_height_sampler', TEXTURE_FORMAT_32F, 256, 256, 0, TEXTURE_FLAG_MIN_LINEAR | TEXTURE_FLAG_MAG_LINEAR, generated.packed_heights) normalmap = create_texture(2, b'u_normal_sampler', TEXTURE_FORMAT_RGB10_A2, 256, 256, 0, TEXTURE_FLAG_MIN_LINEAR | TEXTURE_FLAG_MAG_LINEAR, generated.packed_normals) - select_vertices(tiles_vertices) - terrain_batch = batch.Batch( - generated.size ** 2, BATCH_TRANSLATION_FORMAT_SHORT, BATCH_ORIENTATION_FORMAT_NONE) - unselect_vertices(tiles_vertices) - terrain_environment_inputs = environment.resolve_inputs() - tests_texture = archive.get_texture('tests') - tests_vertices = archive.get_vertices('tests') - blob = archive.get_model('blob') - cube = archive.get_model('cube') - clouds = archive.get_model('clouds') - select_vertices(tests_vertices) - tests_batch = batch.Batch(3, BATCH_TRANSLATION_FORMAT_FLOAT, BATCH_ORIENTATION_FORMAT_FLOAT) - unselect_vertices(tests_vertices) #TODO: generator & for real print("Building tiles...") + select_vertices(tiles_vertices) + terrain_orientation = resolve_param(b'i_orientation') + terrain_batch = batch.Batch(generated.size ** 2, params_format(PARAM_FORMAT_VEC3_SHORT)) vc = generated.volcano_c vr = generated.volcano_r for my, mx in generated.map_coords: @@ -95,15 +88,24 @@ def main(): model = mud else: model = rock - model.spawn(terrain_batch, (float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0), None) + model.spawn(terrain_batch, (float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0)) + unselect_vertices(tiles_vertices) + tests_texture = archive.get_texture('tests') + tests_vertices = archive.get_vertices('tests') + blob = archive.get_model('blob') + cube = archive.get_model('cube') + clouds = archive.get_model('clouds') + select_vertices(tests_vertices) + tests_batch = batch.Batch(3, params_format(PARAM_FORMAT_VEC3_FLOAT, PARAM_FORMAT_VEC3_FLOAT)) blob_translation = vec3((-100.0, -500.0, 0.0)) cube_translation = vec3((100.0, -500.0, 0.0)) cube_orientation = vec3(vec3_forward) clouds_orientation = vec3(vec3_forward) - blob_id = blob.spawn(tests_batch, blob_translation) + blob_id = blob.spawn(tests_batch, blob_translation, vec3_forward) cube_id = cube.spawn(tests_batch, cube_translation, cube_orientation) clouds_id = clouds.spawn(tests_batch, (0.0, 0.0, 32.0), clouds_orientation) + unselect_vertices(tests_vertices) proj_hfov = pi * 0.25 proj_ratio = 16.0 / 9.0 @@ -123,7 +125,7 @@ def main(): sky_camera.set_projection(proj_hfov, proj_ratio, proj_near_z, proj_far_z) unselect_shader(sky_shader) - sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 0.0))) + sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 0.25))) sun_power = 1.0 origin = vec3((0.0, -1200.0, 500.0)) lookat = vec3((0.0, 500.0, -500.0)) @@ -160,16 +162,16 @@ def main(): mat3_rotation(_rotation, up, (current_time * 0.21) % tau) mat3_mul_vec3(_blob_translation, _rotation, blob_translation) - tests_batch.set_translation(blob_id, _blob_translation) - tests_batch.set_orientation(blob_id, vec3(math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0)))) + tests_batch.set_param(0, blob_id, _blob_translation) + tests_batch.set_param(1, blob_id, vec3(math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0)))) mat3_mul_vec3(_cube_translation, _rotation, cube_translation) - tests_batch.set_translation(cube_id, _cube_translation) + tests_batch.set_param(0, cube_id, _cube_translation) mat3_rotation(_rotation, up, (current_time * 0.43) % tau) mat3_mul_vec3(_cube_orientation, _rotation, cube_orientation) - tests_batch.set_orientation(cube_id, _cube_orientation) + tests_batch.set_param(1, cube_id, _cube_orientation) mat3_rotation(_rotation, up, (current_time * -0.037) % tau) mat3_mul_vec3(_clouds_orientation, _rotation, clouds_orientation) - tests_batch.set_orientation(clouds_id, _clouds_orientation) + tests_batch.set_param(1, clouds_id, _clouds_orientation) select_shader(terrain_shader) select_texture(heightmap) @@ -177,12 +179,12 @@ def main(): terrain_camera.set_view(_origin, _lookat) terrain_camera.update_inputs() - environment_values = environment.from_sun( - terrain_camera.view, sun_direction, sun_power) + environment_values = environment.from_sun(terrain_camera.view, sun_direction, sun_power) environment.update_inputs(terrain_environment_inputs, environment_values) select_texture(tiles_texture) select_vertices(tiles_vertices) + set_param_vec3(terrain_orientation, vec3(vec3_forward)) draw_begin = time.thread_time() terrain_batch.draw() draw_end = time.thread_time() @@ -204,8 +206,7 @@ def main(): vec3(math.vec3_scale(_origin, 0.001)), vec3(math.vec3_scale(_lookat, 0.001))) sky_camera.update_inputs() - environment_values = environment.from_sun( - sky_camera.view, sun_direction, sun_power) + environment_values = environment.from_sun(sky_camera.view, sun_direction, sun_power) environment.update_inputs(sky_environment_inputs, environment_values) set_input_float(sea_phase, (current_time * 0.023) % 1.0) select_texture(sea_polar_textures) diff --git a/game/resources.py b/game/resources.py index d8b9750..197a8d2 100644 --- a/game/resources.py +++ b/game/resources.py @@ -192,8 +192,8 @@ class Model: self.flags = flags self.mesh = mesh - def spawn(self, batch, translation = engine.vec3_zero, orientation = engine.vec3_forward): - return batch.append(self.flags, self.mesh, translation, orientation) + def spawn(self, batch, *params): + return batch.append(self.flags, self.mesh, params) def create_model(data): return Model(data.flags, data.mesh)