In model, we have now int and float instead of strings

This commit is contained in:
Thomas FORGIONE 2016-11-22 11:40:58 +01:00
parent e5da208ae7
commit 14946a0f3e
3 changed files with 33 additions and 17 deletions

View File

@ -7,9 +7,9 @@ class Vertex:
self.z = z
def from_array(self, arr):
self.x = arr[0] if len(arr) > 0 else None
self.y = arr[1] if len(arr) > 1 else None
self.z = arr[2] if len(arr) > 2 else None
self.x = float(arr[0]) if len(arr) > 0 else None
self.y = float(arr[1]) if len(arr) > 1 else None
self.z = float(arr[2]) if len(arr) > 2 else None
return self
Normal = Vertex
@ -22,9 +22,9 @@ class FaceVertex:
self.normal = normal
def from_array(self, arr):
self.vertex = arr[0] if len(arr) > 0 else None
self.texture = arr[1] if len(arr) > 1 else None
self.normal = arr[2] if len(arr) > 2 else None
self.vertex = int(arr[0]) if len(arr) > 0 else None
self.texture = int(arr[1]) if len(arr) > 1 else None
self.normal = int(arr[2]) if len(arr) > 2 else None
return self
class Face:
@ -70,6 +70,22 @@ class ModelParser:
line = line.rstrip()
self.parse_line(line)
def gl_draw(self):
import OpenGL.GL as gl
gl.glColor3f(1.0,0.0,0.0)
gl.glBegin(gl.GL_QUADS)
for face in self.faces:
v1 = self.vertices[face.a.vertex]
v2 = self.vertices[face.b.vertex]
v3 = self.vertices[face.c.vertex]
gl.glVertex3f(v1.x, v1.y, v1.z)
gl.glVertex3f(v2.x, v2.y, v2.z)
gl.glVertex3f(v3.x, v3.y, v3.z)
gl.glEnd()
class Exporter:
def __init__(self, model):
self.model = model

View File

@ -31,7 +31,7 @@ class OBJParser(ModelParser):
for i in range(len(splits)):
for j in range(len(splits[i])):
if splits[i][j] is not '':
splits[i][j] = str(int(splits[i][j]) - 1)
splits[i][j] = int(splits[i][j]) - 1
self.add_face(Face().from_array(splits))
@ -43,19 +43,19 @@ class OBJExporter(Exporter):
string = ""
for vertex in self.model.vertices:
string += "v " + ' '.join([vertex.x, vertex.y, vertex.z]) + "\n"
string += "v " + ' '.join([str(vertex.x), str(vertex.y), str(vertex.z)]) + "\n"
string += "\n"
if len(self.model.tex_coords) > 0:
for tex_coord in self.model.tex_coords:
string += "vt " + ' '.join([tex_coord.x, tex_coord.y]) + "\n"
string += "vt " + ' '.join([str(tex_coord.x), str(tex_coord.y)]) + "\n"
string += "\n"
if len(self.model.normals) > 0:
for normal in self.model.normals:
string += "vn " + ' '.join([normal.x, normal.y, normal.z]) + "\n"
string += "vn " + ' '.join([str(normal.x), str(normal.y), str(normal.z)]) + "\n"
string += "\n"
@ -64,15 +64,15 @@ class OBJExporter(Exporter):
arr = []
for v in [face.a, face.b, face.c]:
sub_arr = []
sub_arr.append(str(int(v.vertex) + 1))
sub_arr.append(str(v.vertex + 1))
if v.normal is None:
if v.texture is not None:
sub_arr.append('')
sub_arr.append(str(int(v.texture) + 1))
sub_arr.append(str(v.texture + 1))
elif v.texture is not None:
sub_arr.append(str(int(v.texture) + 1))
sub_arr.append(str(v.texture + 1))
if v.normal is not None:
sub_arr.append(str(int(v.normal) + 1))
sub_arr.append(str(v.normal + 1))
arr.append('/'.join(sub_arr))
string += ' '.join(arr) + '\n'

View File

@ -65,7 +65,7 @@ class PLYContentParser:
if self.current_element.name == 'vertex':
self.parent.add_vertex(Vertex().from_array(split))
elif self.current_element.name == 'face':
self.parent.add_face(Face(FaceVertex(split[1]), FaceVertex(split[2]), FaceVertex(split[3])))
self.parent.add_face(Face(FaceVertex(int(split[1])), FaceVertex(int(split[2])), FaceVertex(int(split[3]))))
self.counter += 1
@ -98,10 +98,10 @@ class PLYExporter(Exporter):
# Content of the model
for vertex in self.model.vertices:
string += vertex.x + " " + vertex.y + " " + vertex.z + "\n"
string += str(vertex.x) + " " + str(vertex.y) + " " + str(vertex.z) + "\n"
for face in self.model.faces:
string += "3 " + face.a.vertex + " " + face.b.vertex + " " + face.c.vertex + "\n"
string += "3 " + str(face.a.vertex) + " " + str(face.b.vertex) + " " + str(face.c.vertex) + "\n"
return string