4 from OpenGL.GL import *
\r
15 class VertexArray(object):
\r
16 def __init__(self, vertices):
\r
17 self.vertices=vertices
\r
20 return "<VertexArray %d>" % len(self.vertices)
\r
24 glEnableClientState(GL_VERTEX_ARRAY)
\r
25 glVertexPointer(3, GL_FLOAT, 0, self.vertices)
\r
27 glDrawArrays(GL_TRIANGLES, 0, len(self.vertices))
\r
29 glDisableClientState(GL_VERTEX_ARRAY)
\r
42 class VertexArrayWithUV(object):
\r
43 def __init__(self, vertices, uvarray):
\r
44 self.vertices=vertices
\r
45 self.uvarray=uvarray
\r
48 return "<VertexArrayWithUV %d>" % len(self.vertices)
\r
52 glEnableClientState(GL_VERTEX_ARRAY)
\r
53 glVertexPointer(3, GL_FLOAT, 0, self.vertices)
\r
55 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
\r
56 glTexCoordPointer(2, GL_FLOAT, 0, self.uvarray)
\r
58 triangle_count=int(len(self.vertices)/3)
\r
59 glDrawArrays(GL_TRIANGLES, 0, triangle_count)
\r
61 glDisableClientState(GL_TEXTURE_COORD_ARRAY)
\r
62 glDisableClientState(GL_VERTEX_ARRAY)
\r
64 def get_boundingbox(self):
\r
65 vertices_size=len(self.vertices)
\r
66 if(vertices_size==0):
\r
67 return ([0, 0, 0], [0, 0, 0])
\r
68 def vertex_gen_factory():
\r
69 for i in range(0, vertices_size, 3):
\r
75 vertex_gen=vertex_gen_factory()
\r
79 for v in vertex_gen:
\r
80 min_v[0]=min(min_v[0], v[0])
\r
81 min_v[1]=min(min_v[1], v[1])
\r
82 min_v[2]=min(min_v[2], v[2])
\r
83 max_v[0]=max(max_v[0], v[0])
\r
84 max_v[1]=max(max_v[1], v[1])
\r
85 max_v[2]=max(max_v[2], v[2])
\r
86 return (min_v, max_v)
\r
97 class IndexedVertexArray(object):
\r
111 def addVertex(self, pos, normal, uv, color, b0, b1, w0):
\r
113 self.normal+=normal
\r
120 def addMaterial(self, material):
\r
121 self.materials.append(material)
\r
123 self.indicesMap[material]=indices
\r
128 glEnableClientState(GL_VERTEX_ARRAY)
\r
129 glVertexPointer(4, GL_FLOAT, 0, self.vertices)
\r
131 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
\r
132 glTexCoordPointer(2, GL_FLOAT, 0, self.uvlist)
\r
134 for m in self.materials:
\r
137 indices=self.indicesMap[m]
\r
139 glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)
\r
142 glDisableClientState(GL_TEXTURE_COORD_ARRAY)
\r
143 glDisableClientState(GL_VERTEX_ARRAY)
\r
145 def optimize(self):
\r
146 self.vertices=numpy.array(self.vertices, numpy.float32)
\r
147 self.uvlist=numpy.array(self.uvlist, numpy.float32)
\r
148 for m, indices in self.indicesMap.items():
\r
149 self.indicesMap[m]=numpy.array(indices, numpy.uint32)
\r
151 def get_boundingbox(self):
\r
152 vertices_size=len(self.vertices)
\r
153 if(vertices_size==0):
\r
154 return ([0, 0, 0], [0, 0, 0])
\r
155 def vertex_gen_factory():
\r
156 for i in range(0, vertices_size, 4):
\r
159 self.vertices[i+1],
\r
162 vertex_gen=vertex_gen_factory()
\r
166 for v in vertex_gen:
\r
179 return (min_v, max_v)
\r