Starting to work with VBOs

This commit is contained in:
Thomas FORGIONE 2016-11-23 12:07:47 +01:00
parent 0bd7eae818
commit 1b769e05d3
2 changed files with 55 additions and 25 deletions

View File

@ -92,6 +92,7 @@ class ModelParser:
self.faces = []
self.bounding_box = BoundingBox()
self.center_and_scale = True
self.vbo = None
def add_vertex(self, vertex):
self.vertices.append(vertex)
@ -128,6 +129,16 @@ class ModelParser:
gl.glScalef(1/scale, 1/scale, 1/scale)
gl.glTranslatef(-center.x, -center.y, -center.z)
if self.vbo is not None:
self.vbo.bind()
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glVertexPointerf(self.vbo)
gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(self.vbo.data) * 9)
self.vbo.unbind()
else:
gl.glBegin(gl.GL_TRIANGLES)
for face in self.faces:
v1 = self.vertices[face.a.vertex]
@ -156,6 +167,21 @@ class ModelParser:
if self.center_and_scale:
gl.glPopMatrix()
def generate_vbo(self):
from OpenGL.arrays import vbo
from numpy import array
# Build VBO
l = []
for face in self.faces:
v1 = self.vertices[face.a.vertex]
v2 = self.vertices[face.b.vertex]
v3 = self.vertices[face.c.vertex]
l += [[v1.x, v1.y, v1.z], [v2.x, v2.y, v2.z], [v3.x, v3.y, v3.z]]
self.vbo = vbo.VBO(array(l, 'f'))
def generate_vertex_normals(self):
self.normals = [Normal() for i in self.vertices]

View File

@ -42,8 +42,9 @@ class TrackBallControls:
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
dTheta = dV.norm2()
print(dTheta)
if abs(dTheta) < 0.01:
if abs(dTheta) < 0.00001:
return
dV.normalize()
@ -95,6 +96,7 @@ def main(args):
running = True
model = load_model(args.input)
# model.generate_vbo()
while running:
for event in pygame.event.get():
@ -109,6 +111,10 @@ def main(args):
if event.button == 1:
pygame.mouse.get_rel()
# Update physics
controls.update()
# Draw frame
init_frame()
glPushMatrix()
@ -119,9 +125,7 @@ def main(args):
glFlush()
pygame.display.flip()
# Update physics
controls.update()
# Sleep
pygame.time.wait(10)