PyOpenGL Texture_3D 和 Numpy 不渲染
我正在尝试从TIFF图像加载数据,并将其渲染成numpy矩阵,进而制作一个3D纹理。TIFF图像是强度图像,我希望将这个强度图像重新映射为亮度或颜色图像。
虽然我觉得我理解GL_Texture_3D的过程,但在运行代码时没有错误,结果却没有任何可视化的效果。我肯定是做了一些简单的事情错了。有没有人能帮我理解我的错误在哪里?
这是代码的简短版本[已修正]:
编辑2:尝试正确转换类型:根据Reto Koradi的建议,我可能遇到了类型转换的问题。所以我将numpy数组转换为uint8,确保值不全为零,然后将其传递给channel_RGBA_buffer的uint8数组。结果仍然是一个黑色窗口。
编辑3:使用Glumpy的方法:我查看了Glumpy的“textures”函数(https://code.google.com/p/glumpy/source/browse/glumpy/image/texture.py?r=8cbc2aa2b277c07b59fba964edd327370a8e8091),我将目标GL格式从GL_RGBA改为GL_RGBA16,并在调用glTexImage3D后添加了glBindTexture(GL_TEXTURE_3D, 0)。我仍然不明白为什么要用0而不是纹理。
编辑4:修正了两行的缩进,使示例可执行
结果仍然是一个黑色窗口,但当我关闭这个窗口时,瞬间我看到数据出现。看起来我好像多加了一层。
import sys
import numpy
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from OpenGL.arrays import vbo
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GLUT.freeglut import *
class test_TIFF_render:
def __init__(self):
self.something = True
def load_texture(self):
global texture
#read file as array, for simplicity I will just make a random numpy 3D matrix
tiff_mean = numpy.random.rand(3,300,300)
tiff_mean = tiff_mean*1000
tiff_mean = tiff_mean.astype('uint8')
shape_array = tiff_mean.shape
shape_array = tiff_mean.shape
#we have the data now, let's set the texture
tiff_mean = tiff_mean.reshape(tiff_mean.shape[1]*tiff_mean.shape[2]*tiff_mean.shape[0])
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_3D,texture)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R,GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
#now we need to convert the intensity data organized in 3D matrix into an RGBA buffer array (pixel1R, pixel1G, pixel1B, pixel1A, pixel2R....)
channel_RGBA_buffer = numpy.zeros(tiff_mean.shape[0]*4, numpy.uint8)
for i in range(0,tiff_mean.shape[0]):
channel_RGBA_buffer[i*4] = tiff_mean[i] #R
channel_RGBA_buffer[i*4+1] = tiff_mean[i] #G
channel_RGBA_buffer[i*4+2] = tiff_mean[i] #B
channel_RGBA_buffer[i*4+3] = tiff_mean[i] #A
if numpy.mod(i,100000)==0:
print('count %d',i)
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16, shape_array[1], shape_array[2], shape_array[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, channel_RGBA_buffer)
glBindTexture( GL_TEXTURE_3D, 0 )
def display(self):
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glEnable( GL_ALPHA_TEST )
glAlphaFunc( GL_GREATER, 0.03)
glEnable(GL_BLEND)
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )
glMatrixMode( GL_TEXTURE )
glLoadIdentity()
glEnable(GL_TEXTURE_3D);
glBindTexture( GL_TEXTURE_3D, texture);
dOrthoSize = 1
for Indx in self.my_range(-1,1,0.01):
TexIndex = round(Indx,2)
glBegin(GL_QUADS)
glTexCoord3f(0.0, 0.0, (TexIndex+1.0)/2.0)
glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex)
glTexCoord3f(1.0, 0.0, (TexIndex+1.0)/2.0)
glVertex3f(dOrthoSize,-dOrthoSize,TexIndex)
glTexCoord3f(1.0, 1.0, (TexIndex+1.0)/2.0)
glVertex3f(dOrthoSize,dOrthoSize,TexIndex)
glTexCoord3f(0.0, 1.0, (TexIndex+1.0)/2.0)
glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);
glEnd()
glBindTexture( GL_TEXTURE_3D, texture )
def my_range(self,start, end, step):
while start <= end:
yield start
start += step
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(300,300)
window = glutCreateWindow(b"I really hope this works")
test111 = test_TIFF_render()
test111.load_texture()
glutDisplayFunc(test111.display)
glutMainLoop()
我在运行Win7 64位,使用Python3.3。
1 个回答
我没有用过numpy,但根据我阅读的文档(http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html),numpy.zeros()
默认会生成一个包含 float64
类型值的数组。然后你在调用 glTexImage3D()
时传入 GL_INT
作为类型参数。看起来这里的类型不匹配。
通常,用于纹理数据的数据类型是无符号字节。所以我觉得应该像这样:
channel_RGBA_buffer = numpy.zeros(tiff_mean.shape[0]*4, numpy.uint8)
...
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, shape_array[1], shape_array[2], shape_array[0], 0,
GL_RGBA, GL_UNSIGNED_BYTE, channel_RGBA_buffer)
编辑,更新代码的新版本:看起来你知道如何生成0.0到1.0之间的随机浮点值,然后把它们转换成字节(uint8
)值,再用作纹理数据。我相信这样会导致你所有的字节值都是0,或者可能部分是1,这取决于四舍五入的方式。不过,字节的取值范围是0到255。你可能在把随机浮点数转换成纹理数据时需要乘以255.0:
channel_RGBA_buffer[i*4] = 255.0 * tiff_mean[i] #R
channel_RGBA_buffer[i*4+1] = 255.0 * tiff_mean[i] #G
channel_RGBA_buffer[i*4+2] = 255.0 * tiff_mean[i] #B
channel_RGBA_buffer[i*4+3] = 255.0 * tiff_mean[i] #A