opengl - GLSL 1.5 和 OpenGL 3.3 : Passing color to vertex shader seems to fail

标签 opengl attributes colors glsl vertex-shader

将顶点属性传递给正在运行的着色器程序时出现问题。我想传递两个属性,位置和 RGBA 颜色。绑定(bind)属性位置适用于位置。但是,它不适用于颜色。因此,所有几何体最终都呈现为黑色。

这就是我所做的:

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;
}

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

最佳答案

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

glBindFragDataLocation(programHandler, 1, "FragColor");

它应该是:
glBindFragDataLocation(programHandler, 0, "FragColor");

关于opengl - GLSL 1.5 和 OpenGL 3.3 : Passing color to vertex shader seems to fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372955/

相关文章:

java - 如何在 LWJGL 中在 3D 上渲染具有透明度的 2D 纹理

python - 使用实例属性作为字典值

ios - 如何在 iOS 项目中使用 HDR 颜色?

attributes - @nogc 属性是否在 d 中实现?

python - 类似于本地文件的 HTTP Headers 的属性系统

javascript - 使用 JavaScript 基于值的 ASP.NET 条件格式

c# - 如何编写工具,在 Windows 中反转颜色?

opengl - 如何处理 OpenGL 索引缓冲区数组中的多个纹理以与数据着色器一起使用?

c++ - glewInit() 失败和 OpenGL 错误 1282

c++ - 渲染各向异性纹理的更快方法?