changeset 396:34a91f918e7c

Use Buffer Objects instead of host pointers.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Feb 2013 21:19:20 +0100
parents 43413d4ff05b
children c5ba11ede097
files pytouhou/ui/gamerunner.py pytouhou/ui/renderer.pyx
diffstat 2 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -17,7 +17,7 @@ import traceback
 
 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable,
                        glHint, glEnableClientState, glViewport, glScissor,
-                       glLoadMatrixf,
+                       glLoadMatrixf, glGenBuffers, glDeleteBuffers,
                        GL_MODELVIEW, GL_PROJECTION,
                        GL_TEXTURE_2D, GL_BLEND, GL_FOG,
                        GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
@@ -31,6 +31,8 @@ from .gamerenderer import GameRenderer
 from .music import MusicPlayer, SFXPlayer, NullPlayer
 from .shaders.eosd import GameShader, BackgroundShader
 
+from ctypes import c_uint, byref
+
 
 logger = get_logger(__name__)
 
@@ -55,6 +57,8 @@ class GameRunner(pyglet.window.Window, G
             self.background_shader = BackgroundShader()
             self.interface_shader = self.game_shader
 
+            self.vbo = c_uint(0)
+
         if game:
             self.load_game(game, background, replay)
 
@@ -102,6 +106,8 @@ class GameRunner(pyglet.window.Window, G
             glEnableClientState(GL_COLOR_ARRAY)
             glEnableClientState(GL_VERTEX_ARRAY)
             glEnableClientState(GL_TEXTURE_COORD_ARRAY)
+        else:
+            glGenBuffers(1, byref(self.vbo))
 
         self.proj = self.perspective(30, float(self.game.width) / float(self.game.height),
                                      101010101./2010101., 101010101./10101.)
@@ -122,6 +128,9 @@ class GameRunner(pyglet.window.Window, G
             else:
                 self.update()
 
+        if not self.use_fixed_pipeline:
+            glDeleteBuffers(1, byref(self.vbo))
+
 
     def _event_text_symbol(self, ev):
         # XXX: Ugly workaround to a pyglet bug on X11
--- a/pytouhou/ui/renderer.pyx
+++ b/pytouhou/ui/renderer.pyx
@@ -24,9 +24,10 @@ from struct import pack
 from pyglet.gl import (glVertexPointer, glTexCoordPointer, glColorPointer,
                        glVertexAttribPointer, glEnableVertexAttribArray,
                        glBlendFunc, glBindTexture, glDrawElements,
-                       GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT,
-                       GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE,
-                       GL_TEXTURE_2D, GL_TRIANGLES)
+                       glBindBuffer, glBufferData, GL_ARRAY_BUFFER,
+                       GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT,
+                       GL_INT, GL_FLOAT, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+                       GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES)
 
 from .sprite cimport get_sprite_rendering_data
 from .texture cimport TextureManager
@@ -87,11 +88,15 @@ cdef class Renderer:
             glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), <long> &self.vertex_buffer[0].u)
             glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), <long> &self.vertex_buffer[0].r)
         else:
-            glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <long> &self.vertex_buffer[0].x)
+            glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
+            glBufferData(GL_ARRAY_BUFFER, nb_vertices * sizeof(Vertex), <long> &self.vertex_buffer[0], GL_DYNAMIC_DRAW)
+
+            #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
+            glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), 0)
             glEnableVertexAttribArray(0)
-            glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <long> &self.vertex_buffer[0].u)
+            glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), 12)
             glEnableVertexAttribArray(1)
-            glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <long> &self.vertex_buffer[0].r)
+            glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), 20)
             glEnableVertexAttribArray(2)
 
         for (texture_key, blendfunc), indices in indices_by_texture.items():
@@ -101,6 +106,9 @@ cdef class Renderer:
             glBindTexture(GL_TEXTURE_2D, self.texture_manager[texture_key])
             glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, indices)
 
+        if not self.use_fixed_pipeline:
+            glBindBuffer(GL_ARRAY_BUFFER, 0)
+
 
     cpdef ortho_2d(self, left, right, bottom, top):
         mat = Matrix()