【发布时间】:2021-05-19 03:39:53
【问题描述】:
为了了解glTexImage2D,我用Python写了如下测试代码:
import numpy as np
import OpenGL.GL as gl
import glfw
# initiating context
glfw.init()
w = glfw.create_window(100, 100, 'dd', None, None)
glfw.make_context_current(w)
# prepare sample image
width, height = 3, 3
image = np.array([i for i in range(width * height * 3)], dtype='ubyte')
# prepare texture
t = gl.glGenTextures(1)
gl.glBindTexture(gl.GL_TEXTURE_2D, t)
iformat = gl.GL_RGB
# upload texture
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, iformat, width, height, 0, iformat, gl.GL_UNSIGNED_BYTE, image)
# read texture
p = gl.glGetTexImage(gl.GL_TEXTURE_2D, 0, iformat, gl.GL_UNSIGNED_BYTE)
# display pixel values
pixels = iter(p)
for x in range(width):
for y in range(height):
print([next(pixels), next(pixels), next(pixels)])
出乎意料,打印结果如下:
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[12, 13, 14]
[15, 16, 17]
[18, 19, 20]
[24, 25, 26]
[0, 0, 0]
[0, 0, 93]
显然有些值丢失了。为什么会这样?我错过了什么吗?
【问题讨论】:
标签: python python-3.x opengl textures pyopengl