예제 #1
0
    def __init__(self):
        pts = generate_cyl_points().T

        colors = np.zeros_like(pts)
        colors[:,1]=1.0 # all green

        n_pts = len(pts)
        pts = map(float,pts.flat) # convert to flat list of floats
        colors = map(float, colors.flat)

        # Create ctypes arrays of the lists
        vertices = (gl.GLfloat * (n_pts*3))(*pts)
        colors = (gl.GLfloat * (n_pts*3))(*colors)

        # Create a list of triangle indices.
        indices = range(n_pts)
        indices = (gl.GLuint * n_pts)(*indices)

        # Compile a display list
        self.list = gl.glGenLists(1)
        gl.glNewList(self.list, gl.GL_COMPILE)

        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices)
        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        gl.glColorPointer(3, gl.GL_FLOAT, 0, colors)
        gl.glDrawElements(gl.GL_POINTS, len(indices), gl.GL_UNSIGNED_INT, indices)
        gl.glPopClientAttrib()

        gl.glEndList()
예제 #2
0
    def draw(self, win=None):
        """Draw the current frame to a particular visual.Window (or to the
        default win for this object if not specified). The current
        position in the movie will be determined automatically.

        This method should be called on every frame that the movie is
        meant to appear.
        """

        if (self.status == NOT_STARTED or
                (self.status == FINISHED and self.loop)):
            self.play()
        elif self.status == FINISHED and not self.loop:
            return
        if win is None:
            win = self.win
        self._selectWindow(win)
        self._updateFrameTexture()  # will check if it's needed

        # scale the drawing frame and get to centre of field
        GL.glPushMatrix()  # push before drawing, pop after
        # push the data for client attributes
        GL.glPushClientAttrib(GL.GL_CLIENT_ALL_ATTRIB_BITS)

        self.win.setScale('pix')
        # move to centre of stimulus and rotate
        vertsPix = self.verticesPix

        # bind textures
        GL.glActiveTexture(GL.GL_TEXTURE1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glEnable(GL.GL_TEXTURE_2D)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self._texID)
        GL.glEnable(GL.GL_TEXTURE_2D)

        # sets opacity (1,1,1 = RGB placeholder)
        GL.glColor4f(1, 1, 1, self.opacity)

        array = (GL.GLfloat * 32)(
            1, 1,  # texture coords
            vertsPix[0, 0], vertsPix[0, 1], 0.,  # vertex
            0, 1,
            vertsPix[1, 0], vertsPix[1, 1], 0.,
            0, 0,
            vertsPix[2, 0], vertsPix[2, 1], 0.,
            1, 0,
            vertsPix[3, 0], vertsPix[3, 1], 0.,
        )

        # 2D texture array, 3D vertex array
        GL.glInterleavedArrays(GL.GL_T2F_V3F, 0, array)
        GL.glDrawArrays(GL.GL_QUADS, 0, 4)
        GL.glPopClientAttrib()
        GL.glPopAttrib()
        GL.glPopMatrix()
        # unbind the textures
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glEnable(GL.GL_TEXTURE_2D)  # implicitly disables 1D
예제 #3
0
    def draw(self, win=None):
        """Draw the current frame to a particular visual.Window (or to the
        default win for this object if not specified). The current
        position in the movie will be determined automatically.

        This method should be called on every frame that the movie is
        meant to appear.
        """

        if (self.status == NOT_STARTED or
                (self.status == FINISHED and self.loop)):
            self.play()
        elif self.status == FINISHED and not self.loop:
            return
        if win is None:
            win = self.win
        self._selectWindow(win)
        self._updateFrameTexture()  # will check if it's needed

        # scale the drawing frame and get to centre of field
        GL.glPushMatrix()  # push before drawing, pop after
        # push the data for client attributes
        GL.glPushClientAttrib(GL.GL_CLIENT_ALL_ATTRIB_BITS)

        self.win.setScale('pix')
        # move to centre of stimulus and rotate
        vertsPix = self.verticesPix

        # bind textures
        GL.glActiveTexture(GL.GL_TEXTURE1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glEnable(GL.GL_TEXTURE_2D)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self._texID)
        GL.glEnable(GL.GL_TEXTURE_2D)

        # sets opacity (1,1,1 = RGB placeholder)
        GL.glColor4f(1, 1, 1, self.opacity)

        array = (GL.GLfloat * 32)(
            1, 1,  # texture coords
            vertsPix[0, 0], vertsPix[0, 1], 0.,  # vertex
            0, 1,
            vertsPix[1, 0], vertsPix[1, 1], 0.,
            0, 0,
            vertsPix[2, 0], vertsPix[2, 1], 0.,
            1, 0,
            vertsPix[3, 0], vertsPix[3, 1], 0.,
        )

        # 2D texture array, 3D vertex array
        GL.glInterleavedArrays(GL.GL_T2F_V3F, 0, array)
        GL.glDrawArrays(GL.GL_QUADS, 0, 4)
        GL.glPopClientAttrib()
        GL.glPopAttrib()
        GL.glPopMatrix()
        # unbind the textures
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glEnable(GL.GL_TEXTURE_2D)  # implicitly disables 1D
예제 #4
0
    def __init__(self):
        pts = generate_cyl_points().T

        colors = np.zeros_like(pts)
        colors[:,1]=1.0 # all green

        n_pts = len(pts)
        pts = map(float,pts.flat) # convert to flat list of floats
        colors = map(float, colors.flat)

        # Create ctypes arrays of the lists
        vertices = (gl.GLfloat * (n_pts*3))(*pts)
        colors = (gl.GLfloat * (n_pts*3))(*colors)

        # Create a list of triangle indices.
        indices = range(n_pts)
        indices = (gl.GLuint * n_pts)(*indices)

        # Compile a display list
        self.list = gl.glGenLists(1)
        gl.glNewList(self.list, gl.GL_COMPILE)

        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices)
        gl.glEnableClientState(gl.GL_COLOR_ARRAY)
        gl.glColorPointer(3, gl.GL_FLOAT, 0, colors)
        gl.glDrawElements(gl.GL_LINES, len(indices), gl.GL_UNSIGNED_INT, indices)
        gl.glPopClientAttrib()

        gl.glEndList()
예제 #5
0
    def batch_draw(csl):
        glPushMatrix()
        glLoadIdentity()

        vertices = []
        for cs in csl:
            ax, ay = cs.abs_coords()
            if cs.ft_anim:
                qs = cs.question_scale
                bs = cs.back_scale
                aa = cs.ftanim_alpha
                ca = cs.ftanim_cardalpha
                if cs.gray:
                    c = (.66, .66, .66, ca)
                else:
                    c = (1., 1., 1., ca)
                vertices += cs.img.get_t2c4n3v3_vertices(c, ax, ay)

                n, s = cs.number, cs.suit
                if n: vertices += game_res.cardnum[s % 2 * 13 + n - 1].get_t2c4n3v3_vertices(c, ax + 5, ay + 105)
                if s: vertices += game_res.suit[s - 1].get_t2c4n3v3_vertices(c, ax + 6, ay + 94)

                c = (1, 1, 1, aa)

                if qs:
                    vertices += game_res.card_question.get_t2c4n3v3_vertices(c, ax+(1-qs)*45, ay, 0, qs*91)

                if bs:
                    vertices += game_res.card_hidden.get_t2c4n3v3_vertices(c, ax+(1-bs)*45, ay, 0, bs*91)
            else:
                a = cs.alpha
                if cs.gray:
                    c = (.66, .66, .66, a)
                else:
                    c = (1., 1., 1., a)
                vertices += cs.img.get_t2c4n3v3_vertices(c, ax, ay)
                resides_in = cs.card.resides_in
                if resides_in and resides_in.type == 'showncards':
                    vertices += game_res.card_showncardtag.get_t2c4n3v3_vertices(c, ax, ay)

                n, s = cs.number, cs.suit
                if n: vertices += game_res.cardnum[s % 2 * 13 + n - 1].get_t2c4n3v3_vertices(c, ax + 5, ay + 105)
                if s: vertices += game_res.suit[s-1].get_t2c4n3v3_vertices(c, ax+6, ay+94)

                vertices += game_res.card_shinesoft.get_t2c4n3v3_vertices(
                    (1., 1., 1., cs.shine_alpha), ax-6, ay-6
                )

        if vertices:
            n = len(vertices)
            buf = (GLfloat*n)()
            buf[:] = vertices
            glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
            glInterleavedArrays(GL_T2F_C4F_N3F_V3F, 0, buf)
            with get_atlas('card').texture:
                glDrawArrays(GL_QUADS, 0, n/12)
            glPopClientAttrib()

        glPopMatrix()
예제 #6
0
    def __init__(self, radius, inner_radius, slices, inner_slices):
        # Create the vertex and normal arrays.
        vertices = []
        normals = []

        u_step = 2 * math.pi / (slices - 1)
        v_step = 2 * math.pi / (inner_slices - 1)
        u = 0.
        for i in range(slices):
            cos_u = math.cos(u)
            sin_u = math.sin(u)
            v = 0.
            for j in range(inner_slices):
                cos_v = math.cos(v)
                sin_v = math.sin(v)

                d = (radius + inner_radius * cos_v)
                x = d * cos_u
                y = d * sin_u
                z = inner_radius * sin_v

                nx = cos_u * cos_v
                ny = sin_u * cos_v
                nz = sin_v

                vertices.extend([x, y, z])
                normals.extend([nx, ny, nz])
                v += v_step
            u += u_step

        # Create ctypes arrays of the lists
        vertices = (gl.GLfloat * len(vertices))(*vertices)
        normals = (gl.GLfloat * len(normals))(*normals)

        # Create a list of triangle indices.
        indices = []
        for i in range(slices - 1):
            for j in range(inner_slices - 1):
                p = i * inner_slices + j
                indices.extend([p, p + inner_slices, p + inner_slices + 1])
                indices.extend([p, p + inner_slices + 1, p + 1])
        indices = (gl.GLuint * len(indices))(*indices)

        # Compile a display list
        self.list = gl.glGenLists(1)
        gl.glNewList(self.list, gl.GL_COMPILE)

        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glEnableClientState(gl.GL_NORMAL_ARRAY)
        gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices)
        gl.glNormalPointer(gl.GL_FLOAT, 0, normals)
        gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT,
                          indices)
        gl.glPopClientAttrib()

        gl.glEndList()
예제 #7
0
    def __init__(self, radius, inner_radius, slices, inner_slices):
        # Create the vertex and normal arrays.
        vertices = []
        normals = []

        u_step = 2 * pi / (slices - 1)
        v_step = 2 * pi / (inner_slices - 1)
        u = 0.
        for i in range(slices):
            cos_u = cos(u)
            sin_u = sin(u)
            v = 0.
            for j in range(inner_slices):
                cos_v = cos(v)
                sin_v = sin(v)

                d = (radius + inner_radius * cos_v)
                x = d * cos_u
                y = d * sin_u
                z = inner_radius * sin_v

                nx = cos_u * cos_v
                ny = sin_u * cos_v
                nz = sin_v

                vertices.extend([x, y, z])
                normals.extend([nx, ny, nz])
                v += v_step
            u += u_step

        # Create ctypes arrays of the lists
        vertices = (GLfloat * len(vertices))(*vertices)
        normals = (GLfloat * len(normals))(*normals)

        # Create a list of triangle indices.
        indices = []
        for i in range(slices - 1):
            for j in range(inner_slices - 1):
                p = i * inner_slices + j
                indices.extend([p, p + inner_slices, p + inner_slices + 1])
                indices.extend([p, p + inner_slices + 1, p + 1])
        indices = (GLuint * len(indices))(*indices)

        # Compile a display list
        self.list = glGenLists(1)
        glNewList(self.list, GL_COMPILE)

        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glEnableClientState(GL_VERTEX_ARRAY)
        glEnableClientState(GL_NORMAL_ARRAY)
        glVertexPointer(3, GL_FLOAT, 0, vertices)
        glNormalPointer(GL_FLOAT, 0, normals)
        glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices)
        glPopClientAttrib()

        glEndList()
예제 #8
0
    def batch_draw_status(gcps):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        vertices = []
        for port in gcps:
            p = port.player
            if not getattr(p, 'ui_meta', False): continue

            hp = game_res.hp; hp_bg = game_res.hp_bg
            if getattr(p, 'dead', False):
                hp = hp.grayed
                hp_bg = hp_bg.grayed

            # hp bar
            w = hp.width
            x = port.x; y = port.y
            for i in xrange(getattr(p, 'maxlife', 0)):
                vertices.extend(
                    hp_bg.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

            for i in xrange(max(getattr(p, 'life', 0), 0)):
                vertices.extend(
                    hp.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

        nums = game_res.num
        for port in gcps:
            x, y, w, h = port.x, port.y, port.width, port.height
            p = port.player
            try:
                n = len(p.cards) + len(p.showncards)
                seq = str(n)
                ox = (32 - len(seq)*14)//2
                for i, ch in enumerate(seq):
                    n = ord(ch) - ord('0')
                    #x, y = w - 34 + ox + i*14, 68
                    vertices.extend(nums[n].get_t4f_v4f_vertices(
                        x + w - 34 + ox + i*14,
                        y + 68
                    ))
            except AttributeError:
                pass

        if vertices:
            with nums[0].owner:
                n = len(vertices)
                buf = (GLfloat*n)()
                buf[:] = vertices
                glInterleavedArrays(GL_T4F_V4F, 0, buf)
                glDrawArrays(GL_QUADS, 0, n/8)

        glPopClientAttrib()
예제 #9
0
    def batch_draw_status(gcps):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        vertices = []
        for port in gcps:
            char = port.character
            if not char: continue

            hp, hp_bg = L('thb-hp'), L('thb-hp_bg')
            if char.dead:
                hp = hp.grayed
                hp_bg = hp_bg.grayed

            # hp bar
            w = hp.width
            x, y = port.x, port.y
            for i in xrange(char.maxlife):
                vertices.extend(
                    hp_bg.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

            for i in xrange(max(char.life, 0)):
                vertices.extend(
                    hp.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

        nums = L('thb-num')
        for port in gcps:
            x, y, w = port.x, port.y, port.width
            char = port.character
            if not char: continue

            n = len(char.cards) + len(char.showncards)
            seq = str(n)
            ox = (32 - len(seq)*14)//2
            for i, ch in enumerate(seq):
                n = ord(ch) - ord('0')
                # x, y = w - 34 + ox + i*14, 68
                vertices.extend(nums[n].get_t4f_v4f_vertices(
                    x + w - 34 + ox + i*14,
                    y + 68
                ))

        if vertices:
            with nums[0].owner:
                n = len(vertices)
                buf = (GLfloat*n)()
                buf[:] = vertices
                glInterleavedArrays(GL_T4F_V4F, 0, buf)
                glDrawArrays(GL_QUADS, 0, n/8)

        glPopClientAttrib()
예제 #10
0
    def batch_draw_status(gcps):
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        vertices = []
        for port in gcps:
            char = port.character
            if not char: continue

            hp, hp_bg = L('thb-hp'), L('thb-hp_bg')
            if char.dead:
                hp = hp.grayed
                hp_bg = hp_bg.grayed

            # hp bar
            w = hp.width
            x, y = port.x, port.y
            for i in xrange(char.maxlife):
                vertices.extend(
                    hp_bg.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

            for i in xrange(max(char.life, 0)):
                vertices.extend(
                    hp.get_t4f_v4f_vertices(5+x+i*w, 56+y)
                )

        nums = L('thb-num')
        for port in gcps:
            x, y, w = port.x, port.y, port.width
            char = port.character
            if not char: continue

            n = len(char.cards) + len(char.showncards)
            seq = str(n)
            ox = (32 - len(seq)*14)//2
            for i, ch in enumerate(seq):
                n = ord(ch) - ord('0')
                # x, y = w - 34 + ox + i*14, 68
                vertices.extend(nums[n].get_t4f_v4f_vertices(
                    x + w - 34 + ox + i*14,
                    y + 68
                ))

        if vertices:
            with nums[0].owner:
                n = len(vertices)
                buf = (GLfloat*n)()
                buf[:] = vertices
                glInterleavedArrays(GL_T4F_V4F, 0, buf)
                glDrawArrays(GL_QUADS, 0, n/8)

        glPopClientAttrib()
예제 #11
0
파일: camera.py 프로젝트: Knio/miru
    def render(self):
        gl.glPushAttrib(gl.GL_ENABLE_BIT)
        gl.glEnable(gl.GL_BLEND)
        gl.glDisable(gl.GL_DEPTH_TEST)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)

        for o in self.objects:
            o.draw()

        gl.glPopClientAttrib()
        gl.glPopAttrib()
예제 #12
0
파일: gl.py 프로젝트: GaZ3ll3/enable
    def draw_image(self, img, rect=None, force_copy=False):
        """ Renders a GraphicsContextArray into this GC """
        xform = self.get_ctm()
        x0 = xform[4]
        y0 = xform[5]

        image = image_as_array(img)
        shape = image.shape
        if shape[2] == 4:
            fmt = "RGBA"
        else:
            fmt = "RGB"
        aii = ArrayImage(image, format=fmt)
        texture = aii.texture

        # The texture coords consists of (u,v,r) for each corner of the
        # texture rectangle.  The coordinates are stored in the order
        # bottom left, bottom right, top right, top left.
        x, y, w, h = rect
        texture.width = w
        texture.height = h
        t = texture.tex_coords
        points = array([
            [x,   y+h],
            [x+w, y+h],
            [x+w, y],
            [x,   y],
        ])
        p = transform_points(affine_from_values(*xform), points)
        a = (gl.GLfloat*32)(
            t[0],   t[1],   t[2],  1.,
            p[0,0], p[0,1], 0,     1.,
            t[3],   t[4],   t[5],  1.,
            p[1,0], p[1,1], 0,     1.,
            t[6],   t[7],   t[8],  1.,
            p[2,0], p[2,1], 0,     1.,
            t[9],   t[10],  t[11], 1.,
            p[3,0], p[3,1], 0,     1.,
        )
        gl.glPushAttrib(gl.GL_ENABLE_BIT)
        gl.glEnable(texture.target)
        gl.glBindTexture(texture.target, texture.id)
        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glInterleavedArrays(gl.GL_T4F_V4F, 0, a)
        gl.glDrawArrays(gl.GL_QUADS, 0, 4)
        gl.glPopClientAttrib()
        gl.glPopAttrib()
예제 #13
0
    def batch_draw(csl):
        glPushMatrix()
        glLoadIdentity()
        vertices = []
        for cs in csl:
            ax, ay = cs.abs_coords()
            vertices += cs.img.get_t4f_v4f_vertices(ax, ay)

            s = cs.card.suit
            n = cs.card.number

            ssuit = L('thb-smallsuit')
            snum = L('thb-smallnum')

            if n == 10:  # special case
                # g[0].blit(1+g[0].vertices[0], 33+g[0].vertices[1])
                # g[1].blit(5+g[1].vertices[0], 33+g[1].vertices[1])
                vertices += snum[s % 2 * 14 + 10].get_t4f_v4f_vertices(
                    ax - 1, ay + 31)
                vertices += snum[s % 2 * 14 + 0].get_t4f_v4f_vertices(
                    ax + 3, ay + 31)
            else:
                vertices += snum[s % 2 * 14 + n].get_t4f_v4f_vertices(
                    ax + 1, ay + 31)

            vertices += ssuit[s - 1].get_t4f_v4f_vertices(ax + 1, ay + 22)

            if cs.selected:
                vertices += L('thb-card-small-selected').get_t4f_v4f_vertices(
                    ax, ay)
            else:
                vertices += L('thb-card-small-frame').get_t4f_v4f_vertices(
                    ax, ay)

        n = len(vertices)
        buf = (GLfloat * n)()
        buf[:] = vertices
        glColor3f(1., 1., 1.)
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glInterleavedArrays(GL_T4F_V4F, 0, buf)
        with get_atlas('card').texture:
            glDrawArrays(GL_QUADS, 0, n / 8)

        glPopClientAttrib()
        glPopMatrix()
예제 #14
0
    def batch_draw(csl):
        glPushMatrix()
        glLoadIdentity()
        vertices = []
        for cs in csl:
            ax, ay = cs.abs_coords()
            vertices += cs.img.get_t4f_v4f_vertices(ax, ay)

            s = cs.card.suit
            n = cs.card.number

            ssuit = game_res.smallsuit
            snum = game_res.smallnum

            if n == 10:  # special case
                # g[0].blit(1+g[0].vertices[0], 33+g[0].vertices[1])
                # g[1].blit(5+g[1].vertices[0], 33+g[1].vertices[1])
                vertices += snum[s % 2 * 14 + 10].get_t4f_v4f_vertices(ax - 1, ay + 31)
                vertices += snum[s % 2 * 14 + 0].get_t4f_v4f_vertices(ax + 3, ay + 31)
            else:
                vertices += snum[s % 2 * 14 + n].get_t4f_v4f_vertices(ax + 1, ay + 31)

            vertices += ssuit[s - 1].get_t4f_v4f_vertices(ax + 1, ay + 22)

            if cs.selected:
                vertices += game_res.scardframe_selected.get_t4f_v4f_vertices(ax, ay)
            else:
                vertices += game_res.scardframe_normal.get_t4f_v4f_vertices(ax, ay)

        n = len(vertices)
        buf = (GLfloat*n)()
        buf[:] = vertices
        glColor3f(1., 1., 1.)
        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glInterleavedArrays(GL_T4F_V4F, 0, buf)
        with get_atlas('card').texture:
            glDrawArrays(GL_QUADS, 0, n/8)

        glPopClientAttrib()
        glPopMatrix()
예제 #15
0
    def blit_to_texture(self, target, level, x, y, z, internalformat=None):
        '''Draw this image to to the currently bound texture at `target`.

        If `internalformat` is specified, glTexImage is used to initialise
        the texture; otherwise, glTexSubImage is used to update a region.
        '''

        data_format = self.format
        data_pitch = abs(self._current_pitch)

        # Determine pixel format from format string
        matrix = None
        format, type = self._get_gl_format_and_type(data_format)
        if format is None:
            if (len(data_format) in (3, 4)
                    and gl.gl_info.have_extension('GL_ARB_imaging')):

                # Construct a color matrix to convert to GL_RGBA
                def component_column(component):
                    try:
                        pos = 'RGBA'.index(component)
                        return [0] * pos + [1] + [0] * (3 - pos)
                    except ValueError:
                        return [0, 0, 0, 0]

                # pad to avoid index exceptions
                lookup_format = data_format + 'XXX'
                matrix = (component_column(lookup_format[0]) +
                          component_column(lookup_format[1]) +
                          component_column(lookup_format[2]) +
                          component_column(lookup_format[3]))
                format = {3: gl.GL_RGB, 4: gl.GL_RGBA}.get(len(data_format))
                type = gl.GL_UNSIGNED_BYTE

                gl.glMatrixMode(gl.GL_COLOR)
                gl.glPushMatrix()
                gl.glLoadMatrixf((gl.GLfloat * 16)(*matrix))
            else:
                # Need to convert data to a standard form
                data_format = {
                    1: 'L',
                    2: 'LA',
                    3: 'RGB',
                    4: 'RGBA'
                }.get(len(data_format))
                format, type = self._get_gl_format_and_type(data_format)

        # Workaround: don't use GL_UNPACK_ROW_LENGTH
        if gl.current_context._workaround_unpack_row_length:
            data_pitch = self.width * len(data_format)

        # Get data in required format (hopefully will be the same format it's
        # already in, unless that's an obscure format, upside-down or the
        # driver is old).
        data = self._convert(data_format, data_pitch)

        if data_pitch & 0x1:
            alignment = 1
        elif data_pitch & 0x2:
            alignment = 2
        else:
            alignment = 4
        row_length = data_pitch / len(data_format)
        gl.glPushClientAttrib(gl.GL_CLIENT_PIXEL_STORE_BIT)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)
        gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, row_length)
        self._apply_region_unpack()
        gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(target, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)

        if target == gl.GL_TEXTURE_3D:
            assert not internalformat
            gl.glTexSubImage3D(target, level, x, y, z, self.width, self.height,
                               1, format, type, data)
        elif internalformat:
            gl.glTexImage2D(target, level, internalformat, self.width,
                            self.height, 0, format, type, data)
        else:
            gl.glTexSubImage2D(target, level, x, y, self.width, self.height,
                               format, type, data)
        gl.glPopClientAttrib()

        if matrix:
            gl.glPopMatrix()
            gl.glMatrixMode(gl.GL_MODELVIEW)
예제 #16
0
    def draw_image(self, img, rect=None, force_copy=False):
        """ Renders a GraphicsContextArray into this GC """
        xform = self.get_ctm()

        image = image_as_array(img)
        shape = image.shape
        if shape[2] == 4:
            fmt = "RGBA"
        else:
            fmt = "RGB"
        aii = ArrayImage(image, format=fmt)
        texture = aii.texture

        # The texture coords consists of (u,v,r) for each corner of the
        # texture rectangle.  The coordinates are stored in the order
        # bottom left, bottom right, top right, top left.
        x, y, w, h = rect
        texture.width = w
        texture.height = h
        t = texture.tex_coords
        points = array([
            [x, y + h],
            [x + w, y + h],
            [x + w, y],
            [x, y],
        ])
        p = transform_points(affine_from_values(*xform), points)
        a = (gl.GLfloat * 32)(
            t[0],
            t[1],
            t[2],
            1.,
            p[0, 0],
            p[0, 1],
            0,
            1.,
            t[3],
            t[4],
            t[5],
            1.,
            p[1, 0],
            p[1, 1],
            0,
            1.,
            t[6],
            t[7],
            t[8],
            1.,
            p[2, 0],
            p[2, 1],
            0,
            1.,
            t[9],
            t[10],
            t[11],
            1.,
            p[3, 0],
            p[3, 1],
            0,
            1.,
        )
        gl.glPushAttrib(gl.GL_ENABLE_BIT)
        gl.glEnable(texture.target)
        gl.glBindTexture(texture.target, texture.id)
        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glInterleavedArrays(gl.GL_T4F_V4F, 0, a)
        gl.glDrawArrays(gl.GL_QUADS, 0, 4)
        gl.glPopClientAttrib()
        gl.glPopAttrib()
예제 #17
0
    def render(self, scene):
        """ Add a ring to the view.

        :param scene: The view to render the model into
        :type scene: pyglet_helper.objects.View
        """
        if self.degenerate:
            return
        # The number of subdivisions around the hoop's radial direction.
        if self.thickness:
            band_coverage = scene.pixel_coverage(self.pos, self.thickness)
        else:
            band_coverage = scene.pixel_coverage(self.pos, self.radius * 0.1)
        if band_coverage < 0:
            band_coverage = 1000
        bands = sqrt(band_coverage * 4.0)
        bands = clamp(4, bands, 40)
        # The number of subdivisions around the hoop's tangential direction.
        ring_coverage = scene.pixel_coverage(self.pos, self.radius)
        if ring_coverage < 0:
            ring_coverage = 1000
        rings = sqrt(ring_coverage * 4.0)
        rings = clamp(4, rings, 80)
        slices = int(rings)
        inner_slices = int(bands)
        radius = self.radius
        inner_radius = self.thickness

        # Create the vertex and normal arrays.
        vertices = []
        normals = []

        outer_angle_step = 2 * pi / (slices - 1)
        inner_angle_step = 2 * pi / (inner_slices - 1)
        outer_angle = 0.
        for i in range(slices):
            cos_outer_angle = cos(outer_angle)
            sin_outer_angle = sin(outer_angle)
            inner_angle = 0.
            for j in range(inner_slices):
                cos_inner_angle = cos(inner_angle)
                sin_inner_angle = sin(inner_angle)

                diameter = (radius + inner_radius * cos_inner_angle)
                vertex_x = diameter * cos_outer_angle
                vertex_y = diameter * sin_outer_angle
                vertex_z = inner_radius * sin_inner_angle

                normal_x = cos_outer_angle * cos_inner_angle
                normal_y = sin_outer_angle * cos_inner_angle
                normal_z = sin_inner_angle

                vertices.extend([vertex_x, vertex_y, vertex_z])
                normals.extend([normal_x, normal_y, normal_z])
                inner_angle += inner_angle_step
            outer_angle += outer_angle_step

        # Create ctypes arrays of the lists
        vertices = (gl.GLfloat *len(vertices))(*vertices)
        normals = (gl.GLfloat * len(normals))(*normals)

        # Create a list of triangle indices.
        indices = []
        for i in range(slices - 1):
            for j in range(inner_slices - 1):
                pos = i * inner_slices + j
                indices.extend([pos, pos + inner_slices, pos + inner_slices +
                                1])
                indices.extend([pos, pos + inner_slices + 1, pos + 1])
        indices = (gl.GLuint * len(indices))(*indices)

        # Compile a display list
        self.list = gl.glGenLists(1)
        gl.glNewList(self.list, gl.GL_COMPILE)
        self.color.gl_set(self.opacity)

        gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
        gl.glEnableClientState(gl.GL_NORMAL_ARRAY)
        self.model_world_transform(scene.gcf,
                                   Vector([self.radius, self.radius,
                                           self.radius])).gl_mult()

        gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices)
        gl.glNormalPointer(gl.GL_FLOAT, 0, normals)
        gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT,
                          indices)
        gl.glPopClientAttrib()

        gl.glEndList()
        gl.glCallList(self.list)
예제 #18
0
    def DrawFixedImage(self, ImageViewModel, color, BoundingBox=None, z=None):
        '''Draw a fixed image, bounding box indicates the visible area.  Everything is drawn if BoundingBox is None'''
        
        if z is None:
            z = self.z
        
        if hasattr(self.rendercache, 'FixedImageDataGrid'):
            FixedImageDataGrid = self.rendercache.FixedImageDataGrid
        else:
            FixedImageDataGrid = []
            for i in range(0, ImageViewModel.NumCols):
                FixedImageDataGrid.append([None] * ImageViewModel.NumRows)
                self.rendercache.FixedImageDataGrid = FixedImageDataGrid

        if color is None:
            color = (1.0, 1.0, 1.0, 1.0)
        
        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glDisable(gl.GL_CULL_FACE)
        
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_BORDER)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_BORDER)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)

        pyglet.gl.glColor4f(color[0], color[1], color[2], color[3])

        for ix in range(0, ImageViewModel.NumCols):
            column = ImageViewModel.ImageArray[ix]
            cacheColumn = FixedImageDataGrid[ix]
            for iy in range(0, ImageViewModel.NumRows):

                texture = column[iy]
                t = (0.0, 0.0, z,
                     1.0, 0.0, z,
                     1.0, 1.0, z,
                     0.0, 1.0, z) 
                w, h = ImageViewModel.TextureSize
                x = ImageViewModel.TextureSize[1] * ix
                y = ImageViewModel.TextureSize[0] * iy

                # Check bounding box if it exists
                if not BoundingBox is None:
                    if not nornir_imageregistration.spatial.Rectangle.contains(BoundingBox, [y, x, y + h, x + w]):
                        continue

                array = None
                if cacheColumn[iy] is None: 
                    array = (gl.GLfloat * 32)(
                         t[0], t[1], t[2], 1.,
                         x, y, z, 1.,
                         t[3], t[4], t[5], 1.,
                         x + w, y, z, 1.,
                         t[6], t[7], t[8], 1.,
                         x + w, y + h, z, 1.,
                         t[9], t[10], t[11], 1.,
                         x, y + h, z, 1.)

                    cacheColumn[iy] = array
                else:
                    array = cacheColumn[iy]

                gl.glBindTexture(gl.GL_TEXTURE_2D, texture)
                gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
                gl.glInterleavedArrays(gl.GL_T4F_V4F, 0, array)
                gl.glDrawArrays(gl.GL_QUADS, 0, 4)
                gl.glPopClientAttrib()
        
        pyglet.gl.glColor4f(1.0, 1.0, 1.0, 1.0)
예제 #19
0
파일: gl.py 프로젝트: GaZ3ll3/enable
    def blit_to_texture(self, target, level, x, y, z, internalformat=None):
        '''Draw this image to to the currently bound texture at `target`.

        If `internalformat` is specified, glTexImage is used to initialise
        the texture; otherwise, glTexSubImage is used to update a region.
        '''

        data_format = self.format
        data_pitch = abs(self._current_pitch)

        # Determine pixel format from format string
        matrix = None
        format, type = self._get_gl_format_and_type(data_format)
        if format is None:
            if (len(data_format) in (3, 4) and
                gl.gl_info.have_extension('GL_ARB_imaging')):
                # Construct a color matrix to convert to GL_RGBA
                def component_column(component):
                    try:
                        pos = 'RGBA'.index(component)
                        return [0] * pos + [1] + [0] * (3 - pos)
                    except ValueError:
                        return [0, 0, 0, 0]
                # pad to avoid index exceptions
                lookup_format = data_format + 'XXX'
                matrix = (component_column(lookup_format[0]) +
                          component_column(lookup_format[1]) +
                          component_column(lookup_format[2]) +
                          component_column(lookup_format[3]))
                format = {
                    3: gl.GL_RGB,
                    4: gl.GL_RGBA}.get(len(data_format))
                type = gl.GL_UNSIGNED_BYTE

                gl.glMatrixMode(gl.GL_COLOR)
                gl.glPushMatrix()
                gl.glLoadMatrixf((gl.GLfloat * 16)(*matrix))
            else:
                # Need to convert data to a standard form
                data_format = {
                    1: 'L',
                    2: 'LA',
                    3: 'RGB',
                    4: 'RGBA'}.get(len(data_format))
                format, type = self._get_gl_format_and_type(data_format)

        # Workaround: don't use GL_UNPACK_ROW_LENGTH
        if gl.current_context._workaround_unpack_row_length:
            data_pitch = self.width * len(data_format)

        # Get data in required format (hopefully will be the same format it's
        # already in, unless that's an obscure format, upside-down or the
        # driver is old).
        data = self._convert(data_format, data_pitch)

        if data_pitch & 0x1:
            alignment = 1
        elif data_pitch & 0x2:
            alignment = 2
        else:
            alignment = 4
        row_length = data_pitch / len(data_format)
        gl.glPushClientAttrib(gl.GL_CLIENT_PIXEL_STORE_BIT)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)
        gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, row_length)
        self._apply_region_unpack()
        gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
        gl.glTexParameteri(target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri(target, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)


        if target == gl.GL_TEXTURE_3D:
            assert not internalformat
            gl.glTexSubImage3D(target, level,
                            x, y, z,
                            self.width, self.height, 1,
                            format, type,
                            data)
        elif internalformat:
            gl.glTexImage2D(target, level,
                         internalformat,
                         self.width, self.height,
                         0,
                         format, type,
                         data)
        else:
            gl.glTexSubImage2D(target, level,
                            x, y,
                            self.width, self.height,
                            format, type,
                            data)
        gl.glPopClientAttrib()

        if matrix:
            gl.glPopMatrix()
            gl.glMatrixMode(gl.GL_MODELVIEW)
예제 #20
0
    def batch_draw(csl):
        glPushMatrix()
        glLoadIdentity()

        vertices = []
        for cs in csl:
            ax, ay = cs.abs_coords()
            if cs.ft_anim:
                qs = cs.question_scale
                bs = cs.back_scale
                aa = cs.ftanim_alpha
                ca = cs.ftanim_cardalpha
                if cs.gray:
                    c = (.66, .66, .66, ca)
                else:
                    c = (1., 1., 1., ca)
                vertices += cs.img.get_t2c4n3v3_vertices(c, ax, ay)

                n, s = cs.number, cs.suit
                if n: vertices += L('thb-cardnum')[s % 2 * 13 + n - 1].get_t2c4n3v3_vertices(c, ax + 5, ay + 105)
                if s: vertices += L('thb-suit')[s - 1].get_t2c4n3v3_vertices(c, ax + 6, ay + 94)

                c = (1, 1, 1, aa)

                if qs:
                    vertices += L('thb-card-question').get_t2c4n3v3_vertices(c, ax+(1-qs)*45, ay, 0, qs*91)

                if bs:
                    vertices += L('thb-card-hidden').get_t2c4n3v3_vertices(c, ax+(1-bs)*45, ay, 0, bs*91)
            else:
                a = cs.alpha
                if cs.gray:
                    c = (.66, .66, .66, a)
                else:
                    c = (1., 1., 1., a)
                vertices += cs.img.get_t2c4n3v3_vertices(c, ax, ay)
                resides_in = cs.card.resides_in
                if resides_in and resides_in.type == 'showncards':
                    vertices += L('thb-card-showncardtag').get_t2c4n3v3_vertices(c, ax, ay)

                n, s = cs.number, cs.suit
                if n: vertices += L('thb-cardnum')[s % 2 * 13 + n - 1].get_t2c4n3v3_vertices(c, ax + 5, ay + 105)
                if s: vertices += L('thb-suit')[s-1].get_t2c4n3v3_vertices(c, ax+6, ay+94)

                if cs.selected:
                    c = (0., 0., 2., 1.0)
                else:
                    c = (1., 1., 1., cs.shine_alpha)

                vertices += L('thb-card-shinesoft').get_t2c4n3v3_vertices(c, ax-6, ay-6)

        if vertices:
            n = len(vertices)
            buf = (GLfloat*n)()
            buf[:] = vertices
            glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
            glInterleavedArrays(GL_T2F_C4F_N3F_V3F, 0, buf)
            with get_atlas('card').texture:
                glDrawArrays(GL_QUADS, 0, n/12)
            glPopClientAttrib()

        glPopMatrix()
예제 #21
0
    def DrawFixedImage(self, ImageViewModel, color, BoundingBox=None, z=None):
        '''Draw a fixed image, bounding box indicates the visible area.  Everything is drawn if BoundingBox is None'''
        
        if z is None:
            z = self.z
        
        if hasattr(self.rendercache, 'FixedImageDataGrid'):
            FixedImageDataGrid = self.rendercache.FixedImageDataGrid
        else:
            FixedImageDataGrid = []
            for i in range(0, ImageViewModel.NumCols):
                FixedImageDataGrid.append([None] * ImageViewModel.NumRows)
                self.rendercache.FixedImageDataGrid = FixedImageDataGrid

        if color is None:
            color = (1.0, 1.0, 1.0, 1.0)

        for ix in range(0, ImageViewModel.NumCols):
            column = ImageViewModel.ImageArray[ix]
            cacheColumn = FixedImageDataGrid[ix]
            for iy in range(0, ImageViewModel.NumRows):

                texture = column[iy]
                t = (0.0, 0.0, z,
                     1.0, 0.0, z,
                     1.0, 1.0, z,
                     0.0, 1.0, z) 
                w, h = ImageViewModel.TextureSize
                x = ImageViewModel.TextureSize[1] * ix
                y = ImageViewModel.TextureSize[0] * iy

                # Check bounding box if it exists
                if not BoundingBox is None:
                    if not nornir_imageregistration.spatial.Rectangle.contains(BoundingBox, [y, x, y + h, x + w]):
                        continue

                array = None
                if cacheColumn[iy] is None: 
                    array = (gl.GLfloat * 32)(
                         t[0], t[1], t[2], 1.,
                         x, y, z, 1.,
                         t[3], t[4], t[5], 1.,
                         x + w, y, z, 1.,
                         t[6], t[7], t[8], 1.,
                         x + w, y + h, z, 1.,
                         t[9], t[10], t[11], 1.,
                         x, y + h, z, 1.)

                    cacheColumn[iy] = array
                else:
                    array = cacheColumn[iy]


                gl.glEnable(gl.GL_TEXTURE_2D)
                gl.glDisable(gl.GL_CULL_FACE)

                pyglet.gl.glColor4f(color[0], color[1], color[2], color[3])

                gl.glBindTexture(gl.GL_TEXTURE_2D, texture)
                gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_BORDER)
                gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_BORDER)
                gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
                gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
                gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
                gl.glInterleavedArrays(gl.GL_T4F_V4F, 0, array)
                gl.glDrawArrays(gl.GL_QUADS, 0, 4)
                gl.glPopClientAttrib()

                pyglet.gl.glColor4f(1.0, 1.0, 1.0, 1.0)