def check_shaderError(shader, flag, isProgram, errorMessage): success = bgl.Buffer(bgl.GL_INT, 1) slen = 1024 if isProgram: bgl.glGetProgramiv(shader, flag, success) check_error("glGetProgramiv") else: bgl.glGetShaderiv(shader, flag, success) check_error("glGetShaderiv") import numpy as np from .bgl_ext import VoidBufValue offset = VoidBufValue(None) error = bgl.Buffer(bgl.GL_BYTE, slen) if isProgram: bgl.glGetProgramInfoLog(shader, slen, offset.buf, error) check_error("glGetProgramInfoLog") else: bgl.glGetShaderInfoLog(shader, slen, offset.buf, error) check_error("glGetShaderInfoLog") print(np.bytes_(error).decode("utf-8")) del offset if success[0] != bgl.GL_TRUE: print(errorMessage, np.bytes_(error).decode("utf-8")) raise RuntimeError(errorMessage, error)
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 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 __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 gl_compile_error(program_name, shader_name, shader): """ Print OpenGL compilation errors, if any, for a given shader. program_name and shader_name are arbitrary human readable strings. shader is an int (an OpenGL shader name) """ buf = bgl.Buffer(bgl.GL_INT,1) bgl.glGetShaderiv(shader,bgl.GL_COMPILE_STATUS,buf) if buf[0] == bgl.GL_TRUE: return False err = buf[0] charbuf = bgl.Buffer(bgl.GL_BYTE,4000) lenbuf = bgl.Buffer(bgl.GL_INT,1) bgl.glGetShaderInfoLog(shader,4000,lenbuf,charbuf) print('GL Error: %d (%s:%s)\n%s'%(err, program_name, shader_name, "".join(map( chr, charbuf[:lenbuf[0]])))) return True
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 check_shaderError(shader, flag, isProgram, errorMessage): success = bgl.Buffer(bgl.GL_INT, 1) if isProgram: bgl.glGetProgramiv(shader, flag, success) else: bgl.glGetShaderiv(shader, flag, success) if success[0] == bgl.GL_FALSE: import numpy as np import ctypes offset = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int * 1).from_address(0)) error = bgl.Buffer(bgl.GL_BYTE, 1024) if isProgram: bgl.glGetProgramInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) else: bgl.glGetShaderInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) del offset raise #RuntimeError(errorMessage, bgl.glGetShaderInfoLog(shader))
def check_shaderError(shader, flag, isProgram, errorMessage): success = bgl.Buffer(bgl.GL_INT, 1) if isProgram: bgl.glGetProgramiv(shader, flag, success) else: bgl.glGetShaderiv(shader, flag, success) if success[0] == bgl.GL_FALSE: import numpy as np from .bgl_ext import VoidBufValue offset = VoidBufValue(None) error = bgl.Buffer(bgl.GL_BYTE, 1024) if isProgram: bgl.glGetProgramInfoLog(shader, 1024, offset.buf, error) print(errorMessage, np.bytes_(error).decode("utf-8")) else: bgl.glGetShaderInfoLog(shader, 1024, offset.buf, error) print(errorMessage, np.bytes_(error).decode("utf-8")) del offset raise #RuntimeError(errorMessage, bgl.glGetShaderInfoLog(shader))
def check_shaderError(shader, flag, isProgram, errorMessage): success = bgl.Buffer(bgl.GL_INT, 1) if isProgram: bgl.glGetProgramiv(shader, flag, success) else: bgl.glGetShaderiv(shader, flag, success) if success[0] == bgl.GL_FALSE: import numpy as np import ctypes offset = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0)) error = bgl.Buffer(bgl.GL_BYTE, 1024) if isProgram: bgl.glGetProgramInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) else: bgl.glGetShaderInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) del offset raise #RuntimeError(errorMessage, bgl.glGetShaderInfoLog(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'}