-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.py
66 lines (45 loc) · 1.95 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
class Mesh(object):
def __init__(self):
self.vertices = []
self.texture_coords = []
self.vertex_normals = []
self.faces = []
def read_obj(self, file_in):
for line in file_in:
words = line.split()
if not words:
continue
data_type = words[0]
data = words[1:]
if date_type == '#': # Comment
continue
if data_type == 'v': # Vertex
# v x y z
vertex = float(data[0]), float(data[1]), float(data[2])
self.vertices.append(vertex)
elif data_type == 'vt': # Texture coordinate
#vt u v
texture_coord = float(data[0]), float(data[1])
self.vertices.append(texture_coord)
elif data_type == 'vn': # Vertex normal
#vn x y z
normal = float(data[0]), float(data[1]), float(data[2])
self.vertex_normals.append(normal)
elif data_type == 'f': # Face
#f vertex_index, texture_index, normal_index
for word in data:
triplet = word.split('/')
face = int(triplet[0]), int(triplet[1]), int(triplet[2])
self.faces.append(face)
def draw(self):
glBegin(GL_TRIS)
for vertex_index, texture_index, normal_index in self.faces:
glTexCoord(self.texture_coords[texture_index])
glNormal(self.normals[normal_index])
glVertex(vertices[vertex_index])
glEnd()
if __name__ == "__main__":
mesh = Mesh()
mesh.read_obj(file("will31.obj"))
print mesh.vertices