Added VBOs support (vertices and normals)

This commit is contained in:
Thomas FORGIONE 2016-11-23 15:38:50 +01:00
parent 1b769e05d3
commit 8f7274f525
3 changed files with 15671 additions and 12 deletions

View File

@ -92,7 +92,9 @@ class ModelParser:
self.faces = [] self.faces = []
self.bounding_box = BoundingBox() self.bounding_box = BoundingBox()
self.center_and_scale = True self.center_and_scale = True
self.vbo = None self.vertex_vbo = None
self.tex_coord_vbo = None
self.normal_vbo = None
def add_vertex(self, vertex): def add_vertex(self, vertex):
self.vertices.append(vertex) self.vertices.append(vertex)
@ -129,13 +131,19 @@ class ModelParser:
gl.glScalef(1/scale, 1/scale, 1/scale) gl.glScalef(1/scale, 1/scale, 1/scale)
gl.glTranslatef(-center.x, -center.y, -center.z) gl.glTranslatef(-center.x, -center.y, -center.z)
if self.vbo is not None: if self.vertex_vbo is not None:
self.vbo.bind() self.vertex_vbo.bind()
gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glVertexPointerf(self.vbo) gl.glVertexPointerf(self.vertex_vbo)
gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(self.vbo.data) * 9) self.vertex_vbo.unbind()
self.vbo.unbind()
self.normal_vbo.bind()
gl.glEnableClientState(gl.GL_NORMAL_ARRAY);
gl.glNormalPointerf(self.normal_vbo)
self.normal_vbo.unbind()
gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(self.vertex_vbo.data) * 9)
else: else:
@ -167,21 +175,28 @@ class ModelParser:
if self.center_and_scale: if self.center_and_scale:
gl.glPopMatrix() gl.glPopMatrix()
def generate_vbo(self): def generate_vbos(self):
from OpenGL.arrays import vbo from OpenGL.arrays import vbo
from numpy import array from numpy import array
# Build VBO # Build VBO
l = [] v = []
n = []
for face in self.faces: for face in self.faces:
v1 = self.vertices[face.a.vertex] v1 = self.vertices[face.a.vertex]
v2 = self.vertices[face.b.vertex] v2 = self.vertices[face.b.vertex]
v3 = self.vertices[face.c.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]] v += [[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')) n1 = self.normals[face.a.normal]
n2 = self.normals[face.b.normal]
n3 = self.normals[face.c.normal]
n += [[n1.x, n1.y, n1.z], [n2.x, n2.y, n2.z], [n3.x, n3.y, n3.z]]
self.vertex_vbo = vbo.VBO(array(v, 'f'))
self.normal_vbo = vbo.VBO(array(n, 'f'))
def generate_vertex_normals(self): def generate_vertex_normals(self):
self.normals = [Normal() for i in self.vertices] self.normals = [Normal() for i in self.vertices]

15645
examples/link.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,6 @@ class TrackBallControls:
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0) dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
dTheta = dV.norm2() dTheta = dV.norm2()
print(dTheta)
if abs(dTheta) < 0.00001: if abs(dTheta) < 0.00001:
return return
@ -96,7 +95,7 @@ def main(args):
running = True running = True
model = load_model(args.input) model = load_model(args.input)
# model.generate_vbo() model.generate_vbos()
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():