ios - 片段着色器未读取顶点着色器 'colorVarying' 的输出

标签 ios opengl-es opengl-es-2.0 glsl shader

如下所示,错误很奇怪。我在 iPad 程序中使用 OpenGLES 2.0 和着色器,但代码或项目配置似乎出了问题。该模型完全没有颜色(黑色)绘制。

2012-12-01 14:21:56.707 medicare[6414:14303] Program link log:
WARNING: Could not find vertex shader attribute 'color' to match BindAttributeLocation request.
WARNING: Output of vertex shader 'colorVarying' not read by fragment shader
[Switching to process 6414 thread 0x1ad0f]

我使用 glBindAttibLocation 来传递位置和普通数据,如下所示:

// This needs to be done prior to linking.
glBindAttribLocation(_program, INDEX_POSITION, "position");
glBindAttribLocation(_program, INDEX_NORMAL, "normal");

glBindAttribLocation(_program, INDEX_COLOR, "color"); //pass color to shader

我的项目中有两个着色器。那么对于这个奇怪的错误有什么好的解决方案吗?非常感谢!

我的顶点着色器:

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

attribute vec4 position;
attribute vec3 normal;
attribute vec4 color;

varying lowp vec4 DestinationColor;
void main()
{
    //vec4 a_Color = vec4(0.9, 0.4, 0.4, 1.0);
    vec4 a_Color = color;
    vec3 u_LightPos = vec3(1.0, 1.0, 2.0);

    float distance = 2.4;
    vec3 eyeNormal=normalize(normalMatrix * normal);

    float diffuse = max(dot(eyeNormal, u_LightPos), 0.0); // remove approx ambient light
    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
    DestinationColor = a_Color * diffuse; // average between ambient and diffuse   a_Color * (diffuse + 0.3)/2.0;

    gl_Position = modelViewProjectionMatrix * position;
}

我的片段着色器是:

varying lowp vec4 DestinationColor;

void main()
{
  gl_FragColor = DestinationColor;
}

非常简单。非常感谢!

最佳答案

我认为这里有一些问题。首先,您对属性的使用可能不正确。属性就像每个顶点都会变化的元素。您的数据结构中是否有颜色作为元素?因为如果没有,着色器将无法正常工作。

And I use glBindAttibLocation to pass position and normal data like this:

不,你不知道。 glBindAttribLocation“将通用顶点属性索引与命名属性变量关联起来”。它不传递数据。它将索引(闪烁)与变量相关联。稍后您可以使用 glVertexAttribPointer 传递内容。

我什至不使用绑定(bind)..我这样做 - 设置属性:

glAttributes[PROGNAME][A_vec3_vertexPosition] = glGetAttribLocation(glPrograms[PROGNAME],   "a_vertexPosition");
glEnableVertexAttribArray(glAttributes[PROGNAME][A_vec3_vertexPosition]);

然后在调用 glDrawElemetns 之前将指针传递给它,以便它可以获取数据:

glVertexAttribPointer(glAttributes[PROGNAME][A_vec3_vertexPosition], 3, GL_FLOAT, GL_FALSE, stride, (void *) 0);

在那里,我使用一个名为 glAttributes 的二维整数数组来保存所有属性索引。但您可以像现在一样使用闪烁。

错误消息告诉您出了什么问题。在你的顶点着色器中你说:

attribute vec4 color;

但是在下面你还有一个a_Color:

DestinationColor = a_Color * diffuse;

与变量名称保持一致。我现在将 a_ v_ 和 u_ 放在所有变量的前面,以尝试弄清楚它是什么类型的变量。你所说的 a_ 确实存在变化。

我还怀疑错误消息不是来自您因错误而发布的同一版本的着色器和代码:

WARNING: Output of vertex shader 'colorVarying' not read by fragment shader

有关 colorVarying 的错误令人困惑,因为它甚至不在这个版本的顶点着色器中。重新发布当前版本的着色器以及从中获得的错误消息,这样可以更轻松地为您提供帮助。

关于ios - 片段着色器未读取顶点着色器 'colorVarying' 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13657076/

相关文章:

android - 将纹理应用于立方体,立方体的每个面上都有不同的纹理

iphone - 射线-三角形交叉点拾取不起作用

opengl-es - 顶点着色器多久被调用一次?

iphone - 如何覆盖 UILabel 的 setFrame

ios - UIImageView 上的 SizeToFit 不起作用

opengl-es - 使用具有球体 Material 属性和 float 的 '/' 运算符时出错 (glsl)

ios - 如何创建GPUImage网格效果

glsl - 如何使用 Open GL ES 2.0 或 WebGL 创建雾?

iphone - 如果在 addSubView 之后调用,UIButton 不会移动

ios - 无法点击 CollectionViewCell 中的按钮