GLSL - 检查设置属性

时间:2010-12-11 23:35:53

标签: opengl-es glsl

我有一个顶点着色器,其属性可以在任何给定的帧中设置,也可以不设置。如何检查这些属性是否已设置?

我想做什么:

attribute vec3 position;
attribute vec3 normal;
attribute vec4 color;
attribute vec2 textureCoord;


uniform mat4 perspective;
uniform mat4 modelview;
uniform mat4 camera;
uniform sampler2D textureSampler;

varying lowp vec4 vColor;

void main() {
    gl_Position = perspective * camera * modelview * vec4(position, 1.0);
 if ((bool)textureCoord) { // syntax error
     vColor = texture2D(textureSampler, textureCoord);
 } else {
     vColor = (bool)color ? color : vec4((normal.xyz + 1.0)/2.0 , 1.0); 
 }
}

1 个答案:

答案 0 :(得分:9)

  

我有一个顶点着色器,其属性可以在任何给定的帧中设置,也可以不设置。

不,你没有。 :)

使用属性,属性不可能“设置”。每个顶点着色器实例都从每个声明的属性中接收有效值。

如果glEnableVertexArray未启用属性数组,则将传递默认属性(由glVertexAttrib指定及其默认值)。


在您的情况下,您可以:

  • 使用或不使用纹理编辑不同版本的着色器(条件编译是你的朋友;谷歌为UberShader),
  • 使用像“useTexturing”这样的统一变量来保存着色器开关。

选择一个。