Esempio n. 1
0
 def set_state(self):
     """
     Enables a scissor test on our region
     """
     gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT | gl.GL_CURRENT_BIT)
     self.was_scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
     gl.glEnable(gl.GL_SCISSOR_TEST)
     gl.glScissor(int(self.x), int(self.y), int(self.width), int(self.height))
Esempio n. 2
0
def disabled(*gl_flags):
    disabled = set()
    for f in gl_flags:
        if gl.glIsEnabled(f):
            disabled.add(f)
            gl.glDisable(f)
    yield
    for f in disabled:
        gl.glEnable(f)
Esempio n. 3
0
def enabled(*gl_flags):
    enabled = set()
    for f in gl_flags:
        if not gl.glIsEnabled(f):
            enabled.add(f)
            gl.glEnable(f)
    yield
    for f in enabled:
        gl.glDisable(f)
Esempio n. 4
0
 def set_state(self):
     """
     Enables a scissor test on our region
     """
     gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT
                     | gl.GL_CURRENT_BIT)
     self.was_scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
     gl.glEnable(gl.GL_SCISSOR_TEST)
     gl.glScissor(int(self.x), int(self.y), int(self.width),
                  int(self.height))
Esempio n. 5
0
    def set_state(self):
        # preserve gl scissors info
        self._scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
        self._old_scissor_flat = (gl.GLint * 4)()  # 4-tuple
        gl.glGetIntegerv(gl.GL_SCISSOR_BOX, self._old_scissor_flat)

        # set our scissor
        if not self._scissor_enabled:
            gl.glEnable(gl.GL_SCISSOR_TEST)

        gl.glScissor(*self._scissor_flat)
Esempio n. 6
0
    def set_state(self):
        # preserve gl scissors info
        self._scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
        self._old_scissor_flat = (gl.GLint * 4)()  # 4-tuple
        gl.glGetIntegerv(gl.GL_SCISSOR_BOX, self._old_scissor_flat)

        # set our scissor
        if not self._scissor_enabled:
            gl.glEnable(gl.GL_SCISSOR_TEST)

        gl.glScissor(*self._scissor_flat)
Esempio n. 7
0
    def set_state(self):
        '''
        Enables a scissor test on our region
        '''
        gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT |
                        gl.GL_CURRENT_BIT)
        self.was_scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
        gl.glEnable(gl.GL_SCISSOR_TEST)
        gl.glScissor(int(self.x), int(self.y),
                     int(self.width), int(self.height))

        if self._scale != 1.0:
            gl.glPushMatrix(gl.GL_MODELVIEW_MATRIX)
            gl.glScalef(self._scale,self._scale,1.0)
Esempio n. 8
0
def drawTree(tree, x, y, height, width, r, g, b, a, angle=0):
    """Draw a tree recursively
        
    Args:
        tree: The Tree to be drawn
        x: A number indicating the x position of the tree's base's middle in
            the window
        y: A number indicating the y position of the tree's base's middle in
            the window's coordinate
        height: the height of the tree's trunk in window's coordinate
        width: the width of the tree's trunk in window's coordinate
        r: A integer in the range [0, 255] representing the red component of
            the tree's color
        g: A integer in the range [0, 255] representing the green component of
            the tree's color
        b: A integer in the range [0, 255] representing the blue component of
            the tree's color
        a: A integer in the range [0, 255] representing the alpha component of
            the tree's color
        angle: A float indicating the angle in radiant the trunk of the tree 
            make with the window's bottom side
    """

    if not glIsEnabled(GL_BLEND):
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    #Vector indicating the up direction of the tree
    dir_x = sin(angle)
    dir_y = cos(angle)

	#Center the Tree on the coordinate
    drawOrientedRectangle(x - width * dir_y / 2, y + width * dir_x / 2, 
    	dir_x, dir_y, width, height, r, g, b, a)

    for branch in tree.branches:
        drawTree(branch.tree, 
            x + dir_x * branch.height * height, y + dir_y * branch.height * height,
            height * branch.ratio, width * branch.ratio,
            r, g, b, a,
            angle + branch.angle)
Esempio n. 9
0
        length: The maximum length of the branches that will be drawn
        x: A number indicating the x position of the tree's base's middle in
            the window
        y: A number indicating the y position of the tree's base's middle in
            the window's coordinate
        height: the height of the tree's trunk in window's coordinate
        width: the width of the tree's trunk in window's coordinate
        colors: A SuccesiveInterval object that link some colors to interval of
        	color length.
        default: The color used when no interval are for found for a given
        	length
        angle: A float indicating the angle in radiant the trunk of the tree 
            make with the window's bottom side
	"""

    if not glIsEnabled(GL_BLEND):
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    #Vector indicating the up direction of the tree
    dir_x = sin(angle)
    dir_y = cos(angle)

	#Center the Tree on the coordinate
    drawOrientedRectangle(x - width * dir_y / 2, y + width * dir_x / 2, 
    	dir_x, dir_y, width, height, *colors.get(len(tree), default))

    for branch in tree.branches:
        drawTreeMultiColor(branch.tree, 
            x + dir_x * branch.height * height, y + dir_y * branch.height * height,
            height * branch.ratio, width * branch.ratio,
Esempio n. 10
0
 def _enable_scissor_test(self):
     gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT | gl.GL_CURRENT_BIT)
     self._was_scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST)
     gl.glEnable(gl.GL_SCISSOR_TEST)
     gl.glScissor(int(self.x), int(self.y), int(self.width), int(self.height))
Esempio n. 11
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)

        # backup GL state
        # todo: provide cleaner version of this backup-restore code
        last_program = gl.GLint()
        gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM, byref(last_program))
        last_texture = gl.GLint()
        gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D, byref(last_texture))
        last_active_texture = gl.GLint()
        gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE, byref(last_active_texture))
        last_array_buffer = gl.GLint()
        gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING, byref(last_array_buffer))
        last_element_array_buffer = gl.GLint()
        gl.glGetIntegerv(gl.GL_ELEMENT_ARRAY_BUFFER_BINDING, byref(last_element_array_buffer))
        last_vertex_array = gl.GLint()
        gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING, byref(last_vertex_array))
        last_blend_src = gl.GLint()
        gl.glGetIntegerv(gl.GL_BLEND_SRC, byref(last_blend_src))
        last_blend_dst = gl.GLint()
        gl.glGetIntegerv(gl.GL_BLEND_DST, byref(last_blend_dst))
        last_blend_equation_rgb = gl.GLint()
        gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB, byref(last_blend_equation_rgb))
        last_blend_equation_alpha = gl.GLint()
        gl.glGetIntegerv(gl.GL_BLEND_EQUATION_ALPHA, byref(last_blend_equation_alpha))
        last_viewport = (gl.GLint*4)()
        gl.glGetIntegerv(gl.GL_VIEWPORT, last_viewport)
        last_scissor_box = (gl.GLint*4)()
        gl.glGetIntegerv(gl.GL_SCISSOR_BOX, last_scissor_box)
        last_enable_blend = gl.GLint()
        gl.glIsEnabled(gl.GL_BLEND, byref(last_enable_blend))
        last_enable_cull_face = gl.GLint()
        gl.glIsEnabled(gl.GL_CULL_FACE, byref(last_enable_cull_face))
        last_enable_depth_test = gl.GLint()
        gl.glIsEnabled(gl.GL_DEPTH_TEST, byref(last_enable_depth_test))
        last_enable_scissor_test = gl.GLint()
        gl.glIsEnabled(gl.GL_SCISSOR_TEST, byref(last_enable_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, (gl.GLfloat * 16)(*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, 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, 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, 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, c_void_p(idx_buffer_offset))

                idx_buffer_offset += command.elem_count * imgui.INDEX_SIZE

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

        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])