简体   繁体   English

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

[英]GLSL 1.5 & OpenGL 3.3: Passing color to vertex shader seems to fail

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. 我想传递两个属性,即位置和RGBA颜色。 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? 为什么绑定“InPosition”工作和“InColor”不工作? I am aware that the GLSL compiler optimizes the shader code, so that unused variables cannot be bound. 我知道GLSL编译器优化着色器代码,因此不能绑定未使用的变量。 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. 毕竟,我通过将它传递给片段着色器来使用它。

A blind shot, i believe this is wrong: 盲目的,我相信这是错误的:

glBindFragDataLocation(programHandler, 1, "FragColor");

It should be: 它应该是:

glBindFragDataLocation(programHandler, 0, "FragColor");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM