简体   繁体   中英

GLSL - Checking for set attributes

I have a vertex shader with attributes that may or may not be set in any given frame. How can I check whether or not these attributes have been set?

What I'd like to do:

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

I have a vertex shader with attributes that may or may not be set in any given frame.

No, you don't. :)

With attributes, it's impossible that an attribute wouldn't be "set". Every vertex shader instance receives valid values from every declared attribute.

If the attribute array is not enabled by glEnableVertexArray , then the default attribute (as specified by glVertexAttrib and its defaults) will be passed.


In your case, you can either:

  • compile your shader in different versions with or without texturing (conditional compilation is your friend; google for UberShader),
  • use an uniform variable like "useTexturing" to save on shader switches.

Pick one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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