Mercurial > touhou
changeset 394:346614f788f1
Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 06 Feb 2013 20:57:16 +0100 |
parents | 9e2cbb2c2c64 |
children | 43413d4ff05b |
files | pytouhou/ui/gamerenderer.py pytouhou/ui/gamerunner.py pytouhou/ui/renderer.pyx pytouhou/ui/shader.py pytouhou/ui/shaders/eosd.py |
diffstat | 5 files changed, 73 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/pytouhou/ui/gamerenderer.py +++ b/pytouhou/ui/gamerenderer.py @@ -19,6 +19,7 @@ from pyglet.gl import (glClear, glMatrix glDisable, glEnable, glFogi, glFogf, glFogfv, glBlendFunc, glBindTexture, glVertexPointer, glTexCoordPointer, glColorPointer, glDrawArrays, + glVertexAttribPointer, glEnableVertexAttribArray, GL_DEPTH_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW, GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END, GL_FOG_COLOR, GL_DEPTH_TEST, GL_SRC_ALPHA, @@ -89,13 +90,13 @@ class GameRenderer(Renderer): model = Matrix() model.data[3] = [-x, -y, -z, 1] view = self.setup_camera(dx, dy, dz) - model_view = model * view - model_view_projection = model * view * self.proj if self.use_fixed_pipeline: + model_view_projection = model * view * self.proj glMatrixMode(GL_MODELVIEW) glLoadMatrixf(model_view_projection.get_c_data()) else: + model_view = model * view self.background_shader.uniform_matrixf('model_view', model_view.get_c_data()) self.background_shader.uniform_matrixf('projection', self.proj.get_c_data()) @@ -103,9 +104,17 @@ class GameRenderer(Renderer): for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back): glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) - glVertexPointer(3, GL_FLOAT, 0, vertices) - glTexCoordPointer(2, GL_FLOAT, 0, uvs) - glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) + if self.use_fixed_pipeline: + glVertexPointer(3, GL_FLOAT, 0, vertices) + glTexCoordPointer(2, GL_FLOAT, 0, uvs) + glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) + else: + glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, vertices) + glEnableVertexAttribArray(0) + glVertexAttribPointer(1, 2, GL_FLOAT, False, 0, uvs) + glEnableVertexAttribArray(1) + glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, 0, colors) + glEnableVertexAttribArray(2) glDrawArrays(GL_QUADS, 0, nb_vertices) glDisable(GL_DEPTH_TEST) else:
--- a/pytouhou/ui/gamerunner.py +++ b/pytouhou/ui/gamerunner.py @@ -95,12 +95,13 @@ class GameRunner(pyglet.window.Window, G # Initialize OpenGL glEnable(GL_BLEND) - glEnable(GL_TEXTURE_2D) - glHint(GL_FOG_HINT, GL_NICEST) - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) - glEnableClientState(GL_COLOR_ARRAY) - glEnableClientState(GL_VERTEX_ARRAY) - glEnableClientState(GL_TEXTURE_COORD_ARRAY) + if self.use_fixed_pipeline: + glEnable(GL_TEXTURE_2D) + glHint(GL_FOG_HINT, GL_NICEST) + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) + glEnableClientState(GL_COLOR_ARRAY) + glEnableClientState(GL_VERTEX_ARRAY) + glEnableClientState(GL_TEXTURE_COORD_ARRAY) self.proj = self.perspective(30, float(self.game.width) / float(self.game.height), 101010101./2010101., 101010101./10101.)
--- a/pytouhou/ui/renderer.pyx +++ b/pytouhou/ui/renderer.pyx @@ -22,6 +22,7 @@ import ctypes 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, @@ -82,9 +83,17 @@ cdef class Renderer: nb_vertices += 4 for (texture_key, blendfunc), indices in indices_by_texture.items(): - glVertexPointer(3, GL_INT, 24, <long> &self.vertex_buffer[0].x) - glTexCoordPointer(2, GL_FLOAT, 24, <long> &self.vertex_buffer[0].u) - glColorPointer(4, GL_UNSIGNED_BYTE, 24, <long> &self.vertex_buffer[0].r) + if self.use_fixed_pipeline: + glVertexPointer(3, GL_INT, sizeof(Vertex), <long> &self.vertex_buffer[0].x) + 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) + glEnableVertexAttribArray(0) + glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <long> &self.vertex_buffer[0].u) + glEnableVertexAttribArray(1) + glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <long> &self.vertex_buffer[0].r) + glEnableVertexAttribArray(2) nb_indices = len(indices) indices = pack(str(nb_indices) + 'H', *indices)
--- a/pytouhou/ui/shader.py +++ b/pytouhou/ui/shader.py @@ -16,7 +16,7 @@ from pyglet.gl import (glCreateProgram, glUseProgram, glGetUniformLocation, glUniform1f, glUniform2f, glUniform3f, glUniform4f, glUniform1i, glUniform2i, glUniform3i, glUniform4i, - glUniformMatrix4fv) + glUniformMatrix4fv, glBindAttribLocation) from ctypes import (c_char, c_char_p, c_int, POINTER, byref, cast, create_string_buffer) @@ -43,6 +43,11 @@ class Shader(object): # create the fragment shader self.createShader(frag, GL_FRAGMENT_SHADER) + #TODO: put those elsewhere. + glBindAttribLocation(self.handle, 0, 'in_position') + glBindAttribLocation(self.handle, 1, 'in_texcoord') + glBindAttribLocation(self.handle, 2, 'in_color') + # attempt to link the program self.link()
--- a/pytouhou/ui/shaders/eosd.py +++ b/pytouhou/ui/shaders/eosd.py @@ -21,22 +21,32 @@ class GameShader(Shader): Shader.__init__(self, [''' #version 120 + attribute vec3 in_position; + attribute vec2 in_texcoord; + attribute vec4 in_color; + uniform mat4 mvp; + varying vec2 texcoord; + varying vec4 color; + void main() { - gl_Position = mvp * gl_Vertex; - gl_FrontColor = gl_Color; - gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = mvp * vec4(in_position, 1.0); + texcoord = in_texcoord; + color = in_color; } - '''], [ ''' + '''], [''' #version 120 + varying vec2 texcoord; + varying vec4 color; + uniform sampler2D color_map; void main() { - gl_FragColor = texture2D(color_map, gl_TexCoord[0].st) * gl_Color; + gl_FragColor = texture2D(color_map, texcoord) * color; } ''']) @@ -46,33 +56,41 @@ class BackgroundShader(Shader): Shader.__init__(self, [''' #version 120 + attribute vec3 in_position; + attribute vec2 in_texcoord; + attribute vec4 in_color; + uniform mat4 model_view; uniform mat4 projection; + varying vec2 texcoord; + varying vec4 color; varying float fog_density; void main() { - gl_Position = model_view * gl_Vertex; - gl_FrontColor = gl_Color; - gl_TexCoord[0] = gl_MultiTexCoord0; + vec4 position = model_view * vec4(in_position, 1.0); + texcoord = in_texcoord; + color = in_color; - float fog_position = -gl_Position.z / gl_Position.w; + float fog_position = -position.z / position.w; fog_density = clamp((gl_Fog.end - fog_position) * gl_Fog.scale, 0.0f, 1.0f); - gl_Position = projection * gl_Position; + gl_Position = projection * position; } - '''], [ ''' + '''], [''' #version 120 + varying vec2 texcoord; + varying vec4 color; + varying float fog_density; + uniform sampler2D color_map; - varying float fog_density; - void main() { - vec4 color = texture2D(color_map, gl_TexCoord[0].st) * gl_Color; - gl_FragColor = mix(gl_Fog.color, color, fog_density); - gl_FragColor.w = color.w; + vec4 temp_color = texture2D(color_map, texcoord) * color; + gl_FragColor = mix(gl_Fog.color, temp_color, fog_density); + gl_FragColor.a = temp_color.a; } '''])