1

我最近开始使用 ModernGL,今天我想开始使用纹理数组。我只是坚持如何在 Moderngl 中传递各个子纹理?在 OpenGL 中,我会调用glTexSubImage3D. 但是,在 ModernGL 文档中,Context.texture_array需要 3 个参数:sizecomponentsdata. 我认为data应该是所有的图像堆叠?我将如何使用 PIL 和可能的 numpy 来解决这个问题?

4

1 回答 1

1

您可以单独阅读每个图像并将图像附加到列表中。最后将列表转换为numpy.array.
在以下代码段imageList中是文件名列表,width并且height是单个图像的大小(图像必须全部相同):

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)
于 2020-09-02T19:01:40.000 回答