Skip to content

Commit bd3e234

Browse files
committed
Working gltf loader
1 parent d55d315 commit bd3e234

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

demosys/scene/loaders/gltf.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import os
88
import struct
99

10+
import moderngl as mgl
1011
from OpenGL import GL
11-
from OpenGL.arrays.vbo import VBO
1212
from PIL import Image
1313

1414
from pyrr import matrix44, Matrix44, quaternion
1515

16-
from demosys.opengl import VAO
16+
from demosys import context
1717
from demosys.opengl import Texture2D
1818
from demosys.opengl import samplers
19+
from demosys.opengl import VAO
1920
from demosys.opengl.constants import TYPE_INFO
2021
from demosys.scene import (
2122
Node,
@@ -39,7 +40,6 @@
3940
5123: numpy.dtype(numpy.uint16), # GL_UNSIGNED_SHORT
4041
5125: numpy.dtype(numpy.uint32), # GL_UNSIGNED_INT
4142
5126: numpy.dtype(numpy.float32), # GL_FLOAT
42-
4343
}
4444

4545
ACCESSOR_TYPE = {
@@ -366,31 +366,32 @@ def load(self, materials):
366366
# According to the spec they can have different materials and vertex format
367367
for primitive in self.primitives:
368368

369-
vao = VAO(self.name, mode=primitive.mode or GL.GL_TRIANGLES)
369+
vao = VAO(self.name, mode=primitive.mode or mgl.TRIANGLES)
370370

371371
# Index buffer
372372
component_type, index_vbo = self.load_indices(primitive)
373-
if index_vbo:
374-
vao.set_element_buffer(component_type.value, index_vbo)
373+
if index_vbo is not None:
374+
# FIXME: Support u1 and u2 buffers
375+
vao.index_buffer('u', context.ctx().buffer(index_vbo.astype('u4').tobytes()))
375376

376377
attributes = {}
377-
378378
vbos = self.prepare_attrib_mapping(primitive)
379379

380380
for vbo_info in vbos:
381-
vbo = vbo_info.create()
382-
vao.add_array_buffer(vbo_info.component_type.value, vbo)
381+
dtype, buffer = vbo_info.create()
382+
vao.buffer(
383+
buffer,
384+
" ".join(["{}f".format(attr[1]) for attr in vbo_info.attributes]),
385+
[name_map[attr[0]] for attr in vbo_info.attributes],
386+
)
383387

384388
for attr in vbo_info.attributes:
385-
vao.map_buffer(vbo, name_map[attr[0]], attr[1])
386389
attributes[attr[0]] = {
387390
'name': name_map[attr[0]],
388391
'components': attr[1],
389392
'type': vbo_info.component_type.value,
390393
}
391394

392-
vao.build()
393-
394395
bbox_min, bbox_max = self.get_bbox(primitive)
395396
meshes.append(Mesh(
396397
self.name, vao=vao, attributes=attributes,
@@ -405,8 +406,8 @@ def load_indices(self, primitive):
405406
if getattr(primitive, "indices") is None:
406407
return None, None
407408

408-
_, component_type, vbo = primitive.indices.read(target=GL.GL_ELEMENT_ARRAY_BUFFER)
409-
return component_type, vbo
409+
_, component_type, buffer = primitive.indices.read()
410+
return component_type, buffer
410411

411412
def prepare_attrib_mapping(self, primitive):
412413
"""Pre-parse buffer mappings for each VBO to detect interleaved data for a primitive"""
@@ -458,9 +459,12 @@ def merge(self, info):
458459
def create(self):
459460
"""Create the VBO"""
460461
dtype = NP_COMPONENT_DTYPE[self.component_type.value]
461-
data = self.buffer.read(byte_length=self.byte_length, byte_offset=self.byte_offset)
462-
return VBO(numpy.frombuffer(data, count=self.count * self.components, dtype=dtype),
463-
target=self.target)
462+
data = numpy.frombuffer(
463+
self.buffer.read(byte_length=self.byte_length, byte_offset=self.byte_offset),
464+
count=self.count * self.components,
465+
dtype=dtype,
466+
)
467+
return dtype, data
464468

465469
def __str__(self):
466470
return "VBOInfo<buffer={}, buffer_view={}, target={}, \n" \
@@ -487,15 +491,14 @@ def __init__(self, accessor_id, data):
487491
self.max = numpy.array(data.get('max') or [0.5, 0.5, 0.5], dtype=numpy.float32)
488492
self.type = data.get('type')
489493

490-
def read(self, target=None):
494+
def read(self):
491495
"""
492496
Reads buffer data
493497
:return: component count, component type, data
494498
"""
495499
# ComponentType helps us determine the datatype
496500
dtype = NP_COMPONENT_DTYPE[self.componentType.value]
497501
return ACCESSOR_TYPE[self.type], self.componentType, self.bufferView.read(
498-
target=target,
499502
byte_offset=self.byteOffset,
500503
dtype=dtype,
501504
count=self.count * ACCESSOR_TYPE[self.type],
@@ -523,13 +526,12 @@ def __init__(self, view_id, data):
523526
# Valid: 34962 (ARRAY_BUFFER) and 34963 (ELEMENT_ARRAY_BUFFER) or None
524527
self.target = data.get('target')
525528

526-
def read(self, byte_offset=0, dtype=None, count=0, target=None):
529+
def read(self, byte_offset=0, dtype=None, count=0):
527530
data = self.buffer.read(
528531
byte_offset=byte_offset + self.byteOffset,
529532
byte_length=self.byteLength,
530533
)
531-
vbo = VBO(numpy.frombuffer(data, count=count, dtype=dtype),
532-
target=BUFFER_TARGETS[target])
534+
vbo = numpy.frombuffer(data, count=count, dtype=dtype)
533535
return vbo
534536

535537
def read_raw(self):

0 commit comments

Comments
 (0)