From 1f40684cffbb37163cb6eae9d5bc11d93f37713f Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 29 Nov 2016 12:13:43 +0100 Subject: [PATCH] Start to work on textures --- d3/model/basemodel.py | 30 ++++++++++++++++-------------- d3/model/obj.py | 8 +++++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/d3/model/basemodel.py b/d3/model/basemodel.py index d4fe350..0a682e4 100644 --- a/d3/model/basemodel.py +++ b/d3/model/basemodel.py @@ -9,18 +9,18 @@ Normal = Vertex TexCoord = Vertex class FaceVertex: - def __init__(self, vertex = None, texture = None, normal = None): + def __init__(self, vertex = None, tex_coord = None, normal = None): self.vertex = vertex - self.texture = texture + self.tex_coord = tex_coord self.normal = normal def from_array(self, arr): self.vertex = int(arr[0]) if len(arr) > 0 else None try: - self.texture = int(arr[1]) if len(arr) > 1 else None + self.tex_coord = int(arr[1]) if len(arr) > 1 else None except: - self.texture = None + self.tex_coord = None try: self.normal = int(arr[2]) if len(arr) > 2 else None @@ -30,11 +30,11 @@ class FaceVertex: return self class Face: - def __init__(self, a = None, b = None, c = None, mtl = None): + def __init__(self, a = None, b = None, c = None, material = None): self.a = a self.b = b self.c = c - self.mtl = mtl + self.material = material # Expects array of array def from_array(self, arr): @@ -89,12 +89,6 @@ class ModelParser: line = line.rstrip() self.parse_line(line) - for material in self.mtl.materials: - print(material.name) - print('\tKa ' + str(material.Ka)) - print('\tKd ' + str(material.Kd)) - print('\tKs ' + str(material.Ks)) - def draw(self): import OpenGL.GL as gl @@ -158,6 +152,7 @@ class ModelParser: # Build VBO v = [] n = [] + t = [] for face in self.faces: v1 = self.vertices[face.a.vertex] @@ -171,8 +166,15 @@ class ModelParser: n3 = self.normals[face.c.normal] n += [[n1.x, n1.y, n1.z], [n2.x, n2.y, n2.z], [n3.x, n3.y, n3.z]] - self.vertex_vbo = vbo.VBO(array(v, 'f')) - self.normal_vbo = vbo.VBO(array(n, 'f')) + if face.a.tex_coord is not None: + t1 = self.tex_coords[face.a.tex_coord] + t2 = self.tex_coords[face.b.tex_coord] + t3 = self.tex_coords[face.c.tex_coord] + t += [[t1.x, t1.y], [t2.x, t2.y], [t3.x, t3.y]] + + self.vertex_vbo = vbo.VBO(array(v, 'f')) + self.normal_vbo = vbo.VBO(array(n, 'f')) + self.texture_vbo = vbo.VBO(array(t, 'f')) def generate_vertex_normals(self): self.normals = [Normal() for i in self.vertices] diff --git a/d3/model/obj.py b/d3/model/obj.py index f86d119..c3bad83 100644 --- a/d3/model/obj.py +++ b/d3/model/obj.py @@ -13,7 +13,7 @@ class OBJParser(ModelParser): def __init__(self): super().__init__() - self.mtl = None + self.current_material = None def parse_line(self, string): if string == '': @@ -24,7 +24,7 @@ class OBJParser(ModelParser): split = split[1:] if first == 'usemtl': - self.currentMaterial = split[0] + self.current_material = split[0] elif first == 'mtllib': path = os.path.join(os.path.dirname(self.path), split[0]) self.mtl = MTLParser(self) @@ -43,7 +43,9 @@ class OBJParser(ModelParser): if splits[i][j] is not '': splits[i][j] = int(splits[i][j]) - 1 - self.add_face(Face().from_array(splits)) + face = Face().from_array(splits) + face.material = self.current_material + self.add_face(face) class MTLParser: