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, buffer, INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, param_type, params_format,
create_batch, fill_batch, draw_batch, destroy_batch) create_batch, fill_batch, draw_batch, destroy_batch)
_flags_p = POINTER(c_ubyte) _flags_t = c_ubyte
_mesh_p = POINTER(c_ushort) _flags_p = POINTER(_flags_t)
_mesh_t = c_ushort
_mesh_p = POINTER(_mesh_t)
class Batch: class Batch:
__slots__ = ('_batch', 'vertices', 'max_size', 'size', __slots__ = ('_batch', 'vertices', 'max_size', 'size',
@ -36,9 +38,9 @@ class Batch:
self.vertices = vertices self.vertices = vertices
self.max_size = max_size self.max_size = max_size
self.size = 0 self.size = 0
self.flags = buffer(c_ubyte, max_size) self.flags = buffer(_flags_t, max_size)
self._flags = _flags_p(self.flags) 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) self._meshes = _mesh_p(self.mesh)
if nparams: if nparams:
self.param = tuple(map(lambda f: buffer(param_type(f), max_size), params_decls.values())) 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...") print("Building tiles...")
tiles_texture = Texture(*archive.get_texture('tiles')) tiles_texture = Texture(*archive.get_texture('tiles'))
tiles_vertices_data = archive.get_vertices('tiles') tiles_vertices = archive.get_vertices('tiles')
tiles_vertices = Vertices(*tiles_vertices_data) water_mesh = tiles_vertices.get_mesh('water')
water_mesh = tiles_vertices_data.get_mesh('water') sand_mesh = tiles_vertices.get_mesh('sand')
sand_mesh = tiles_vertices_data.get_mesh('sand') grass_mesh = tiles_vertices.get_mesh('grass')
grass_mesh = tiles_vertices_data.get_mesh('grass') forest_mesh = tiles_vertices.get_mesh('forest')
forest_mesh = tiles_vertices_data.get_mesh('forest') rock_mesh = tiles_vertices.get_mesh('rock')
rock_mesh = tiles_vertices_data.get_mesh('rock') mud_mesh = tiles_vertices.get_mesh('mud')
mud_mesh = tiles_vertices_data.get_mesh('mud') lava_mesh = tiles_vertices.get_mesh('lava')
lava_mesh = tiles_vertices_data.get_mesh('lava') tiles_batch = Batch(Vertices(*tiles_vertices), generated.size ** 2, translation = PARAM_FORMAT_VEC3_SHORT)
tiles_batch = Batch(tiles_vertices, generated.size ** 2, translation = PARAM_FORMAT_VEC3_SHORT)
#TODO: generator & for real #TODO: generator & for real
vc = generated.volcano_c vc = generated.volcano_c
@ -175,12 +174,11 @@ def create_scene(keyboard, mouse):
tiles_batch.freeze(flags = True, mesh = True, translation = True) tiles_batch.freeze(flags = True, mesh = True, translation = True)
tests_texture = Texture(*archive.get_texture('tests')) tests_texture = Texture(*archive.get_texture('tests'))
tests_vertices_data = archive.get_vertices('tests') tests_vertices = archive.get_vertices('tests')
tests_vertices = Vertices(*tests_vertices_data) blob_mesh = tests_vertices.get_mesh('blob')
blob_mesh = tests_vertices_data.get_mesh('blob') cube_mesh = tests_vertices.get_mesh('cube')
cube_mesh = tests_vertices_data.get_mesh('cube') clouds_mesh = tests_vertices.get_mesh('clouds')
clouds_mesh = tests_vertices_data.get_mesh('clouds') tests_batch = Batch(Vertices(*tests_vertices), 3,
tests_batch = Batch(tests_vertices, 3,
translation = PARAM_FORMAT_VEC3_FLOAT, translation = PARAM_FORMAT_VEC3_FLOAT,
orientation = PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE) orientation = PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE)
blob_forward = math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0)) 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.fill()
tests_batch.freeze(flags = True, mesh = True) tests_batch.freeze(flags = True, mesh = True)
sea_polar_textures = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png')) sea_polar = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png'))
sea_detail_texture = sea.load_detail_texture('data/sea_bump.png') sea_detail = sea.load_detail_texture('data/sea_bump.png')
sea_triangles = sea.sea_triangles(64, proj_far_z - 0.1, proj_ratio) sea_triangles = sea.sea_triangles(64, proj_far_z - 0.1, proj_ratio)
assert tiles_shader.u_height_sampler == tests_shader.u_height_sampler assert tiles_shader.u_height_sampler == tests_shader.u_height_sampler
@ -215,8 +213,7 @@ def create_scene(keyboard, mouse):
PerfGroup('tests_batch', PerfGroup('tests_batch',
DrawNode(tests_batch))))), DrawNode(tests_batch))))),
FuncNode(update_sea, (camera, sea_phase)), FuncNode(update_sea, (camera, sea_phase)),
TextureGroup( TextureGroup({sea_shader.u_polar_sampler: sea_polar, sea_shader.u_detail_sampler: sea_detail},
{sea_shader.u_polar_sampler: sea_polar_textures, sea_shader.u_detail_sampler: sea_detail_texture},
ShaderGroup(sea_shader, ShaderGroup(sea_shader,
InputNode(sea_shader, camera, environment, sea_phase), InputNode(sea_shader, camera, environment, sea_phase),
PerfGroup('sea_triangles', PerfGroup('sea_triangles',
@ -228,14 +225,14 @@ def loop(display):
mouse = Mouse(events, wheel = 60, wheel_min = 20) mouse = Mouse(events, wheel = 60, wheel_min = 20)
scene = create_scene(keyboard, mouse) scene = create_scene(keyboard, mouse)
print("Running... Ctrl+c to quit") print("Running... Ctrl+c to quit")
time = Time()
try: try:
time = Time()
while not keyboard.quit: while not keyboard.quit:
events.update() events.update()
time.update()
clear_buffer(True, True, True) clear_buffer(True, True, True)
scene.draw(time) scene.draw(time)
swap_buffers(display) swap_buffers(display)
time.update()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

View File

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

View File

@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from itertools import chain
import struct import struct
from array import array from array import array
from pathlib import Path from pathlib import Path
@ -147,7 +148,7 @@ class VerticesData:
self.name = name self.name = name
self.vertices = vertices self.vertices = vertices
self.indices = indices self.indices = indices
self.meshes = array('I', meshes.values()) self.meshes = meshes.values()
self.meshes_db = dict(zip(meshes.keys(), range(len(meshes)))) self.meshes_db = dict(zip(meshes.keys(), range(len(meshes))))
def __iter__(self): def __iter__(self):
@ -155,7 +156,7 @@ class VerticesData:
yield len(self.vertices) // VERTEX_SIZE yield len(self.vertices) // VERTEX_SIZE
yield self.vertices yield self.vertices
yield self.indices yield self.indices
yield self.meshes yield array('I', chain.from_iterable(self.meshes))
def get_mesh(self, name): def get_mesh(self, name):
return self.meshes_db[name] return self.meshes_db[name]
@ -167,19 +168,20 @@ class VerticesData:
nvertices, nindices, nmeshes = _read_struct(file, 'III') nvertices, nindices, nmeshes = _read_struct(file, 'III')
vertices = _read_array(file, 'B', nvertices * VERTEX_SIZE) vertices = _read_array(file, 'B', nvertices * VERTEX_SIZE)
indices = _read_array(file, 'H', nindices) indices = _read_array(file, 'H', nindices)
meshes = _read_array(file, 'I', nmeshes)
names = [_read_string(file) for _ in range(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))) return cls(name, vertices, indices, dict(zip(names, meshes)))
def to_archive(self, file): def to_archive(self, file):
_write_magic(file, b'VERT') _write_magic(file, b'VERT')
_write_string(file, self.name) _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.vertices)
_write_array(file, self.indices) _write_array(file, self.indices)
_write_array(file, self.meshes)
for name in self.meshes_db.keys(): for name in self.meshes_db.keys():
_write_string(file, name) _write_string(file, name)
for mesh in self.meshes:
_write_struct(file, 'II', *mesh)
class Archive: class Archive:
__slots__ = 'textures_db', 'vertices_db' __slots__ = 'textures_db', 'vertices_db'