psychopy.tools.gltools.createTexImage2D

psychopy.tools.gltools.createTexImage2D(width, height, target=3553, level=0, internalFormat=32856, pixelFormat=6408, dataType=5126, data=None, unpackAlignment=4, texParams=None)[source]

Create a 2D texture in video memory. This can only create a single 2D texture with targets GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE.

Parameters:
  • width (int) – Texture width in pixels.

  • height (int) – Texture height in pixels.

  • target (int) – The target texture should only be either GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE.

  • level (int) – LOD number of the texture, should be 0 if GL_TEXTURE_RECTANGLE is the target.

  • internalFormat (int) – Internal format for texture data (e.g. GL_RGBA8, GL_R11F_G11F_B10F).

  • pixelFormat (int) – Pixel data format (e.g. GL_RGBA, GL_DEPTH_STENCIL)

  • dataType (int) – Data type for pixel data (e.g. GL_FLOAT, GL_UNSIGNED_BYTE).

  • data (ctypes or None) – Ctypes pointer to image data. If None is specified, the texture will be created but pixel data will be uninitialized.

  • unpackAlignment (int) – Alignment requirements of each row in memory. Default is 4.

  • texParams (dict) – Optional texture parameters specified as dict. These values are passed to glTexParameteri. Each tuple must contain a parameter name and value. For example, texParameters={GL.GL_TEXTURE_MIN_FILTER: GL.GL_LINEAR, GL.GL_TEXTURE_MAG_FILTER: GL.GL_LINEAR}.

Returns:

A TexImage2D descriptor.

Return type:

TexImage2D

Notes

The ‘userData’ field of the returned descriptor is a dictionary that can be used to store arbitrary data associated with the texture.

Previous textures are unbound after calling ‘createTexImage2D’.

Examples

Creating a texture from an image file:

import pyglet.gl as GL  # using Pyglet for now

# empty texture
textureDesc = createTexImage2D(1024, 1024, internalFormat=GL.GL_RGBA8)

# load texture data from an image file using Pillow and NumPy
from PIL import Image
import numpy as np
im = Image.open(imageFile)  # 8bpp!
im = im.transpose(Image.FLIP_TOP_BOTTOM)  # OpenGL origin is at bottom
im = im.convert("RGBA")
pixelData = np.array(im).ctypes  # convert to ctypes!

width = pixelData.shape[1]
height = pixelData.shape[0]
textureDesc = gltools.createTexImage2D(
    width,
    height,
    internalFormat=GL.GL_RGBA,
    pixelFormat=GL.GL_RGBA,
    dataType=GL.GL_UNSIGNED_BYTE,
    data=pixelData,
    unpackAlignment=1,
    texParameters=[(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR),
                   (GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)])

GL.glBindTexture(GL.GL_TEXTURE_2D, textureDesc.id)

Back to top