Search code examples
arraysglslfragment-shader

how to create an array of vec2 and pushing to it?


I am trying to create an empty array of vec2 in fragment shader.

also what are the syntax for getting the length and pushing vec2 to the array?


Solution

  • You cannot "push" values to and array in GLSL. The size of the array cannot be changed. All you can do is create an array of constant size and use an extra variable that tells you how many values of the array have meaning:

    vec2 array[10];
    int size = 0;
    
    array[size] = vec2(1.0, 0.0);
    size = size + 1;
    

    The length of an array can be get with length(). e.g.: array.length();. See also GLSL 4.6 - Basic Types.