示例#1
0
def createProgram(vertexShader, fragmentShader):
    program = bgl.glCreateProgram()
    bgl.glAttachShader(program, vertexShader)
    bgl.glAttachShader(program, fragmentShader)
    bgl.glLinkProgram(program)
    success = bgl.Buffer(bgl.GL_INT, [1])
    bgl.glGetProgramiv(program, bgl.GL_LINK_STATUS, success)
    #var success = gl.getProgramParameter(program, gl.LINK_STATUS);
    if (success[0] == bgl.GL_TRUE):
        print("shader link success")
        return program

    print("shader program linking failed")
    
    bgl.glGetProgramiv(program, bgl.GL_INFO_LOG_LENGTH, success)
    start = bgl.Buffer(bgl.GL_INT, [1])
    start[0] =0
    log = bgl.Buffer(bgl.GL_BYTE, [success[0]])
    bgl.glGetProgramInfoLog(program, success[0], 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.glDeleteProgram(program)
示例#2
0
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)
示例#3
0
def create_program(*shaders):
    program = bgl.glCreateProgram()
    for shader in shaders:
        bgl.glAttachShader(program, shader)
    bgl.glLinkProgram(program)
    status = bgl.Buffer(bgl.GL_INT, 1)
    bgl.glGetProgramiv(program, bgl.GL_LINK_STATUS, status)
    if status[0] != bgl.GL_TRUE:
        infoLog = bgl.Buffer(bgl.GL_BYTE, 1024)
        length = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetProgramInfoLog(program, 1024, length, infoLog)
        raise RuntimeError(''.join(chr(infoLog[i]) for i in range(length[0])))
    return program
示例#4
0
    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
示例#5
0
def gl_link_error(msg, program):
    """
    Print OpenGL link errors, if any, for a given program.
    
    msg is an rbitrary human readable string
    program is an int (an OpenGL program name)
    """
    buf = bgl.Buffer(bgl.GL_INT,1)
    bgl.glGetProgramiv(program, bgl.GL_LINK_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.glGetProgramInfoLog(program,4000,lenbuf,charbuf)
    print('GL Error: %d (%s)\n%s'%(err, msg, "".join(map( chr, charbuf[:lenbuf[0]]))))
    return True
示例#6
0
def dump_attributes_and_uniforms(program):
    """
    Print a list of all active attributes and uniforms of a linked program.
    
    program is an int (an OpenGL program name)
    """
    count = bgl.Buffer(bgl.GL_INT,1)
    size = bgl.Buffer(bgl.GL_INT,1)
    atype = bgl.Buffer(bgl.GL_INT,1)
    length = bgl.Buffer(bgl.GL_INT,1)
    name = bgl.Buffer(bgl.GL_BYTE, 128)

    print("active attributes")
    bgl.glGetProgramiv(program,bgl.GL_ACTIVE_ATTRIBUTES, count)
    for i in range(count[0]):
        bgl.glGetActiveAttrib(program, i, 128, length, size, atype, name)
        print([k for k,v in vars(bgl).items() if v == atype[0]], "".join([chr(c) for c in name.to_list()[:length[0]] if c > 0]), "[%d]"%(size[0],) if size[0]>1 else "")
    print("active uniforms")
    bgl.glGetProgramiv(program,bgl.GL_ACTIVE_UNIFORMS, count)
    for i in range(count[0]):
        bgl.glGetActiveUniform(program, i, 128, length, size, atype, name)
        print([k for k,v in vars(bgl).items() if v == atype[0]], "".join([chr(c) for c in name.to_list()[:length[0]] if c > 0]), "[%d]"%(size[0],) if size[0]>1 else "")
示例#7
0
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))
示例#8
0
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))
示例#9
0
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))
示例#10
0
    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'}