def __init__(self, name, srcVertex, srcFragment, funcStart=None, funcEnd=None, checkErrors=True, bindTo0=None): self.drawing = Drawing.get_instance() self.name = name self.shaderProg = bgl.glCreateProgram() self.shaderVert = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) self.shaderFrag = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) self.checkErrors = checkErrors srcVertex = '\n'.join(l.strip() for l in srcVertex.split('\n')) srcFragment = '\n'.join(l.strip() for l in srcFragment.split('\n')) bgl.glShaderSource(self.shaderVert, srcVertex) bgl.glShaderSource(self.shaderFrag, srcFragment) dprint('RetopoFlow Shader Info: %s (%d)' % (self.name,self.shaderProg)) logv = self.shader_compile(name, self.shaderVert) logf = self.shader_compile(name, self.shaderFrag) if len(logv.strip()): dprint(' vert log:\n' + '\n'.join((' '+l) for l in logv.splitlines())) if len(logf.strip()): dprint(' frag log:\n' + '\n'.join((' '+l) for l in logf.splitlines())) bgl.glAttachShader(self.shaderProg, self.shaderVert) bgl.glAttachShader(self.shaderProg, self.shaderFrag) if bindTo0: bgl.glBindAttribLocation(self.shaderProg, 0, bindTo0) bgl.glLinkProgram(self.shaderProg) self.shaderVars = {} lvars = [l for l in srcVertex.splitlines() if l.startswith('in ')] lvars += [l for l in srcVertex.splitlines() if l.startswith('attribute ')] lvars += [l for l in srcVertex.splitlines() if l.startswith('uniform ')] lvars += [l for l in srcFragment.splitlines() if l.startswith('uniform ')] for l in lvars: m = re.match('^(?P<qualifier>[^ ]+) +(?P<type>[^ ]+) +(?P<name>[^ ;]+)', l) assert m m = m.groupdict() q,t,n = m['qualifier'],m['type'],m['name'] locate = bgl.glGetAttribLocation if q in {'in','attribute'} else bgl.glGetUniformLocation if n in self.shaderVars: continue self.shaderVars[n] = { 'qualifier': q, 'type': t, 'location': locate(self.shaderProg, n), 'reported': False, } dprint(' attribs: ' + ', '.join((k + ' (%d)'%self.shaderVars[k]['location']) for k in self.shaderVars if self.shaderVars[k]['qualifier'] in {'in','attribute'})) dprint(' uniforms: ' + ', '.join((k + ' (%d)'%self.shaderVars[k]['location']) for k in self.shaderVars if self.shaderVars[k]['qualifier'] in {'uniform'})) self.funcStart = funcStart self.funcEnd = funcEnd self.mvpmatrix_buffer = bgl.Buffer(bgl.GL_FLOAT, [4,4])
def compile_program(vshader,fshader,gshader, program_name): """ Compile and link OpenGL shaders into a program. It prints any errors along with the sourcecode on the console After a successful linking it also shows a list of active attributes and uniforms. vshader, fshader and gshader are strings containing the GLSL source code for vertex, fragment and geometry shader respectively. Any of those might be None Returns an int (an OpenGL program name) program_name is arbitray human readable string """ program = bgl.glCreateProgram() error = False if vshader is not None: vertex_shader = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) bgl.glShaderSource(vertex_shader,vshader) bgl.glCompileShader(vertex_shader) if not gl_compile_error(program_name, "vertexshader", vertex_shader): bgl.glAttachShader(program, vertex_shader) else: dump_source(vshader) error = True if fshader is not None: fragment_shader = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) bgl.glShaderSource(fragment_shader,fshader) bgl.glCompileShader(fragment_shader) if not gl_compile_error(program_name, "fragmentshader", fragment_shader): bgl.glAttachShader(program, fragment_shader) else: dump_source(fshader) error = True if gshader is not None: geometry_shader = bgl.glCreateShader(bgl.GL_GEOMETRY_SHADER) bgl.glShaderSource(geometry_shader,gshader) bgl.glCompileShader(geometry_shader) if not gl_compile_error(program_name, "geometryshader", geometry_shader): bgl.glAttachShader(program, geometry_shader) else: dump_source(gshader) error = True if not error: bgl.glLinkProgram(program) gl_link_error(program_name, program) dump_attributes_and_uniforms(program) return program
def __init__(self, vertexcode, geomcode, fragcode): success = bgl.Buffer(bgl.GL_INT, 1) program = bgl.glCreateProgram() shader1 = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) bgl.glShaderSource(shader1, vertexcode) check_error("glShaderSource") bgl.glCompileShader(shader1) check_error("glCompileShader") bgl.glGetShaderiv(shader1, bgl.GL_COMPILE_STATUS, success) if success[0] != bgl.GL_TRUE: print("shader vertexcode compile error") shader2 = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) bgl.glShaderSource(shader2, fragcode) check_error("glShaderSource") bgl.glCompileShader(shader2) check_error("glCompileShader") bgl.glGetShaderiv(shader2, bgl.GL_COMPILE_STATUS, success) check_error("glGetShaderiv") if success[0] != bgl.GL_TRUE: print("shader fragcode compile error") bgl.glAttachShader(program, shader1) check_error("glAttachShader") bgl.glAttachShader(program, shader2) check_error("glAttachShader") bgl.glLinkProgram(program) check_error("glLinkProgram") bgl.glGetProgramiv(program, bgl.GL_LINK_STATUS, success) check_error("glGetProgramiv") if success[0] != bgl.GL_TRUE: print("Program link error") bgl.glValidateProgram(program) check_error("glValidateProgram") bgl.glGetProgramiv(program, bgl.GL_VALIDATE_STATUS, success) check_error("glGetProgramiv") if success[0] != bgl.GL_TRUE: print("Program invalid") self.program = program
def __init__(self, srcVertex, srcFragment, funcStart=None): self.shaderProg = bgl.glCreateProgram() self.shaderVert = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) self.shaderFrag = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) bgl.glShaderSource(self.shaderVert, srcVertex) bgl.glShaderSource(self.shaderFrag, srcFragment) print('RetopoFlow Shader Info') logv = self.shader_compile(self.shaderVert) logf = self.shader_compile(self.shaderFrag) if len(logv.strip()): print(' vert log:\n' + '\n'.join((' '+l) for l in logv.splitlines())) if len(logf.strip()): print(' frag log:\n' + '\n'.join((' '+l) for l in logf.splitlines())) bgl.glAttachShader(self.shaderProg, self.shaderVert) bgl.glAttachShader(self.shaderProg, self.shaderFrag) bgl.glLinkProgram(self.shaderProg) self.shaderVars = {} lvars = [l for l in srcVertex.splitlines() if l.startswith('in ')] lvars += [l for l in srcVertex.splitlines() if l.startswith('attribute ')] lvars += [l for l in srcVertex.splitlines() if l.startswith('uniform ')] lvars += [l for l in srcFragment.splitlines() if l.startswith('uniform ')] for l in lvars: m = re.match('^(?P<qualifier>[^ ]+) +(?P<type>[^ ]+) +(?P<name>[^ ;]+)', l) assert m m = m.groupdict() q,t,n = m['qualifier'],m['type'],m['name'] locate = bgl.glGetAttribLocation if q in {'in','attribute'} else bgl.glGetUniformLocation if n in self.shaderVars: continue self.shaderVars[n] = { 'qualifier': q, 'type': t, 'location': locate(self.shaderProg, n), } print(' attribs: ' + ', '.join(k for k in self.shaderVars if self.shaderVars[k]['qualifier'] in {'in','attribute'})) print(' uniforms: ' + ', '.join(k for k in self.shaderVars if self.shaderVars[k]['qualifier'] in {'uniform'})) self.funcStart = funcStart
def compile_shader(): global program program = bgl.glCreateProgram() shaderVert = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) shaderFrag = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) bgl.glShaderSource(shaderVert, shaderVertString) bgl.glShaderSource(shaderFrag, shaderFragString) bgl.glCompileShader(shaderVert) bgl.glCompileShader(shaderFrag) bgl.glAttachShader(program, shaderVert) bgl.glAttachShader(program, shaderFrag) bgl.glLinkProgram(program) bgl.glDeleteShader(shaderVert) bgl.glDeleteShader(shaderFrag)
def compile_shader(type, source): shader = bgl.glCreateShader(type) bgl.glShaderSource(shader, source) bgl.glCompileShader(shader) status = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetShaderiv(shader, bgl.GL_COMPILE_STATUS, status) if status[0] != bgl.GL_TRUE: infoLog = bgl.Buffer(bgl.GL_BYTE, 1024) length = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetShaderInfoLog(shader, 1024, length, infoLog) raise RuntimeError(''.join(chr(infoLog[i]) for i in range(length[0]))) return shader
def create_shader(source, shaderType): shader = bgl.glCreateShader(shaderType) if shader == 0: raise RuntimeError("Error: Shader creation failed!") bgl.glShaderSource(shader, source) bgl.glCompileShader(shader) check_shaderError(shader, bgl.GL_COMPILE_STATUS, False, "Error: Shader compilation failed:") return shader
def create_shader(source, shaderType): shader = bgl.glCreateShader(shaderType) if shader == 0: raise RuntimeError("Error: Shader creation failed!") bgl.glShaderSource(shader, source) bgl.glCompileShader(shader) check_shaderError(shader, bgl.GL_COMPILE_STATUS, False, "Error: Shader compilation failed:") return shader
def __init__(self): #vertex_shader = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) fragment_shader = bgl.glCreateShader(bgl.GL_FRAGMENT_SHADER) program = bgl.glCreateProgram() #bgl.glShaderSource(vertex_shader,vs) bgl.glShaderSource(fragment_shader, fs) #bgl.glCompileShader(vertex_shader) bgl.glCompileShader(fragment_shader) #utils.attachShader(program, vertex_shader) utils.attachShader(program, fragment_shader) bgl.glLinkProgram(program) self.program = program
def createShader(shader_type, shader_source): shader = bgl.glCreateShader(shader_type) bgl.glShaderSource(shader, shader_source) bgl.glCompileShader(shader) success = bgl.Buffer(bgl.GL_INT, [1]) bgl.glGetShaderiv(shader, bgl.GL_COMPILE_STATUS, success) if (success[0] == bgl.GL_TRUE): print("shader compiled") return shader bgl.glGetShaderiv(shader, bgl.GL_INFO_LOG_LENGTH, success); success[0] = success[0] + 1 log = bgl.Buffer(bgl.GL_BYTE, [success[0]]) start = bgl.Buffer(bgl.GL_INT, [1]) start[0] =0 bgl.glGetShaderInfoLog(shader, success[0]+1,start, log) py_log = log[:] py_log_str = "" for c in py_log: py_log_str += str(chr(c)) print(str(py_log_str)) bgl.glDeleteShader(shader)
def execute(self, context): namespace['projection_matrix'] = bgl.Buffer(bgl.GL_FLOAT, (4, 4)) namespace['points'] = (-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0) namespace['colors'] = (0.0, 1.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.5, 1.0, 0.0, 1.0, 0.5) namespace['data_point'] = bgl.Buffer(bgl.GL_FLOAT, len(namespace['points']), namespace['points']) namespace['data_color'] = bgl.Buffer(bgl.GL_FLOAT, len(namespace['colors']), namespace['colors']) namespace['vertex_shader_info'] = bgl.Buffer(bgl.GL_INT, 1) namespace['fragment_shader_info'] = bgl.Buffer(bgl.GL_INT, 1) namespace['shader_program_info'] = bgl.Buffer(bgl.GL_INT, 1) namespace['vao'] = bgl.Buffer(bgl.GL_INT, 1) namespace['vbo_point'] = bgl.Buffer(bgl.GL_INT, 1) namespace['vbo_color'] = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenBuffers(1, namespace['vbo_point']) bgl.glGenBuffers(1, namespace['vbo_color']) # Shaders namespace['shader_program'] = bgl.glCreateProgram() namespace['vertex_shader'] = bgl.glCreateShader(bgl.GL_VERTEX_SHADER) namespace['fragment_shader'] = bgl.glCreateShader( bgl.GL_FRAGMENT_SHADER) bgl.glShaderSource(namespace['vertex_shader'], vertex_shader_source) bgl.glShaderSource(namespace['fragment_shader'], fragment_shader_source) bgl.glCompileShader(namespace['vertex_shader']) bgl.glCompileShader(namespace['fragment_shader']) bgl.glGetShaderiv(namespace['vertex_shader'], bgl.GL_COMPILE_STATUS, namespace['vertex_shader_info']) bgl.glGetShaderiv(namespace['fragment_shader'], bgl.GL_COMPILE_STATUS, namespace['fragment_shader_info']) if namespace['vertex_shader_info'][0] == bgl.GL_TRUE: print("Vertex shader compiled successfully.") elif namespace['vertex_shader_info'][0] == bgl.GL_FALSE: print("Vertex shader failed to compile.") if namespace['fragment_shader_info'][0] == bgl.GL_TRUE: print("Fragment shader compiled successfully.") elif namespace['fragment_shader_info'][0] == bgl.GL_FALSE: print("Fragment shader failed to compile.") bgl.glAttachShader(namespace['shader_program'], namespace['vertex_shader']) bgl.glAttachShader(namespace['shader_program'], namespace['fragment_shader']) bgl.glLinkProgram(namespace['shader_program']) bgl.glGetProgramiv(namespace['shader_program'], bgl.GL_LINK_STATUS, namespace['shader_program_info']) if namespace['shader_program_info'][0] == bgl.GL_TRUE: print("Shader program linked successfully.") elif namespace['shader_program_info'][0] == bgl.GL_FALSE: print("Shader program failed to link.") # glGetUniformLocation can only be used after the shader program is linked, as stated in the OpenGL Specification. namespace['perspective_uniform_location'] = bgl.glGetUniformLocation( namespace['shader_program'], "perspective") bgl.glValidateProgram(namespace['shader_program']) bgl.glGetProgramiv(namespace['shader_program'], bgl.GL_VALIDATE_STATUS, namespace['shader_program_info']) if namespace['shader_program_info'][0] == bgl.GL_TRUE: print("Shader program validated successfully.") elif namespace['shader_program_info'][0] == bgl.GL_FALSE: print("Shader program failed to validate.") draw_handler_add() namespace['timer'] = context.window_manager.event_timer_add( time_step=0.01, window=context.window) namespace['data_timer'] = bgl.Buffer(bgl.GL_FLOAT, 2, [ math.sin(namespace['timer'].time_duration), math.sin(namespace['timer'].time_duration) * 2 ]) context.window_manager.modal_handler_add(self) return {'RUNNING_MODAL'}