def assign(self, varName, varValue): assert varName in self.shaderVars, 'Variable %s not found' % varName try: v = self.shaderVars[varName] q, l, t = v['qualifier'], v['location'], v['type'] if l == -1: if not v['reported']: dprint('ASSIGNING TO UNUSED ATTRIBUTE (%s): %s = %s' % (self.name, varName, str(varValue))) v['reported'] = True return if DEBUG_PRINT: print('%s (%s,%d,%s) = %s' % (varName, q, l, t, str(varValue))) if q in {'in', 'attribute'}: if t == 'float': bgl.glVertexAttrib1f(l, varValue) elif t == 'int': bgl.glVertexAttrib1i(l, varValue) elif t == 'vec2': bgl.glVertexAttrib2f(l, *varValue) elif t == 'vec3': bgl.glVertexAttrib3f(l, *varValue) elif t == 'vec4': bgl.glVertexAttrib4f(l, *varValue) else: assert False, 'Unhandled type %s for attrib %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign attrib %s = %s' % (varName, str(varValue))) elif q in {'uniform'}: # cannot set bools with BGL! :( if t == 'float': bgl.glUniform1f(l, varValue) elif t == 'vec2': bgl.glUniform2f(l, *varValue) elif t == 'vec3': bgl.glUniform3f(l, *varValue) elif t == 'vec4': bgl.glUniform4f(l, *varValue) elif t == 'mat3': bgl.glUniformMatrix3fv(l, 1, bgl.GL_TRUE, varValue) elif t == 'mat4': bgl.glUniformMatrix4fv(l, 1, bgl.GL_TRUE, varValue) else: assert False, 'Unhandled type %s for uniform %s' % ( t, varName) if self.checkErrors: self.drawing.glCheckError( 'assign uniform %s (%s %d) = %s' % (varName, t, l, str(varValue))) else: assert False, 'Unhandled qualifier %s for variable %s' % ( q, varName) except Exception as e: print('ERROR Shader.assign(%s, %s)): %s' % (varName, str(varValue), str(e)))
def draw_vertex_array(key, mode, dimensions, color): if key in VAO and VAO[key] and program: vao = VAO[key] bgl.glUseProgram(program) bgl.glUniform4f(bgl.glGetUniformLocation(program, "color"), *color) bgl.glEnableClientState(bgl.GL_VERTEX_ARRAY) bgl.glVertexPointer(dimensions, bgl.GL_FLOAT, 0, vao) bgl.glDrawArrays(mode, 0, int(len(vao) / dimensions)) bgl.glDisableClientState(bgl.GL_VERTEX_ARRAY) bgl.glUseProgram(0)
def assign(self, varName, varValue): assert varName in self.shaderVars, 'Variable %s not found' % varName try: v = self.shaderVars[varName] q,l,t = v['qualifier'],v['location'],v['type'] if l == -1: if not v['reported']: dprint('ASSIGNING TO UNUSED ATTRIBUTE (%s): %s = %s' % (self.name, varName,str(varValue))) v['reported'] = True return if DEBUG_PRINT: print('%s (%s,%d,%s) = %s' % (varName, q, l, t, str(varValue))) if q in {'in','attribute'}: if t == 'float': bgl.glVertexAttrib1f(l, varValue) elif t == 'int': bgl.glVertexAttrib1i(l, varValue) elif t == 'vec2': bgl.glVertexAttrib2f(l, *varValue) elif t == 'vec3': bgl.glVertexAttrib3f(l, *varValue) elif t == 'vec4': bgl.glVertexAttrib4f(l, *varValue) else: assert False, 'Unhandled type %s for attrib %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign attrib %s = %s' % (varName, str(varValue))) elif q in {'uniform'}: # cannot set bools with BGL! :( if t == 'float': bgl.glUniform1f(l, varValue) elif t == 'vec2': bgl.glUniform2f(l, *varValue) elif t == 'vec3': bgl.glUniform3f(l, *varValue) elif t == 'vec4': bgl.glUniform4f(l, *varValue) elif t == 'mat3': bgl.glUniformMatrix3fv(l, 1, bgl.GL_TRUE, varValue) elif t == 'mat4': bgl.glUniformMatrix4fv(l, 1, bgl.GL_TRUE, varValue) else: assert False, 'Unhandled type %s for uniform %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign uniform %s (%s %d) = %s' % (varName, t, l, str(varValue))) else: assert False, 'Unhandled qualifier %s for variable %s' % (q, varName) except Exception as e: print('ERROR (assign): ' + str(e))