我目前正在尝试使用LWJGL创建用于后处理目的的模糊着色器,但偶然发现了一个问题。
我要更改的Uniform是分辨率,模糊的半径和方向(水平或垂直,因此我可以在两个方向上使用一个Shader)。我将输出渲染到FrameBuffer,然后将其渲染到屏幕。但是不知何故,纹理只是黑色。当我使用常量值而不是均匀值时,它的工作原理完全符合预期,因此问题必定是均匀变量。
这是我的片段着色器的代码:
#version 330 core
out vec4 fragColor;
in vec2 coords;
in vec4 color;
in float samplerIndex;
uniform sampler2D samplers[32]; //Just an Array with ints from 0 to 31
uniform float radius;
uniform vec2 resolution; //Screen Resolution, or width & height of Framebuffer
uniform vec2 direction; //Either 1 or 0 for x and y
void main() {
int index = int(samplerIndex);
vec4 sum = vec4(0.0);
float blurX = radius / resolution.x * direction.x;
float blurY = radius / resolution.y * direction.y;
sum += texture2D(samplers[index], vec2(coords.x - 4.0 * blurX, coords.y - 4.0 * blurY)) * 0.0162162162;
sum += texture2D(samplers[index], vec2(coords.x - 3.0 * blurX, coords.y - 3.0 * blurY)) * 0.0540540541;
sum += texture2D(samplers[index], vec2(coords.x - 2.0 * blurX, coords.y - 2.0 * blurY)) * 0.1216216216;
sum += texture2D(samplers[index], vec2(coords.x - 1.0 * blurX, coords.y - 1.0 * blurY)) * 0.1945945946;
sum += texture2D(samplers[index], vec2(coords.x, coords.y)) * 0.2270270270;
sum += texture2D(samplers[index], vec2(coords.x + 1.0 * blurX, coords.y + 1.0 * blurY)) * 0.1945945946;
sum += texture2D(samplers[index], vec2(coords.x + 2.0 * blurX, coords.y + 2.0 * blurY)) * 0.1216216216;
sum += texture2D(samplers[index], vec2(coords.x + 3.0 * blurX, coords.y + 3.0 * blurY)) * 0.0540540541;
sum += texture2D(samplers[index], vec2(coords.x + 4.0 * blurX, coords.y + 4.0 * blurY)) * 0.0162162162;
fragColor = color * vec4(sum.rgb, 1.0);
}
这是我渲染FrameBuffer的方式:
public void render(boolean fixed) {
model.setTextureID(Shader.getTextureID(texture));
model.buffer(vertices);
indices.put(0).put(1).put(2).put(2).put(3).put(0);
vertices.flip();
indices.flip();
shader.bind();
texture.bind();
shader.setUniform1iv("samplers", Shader.SAMPLERS);
shader.setUniform1f("radius", 10.0f);
shader.setUniform2f("resolution", width, height);
shader.setUniform2f("direction", horizontal, vertical);
if(fixed) {
shader.setUniformMatrix4f("camProjection", Camera.fixedProjection);
shader.setUniformMatrix4f("camTranslation", Camera.fixedTranslation);
} else {
shader.setUniformMatrix4f("camProjection", Camera.projection);
shader.setUniformMatrix4f("camTranslation", Camera.translation);
}
vao.bind();
vbo.bind();
vbo.uploadSubData(0, vertices);
ibo.bind();
ibo.uploadSubData(0, indices);
GL11.glDrawElements(GL11.GL_TRIANGLES, Model.INDICES, GL11.GL_UNSIGNED_INT, 0);
vao.unbind();
texture.unbind();
shader.unbind();
vertices.clear();
indices.clear();
}
虽然进样器制服工作得很好,但问题似乎只影响其他三套制服。我对OpenGL和GLSL没有太多经验,我缺少什么?
由于以下原因,您的着色器代码根本无法工作
samplers[index]
[samplers
是sampler2D
的数组,并且从顶点着色器输入index
中设置了samplerIndex
:
int index = int(samplerIndex);
请参阅您使用的GLSL版本3.30(从OpenGL Shading Language 3.30 Specification - 4.1.7 Samplers开始):
样本聚合成数组在着色器中(使用方括号[])只能使用整数常量表达式进行索引
请参阅GLSL 4.60版(最新)(摘自OpenGL Shading Language 4.60 Specification - 4.1.7. Opaque Types):(此规则适用于GLSL 4.00以后的所有版本)
当聚合到着色器中的数组中时,只能使用动态统一表达式对这些类型进行索引,否则纹理查找将导致未定义的值。
因此,无论是在您使用的GLSL版本中,还是在最新版本中,都不能通过顶点着色器输入(属性)对采样器数组进行索引。
自GLSL 4.00起,就可以通过统一方式对采样器数组进行索引,因为通过统一变量进行索引是dynamically uniform expression。