https://learnopengl.com/Getting-started/Shaders

https://learnopengl.com/Getting-started/Shaders


Why divide by 2 and add 0.5?

float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUseProgram(shaderProgram);
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
  1. The natural range of $\sin$ is $[-1,1]$. Color values in OpenGL should fit in $[0, 1].$
  2. We take $\sin(\text{timeValue})/2$. The range is now $[-0.5, 0.5]$.
  3. Then, we translate this function upwards by 0.5.
  4. Finally, we get $[0, 1]$.