【问题标题】:libgdx: uniform color not sent down to fragment shaderlibgdx:统一颜色未发送到片段着色器
【发布时间】:2017-03-04 04:15:46
【问题描述】:

我有一个使用自定义着色器程序的非常简单的着色器。我正在发送相机转换,作为制服的世界转换,它们都有效。我也以相同的方式发送统一的颜色,但它似乎不会影响屏幕上的颜色。使用set() 它只会变成黑色,就像颜色设置不正确一样。

来自 libgdx 着色器的代码:

protected final int u_projTrans = register(new Uniform("u_projTrans"));
protected final int u_worldTrans = register(new Uniform("u_worldTrans"));
protected final int u_color = register(new Uniform("u_color"));

@Override
public void begin(Camera camera, RenderContext context) {
    program.begin();
    context.setDepthTest(GL20.GL_LEQUAL, 0f, 1f);
    context.setDepthMask(true);
    set(u_projTrans, camera.combined);
}

@Override
public void render(Renderable renderable) {
    set(u_worldTrans, renderable.worldTransform);

    set(u_color, 1, 1, 1); // binding, but no change in color
    // program.setUniformf("u_color", 1, 1, 1); // works

    renderable.meshPart.render(program);
}

查看上面的代码,我在 begin() 函数中绑定了 u_projTrans,在 render() 函数中绑定了 u_worldTrans。我知道这些都是根据我看到的图像工作的。

顶点着色器:

attribute vec3 a_position;
uniform mat4 u_projTrans;
uniform mat4 u_worldTrans;

void main() {
    gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}

片段着色器:

uniform vec3 u_color;

void main() {
    gl_FragColor = vec4(u_color, 1); // hard-coding this to a color works, so the value in u_color seems to be (0,0,0)
}

当我尝试设置颜色统一时,它说它正确绑定,但我只是没有像值设置为 (0,0,0) 那样颜色发生任何变化。但是,使用 program.setUniformf() 可以。使用set(),我将它完全绑定为其他人,所以我不知道除了将其发送为vec3 而不是mat4 之外有什么不同。

有谁知道为什么我在set(u_color, 1, 1, 1) 中传递的值没有进入着色器?

【问题讨论】:

    标签: opengl libgdx glsl


    【解决方案1】:

    当您调用set(u_color, 1, 1, 1); 时,您正在使用set(Uniform, int, int int) 方法。您想要的是set(Uniform, float, float, float) 方法,所以请改用set(u_color, 1f, 1f, 1f)

    【讨论】:

    • 修复了它。我很困惑,因为我认为它可能将其解释为 int 并尝试了更高的值,例如 1000,但它仍然是黑色的。我没有意识到它会默默地失败。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多