Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL - Checking for set attributes

Tags:

glsl

opengl-es

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); 
 }
}
like image 574
schellsan Avatar asked Dec 29 '22 04:12

schellsan


1 Answers

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.

like image 193
Kos Avatar answered Jan 03 '23 14:01

Kos