GLSL 2.0 Attribute Question

What does the [] operator do when accessing the vec4 attribute?


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 accessing the individual vec4 fields (.x, .y, .z, .w) or is it somehow handling vec4 and the vec4 array and doing something else?

+3
source share
2 answers

Yes to access individual fields.

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

You can also use array subtyping to access the matrix peers:

mat4 m;
vec4 c = m[1]; // access the second column of m
+4
source

Yes, it indexes the components x / y / z / w.

+1
source

Source: https://habr.com/ru/post/1773983/


All Articles