GLSL 1.5 和OpenGL 3.3:将颜色传递给顶点着色器似乎失败

发布于 2024-10-06 12:21:54 字数 1745 浏览 6 评论 0原文

我在将顶点属性传递给正在运行的着色器程序时遇到问题。我想传递两个属性:位置和 RGBA 颜色。绑定属性位置适用于位置。但是,它不适用于颜色。因此,所有几何图形最终都被渲染为黑色。

这就是我所做的:

GLuint programHandler;

// create program
// compile & attach vertex shader
// compile & attach fragment shader

glBindAttribLocation(programHandler, 0, "InPosition");
glBindAttribLocation(programHandler, 1, "InColor");
glBindFragDataLocation(programHandler, 1, "FragColor");

glLinkProgram(programHandler);
glUseProgram(programHandler);

// initialize uniform variables

// I'm trying to get the attribute locations now.
// I expect location "0" for InPosition and location "1" for InColor.
// In fact, it gets me a "-1" for InColor. I simply cannot see the reason for that behaviour

GLint positionLocation = glGetAttribLocation(programHandler, "InPosition"); // returns 0
GLint colorLocation = glGetAttribLocation(programHandler, "InColor"); // returns -1

glEnableVertexAttribArray(positionLocation);
glEnableVertexAttribArray(colorLocation); // fails!

我的顶点着色器非常基本。我真正做的就是变换顶点并将颜色传递给片段着色器:

#version 150 core

// input
uniform mat4 ModelviewMatrix;
uniform mat4 ProjectionMatrix;
in vec3 InPosition;
in vec4 InColor;

// output
out vec4 PassColor;

void main(void) {
    // passing color through to fragment shader
    PassColor = InColor;

    // transformation
    gl_Position = ProjectionMatrix * ModelviewMatrix * vec4(InPosition, 1.0);
}

我的片段着色器应该简单地返回该颜色:

#version 150 core
precision highp float;

// input
in vec4 PassColor;

// output
out vec4 FragColor;

void main(void) {
    FragColor = PassColor;
}

为什么绑定“InPosition”有效而“InColor”无效?我知道 GLSL 编译器优化了着色器代码,因此无法绑定未使用的变量。但是,我不明白为什么应该在这里优化颜色。毕竟,我通过将其传递给片段着色器来使用它。

I have a problem when passing vertex attributes to the running shader program. I'd like to pass two attributes, the position and a RGBA-color. Binding the attribute location works for the position. However, it does not work for the color. Therefore, all the geometry is eventually rendered black.

This is what I do:

GLuint programHandler;

// create program
// compile & attach vertex shader
// compile & attach fragment shader

glBindAttribLocation(programHandler, 0, "InPosition");
glBindAttribLocation(programHandler, 1, "InColor");
glBindFragDataLocation(programHandler, 1, "FragColor");

glLinkProgram(programHandler);
glUseProgram(programHandler);

// initialize uniform variables

// I'm trying to get the attribute locations now.
// I expect location "0" for InPosition and location "1" for InColor.
// In fact, it gets me a "-1" for InColor. I simply cannot see the reason for that behaviour

GLint positionLocation = glGetAttribLocation(programHandler, "InPosition"); // returns 0
GLint colorLocation = glGetAttribLocation(programHandler, "InColor"); // returns -1

glEnableVertexAttribArray(positionLocation);
glEnableVertexAttribArray(colorLocation); // fails!

My vertex shader is very basic. All I really do is transforming vertices and passing the color to the fragment shader:

#version 150 core

// input
uniform mat4 ModelviewMatrix;
uniform mat4 ProjectionMatrix;
in vec3 InPosition;
in vec4 InColor;

// output
out vec4 PassColor;

void main(void) {
    // passing color through to fragment shader
    PassColor = InColor;

    // transformation
    gl_Position = ProjectionMatrix * ModelviewMatrix * vec4(InPosition, 1.0);
}

My fragment shader should simply return that color:

#version 150 core
precision highp float;

// input
in vec4 PassColor;

// output
out vec4 FragColor;

void main(void) {
    FragColor = PassColor;
}

Why does binding "InPosition" work and "InColor" does not? I am aware that the GLSL compiler optimizes the shader code, so that unused variables cannot be bound. But, I don't see why the color should be optimized out here. After all, I use it by passing it to the fragment shader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2024-10-13 12:21:54

盲目射击,我认为这是错误的:

glBindFragDataLocation(programHandler, 1, "FragColor");

应该是:

glBindFragDataLocation(programHandler, 0, "FragColor");

A blind shot, i believe this is wrong:

glBindFragDataLocation(programHandler, 1, "FragColor");

It should be:

glBindFragDataLocation(programHandler, 0, "FragColor");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文