Texture2D

demosys.opengl.Texture2D

A Texture is an OpenGL object that contains one or more images that all have the same image format. A texture can be used in two ways. It can be the source of a texture access from a Shader, or it can be used as a render target.

Create

classmethod Texture2D.create(size, components=4, data=None, samples=0, alignment=1, dtype='f1', mipmap=False) → Texture2D

Creates a 2d texture. All parameters are passed on the texture initializer.

Parameters:
  • size – (tuple) Width and height of the texture
  • components – Number of components
  • data – Buffer data for the texture
  • samples – Number of samples when using multisaple texture
  • alignment – Data alignment (1, 2, 4 or 8)
  • dtype – Datatype for each component
  • mipmap – Generate mipmaps
Returns:

Texture2D object

classmethod Texture2D.from_image(path, image=None, **kwargs)

Creates a texture from a image file using Pillow/PIL. Additional parameters is passed to the texture initializer.

Parameters:
  • path – The path to the file
  • image – The PIL/Pillow image object (Can be None)
Returns:

Texture2D object

Methods

Texture2D.use(location=0)

Bind the texture to a channel/location.

Parameters:location – The texture location. (GL_TEXTURE0 + location)
Texture2D.build_mipmaps(base=0, max_level=1000)

Build mipmaps for this texture

Parameters:
  • base – Level to build from
  • max_level – Max levels
Texture2D.set_image(image, flip=True)

Set pixel data using a image file with PIL/Pillow.

Parameters:
  • image – The PIL/Pillow image object
  • flip – Flip the image top to bottom
Texture2D.draw(pos=(0.0, 0.0), scale=(1.0, 1.0))

Draw texture using a fullscreen quad. By default this will conver the entire screen.

Parameters:
  • pos – (tuple) offset x, y
  • scale – (tuple) scale x, y
Texture2D.read(level:int=0, alignment:int=1) → bytes

Read the content of the texture into a buffer.

Parameters:
  • level – The mipmap level.
  • alignment – The byte alignment of the pixels.
Returns:

bytes

Texture2D.read_into(buffer:bytearray, level:int=0, alignment:int=1, write_offset:int=0)

Read the content of the texture into a buffer.

Parameters:
  • buffer – (bytearray) The buffer that will receive the pixels.
  • level – (int) The mipmap level.
  • alignment – (int) The byte alignment of the pixels.
  • write_offset – (int) The write offset.
Texture2D.write(data:bytes, viewport=None, level:int=0, alignment:int=1)

Update the content of the texture.

Parameters:
  • data – (bytes) – The pixel data.
  • viewport – (tuple) – The viewport.
  • level – (int) – The mipmap level.
  • alignment – (int) – The byte alignment of the pixels.
Texture2D.release()

Release/free the ModernGL object

Attributes

Texture2D.size

The size of the texture

Texture2D.width

int: Width of the texture

Texture2D.height

int: Height of the texture

Texture2D.dtype

str: The data type of the texture

Texture2D.components

int: The number of components in the texture

Texture2D.samples

int: The number of samples of the texture

Texture2D.repeat_x

bool: The repeat_x of the texture

Texture2D.repeat_y

bool: The repeat_y of the texture

Texture2D.filter

tuple: (min, mag) filtering of the texture

Texture2D.anisotropy

float: Number of samples for anisotropic filtering. Any value greater than 1.0 counts as a use of anisotropic filtering

Texture2D.depth

Is this a depth texture?

Texture2D.swizzle

str: The swizzle of the texture

Texture2D.size

The size of the texture

Texture2D.ctx

ModernGL context

Texture2D.glo

int: The internal OpenGL object. This values is provided for debug purposes only.