Using a TextureArray in ModernGL

419 Views Asked by At

I recently started using ModernGL, and today I would like to start working with texture arrays. I'm just stuck on how to pass the individual subtextures in Moderngl? In OpenGL I would call glTexSubImage3D. However, in the ModernGL documentation, Context.texture_array takes 3 arguments: size, components, and data. I think data is supposed to be all the images stacked? How would I go about this using PIL and possibly numpy?

1

There are 1 best solutions below

0
On BEST ANSWER

You can read each image separately and append the images to a list. Finally convert the list to an numpy.array.
In the following snippet imageList is a list of filenames and width and height is the size of an individual image (the images must all be the same size):

def createTextureArray(imageList, width, height)

    depth = len(imageList)

    dataList = []
    for filename in imageList:
        
        image = Image.open(filename)
        if width != image.size[0] or height != image.size[1]:
            raise ValueError(f"image size mismatch: {image.size[0]}x{image.size[1]}")
        
        dataList.append(list(image.getdata()))

    imageArrayData = numpy.array(dataList, numpy.uint8)

    components = 4 # 4 for RGBA, 3 for RGB
    context.texture_array((width, height, depth), components, imageArrayData)