C++ insert vec4 in array (Shader)

If you have a array of vec4 how do you add more vec4 to that array?

vec4 colors [] = {vec4(0)};

for( int i = 0; i < 3; i++) {
	//add vec4 to colors
}

>>> colors = {vec4(0),vec4(0),vec4(0),vec4(0)}

std::vector<vec4> colors; and colors.push_back (vec4(0)); won’t work even in you #include <vector>

You can’t add elements to an array in C++ after declaring its size, as its size is fixed when it is declared. You could also declare an array with some fixed size, and then add more colors to the array by changing the first null element of the array to the color you want to add.

Using a vector with push_back should also work, if you don’t mind not using arrays. What do you mean by that it doesn’t work? Are you receiving an error message?

1 Like

Shader language is only visually similar to C++. It’s a different language completely called GLSL.
So there is no std lib. You have to preallocate the array with sufficient size and later on you can assign new values with
colors[i] = vec4(r, g, b, a)
Where i is the element index.

2 Likes

ok, nice :+1: