Search code examples
glslshader

Compute the cosine of a vector


I'm learning about shader, and I've come across the following GLSL code:

vec3 color = cos(vec3(.5,.3,.4));

How do I compute the cosine of a vector vec3(.5,.3,.4)?


Solution

  • In GLSL most of the functions are overloaded and the argument can be a vector. Operations and functions may operate component wise. In case of cos, the cosine is computed for each component of the vector and the result is stored in a new vector:

    The expression statement

    vec3 color = cos(vec3(.5,.3,.4));
    

    can be read as

    vec3 color = vec3(cos(.5), cos(.3), cos(.4));