Opengl es GLSL-检查集合属性

Opengl es GLSL-检查集合属性,opengl-es,glsl,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 texture

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

我想做的是:

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); 
 }
}
我有一个顶点着色器,其属性可以在任何给定帧中设置,也可以不设置

不,你没有。:)

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

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


在您的情况下,您可以:

  • 在不同版本中编译着色器(有或没有纹理)(条件编译是您的朋友;谷歌用于UberShader)
  • 使用统一变量(如“UseMeturing”)保存着色器开关

选择一个。

我们正在讨论GL ES 2,我按?哦,顺便说一句,我所说的有一个例外-该属性可能会被编译器/链接器优化(即从着色器代码中删除,并用-1回答
GetVertexAttributeLocation
),如果它没有在着色器代码中的任何地方使用。但事实并非如此。@Kos很高兴知道openGL在这一点上能够自我管理。你知道如果没有其他定义,将使用哪些默认值吗?我希望是0或未定义,但我无法从头脑中分辨出来。最好只设定一次