问题描述
我正在按预期方式工作的3D渲染器,但是现在我试图将每个多维数据集批处理到一个绘制调用中(我的渲染器现在只能绘制多维数据集).
I am working on a 3D renderer which was working as expected but now I am trying to batch every cube into a single draw call (my renderer can only draw cubes right now).
这里有我的glsl程序,可以为每个批次运行
Here I have my glsl program that runs for each batch
#type vertex
#version 330 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec3 a_TexCoord;
layout(location = 3) in int a_TexIndex;
uniform mat4 u_ProjectionView;
out vec4 v_Color;
out vec3 v_TexCoord;
out flat int v_TexIndex;
void main()
{
v_Color = a_Color;
v_TexCoord = a_TexCoord;
v_TexIndex = a_TexIndex;
gl_Position = u_ProjectionView * vec4(a_Position, 1.0);
}
#type fragment
#version 330 core
out vec4 color;
in vec4 v_Color;
in vec3 v_TexCoord;
in flat int v_TexIndex;
uniform samplerCube u_Textures[32];
void main()
{
color = texture(u_Textures[v_TexIndex], v_TexCoord) * v_Color;
}
唯一不起作用的部分是最后的采样部分.
The only part that does not work is the sampling part at the very end.
测试代码时,我在前两个索引的 u_Textures
数组中加载了两个纹理,并尝试绘制具有第一个纹理的立方体和具有第二个纹理的另一个立方体.
When I tested my code, I loaded two textures into the u_Textures
array in the first two indices and tried to draw a cube with the first texture and another cube with the second texture.
如果我对这样的索引进行硬编码
If I hardcode an index like this
color = texture(u_Textures[0], v_TexCoord) * v_Color;
或者像这样
color = texture(u_Textures[1], v_TexCoord) * v_Color;
在索引0或1处采样了正确的纹理,但是一旦我放置了 v_TexIndex
变量,它就会给我带来怪异的伪影,或者什么也没有.
The correct texture at index 0 or 1 is sampled but as soon as I place the v_TexIndex
variable, it gives me weird artifacts or just nothing at all.
所以我自动认为我的 v_TexIndex
变量一定不是我期望的变量,但是如果我将行替换为
So I automatically thought that my v_TexIndex
variable must not be what I expect it to be but if I replace the line with
color = vec4(v_TexIndex, 1.0, 1.0, 1.0);
当我期望 v_TexIndex
为1时我得到白色纹理,而当期望其为0时我得到绿松石!
I get a white texture when I expect v_TexIndex
to be 1 and a turquoise when I expect it to be 0!
推荐答案
片段着色器输入(例如 v_TexIndex
)无法索引采样器( u_Textures
)的数组. u_Textures [v_TexIndex]
是未定义的行为,因为 v_TexIndex
不是动态统一.
Arrays of samplers (u_Textures
) cannot be indexed by fragment shader inputs such as v_TexIndex
.u_Textures[v_TexIndex]
is undefined behavior, because v_TexIndex
is not Dynamically uniform.
请参阅GLSL版本4.60(最新版本)(摘自 OpenGL着色语言4.60规范-4.1.7.不透明类型):
See GLSL version 4.60 (most recent) (from OpenGL Shading Language 4.60 Specification - 4.1.7. Opaque Types):
在GLSL版本330中,限制更加严格.请参见 OpenGL着色语言3.30规范-4.1.7采样器:
In the GLSL version 330, the restriction is even harder. See OpenGL Shading Language 3.30 Specification - 4.1.7 Samplers:
这清楚地说明了为什么硬编码" 索引( u_Textures [0]
, u_Textures [1]
)起作用,但是u_Textures [v_TexIndex]
失败,并给出怪异的伪像" .
That clearly explains why the "hardcode" index (u_Textures[0]
, u_Textures[1]
) works, but u_Textures[v_TexIndex]
fails and gives "weird artifacts".
我建议使用s samplerCubeArray
(请参阅采样器),而不是 samplerCube
的数组.
当使用 samplerCubeArray
时,根本不需要任何索引,因为在纹理查找时索引"被编码在纹理坐标的第4个分量中(请参见 纹理
).
I recommend to use s samplerCubeArray
(see Sampler) rather than an array of samplerCube
.
When you use a samplerCubeArray
, then you don't need any indexing at all, because the "index" is encoded in the 4rd component of the texture coordinate at texture lookup (see texture
).
这篇关于为什么glsl变量不能按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!