1- import moderngl
1+ import moderngl as mgl
22import os
33
4- from OpenGL import GL
5-
64from demosys import context
75from demosys .conf import settings
86
7+ VERTEX_SHADER = 'VERTEX_SHADER'
8+ GEOMETRY_SHADER = 'GEOMETRY_SHADER'
9+ FRAGMENT_SHADER = 'FRAGMENT_SHADER'
10+
911
1012class ShaderError (Exception ):
1113 pass
@@ -21,6 +23,7 @@ def __init__(self, path=None, name=None):
2123 :param path: Full file path to the shader
2224 :param name: Name of the shader (debug purposes)
2325 """
26+ self .ctx = context .ctx ()
2427 if not path and not name :
2528 raise ShaderError ("Shader must have a path or a name" )
2629
@@ -86,23 +89,23 @@ def set_vertex_source(self, source):
8689
8790 :param source: (string) Vertex shader source
8891 """
89- self .vertex_source = ShaderSource (GL . GL_VERTEX_SHADER , self .name , source )
92+ self .vertex_source = ShaderSource (VERTEX_SHADER , self .name , source )
9093
9194 def set_fragment_source (self , source ):
9295 """
9396 Set the fragment shader source
9497
9598 :param source: (string) Fragment shader source
9699 """
97- self .frag_source = ShaderSource (GL . GL_FRAGMENT_SHADER , self .name , source )
100+ self .frag_source = ShaderSource (FRAGMENT_SHADER , self .name , source )
98101
99102 def set_geometry_source (self , source ):
100103 """
101104 Set the geometry shader source
102105
103106 :param source: (string) Geometry shader source
104107 """
105- self .geo_source = ShaderSource (GL . GL_GEOMETRY_SHADER , self .name , source )
108+ self .geo_source = ShaderSource (GEOMETRY_SHADER , self .name , source )
106109
107110 def prepare (self ):
108111 """
@@ -130,7 +133,7 @@ def prepare(self):
130133 params .update ({'varyings' : out_attribs })
131134
132135 # Raises mgl.Error
133- self .program = context .ctx () .program (** params )
136+ self .program = self .ctx .program (** params )
134137
135138 # Build internal lookups
136139 self .build_uniform_map ()
@@ -146,7 +149,7 @@ def build_uniform_map(self):
146149 Builds an internal uniform map by querying the program.
147150 This way we don't have to query OpenGL (can cause slowdowns)
148151 """
149- self .uniform_map = {k : v for k , v in self .program ._members .items () if isinstance (v , moderngl .Uniform )}
152+ self .uniform_map = {k : v for k , v in self .program ._members .items () if isinstance (v , mgl .Uniform )}
150153 print ("ShaderProgram {} has {} uniform(s)" .format (self .name , len (self .uniform_map )))
151154
152155 for name , uniform in self .uniform_map .items ():
@@ -161,7 +164,7 @@ def build_attribute_map(self):
161164 This information is also used when the shader and VAO negotiates the buffer binding.
162165 """
163166 for name , attribute in self .program ._members .items ():
164- if not isinstance (attribute , moderngl .Attribute ):
167+ if not isinstance (attribute , mgl .Attribute ):
165168 continue
166169
167170 self .attribute_list .append (attribute )
@@ -203,11 +206,11 @@ def __init__(self, type, name, source):
203206 )
204207
205208 # Add preprocessors to source
206- if self .type == GL . GL_VERTEX_SHADER :
209+ if self .type == VERTEX_SHADER :
207210 self .lines .insert (1 , "#define VERTEX_SHADER 1" )
208- elif self .type == GL . GL_FRAGMENT_SHADER :
211+ elif self .type == FRAGMENT_SHADER :
209212 self .lines .insert (1 , "#define FRAGMENT_SHADER 1" )
210- elif self .type == GL . GL_GEOMETRY_SHADER :
213+ elif self .type == GEOMETRY_SHADER :
211214 self .lines .insert (1 , "#define GEOMETRY_SHADER 1" )
212215
213216 self .source = '\n ' .join (self .lines )
@@ -231,14 +234,3 @@ def print(self):
231234 print ("{}: {}" .format (str (i ).zfill (3 ), line ))
232235
233236 print ("---[ END {} ]---" .format (self .name ))
234-
235- def type_name (self ):
236- """Returns a string representation of the shader type"""
237- if self .type == GL .GL_VERTEX_SHADER :
238- return 'VERTEX_SHADER'
239- if self .type == GL .GL_FRAGMENT_SHADER :
240- return 'FRAGMENT_SHADER'
241- if self .type == GL .GL_GEOMETRY_SHADER :
242- return 'GEOMETRY_SHADER'
243- else :
244- raise ShaderError ("Unknown shader type" )
0 commit comments