Compare commits

...

2 Commits

5 changed files with 35 additions and 37 deletions

2
engine

@ -1 +1 @@
Subproject commit a5adfacdfd6dbb52bd5d1438eef90a370976531e
Subproject commit b46b4bddba6c609307ca198438c60d617e20c752

View File

@ -19,8 +19,10 @@ from engine import (
buffer, INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, param_type, params_format,
create_batch, fill_batch, draw_batch, destroy_batch)
_flags_p = POINTER(c_ubyte)
_mesh_p = POINTER(c_ushort)
_flags_t = c_ubyte
_flags_p = POINTER(_flags_t)
_mesh_t = c_ushort
_mesh_p = POINTER(_mesh_t)
class Batch:
__slots__ = ('_batch', 'vertices', 'max_size', 'size',
@ -36,9 +38,9 @@ class Batch:
self.vertices = vertices
self.max_size = max_size
self.size = 0
self.flags = buffer(c_ubyte, max_size)
self.flags = buffer(_flags_t, max_size)
self._flags = _flags_p(self.flags)
self.mesh = buffer(c_ushort, max_size)
self.mesh = buffer(_mesh_t, max_size)
self._meshes = _mesh_p(self.mesh)
if nparams:
self.param = tuple(map(lambda f: buffer(param_type(f), max_size), params_decls.values()))

View File

@ -129,16 +129,15 @@ def create_scene(keyboard, mouse):
print("Building tiles...")
tiles_texture = Texture(*archive.get_texture('tiles'))
tiles_vertices_data = archive.get_vertices('tiles')
tiles_vertices = Vertices(*tiles_vertices_data)
water_mesh = tiles_vertices_data.get_mesh('water')
sand_mesh = tiles_vertices_data.get_mesh('sand')
grass_mesh = tiles_vertices_data.get_mesh('grass')
forest_mesh = tiles_vertices_data.get_mesh('forest')
rock_mesh = tiles_vertices_data.get_mesh('rock')
mud_mesh = tiles_vertices_data.get_mesh('mud')
lava_mesh = tiles_vertices_data.get_mesh('lava')
tiles_batch = Batch(tiles_vertices, generated.size ** 2, translation = PARAM_FORMAT_VEC3_SHORT)
tiles_vertices = archive.get_vertices('tiles')
water_mesh = tiles_vertices.get_mesh('water')
sand_mesh = tiles_vertices.get_mesh('sand')
grass_mesh = tiles_vertices.get_mesh('grass')
forest_mesh = tiles_vertices.get_mesh('forest')
rock_mesh = tiles_vertices.get_mesh('rock')
mud_mesh = tiles_vertices.get_mesh('mud')
lava_mesh = tiles_vertices.get_mesh('lava')
tiles_batch = Batch(Vertices(*tiles_vertices), generated.size ** 2, translation = PARAM_FORMAT_VEC3_SHORT)
#TODO: generator & for real
vc = generated.volcano_c
@ -175,12 +174,11 @@ def create_scene(keyboard, mouse):
tiles_batch.freeze(flags = True, mesh = True, translation = True)
tests_texture = Texture(*archive.get_texture('tests'))
tests_vertices_data = archive.get_vertices('tests')
tests_vertices = Vertices(*tests_vertices_data)
blob_mesh = tests_vertices_data.get_mesh('blob')
cube_mesh = tests_vertices_data.get_mesh('cube')
clouds_mesh = tests_vertices_data.get_mesh('clouds')
tests_batch = Batch(tests_vertices, 3,
tests_vertices = archive.get_vertices('tests')
blob_mesh = tests_vertices.get_mesh('blob')
cube_mesh = tests_vertices.get_mesh('cube')
clouds_mesh = tests_vertices.get_mesh('clouds')
tests_batch = Batch(Vertices(*tests_vertices), 3,
translation = PARAM_FORMAT_VEC3_FLOAT,
orientation = PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE)
blob_forward = math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0))
@ -192,8 +190,8 @@ def create_scene(keyboard, mouse):
tests_batch.fill()
tests_batch.freeze(flags = True, mesh = True)
sea_polar_textures = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png'))
sea_detail_texture = sea.load_detail_texture('data/sea_bump.png')
sea_polar = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png'))
sea_detail = sea.load_detail_texture('data/sea_bump.png')
sea_triangles = sea.sea_triangles(64, proj_far_z - 0.1, proj_ratio)
assert tiles_shader.u_height_sampler == tests_shader.u_height_sampler
@ -215,8 +213,7 @@ def create_scene(keyboard, mouse):
PerfGroup('tests_batch',
DrawNode(tests_batch))))),
FuncNode(update_sea, (camera, sea_phase)),
TextureGroup(
{sea_shader.u_polar_sampler: sea_polar_textures, sea_shader.u_detail_sampler: sea_detail_texture},
TextureGroup({sea_shader.u_polar_sampler: sea_polar, sea_shader.u_detail_sampler: sea_detail},
ShaderGroup(sea_shader,
InputNode(sea_shader, camera, environment, sea_phase),
PerfGroup('sea_triangles',
@ -228,14 +225,14 @@ def loop(display):
mouse = Mouse(events, wheel = 60, wheel_min = 20)
scene = create_scene(keyboard, mouse)
print("Running... Ctrl+c to quit")
time = Time()
try:
time = Time()
while not keyboard.quit:
events.update()
time.update()
clear_buffer(True, True, True)
scene.draw(time)
swap_buffers(display)
time.update()
except KeyboardInterrupt:
pass

View File

@ -195,7 +195,7 @@ class ObjArchive(Archive):
vertices = []
indices = []
models = {}
meshes = {}
for name, mesh in sorted(objects.items()):
offset = len(indices)
assert offset < 65536
@ -213,7 +213,7 @@ class ObjArchive(Archive):
assert count < 65536
vertices.extend(mesh_vertices)
indices.extend(mesh_indices)
models[name] = (offset, count)
meshes[name] = (offset, count)
name = str(objpath.stem)
assert name not in self.vertices_db.keys()
@ -226,9 +226,6 @@ class ObjArchive(Archive):
n = (pack_10(_nz) << 20) | (pack_10(_ny) << 10) | pack_10(_nx)
t = ((_tl & 1023) << 20) | (pack_u10(_t) << 10) | pack_u10(_s)
return struct.pack('fffII', _px, _py, _pz, n, t)
meshes = {}
for mesh_name, (offset, count) in sorted(models.items()):
meshes[mesh_name] = offset | (count << 16)
self.vertices_db[name] = VerticesData(name,
array('B', b''.join(starmap(pack_vertex, vertices))),
array('H', indices),

View File

@ -13,6 +13,7 @@
# 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 itertools import chain
import struct
from array import array
from pathlib import Path
@ -147,7 +148,7 @@ class VerticesData:
self.name = name
self.vertices = vertices
self.indices = indices
self.meshes = array('I', meshes.values())
self.meshes = meshes.values()
self.meshes_db = dict(zip(meshes.keys(), range(len(meshes))))
def __iter__(self):
@ -155,7 +156,7 @@ class VerticesData:
yield len(self.vertices) // VERTEX_SIZE
yield self.vertices
yield self.indices
yield self.meshes
yield array('I', chain.from_iterable(self.meshes))
def get_mesh(self, name):
return self.meshes_db[name]
@ -167,19 +168,20 @@ class VerticesData:
nvertices, nindices, nmeshes = _read_struct(file, 'III')
vertices = _read_array(file, 'B', nvertices * VERTEX_SIZE)
indices = _read_array(file, 'H', nindices)
meshes = _read_array(file, 'I', nmeshes)
names = [_read_string(file) for _ in range(nmeshes)]
meshes = [_read_struct(file, 'II') for _ in range(nmeshes)]
return cls(name, vertices, indices, dict(zip(names, meshes)))
def to_archive(self, file):
_write_magic(file, b'VERT')
_write_string(file, self.name)
_write_struct(file, 'III', len(self.vertices) // VERTEX_SIZE, len(self.indices), len(self.meshes))
_write_struct(file, 'III', len(self.vertices) // VERTEX_SIZE, len(self.indices), len(self.meshes_db))
_write_array(file, self.vertices)
_write_array(file, self.indices)
_write_array(file, self.meshes)
for name in self.meshes_db.keys():
_write_string(file, name)
for mesh in self.meshes:
_write_struct(file, 'II', *mesh)
class Archive:
__slots__ = 'textures_db', 'vertices_db'