Package glitter :: Package shaders :: Module shader
[hide private]
[frames] | no frames]

Source Code for Module glitter.shaders.shader

 1  """Shader classes. 
 2   
 3  @author: Stephan Wenger 
 4  @date: 2012-02-29 
 5  """ 
 6   
 7  import glitter.raw as _gl 
 8  from glitter.utils import ManagedObject, ShaderCompileError 
9 10 -class Shader(ManagedObject):
11 _generate_id = _gl.glCreateShader 12 _delete_id = _gl.glDeleteShader 13 _db = "shaders" 14
15 - def __init__(self, source=None, compile=None, context=None):
16 if any(x is NotImplemented for x in (self._type,)): 17 raise TypeError("%s is abstract" % self.__class__.__name__) 18 super(Shader, self).__init__(context=context) 19 if source: 20 self.source = source 21 if compile is None: 22 compile = bool(source) 23 if compile: 24 self.compile()
25
26 - def compile(self):
27 with self._context: 28 _gl.glCompileShader(self._id) 29 30 _compile_status = _gl.GLint() 31 _gl.glGetShaderiv(self._id, _gl.GL_COMPILE_STATUS, _gl.pointer(_compile_status)) 32 if _compile_status.value != _gl.GL_TRUE: 33 raise ShaderCompileError(self._log) 34 35 return self._log or None
36 37 @property
38 - def _log(self):
39 with self._context: 40 _info_log_length = _gl.GLint() 41 _gl.glGetShaderiv(self._id, _gl.GL_INFO_LOG_LENGTH, _gl.pointer(_info_log_length)) 42 _info_log = _gl.create_string_buffer(_info_log_length.value) 43 _gl.glGetShaderInfoLog(self._id, _info_log_length, _gl.POINTER(_gl.GLint)(), _info_log) 44 return _info_log.value
45 46 @property
47 - def source(self):
48 with self._context: 49 _shader_source_length = _gl.GLint() 50 _gl.glGetShaderiv(self._id, _gl.GL_SHADER_SOURCE_LENGTH, _gl.pointer(_shader_source_length)) 51 _source = _gl.create_string_buffer(_shader_source_length.value) 52 _gl.glGetShaderSource(self._id, _shader_source_length, _gl.POINTER(_gl.GLint)(), _source) 53 return _source.value
54 55 @source.setter
56 - def source(self, source):
57 with self._context: 58 _gl.glShaderSource(self._id, 1, _gl.pointer(_gl.c_char_p(source)), _gl.POINTER(_gl.GLint)())
59
60 -class VertexShader(Shader):
61 """Vertes shader. 62 63 @todo: wrap C{glGetShaderPrecisionFormat}. 64 """ 65 66 _type = _gl.GL_VERTEX_SHADER
67
68 -class TesselationControlShader(Shader):
69 _type = _gl.GL_TESS_CONTROL_SHADER
70
71 -class TesselationEvaluationShader(Shader):
72 _type = _gl.GL_TESS_EVALUATION_SHADER
73
74 -class GeometryShader(Shader):
75 _type = _gl.GL_GEOMETRY_SHADER
76
77 -class FragmentShader(Shader):
78 """Fragment shader. 79 80 @todo: wrap C{glGetShaderPrecisionFormat}. 81 """ 82 83 _type = _gl.GL_FRAGMENT_SHADER
84 85 __all__ = [ 86 "Shader", 87 "VertexShader", 88 "TesselationControlShader", 89 "TesselationEvaluationShader", 90 "GeometryShader", 91 "FragmentShader", 92 ] 93