diff pytouhou/ui/opengl/background.pyx @ 519:b18f0bd30ad0

Optimise background rendering.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 07 Dec 2013 22:16:38 +0100
parents b3193b43a86c
children dacdcca59b66
line wrap: on
line diff
--- a/pytouhou/ui/opengl/background.pyx
+++ b/pytouhou/ui/opengl/background.pyx
@@ -19,20 +19,19 @@ from pytouhou.lib.opengl cimport \
           glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
           glBindTexture, glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
           GL_STATIC_DRAW, GL_UNSIGNED_BYTE, GL_FLOAT, GL_SRC_ALPHA,
-          GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, glGenBuffers,
-          glEnable, glDisable, GL_DEPTH_TEST, glDrawArrays, GL_QUADS)
+          GL_ONE_MINUS_SRC_ALPHA, GL_TEXTURE_2D, glGenBuffers, glEnable,
+          glDisable, GL_DEPTH_TEST, glDrawElements, GL_TRIANGLES,
+          GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER)
 
 from .sprite cimport get_sprite_rendering_data
 
 
 cdef class BackgroundRenderer:
-    def __cinit__(self):
-        # Allocate buffers
-        self.vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex))
-
-
     def __dealloc__(self):
-        free(self.vertex_buffer)
+        if self.vertex_buffer != NULL:
+            free(self.vertex_buffer)
+        if self.indices != NULL:
+            free(self.indices)
 
 
     def __init__(self, use_fixed_pipeline):
@@ -40,15 +39,19 @@ cdef class BackgroundRenderer:
 
         if not use_fixed_pipeline:
             glGenBuffers(1, &self.vbo)
+            glGenBuffers(1, &self.ibo)
 
 
     cdef void render_background(self):
         if self.use_fixed_pipeline:
+            indices = self.indices
             glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].x)
             glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &self.vertex_buffer[0].u)
             glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &self.vertex_buffer[0].r)
         else:
             glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
+            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo)
+            indices = NULL
 
             #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
             glVertexAttribPointer(0, 3, GL_FLOAT, False, sizeof(Vertex), <void*>0)
@@ -59,23 +62,22 @@ cdef class BackgroundRenderer:
             glEnableVertexAttribArray(2)
 
         glEnable(GL_DEPTH_TEST)
-        glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[self.blendfunc])
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
         glBindTexture(GL_TEXTURE_2D, self.texture)
-        glDrawArrays(GL_QUADS, 0, self.nb_vertices)
+        glDrawElements(GL_TRIANGLES, self.nb_indices, GL_UNSIGNED_SHORT, indices)
         glDisable(GL_DEPTH_TEST)
 
         if not self.use_fixed_pipeline:
             glBindBuffer(GL_ARRAY_BUFFER, 0)
+            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
 
 
     cdef void load(self, background, Renderer renderer):
         cdef float ox, oy, oz, ox2, oy2, oz2
-        cdef unsigned short nb_vertices = 0
-        cdef Vertex* vertex_buffer
+        cdef GLsizei nb_vertices = 0, nb_indices = 0
 
-        self.background = background
-
-        vertex_buffer = self.vertex_buffer
+        vertex_buffer = <Vertex*> malloc(65536 * sizeof(Vertex))
+        indices = <GLushort*> malloc(65536 * sizeof(GLushort))
 
         for ox, oy, oz, model_id, model in background.object_instances:
             for ox2, oy2, oz2, width_override, height_override, sprite in model:
@@ -85,19 +87,38 @@ cdef class BackgroundRenderer:
                 left, right, bottom, top = uvs
                 r, g, b, a = colors
 
+                # Pack data
                 vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a)
                 vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a)
                 vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a)
                 vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a)
 
+                # Add indices
+                indices[nb_indices] = nb_vertices
+                indices[nb_indices+1] = nb_vertices + 1
+                indices[nb_indices+2] = nb_vertices + 2
+                indices[nb_indices+3] = nb_vertices + 2
+                indices[nb_indices+4] = nb_vertices + 3
+                indices[nb_indices+5] = nb_vertices
+
                 nb_vertices += 4
+                nb_indices += 6
+
+        # We only need to keep the rendered vertices and indices in memory,
+        # either in RAM or in VRAM, they will never change until we implement
+        # background animation.
 
         self.texture = renderer.textures[key >> 1]
-        self.blendfunc = key & 1
-        self.nb_vertices = nb_vertices
-        self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
+        self.nb_indices = nb_indices
 
-        if not self.use_fixed_pipeline:
+        if self.use_fixed_pipeline:
+            self.vertex_buffer = <Vertex*> realloc(vertex_buffer, nb_vertices * sizeof(Vertex))
+            self.indices = <GLushort*> realloc(indices, nb_indices * sizeof(GLushort))
+        else:
             glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
-            glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), &self.vertex_buffer[0], GL_STATIC_DRAW)
+            glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), vertex_buffer, GL_STATIC_DRAW)
             glBindBuffer(GL_ARRAY_BUFFER, 0)
+
+            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ibo)
+            glBufferData(GL_ELEMENT_ARRAY_BUFFER, nb_indices * sizeof(GLushort), indices, GL_STATIC_DRAW)
+            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)