comparison pytouhou/ui/shader.pyx @ 458:1b56d62250ab

Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Sep 2013 23:11:54 +0200
parents 878273a984c4
children
comparison
equal deleted inserted replaced
457:4ccc47828002 458:1b56d62250ab
13 (glCreateProgram, glCreateShader, GL_VERTEX_SHADER, 13 (glCreateProgram, glCreateShader, GL_VERTEX_SHADER,
14 GL_FRAGMENT_SHADER, glShaderSource, glCompileShader, glGetShaderiv, 14 GL_FRAGMENT_SHADER, glShaderSource, glCompileShader, glGetShaderiv,
15 GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, glGetShaderInfoLog, 15 GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, glGetShaderInfoLog,
16 glAttachShader, glLinkProgram, glGetProgramiv, glGetProgramInfoLog, 16 glAttachShader, glLinkProgram, glGetProgramiv, glGetProgramInfoLog,
17 GL_LINK_STATUS, glUseProgram, glGetUniformLocation, glUniform1fv, 17 GL_LINK_STATUS, glUseProgram, glGetUniformLocation, glUniform1fv,
18 glUniform4fv, glUniformMatrix4fv, glBindAttribLocation, GLint, 18 glUniform4fv, glUniformMatrix4fv, glBindAttribLocation)
19 GLuint, GLchar, GLfloat, GLenum)
20 19
21 from libc.stdlib cimport malloc, free 20 from libc.stdlib cimport malloc, free
22 from pytouhou.utils.matrix cimport Matrix
23 21
24 22
25 class GLSLException(Exception): 23 class GLSLException(Exception):
26 pass 24 pass
27 25
28 26
29 cdef class Shader: 27 cdef class Shader:
30 cdef GLuint handle
31 cdef bint linked
32 cdef dict location_cache
33
34 # vert and frag take arrays of source strings the arrays will be 28 # vert and frag take arrays of source strings the arrays will be
35 # concattenated into one string by OpenGL 29 # concattenated into one string by OpenGL
36 def __init__(self, vert=None, frag=None): 30 def __init__(self, vert=None, frag=None):
37 # create the program handle 31 # create the program handle
38 self.handle = glCreateProgram() 32 self.handle = glCreateProgram()
118 if loc == -1: 112 if loc == -1:
119 raise GLSLException('Undefined {} uniform.'.format(name)) 113 raise GLSLException('Undefined {} uniform.'.format(name))
120 self.location_cache[name] = loc 114 self.location_cache[name] = loc
121 return self.location_cache[name] 115 return self.location_cache[name]
122 116
123 def bind(self): 117 cdef void bind(self) nogil:
124 # bind the program 118 # bind the program
125 glUseProgram(self.handle) 119 glUseProgram(self.handle)
126 120
127 # upload a floating point uniform 121 # upload a floating point uniform
128 # this program must be currently bound 122 # this program must be currently bound
129 def uniform_1(self, name, GLfloat val): 123 cdef void uniform_1(self, name, GLfloat val):
130 glUniform1fv(self.get_uniform_location(name), 1, &val) 124 glUniform1fv(self.get_uniform_location(name), 1, &val)
131 125
132 # upload a vec4 uniform 126 # upload a vec4 uniform
133 def uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d): 127 cdef void uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d):
134 cdef GLfloat vals[4] 128 cdef GLfloat vals[4]
135 vals[0] = a 129 vals[0] = a
136 vals[1] = b 130 vals[1] = b
137 vals[2] = c 131 vals[2] = c
138 vals[3] = d 132 vals[3] = d
139 glUniform4fv(self.get_uniform_location(name), 1, vals) 133 glUniform4fv(self.get_uniform_location(name), 1, vals)
140 134
141 # upload a uniform matrix 135 # upload a uniform matrix
142 # works with matrices stored as lists, 136 # works with matrices stored as lists,
143 # as well as euclid matrices 137 # as well as euclid matrices
144 def uniform_matrix(self, name, Matrix mat): 138 cdef void uniform_matrix(self, name, Matrix mat):
145 # obtain the uniform location 139 # obtain the uniform location
146 loc = self.get_uniform_location(name) 140 loc = self.get_uniform_location(name)
147 # uplaod the 4x4 floating point matrix 141 # uplaod the 4x4 floating point matrix
148 glUniformMatrix4fv(loc, 1, False, mat.data) 142 glUniformMatrix4fv(loc, 1, False, mat.data)