Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL 2.0 attribute question

What does the [] operator do when addressing an attribute vec4?


attribute vec4 a_MatrixWeights;
...
foo(float weight);
...

void main()
{
  foo(a_MatrixWeights[0]);
  foo(a_MatrixWeights[1]);
  foo(a_MatrixWeights[2]);
  foo(a_MatrixWeights[3]);
}

Is this addressing the individual fields of the vec4 (.x, .y, .z, .w) or is this somehow treating the vec4 and an array of vec4 and doing something else?

like image 667
Michael Dorgan Avatar asked Dec 17 '25 01:12

Michael Dorgan


2 Answers

Yes it's for accessing individual fields.

Array subscripting syntax can also be applied to vectors to provide numeric indexing. So in vec4 pos; pos[2] refers to the third element of pos and is equivalent to pos.z [GLSL spec 1.20.8, 5.5 Vector Components]

Also array subscripting can be use for accessing colums of matrices:

mat4 m;
vec4 c = m[1]; // access the second column of m
like image 169
Stringer Avatar answered Dec 19 '25 18:12

Stringer


Yes, it's indexing the x/y/z/w components.

like image 38
Dr. Snoopy Avatar answered Dec 19 '25 19:12

Dr. Snoopy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!