Support multiple models

This commit is contained in:
Thomas Forgione 2018-02-23 11:31:58 +01:00
parent a30afa2dd6
commit a8d809a7ea
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
4 changed files with 48 additions and 27 deletions

3
.gitignore vendored
View File

@ -91,3 +91,6 @@ ENV/
# Models # Models
assets/models assets/models
target/
**/*.rs.bk
Cargo.lock

View File

@ -99,8 +99,6 @@ class ModelParser:
self.parts = [] self.parts = []
self.materials = [] self.materials = []
self.current_part = None self.current_part = None
self.bounding_box = BoundingBox()
self.center_and_scale = True
self.path = None self.path = None
def init_textures(self): def init_textures(self):
@ -128,7 +126,6 @@ class ModelParser:
new_vertex = Vector(vertex.z, vertex.x, vertex.y) new_vertex = Vector(vertex.z, vertex.x, vertex.y)
self.vertices.append(new_vertex) self.vertices.append(new_vertex)
self.bounding_box.add(new_vertex)
def add_tex_coord(self, tex_coord): def add_tex_coord(self, tex_coord):
"""Adds a texture coordinate element to the current model """Adds a texture coordinate element to the current model
@ -188,19 +185,9 @@ class ModelParser:
""" """
import OpenGL.GL as gl import OpenGL.GL as gl
if self.center_and_scale:
center = self.bounding_box.get_center()
scale = self.bounding_box.get_scale() / 2
gl.glPushMatrix()
gl.glScalef(1/scale, 1/scale, 1/scale)
gl.glTranslatef(-center.x, -center.y, -center.z)
for part in self.parts: for part in self.parts:
part.draw() part.draw()
if self.center_and_scale:
gl.glPopMatrix()
def generate_vbos(self): def generate_vbos(self):
"""Generates the VBOs of each part of the model """Generates the VBOs of each part of the model
""" """

View File

@ -64,7 +64,7 @@ def load_model(path, up_conversion = None):
type = find_type(path, supported_formats) type = find_type(path, supported_formats)
if type is None: if type is None:
raise Exception("File format not supported") raise Exception("File format not supported \"" + str(type) + "\"")
parser = type.create_parser(up_conversion) parser = type.create_parser(up_conversion)
parser.parse_file(path) parser.parse_file(path)

View File

@ -41,9 +41,11 @@ from d3.geometry import Vector
from d3.controls import TrackBallControls, OrbitControls from d3.controls import TrackBallControls, OrbitControls
from d3.camera import Camera from d3.camera import Camera
from d3.shader import Shader from d3.shader import Shader
from d3.model.basemodel import BoundingBox
WINDOW_WIDTH = 1024 WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 1024 WINDOW_HEIGHT = 1024
CENTER_AND_SCALE = True
def resize(width, height): def resize(width, height):
length = min(width, height) length = min(width, height)
@ -74,9 +76,11 @@ def main(args):
pass pass
# Load and parse the model # Load and parse the model
log('Loading model...', file=sys.stderr, end='')
sys.stderr.flush() sys.stderr.flush()
model = load_model(args.input, up_conversion) models = []
for path in args.input:
log('Loading model ' + path + '...', file=sys.stderr, end='')
model = load_model(path, up_conversion)
# Compute normals if not already computed # Compute normals if not already computed
if len(model.normals) == 0: if len(model.normals) == 0:
@ -89,6 +93,8 @@ def main(args):
sys.stderr.flush() sys.stderr.flush()
model.generate_vbos() model.generate_vbos()
models.append(model)
log(' done!\nInitialiazing OpenGL Context', file=sys.stderr, end='') log(' done!\nInitialiazing OpenGL Context', file=sys.stderr, end='')
sys.stderr.flush() sys.stderr.flush()
@ -115,10 +121,21 @@ def main(args):
running = True running = True
bounding_box = BoundingBox()
if CENTER_AND_SCALE:
for model in models:
for vertex in model.vertices:
bounding_box.add(vertex)
log(' done!\nComputing bounding box...', file=sys.stderr, end='')
log(' done!\nInitializing OpenGL textures...', file=sys.stderr, end='') log(' done!\nInitializing OpenGL textures...', file=sys.stderr, end='')
sys.stderr.flush() sys.stderr.flush()
# Initializes OpenGL textures # Initializes OpenGL textures
for model in models:
model.init_textures() model.init_textures()
shader = Shader() shader = Shader()
@ -169,7 +186,21 @@ def main(args):
# gl.glEnd() # gl.glEnd()
shader.bind() shader.bind()
if CENTER_AND_SCALE:
center = bounding_box.get_center()
scale = bounding_box.get_scale() / 2
gl.glPushMatrix()
gl.glScalef(1/scale, 1/scale, 1/scale)
gl.glTranslatef(-center.x, -center.y, -center.z)
for model in models:
model.draw() model.draw()
if CENTER_AND_SCALE:
gl.glPopMatrix()
shader.unbind() shader.unbind()
gl.glPopMatrix() gl.glPopMatrix()
@ -185,7 +216,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.set_defaults(func=main) parser.set_defaults(func=main)
parser.add_argument('-v', '--version', action='version', version='1.0') parser.add_argument('-v', '--version', action='version', version='1.0')
parser.add_argument('-i', '--input', metavar='input', default=None, help='Input model') parser.add_argument('-i', '--input', metavar='input', nargs='+', default=None, help='Input model')
parser.add_argument('-fu', '--from-up', metavar='fup', default=None, parser.add_argument('-fu', '--from-up', metavar='fup', default=None,
help="Initial up vector") help="Initial up vector")
parser.add_argument('-tu', '--to-up', metavar='fup', default=None, parser.add_argument('-tu', '--to-up', metavar='fup', default=None,