Can you tell if a vertex attribute is enabled from within a vertex shader?
Asked Answered
I

2

8

I was wondering if there was a way to tell if a vertex attribute is enabled from within a vertex shader? I know that if the vertex attribute is disabled all the values will be treated as 0.0, so I could do a test like the following:

if (attribute == 0)
{
    // Do something different to normal.
}
else
{
    // Use the attribute.
}

But this has the obvious problem for the case that the attribute is enabled and the value is just set to 0 (it will be treated as if it's disabled)!

The other solution would be to just use a uniform variable that states whether or not to use the attribute, but I wondered if there was anything built into GLSL that would do that?

Impala answered 22/2, 2012 at 20:31 Comment(0)
G
7

No there isn't.

Pass a boolean uniform yourself to emulate it.

Giggle answered 22/2, 2012 at 20:42 Comment(1)
Thanks! That's what I went with. Would be nice if this could become a GLSL feature in the future!Impala
I
18

FYI:

I know that if the vertex attribute is disabled all the values will be treated as 0.0, so I could do a test like the following:

That is not true. If an attribute is disabled, its value comes from regular OpenGL state. Namely, the state set by the glVertexAttrib functions. So it is perfectly legal to have these kinds of "constant attributes" sent to shaders.

That's why the API doesn't have a way for a shader to tell if an attribute is "disabled". A "disabled" attribute may still have meaningful data.

Inclement answered 22/2, 2012 at 22:6 Comment(3)
@OP So your solution of checking for a specific "not enabled"-value could be adapted to use a value different than 0, one that isn't a "real" attribute value that could occur otherwise, by just setting glVertexAttrib to this value. So you don't need an additional boolean.Hasbeen
Oh! Does the glVertexAttrib function set the value that will be used for every vertex? So for example if I called glVertexAttrib1f(3, -1.0f) then all the vertex attributes at location 3 would be set to -1.0f?Impala
@JamesBedford: Yes. Unless that attribute's array is enabled.Inclement
G
7

No there isn't.

Pass a boolean uniform yourself to emulate it.

Giggle answered 22/2, 2012 at 20:42 Comment(1)
Thanks! That's what I went with. Would be nice if this could become a GLSL feature in the future!Impala

© 2022 - 2024 — McMap. All rights reserved.