Compare commits

..

No commits in common. "9f1ca87445bbe84ab84327f04dde2acd854e38cf" and "48714afaf58fbcf10b153a9fc5a61670fe145f7c" have entirely different histories.

5 changed files with 50 additions and 43 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 800 KiB

2
engine

@ -1 +1 @@
Subproject commit 3df976c154f76351702fe9f512f09c072dd67f60
Subproject commit 8f3612bed26d4bdb9f6c62541bf6068285f760b3

View File

@ -16,35 +16,43 @@
from array import array
from engine import (
INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, create_batch, draw_batch, destroy_batch)
INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, BATCH_ORIENTATION_FORMAT_NONE, create_batch, draw_batch, destroy_batch)
class Batch:
__slots__ = '_batch', 'max_size', 'flags', 'meshes', 'params'
__slots__ = '_batch', 'max_size', 'flags', 'meshes', 'translations', 'orientations'
def __init__(self, max_size, params_format):
def __init__(self, max_size, translation_format, orientation_format):
assert max_size <= BATCH_MAX_SIZE
self._batch = create_batch(max_size, params_format)
self._batch = create_batch(max_size, translation_format, orientation_format)
self.max_size = max_size
self.flags = array('B')
self.meshes = array('I')
self.params = [array('f') for _ in params_format]
self.translations = array('f')
if orientation_format != BATCH_ORIENTATION_FORMAT_NONE:
self.orientations = array('f')
else:
self.orientations = None
def __del__(self):
destroy_batch(self._batch)
def append(self, flags, mesh, params):
assert len(params) == len(self.params)
def append(self, flags, mesh, translation, orientation):
assert len(translation) == 3
assert orientation is None or len(orientation) == 3
index = len(self.flags)
assert index < self.max_size
self.flags.append(flags | INSTANCE_FLAG_SPAWNED)
self.meshes.append(mesh)
for _params, param in zip(self.params, params):
_params.extend(param)
self.translations.extend(translation)
if self.orientations is not None:
self.orientations.extend(orientation)
return index
def set_param(self, param, index, value):
size = len(value)
self.params[param][index * size : index * size + size] = value
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 draw(self):
draw_batch(self._batch, self.flags, self.meshes, self.params)
draw_batch(self._batch, self.flags, self.meshes, self.translations, self.orientations)

View File

@ -33,10 +33,8 @@ def main():
terrain_shader = shader.load('terrain')
sky_shader = shader.load('sky')
select_shader(terrain_shader)
terrain_environment_inputs = environment.resolve_inputs()
print("Loading resources...")
select_shader(terrain_shader)
archive = resources.RuntimeArchive.load('data/rk_island.rkar')
tiles_texture = archive.get_texture('tiles')
tiles_vertices = archive.get_vertices('tiles')
@ -47,19 +45,28 @@ 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:
@ -88,24 +95,15 @@ def main():
model = mud
else:
model = rock
model.spawn(terrain_batch, (float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0))
unselect_vertices(tiles_vertices)
model.spawn(terrain_batch, (float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0), None)
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, vec3_forward)
blob_id = blob.spawn(tests_batch, blob_translation)
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
@ -125,7 +123,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.25)))
sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 0.0)))
sun_power = 1.0
origin = vec3((0.0, -1200.0, 500.0))
lookat = vec3((0.0, 500.0, -500.0))
@ -162,16 +160,16 @@ def main():
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)
tests_batch.set_param(1, blob_id, vec3(math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0))))
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))))
mat3_mul_vec3(_cube_translation, _rotation, cube_translation)
tests_batch.set_param(0, cube_id, _cube_translation)
tests_batch.set_translation(cube_id, _cube_translation)
mat3_rotation(_rotation, up, (current_time * 0.43) % tau)
mat3_mul_vec3(_cube_orientation, _rotation, cube_orientation)
tests_batch.set_param(1, cube_id, _cube_orientation)
tests_batch.set_orientation(cube_id, _cube_orientation)
mat3_rotation(_rotation, up, (current_time * -0.037) % tau)
mat3_mul_vec3(_clouds_orientation, _rotation, clouds_orientation)
tests_batch.set_param(1, clouds_id, _clouds_orientation)
tests_batch.set_orientation(clouds_id, _clouds_orientation)
select_shader(terrain_shader)
select_texture(heightmap)
@ -179,12 +177,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()
@ -206,7 +204,8 @@ 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)

View File

@ -192,8 +192,8 @@ class Model:
self.flags = flags
self.mesh = mesh
def spawn(self, batch, *params):
return batch.append(self.flags, self.mesh, params)
def spawn(self, batch, translation = engine.vec3_zero, orientation = engine.vec3_forward):
return batch.append(self.flags, self.mesh, translation, orientation)
def create_model(data):
return Model(data.flags, data.mesh)