-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
82 lines (58 loc) · 2.22 KB
/
model.py
File metadata and controls
82 lines (58 loc) · 2.22 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import numpy
from OpenGL.GL import *
SIZEOF_FLOAT = 4
class OBJModel:
def __init__(self, fname):
vertices = []
vnormals = []
self.num_triangles = 0
self.vertex_array = []
self.normal_array = []
f = open(fname, 'rt')
for line in f.readlines():
if line.startswith('v '):
x, y, z = map(float, line.strip().split()[1:])
vertices.append((x, y, z))
elif line.startswith('vn '):
nx, ny, nz = map(float, line.strip().split()[1:])
vnormals.append((nx, ny, nz))
elif line.startswith('f '):
pp = line.strip().split()[1:]
assert len(pp) == 3 and "Expected triangles only"
assert pp[0].find('//') != -1 and "Normals missing on faces"
for p in pp:
vi, ni = map(int, p.split('//'))
vi -= 1
ni -= 1
self.vertex_array.append(vertices[vi])
self.normal_array.append(vnormals[ni])
self.num_triangles += 1
f.close()
self.num_vertices = len(self.vertex_array)
self.interleaved_array = []
for v, n in zip(self.vertex_array, self.normal_array):
self.interleaved_array.append(v)
self.interleaved_array.append(n)
#self.vertex_array = numpy.array(self.vertex_array, dtype=numpy.float32)
#self.normal_array = numpy.array(self.normal_array, dtype=numpy.float32)
self.interleaved_array = numpy.array(self.interleaved_array, dtype=numpy.float32)
self.interleaved_buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.interleaved_buffer)
glBufferData(GL_ARRAY_BUFFER, self.num_vertices*6*SIZEOF_FLOAT, self.interleaved_array, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
def setup(self):
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self.interleaved_buffer)
glVertexPointer(3, GL_FLOAT, SIZEOF_FLOAT*6, ctypes.c_void_p(0))
glNormalPointer(GL_FLOAT, SIZEOF_FLOAT*6, ctypes.c_void_p(3*SIZEOF_FLOAT))
def draw(self):
glDrawArrays(GL_TRIANGLES, 0, self.num_triangles*3)
def done(self):
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, 0)
if __name__ == '__main__':
import sys
m = OBJModel(sys.argv[1])
print m.vertex_array, m.normal_array