Start to work on textures

This commit is contained in:
Thomas FORGIONE 2016-11-29 12:13:43 +01:00
parent bba3c672db
commit 1f40684cff
2 changed files with 21 additions and 17 deletions

View File

@ -9,18 +9,18 @@ Normal = Vertex
TexCoord = Vertex TexCoord = Vertex
class FaceVertex: 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.vertex = vertex
self.texture = texture self.tex_coord = tex_coord
self.normal = normal self.normal = normal
def from_array(self, arr): def from_array(self, arr):
self.vertex = int(arr[0]) if len(arr) > 0 else None self.vertex = int(arr[0]) if len(arr) > 0 else None
try: 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: except:
self.texture = None self.tex_coord = None
try: try:
self.normal = int(arr[2]) if len(arr) > 2 else None self.normal = int(arr[2]) if len(arr) > 2 else None
@ -30,11 +30,11 @@ class FaceVertex:
return self return self
class Face: 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.a = a
self.b = b self.b = b
self.c = c self.c = c
self.mtl = mtl self.material = material
# Expects array of array # Expects array of array
def from_array(self, arr): def from_array(self, arr):
@ -89,12 +89,6 @@ class ModelParser:
line = line.rstrip() line = line.rstrip()
self.parse_line(line) 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): def draw(self):
import OpenGL.GL as gl import OpenGL.GL as gl
@ -158,6 +152,7 @@ class ModelParser:
# Build VBO # Build VBO
v = [] v = []
n = [] n = []
t = []
for face in self.faces: for face in self.faces:
v1 = self.vertices[face.a.vertex] v1 = self.vertices[face.a.vertex]
@ -171,8 +166,15 @@ class ModelParser:
n3 = self.normals[face.c.normal] 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]] 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')) if face.a.tex_coord is not None:
self.normal_vbo = vbo.VBO(array(n, 'f')) 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): def generate_vertex_normals(self):
self.normals = [Normal() for i in self.vertices] self.normals = [Normal() for i in self.vertices]

View File

@ -13,7 +13,7 @@ class OBJParser(ModelParser):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.mtl = None self.current_material = None
def parse_line(self, string): def parse_line(self, string):
if string == '': if string == '':
@ -24,7 +24,7 @@ class OBJParser(ModelParser):
split = split[1:] split = split[1:]
if first == 'usemtl': if first == 'usemtl':
self.currentMaterial = split[0] self.current_material = split[0]
elif first == 'mtllib': elif first == 'mtllib':
path = os.path.join(os.path.dirname(self.path), split[0]) path = os.path.join(os.path.dirname(self.path), split[0])
self.mtl = MTLParser(self) self.mtl = MTLParser(self)
@ -43,7 +43,9 @@ class OBJParser(ModelParser):
if splits[i][j] is not '': if splits[i][j] is not '':
splits[i][j] = int(splits[i][j]) - 1 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: class MTLParser: