Example #1
0
    def __init__(self, image, mode=1, format=GL.GL_RGBA, texformat=GL.GL_RGBA):
        self.texid = None
        if isinstance(image, (str, unicode)):
            from pyformex.plugins.imagearray import qimage2numpy
            image = qimage2numpy(image, indexed=False)
        else:
            image = np.asarray(image)
        s = "Texture: type %s, size %s" % (image.dtype, image.shape)
        image = np.require(image, dtype='ubyte', requirements='C')
        print(s + "; Converted to: type %s, size %s" %
              (image.dtype, image.shape))
        ny, nx = image.shape[:2]

        # Generate a texture id
        self.texid = GL.glGenTextures(1)
        self.texun = None
        # Make our new texture the current 2D texture
        GL.glEnable(GL.GL_TEXTURE_2D)
        #GL.glTexEnvf(GL.GL_TEXTURE_ENV,GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE)
        # select texture unit 0
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texid)
        GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        # Copy the texture data into the current texture
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, texformat, nx, ny, 0, format,
                        GL.GL_UNSIGNED_BYTE, image)
        self.mode = mode
Example #2
0
def loadImage(file, grey=True):
    """Load a grey image into a numpy array"""
    im = ia.qimage2numpy(file, order='RGB', indexed=False)
    if grey:
        # Do type conversion because auto conversion produces float64
        im = im.sum(axis=-1).astype(Float) / 3.
    return im
Example #3
0
def drawImage(image,w=0,h=0,x=-1,y=-1,color=colors.white,ontop=False):
    """Draws an image as a viewport decoration.

    Parameters:

    - `image`: a QImage or any data that can be converted to a QImage,
      e.g. the name of a raster image file. See also the :func:`loadImage`
      function.
    - `w`,`h`: width and height (in pixels) of the displayed image.
      If the supplied image has a different size, it will be rescaled.
      A value <= 0 will be replaced with the corresponding actual size of
      the image.
    - `x`,`y`: position of the lower left corner of the image. If negative,
      the image will be centered on the current viewport.
    - `color`: the color to mix in (AND) with the image. The default (white)
      will make all pixels appear as in the image.
    - `ontop`: determines whether the image will appear as a background
      (default) or at the front of the 3D scene (as on the camera glass).

    Returns the Decoration drawn.

    Note that the Decoration has a fixed size (and position) on the canvas
    and will not scale when the viewport size is changed.
    The :func:`bgcolor` function can be used to draw an image that completely
    fills the background.
    """
    utils.warn("warn_drawImage_changed")
    from pyformex.plugins.imagearray import qimage2numpy
    from pyformex.opengl.decors import Rectangle

    image = qimage2numpy(image, resize=(w, h), indexed=False)
    w, h = image.shape[:2]
    if x < 0:
        x = (pf.canvas.width() - w) // 2
    if y < 0:
        y = (pf.canvas.height() - h) // 2
    R = Rectangle(x, y, x+w, y+h, color=color, texture=image, ontop=ontop)
    decorate(R)
    return R
Example #4
0
def run():
    global imagefile
    reset()
    clear()
    flat()

    imagefile = askImageFile(imagefile)
    if not imagefile:
        return

    image = qimage2numpy(imagefile, indexed=False)
    if image is None:
        return

    palette = pf.canvas.settings.colormap
    ncolors = len(palette)
    F = Formex('4:0123').replic2(ncolors, 1).setProp(arange(ncolors)).toMesh()
    G = [F.trl(1, (1 + i) * F.sizes()[1]) for i in range(4)]
    for i in range(4):
        G[i].attrib(name='G%s' % i, texture=image, texmode=i, alpha=0.5)
    drawNames(G)
    draw([F, G])
Example #5
0
def run():
    #
    # TODO: RESETALL does not properly layout the canvas in the viewport
    #resetAll()
    clear()
    view('front')
    smooth()
    fonts = utils.listMonoFonts()
    ft = FontTexture.default()

    # - draw a square
    # - use the full character set in the default font as a texture
    # - the font textures are currently upside down, therefore we need
    #   to specify texcoords to flip the image
    F = Formex('4:0123').scale(200).toMesh()
    A = draw(F,
             color=yellow,
             texture=ft,
             texcoords=array([[0, 1], [1, 1], [1, 0], [0, 0]]),
             texmode=2)

    # - draw 20 squares
    # - fill with specific text
    # - put this object on top
    G = Formex('4:0123').replic2(10, 2).scale(20).rot(30).trl([150, 50, 0])
    text = [' pyFormex ', '  rules!  ']
    text = text[1] + text[0]
    tc = FontTexture.default().texCoords(text)
    draw(G,
         color=pyformex_pink,
         texture=ft,
         texcoords=tc,
         texmode=2,
         ontop=True)

    # draw a cross at the center of the square
    # pos is 3D, therefore values are world coordinates
    decorate(Text('+', pos=(100, 100, 0), gravity='', size=100, color=red))

    # draw a string using the default_font texture
    # pos is 2D, therefore values are pixel coordinates
    decorate(Text("Hegemony!", pos=(100, 100), size=50, offset=(0.0, 0.0, 1)))

    # the text is currently adjusted horizontally left, vertically centered
    # on the specified point. Adjustement using gravity will be added later.
    # Also, the color is currently not honoured.
    decorate(Text("Hegemony!", (0, 10), size=20, color=red))
    decorate(Text("Hegemony!", (10, 30), size=20, color=red))

    # use a TextArray to draw text at the corners of the square
    U = TextArray([
        "Lower left corner", "Lower right corner", "Upper right corner",
        "Upper left corner"
    ],
                  pos=F.coords[F.elems[0]],
                  size=30,
                  gravity='NE')
    decorate(U)

    #drawViewportAxes3D((0.,0.,0.),color=blue)

    # draw a cross at the upper corners using an image file
    image = os.path.join(pf.cfg['pyformexdir'], 'data', 'mark_cross.png')
    from pyformex.plugins.imagearray import qimage2numpy
    image = qimage2numpy(image, indexed=False)
    X = Formex('4:0123').scale(40).toMesh().align('000')
    # at the right corner using direct texture drawing techniques
    draw(X,
         texture=image,
         texcoords=array([[0, 1], [1, 1], [1, 0], [0, 0]]),
         texmode=0,
         rendertype=-1,
         opak=False,
         ontop=True,
         offset3d=[
             (200., 200., 0.),
             (200., 200., 0.),
             (200., 200., 0.),
             (200., 200., 0.),
         ])
    # at the left corner, using a Mark
    drawActor(Mark((0, 200, 0), image, size=40, color=red))