简体   繁体   English

访问片段着色器中的3D数组

[英]Access to 3D array in fragment shader

I'm trying to provide access to a 3-dimensional array of scalar data to a fragment shader, from a Python program using PyOpenGL. 我正在尝试使用PyOpenGL从Python程序向片段着色器提供对标量数据的三维数组的访问。

In the fragment shader, I declare the a 3d sampler uniform 在片段着色器中,我声明了一个3d采样器制服

uniform sampler3D vol;

and in the Python program I have the following code to set up a scalar 3d texture 在Python程序中,我有以下代码来设置标量3D纹理

vol = numpy.random.rand(3, 3, 3).astype(np.float32)
texture = glGenTextures(1)
glUniform1i(glGetUniformLocation(program, "vol"), 0)
glActiveTexture(GL_TEXTURE0 + 0)
glBindTexture(GL_TEXTURE_3D, texture)
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, 3, 3, 3, 0, GL_RED, GL_FLOAT, vol)
glEnable(GL_TEXTURE_3D)

However, no matter where from the sampler I take values, eg 但是,无论采样器在哪里,我都会采用值,例如

color = texture(vol, vec3(0, 0, 0));

it appears that I always obtain black (0, 0, 0). 似乎我总是获得黑色(0,0,0)。

What am I doing wrong? 我究竟做错了什么?

I know that the basic setup of my fragment shader works, ie if I write color = vec3(1, 0, 0) I get red pixels. 我知道我的片段着色器的基本设置有效,即如果我写color = vec3(1, 0, 0)我得到红色像素。

I also know that there are no OpenGL errors, because I'm running the program with the option -glerror processed by glutInit() , which leads to OpenGL errors being translated into Python exceptions. 我也知道没有OpenGL错误,因为我正在使用由glutInit()处理的选项-glerror运行程序,这导致OpenGL错误被转换为Python异常。

That is because your GL_RED texture format is clamped to range <0,1> !!! 那是因为你GL_RED纹理格式被钳位范围<0,1>

To remedy you need to use non clamped texture format or disable clamping ... Here examples that are working on my GL implementations: 要解决此问题,您需要使用非钳位纹理格式或禁用钳位...这里的示例适用于我的GL实现:

here formats extracted from both: 这里提取的格式:

glTexImage3D(GL_TEXTURE_3D, 0, GL_R16F, xs,ys,zs, 0, GL_RED, GL_FLOAT, dat);

glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, size, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, pdata);

For scalar data I would use the first option. 对于标量数据,我会使用第一个选项。 There are more formats that are not clamped just try and see... 有更多的格式没有被限制只是试着看...

I have never used the disabling of clamping feature but saw this code somewhere while researching similar issues (not sure if it works): 从来没有使用过禁用钳位功能但是在研究类似问题时看到了这个代码(不确定它是否有效):

glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE);
glClampColorARB(GL_CLAMP_READ_COLOR_ARB, GL_FALSE);
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);

With that theoretically you could use any texture format... 理论上你可以使用任何纹理格式......

To verify you can use this: 要验证您可以使用此:

Also I do not see any parameters of the texture set. 另外,我没有看到纹理集的任何参数。 I would expect something like this: 我希望这样的事情:

glBindTexture(GL_TEXTURE_3D,txrvol);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);

to avoid interpolation messing your data for non exact texture coordinates ... 避免插入乱码数据的非精确纹理坐标...

I figured out the problem: Apparently GL_TEXTURE_3D is by default mipmapped, I only provided level 0, and (how I'm not clear about) another level is selected. 我想出了问题:显然GL_TEXTURE_3D默认为mipmapped,我只提供了0级,并且(我不清楚)是否选择了另一个级别。 The problem is solved by glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0) . 该问题由glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM