def shader_compile(name, shader, src): ''' logging and error-checking not quite working :( ''' bgl.glCompileShader(shader) # report shader compilation log (if any) bufLogLen = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetShaderiv(shader, bgl.GL_INFO_LOG_LENGTH, bufLogLen) if bufLogLen[0] > 0: # report log available bufLog = bgl.Buffer(bgl.GL_BYTE, bufLogLen) bgl.glGetShaderInfoLog(shader, bufLogLen[0], bufLogLen, bufLog) log = ''.join(chr(v) for v in bufLog.to_list() if v) if log: print('SHADER REPORT %s' % name) print('\n'.join([' %s' % l for l in log.splitlines()])) else: print('Shader %s has no report' % name) else: log = '' # report shader compilation status bufStatus = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetShaderiv(shader, bgl.GL_COMPILE_STATUS, bufStatus) if bufStatus[0] == 0: print('ERROR WHILE COMPILING SHADER %s' % name) print('\n'.join([ ' % 4d %s' % (i + 1, l) for (i, l) in enumerate(src.splitlines()) ])) assert False return log
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 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 shader_compile(shader): ''' logging and error-checking not quite working :( ''' bufLen = bgl.Buffer(bgl.GL_BYTE, 4) bufLog = bgl.Buffer(bgl.GL_BYTE, 2000) bgl.glCompileShader(shader) bgl.glGetShaderInfoLog(shader, 2000, bufLen, bufLog) log = ''.join(chr(v) for v in bufLog.to_list() if v) return log
def shader_compile(name, shader): ''' logging and error-checking not quite working :( ''' bufLen = bgl.Buffer(bgl.GL_BYTE, 4) bufLog = bgl.Buffer(bgl.GL_BYTE, 2000) bgl.glCompileShader(shader) # XXX: this test is a hack to determine whether the shader was compiled successfully # TODO: rewrite to use a more correct test (glIsShader?) bgl.glGetShaderInfoLog(shader, 2000, bufLen, bufLog) log = ''.join(chr(v) for v in bufLog.to_list() if v) assert not log and 'was successfully compiled' not in log, 'ERROR WHILE COMPILING SHADER %s: %s' % (name,log) return log
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): #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 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 shader_compile(name, shader): ''' logging and error-checking not quite working :( ''' bufLen = bgl.Buffer(bgl.GL_BYTE, 4) bufLog = bgl.Buffer(bgl.GL_BYTE, 2000) bufStatus = bgl.Buffer(bgl.GL_INT, 1) bgl.glCompileShader(shader) # get shader compilation log and status (successfully compiled?) bgl.glGetShaderiv(shader, bgl.GL_COMPILE_STATUS, bufStatus) bgl.glGetShaderInfoLog(shader, 2000, bufLen, bufLog) log = ''.join(chr(v) for v in bufLog.to_list() if v) if bufStatus[0] == 0: print('ERROR WHILE COMPILING SHADER %s' % name) print('\n'.join([' %s'%l for l in log.splitlines()])) assert False return log
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 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 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'}