From a8d809a7ea7d86f067ea60471518db2c463d866c Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 23 Feb 2018 11:31:58 +0100 Subject: [PATCH] Support multiple models --- .gitignore | 3 +++ d3/model/basemodel.py | 13 ---------- d3/model/tools.py | 2 +- viewer.py | 57 +++++++++++++++++++++++++++++++++---------- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 1f8d1f0..18d643b 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,6 @@ ENV/ # Models assets/models +target/ +**/*.rs.bk +Cargo.lock diff --git a/d3/model/basemodel.py b/d3/model/basemodel.py index ef0ee46..f9ee647 100644 --- a/d3/model/basemodel.py +++ b/d3/model/basemodel.py @@ -99,8 +99,6 @@ class ModelParser: self.parts = [] self.materials = [] self.current_part = None - self.bounding_box = BoundingBox() - self.center_and_scale = True self.path = None def init_textures(self): @@ -128,7 +126,6 @@ class ModelParser: new_vertex = Vector(vertex.z, vertex.x, vertex.y) self.vertices.append(new_vertex) - self.bounding_box.add(new_vertex) def add_tex_coord(self, tex_coord): """Adds a texture coordinate element to the current model @@ -188,19 +185,9 @@ class ModelParser: """ 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: part.draw() - if self.center_and_scale: - gl.glPopMatrix() - def generate_vbos(self): """Generates the VBOs of each part of the model """ diff --git a/d3/model/tools.py b/d3/model/tools.py index 7a427cc..2b3ef06 100644 --- a/d3/model/tools.py +++ b/d3/model/tools.py @@ -64,7 +64,7 @@ def load_model(path, up_conversion = None): type = find_type(path, supported_formats) 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.parse_file(path) diff --git a/viewer.py b/viewer.py index 4639ff8..acc09a6 100755 --- a/viewer.py +++ b/viewer.py @@ -41,9 +41,11 @@ from d3.geometry import Vector from d3.controls import TrackBallControls, OrbitControls from d3.camera import Camera from d3.shader import Shader +from d3.model.basemodel import BoundingBox WINDOW_WIDTH = 1024 WINDOW_HEIGHT = 1024 +CENTER_AND_SCALE = True def resize(width, height): length = min(width, height) @@ -74,20 +76,24 @@ def main(args): pass # Load and parse the model - log('Loading model...', file=sys.stderr, end='') 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 - if len(model.normals) == 0: - log(' done! (' + str(sum(map(lambda x: len(x.faces), model.parts))) + ' faces)\nComputing normals...', file=sys.stderr, end='') + # Compute normals if not already computed + if len(model.normals) == 0: + log(' done! (' + str(sum(map(lambda x: len(x.faces), model.parts))) + ' faces)\nComputing normals...', file=sys.stderr, end='') + sys.stderr.flush() + model.generate_vertex_normals() + + # Generate vbos for smooth rendering + log(' done!\nGenerating vbos...', file=sys.stderr, end='') sys.stderr.flush() - model.generate_vertex_normals() + model.generate_vbos() - # Generate vbos for smooth rendering - log(' done!\nGenerating vbos...', file=sys.stderr, end='') - sys.stderr.flush() - model.generate_vbos() + models.append(model) log(' done!\nInitialiazing OpenGL Context', file=sys.stderr, end='') sys.stderr.flush() @@ -115,11 +121,22 @@ def main(args): 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='') sys.stderr.flush() + # Initializes OpenGL textures - model.init_textures() + for model in models: + model.init_textures() shader = Shader() @@ -169,7 +186,21 @@ def main(args): # gl.glEnd() shader.bind() - model.draw() + + 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() + + if CENTER_AND_SCALE: + gl.glPopMatrix() + shader.unbind() gl.glPopMatrix() @@ -185,7 +216,7 @@ if __name__ == '__main__': parser = argparse.ArgumentParser() parser.set_defaults(func=main) 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, help="Initial up vector") parser.add_argument('-tu', '--to-up', metavar='fup', default=None,