Michael Dorgan
Michael Dorgan

Reputation: 12515

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?

Upvotes: 3

Views: 374

Answers (2)

elmattic
elmattic

Reputation: 12174

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

Upvotes: 4

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

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

Upvotes: 1

Related Questions