Ejemplo n.º 1
0
    def paint_spikes(self, data, scale=1.0):
        """Render the given array of neuron indexes."""

        # convert the data to a VBO
        vbo = glvbo.VBO(data)
        vbo.bind()

        # tell OpenGL that the VBO contains an array of vertices
        gl.glEnableVertexAttribArray(0)

        # array is a list of unsigned ints
        gl.glVertexAttribPointer(0, 1, gl.GL_UNSIGNED_INT,
                                 gl.GL_FALSE, 0, None)

        # activate the program
        gl.glUseProgram(self.program)
        gl.glUniform1f(self.scale, scale)

        gl.glEnable(gl.GL_BLEND)
        gl.glBlendEquationSeparate(gl.GL_FUNC_ADD, gl.GL_FUNC_ADD)
        gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ONE, 
                               gl.GL_ZERO, gl.GL_ONE)

        # draw the spikes
        gl.glDrawArrays(gl.GL_POINTS, 0, len(data))

        gl.glDisable(gl.GL_BLEND)
Ejemplo n.º 2
0
def glfw3_render_gl2(antialiasing,
                     vertex_buffer_size=512 * 1024,
                     element_buffer=128 * 1024):
    # save the opengl state
    last_program = gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM)
    last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D)
    last_active_texture = gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE)
    last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING)
    last_element_array_buffer = gl.glGetIntegerv(
        gl.GL_ELEMENT_ARRAY_BUFFER_BINDING)
    last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING)
    last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC)
    last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST)
    last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB)
    last_blend_equation_alpha = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_ALPHA)
    last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)
    last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX)

    last_enable_blend = gl.glIsEnabled(gl.GL_BLEND)
    last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE)
    last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST)
    last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)

    # render nuklear
    glfw3_render_prime(antialiasing, vertex_buffer_size, element_buffer)

    # restore the opengl state
    gl.glUseProgram(last_program)
    gl.glActiveTexture(last_active_texture)
    gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture)
    gl.glBindVertexArray(last_vertex_array)
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer)
    gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer)
    gl.glBlendEquationSeparate(last_blend_equation_rgb,
                               last_blend_equation_alpha)
    gl.glBlendFunc(last_blend_src, last_blend_dst)
    if last_enable_blend:
        gl.glEnable(gl.GL_BLEND)
    else:
        gl.glDisable(gl.GL_BLEND)
    if last_enable_cull_face:
        gl.glEnable(gl.GL_CULL_FACE)
    else:
        gl.glDisable(gl.GL_CULL_FACE)
    if last_enable_depth_test:
        gl.glEnable(gl.GL_DEPTH_TEST)
    else:
        gl.glDisable(gl.GL_DEPTH_TEST)
    if last_enable_scissor_test:
        gl.glEnable(gl.GL_SCISSOR_TEST)
    else:
        gl.glDisable(gl.GL_SCISSOR_TEST)
    gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2],
                  last_viewport[3])
    gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2],
                 last_scissor_box[3])
Ejemplo n.º 3
0
 def setup_blend(self):
     GL.glEnable(GL.GL_BLEND)
     if self.use_separate_blend:
         GL.glBlendEquationSeparate(*self.blend_equation_separate)
         GL.glBlendFuncSeparate(*self.blend_func_separate)
     else:
         GL.glBlendEquation(self.blend_equation)
         GL.glBlendFunc(*self.blend_func)
     GL.glEnable(GL.GL_DEPTH_TEST)
     GL.glDepthFunc(self.depth_test)
Ejemplo n.º 4
0
def shader_test(event_coll, event):
    """Use transfer function shader"""
    print("Changing shader to projection")
    scene = event_coll.scene
    for coll in scene.collections:
        coll.set_shader("default.v")
        coll.set_shader("transfer_function.f")
    scene.set_shader("passthrough.v")
    scene.set_shader("noop.f")
    for collection in scene.collections:
        collection.set_fields_log(True)
    #scene.update_minmax()
    # https://www.opengl.org/sdk/docs/man/html/glBlendFunc.xhtml
    GL.glBlendEquationSeparate(GL.GL_FUNC_ADD, GL.GL_FUNC_ADD)
    GL.glBlendFuncSeparate(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA, GL.GL_ONE,
                           GL.GL_ZERO)
    return True
Ejemplo n.º 5
0
def glfw3_render(antialiasing,
                 vertex_buffer_size=512 * 1024,
                 element_buffer=128 * 1024):
    # save the opengl state
    last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC)
    last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST)
    last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB)
    last_blend_equation_alpha = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_ALPHA)
    last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)
    last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX)

    last_enable_blend = gl.glIsEnabled(gl.GL_BLEND)
    last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE)
    last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST)
    last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)

    # render nuklear
    glfw3_render_prime(antialiasing, vertex_buffer_size, element_buffer)

    # restore the opengl state
    gl.glBlendEquationSeparate(last_blend_equation_rgb,
                               last_blend_equation_alpha)
    gl.glBlendFunc(last_blend_src, last_blend_dst)
    if last_enable_blend:
        gl.glEnable(gl.GL_BLEND)
    else:
        gl.glDisable(gl.GL_BLEND)
    if last_enable_cull_face:
        gl.glEnable(gl.GL_CULL_FACE)
    else:
        gl.glDisable(gl.GL_CULL_FACE)
    if last_enable_depth_test:
        gl.glEnable(gl.GL_DEPTH_TEST)
    else:
        gl.glDisable(gl.GL_DEPTH_TEST)
    if last_enable_scissor_test:
        gl.glEnable(gl.GL_SCISSOR_TEST)
    else:
        gl.glDisable(gl.GL_SCISSOR_TEST)
    gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2],
                  last_viewport[3])
    gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2],
                 last_scissor_box[3])
Ejemplo n.º 6
0
    def render(self, draw_data):
        # perf: local for faster access
        io = self.io

        display_width, display_height = self.io.display_size
        fb_width = int(display_width * io.display_fb_scale[0])
        fb_height = int(display_height * io.display_fb_scale[1])

        if fb_width == 0 or fb_height == 0:
            return

        draw_data.scale_clip_rects(*io.display_fb_scale)

        # backup GL state
        # todo: provide cleaner version of this backup-restore code
        last_program = gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM)
        last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D)
        last_active_texture = gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE)
        last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING)
        last_element_array_buffer = gl.glGetIntegerv(
            gl.GL_ELEMENT_ARRAY_BUFFER_BINDING)
        last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING)
        last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC)
        last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST)
        last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB)
        last_blend_equation_alpha = gl.glGetIntegerv(
            gl.GL_BLEND_EQUATION_ALPHA)
        last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)
        last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX)
        last_enable_blend = gl.glIsEnabled(gl.GL_BLEND)
        last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE)
        last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST)
        last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)

        gl.glEnable(gl.GL_BLEND)
        gl.glBlendEquation(gl.GL_FUNC_ADD)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glDisable(gl.GL_CULL_FACE)
        gl.glDisable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_SCISSOR_TEST)
        gl.glActiveTexture(gl.GL_TEXTURE0)

        gl.glViewport(0, 0, int(fb_width), int(fb_height))

        ortho_projection = [[2.0 / display_width, 0.0, 0.0, 0.0],
                            [0.0, 2.0 / -display_height, 0.0, 0.0],
                            [0.0, 0.0, -1.0, 0.0], [-1.0, 1.0, 0.0, 1.0]]

        gl.glUseProgram(self._shader_handle)
        gl.glUniform1i(self._attrib_location_tex, 0)
        gl.glUniformMatrix4fv(self._attrib_proj_mtx, 1, gl.GL_FALSE,
                              ortho_projection)
        gl.glBindVertexArray(self._vao_handle)

        for commands in draw_data.commands_lists:
            idx_buffer_offset = 0

            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle)
            # todo: check this (sizes)
            gl.glBufferData(gl.GL_ARRAY_BUFFER,
                            commands.vtx_buffer_size * imgui.VERTEX_SIZE,
                            ctypes.c_void_p(commands.vtx_buffer_data),
                            gl.GL_STREAM_DRAW)

            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._elements_handle)
            # todo: check this (sizes)
            gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER,
                            commands.idx_buffer_size * imgui.INDEX_SIZE,
                            ctypes.c_void_p(commands.idx_buffer_data),
                            gl.GL_STREAM_DRAW)

            # todo: allow to iterate over _CmdList
            for command in commands.commands:
                gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id)

                # todo: use named tuple
                x, y, w, z = command.clip_rect
                gl.glScissor(int(x), int(fb_height - w), int(z - x),
                             int(w - y))

                if imgui.INDEX_SIZE == 2:
                    gltype = gl.GL_UNSIGNED_SHORT
                else:
                    gltype = gl.GL_UNSIGNED_INT

                gl.glDrawElements(gl.GL_TRIANGLES, command.elem_count, gltype,
                                  ctypes.c_void_p(idx_buffer_offset))

                idx_buffer_offset += command.elem_count * imgui.INDEX_SIZE

        # restore modified GL state
        gl.glUseProgram(last_program)
        gl.glActiveTexture(last_active_texture)
        gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture)
        gl.glBindVertexArray(last_vertex_array)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer)
        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer)
        gl.glBlendEquationSeparate(last_blend_equation_rgb,
                                   last_blend_equation_alpha)
        gl.glBlendFunc(last_blend_src, last_blend_dst)

        if last_enable_blend:
            gl.glEnable(gl.GL_BLEND)
        else:
            gl.glDisable(gl.GL_BLEND)

        if last_enable_cull_face:
            gl.glEnable(gl.GL_CULL_FACE)
        else:
            gl.glDisable(gl.GL_CULL_FACE)

        if last_enable_depth_test:
            gl.glEnable(gl.GL_DEPTH_TEST)
        else:
            gl.glDisable(gl.GL_DEPTH_TEST)

        if last_enable_scissor_test:
            gl.glEnable(gl.GL_SCISSOR_TEST)
        else:
            gl.glDisable(gl.GL_SCISSOR_TEST)

        gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2],
                      last_viewport[3])
        gl.glScissor(last_scissor_box[0], last_scissor_box[1],
                     last_scissor_box[2], last_scissor_box[3])
Ejemplo n.º 7
0
    def render(self, draw_data):
        # perf: local for faster access
        io = self.io

        display_width, display_height = io.display_size
        fb_width = int(display_width * io.display_fb_scale[0])
        fb_height = int(display_height * io.display_fb_scale[1])

        if fb_width == 0 or fb_height == 0:
            return

        draw_data.scale_clip_rects(*io.display_fb_scale)

        # note: we are using fixed pipeline for cocos2d/pyglet
        # todo: consider porting to programmable pipeline
        # backup gl state
        last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D)
        last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)
        last_enable_blend = gl.glIsEnabled(gl.GL_BLEND)
        last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE)
        last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST)
        last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
        last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX)
        last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC)
        last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST)
        last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB)
        last_blend_equation_alpha = gl.glGetIntegerv(
            gl.GL_BLEND_EQUATION_ALPHA)

        gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_COLOR_BUFFER_BIT
                        | gl.GL_TRANSFORM_BIT)
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glDisable(gl.GL_CULL_FACE)
        gl.glDisable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_SCISSOR_TEST)

        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        gl.glEnable(gl.GL_TEXTURE_2D)

        gl.glViewport(0, 0, int(fb_width), int(fb_height))
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glPushMatrix()
        gl.glLoadIdentity()
        gl.glOrtho(0, io.display_size.x, io.display_size.y, 0.0, -1., 1.)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glPushMatrix()
        gl.glLoadIdentity()

        for commands in draw_data.commands_lists:
            idx_buffer = commands.idx_buffer_data

            gl.glVertexPointer(
                2, gl.GL_FLOAT, imgui.VERTEX_SIZE,
                ctypes.c_void_p(commands.vtx_buffer_data +
                                imgui.VERTEX_BUFFER_POS_OFFSET))
            gl.glTexCoordPointer(
                2, gl.GL_FLOAT, imgui.VERTEX_SIZE,
                ctypes.c_void_p(commands.vtx_buffer_data +
                                imgui.VERTEX_BUFFER_UV_OFFSET))
            gl.glColorPointer(
                4, gl.GL_UNSIGNED_BYTE, imgui.VERTEX_SIZE,
                ctypes.c_void_p(commands.vtx_buffer_data +
                                imgui.VERTEX_BUFFER_COL_OFFSET))

            for command in commands.commands:
                gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id)

                x, y, z, w = command.clip_rect
                gl.glScissor(int(x), int(fb_height - w), int(z - x),
                             int(w - y))

                if imgui.INDEX_SIZE == 2:
                    gltype = gl.GL_UNSIGNED_SHORT
                else:
                    gltype = gl.GL_UNSIGNED_INT

                gl.glDrawElements(gl.GL_TRIANGLES, command.elem_count, gltype,
                                  ctypes.c_void_p(idx_buffer))

                idx_buffer += (command.elem_count * imgui.INDEX_SIZE)

        gl.glBlendEquationSeparate(last_blend_equation_rgb,
                                   last_blend_equation_alpha)
        gl.glBlendFunc(last_blend_src, last_blend_dst)

        if last_enable_blend:
            gl.glEnable(gl.GL_BLEND)
        else:
            gl.glDisable(gl.GL_BLEND)

        if last_enable_cull_face:
            gl.glEnable(gl.GL_CULL_FACE)
        else:
            gl.glDisable(gl.GL_CULL_FACE)

        if last_enable_depth_test:
            gl.glEnable(gl.GL_DEPTH_TEST)
        else:
            gl.glDisable(gl.GL_DEPTH_TEST)

        if last_enable_scissor_test:
            gl.glEnable(gl.GL_SCISSOR_TEST)
        else:
            gl.glDisable(gl.GL_SCISSOR_TEST)

        gl.glScissor(last_scissor_box[0], last_scissor_box[1],
                     last_scissor_box[2], last_scissor_box[3])

        gl.glDisableClientState(gl.GL_COLOR_ARRAY)
        gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

        if last_texture:
            gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture)

        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glPopMatrix()
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glPopMatrix()
        gl.glPopAttrib()

        gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2],
                      last_viewport[3])