From 7215c6aba3f741e3eba5386da8b9c203a66444e0 Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 31 Mar 2024 20:14:33 -0700 Subject: [PATCH] Add a uniform color. --- src/main.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.cc b/src/main.cc index 039ef8a..5ede541 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,7 @@ #include #include #include +#include void framebuffer_size_callback(GLFWwindow* window, int width, int height); void process_input(GLFWwindow* window); @@ -11,11 +12,9 @@ const char* vertexShaderCode = R"( // Set the GLSL version to 3.3 and use the OpenGL core profile #version 330 core layout (location = 0) in vec3 aPos; -out vec4 vertexColor; void main() { gl_Position = vec4(aPos, 1.0); - vertexColor = vec4(0.5, 0.0, 0.0, 1.0); } )"; @@ -23,10 +22,11 @@ const char* fragmentShaderCode = R"( // Set the GLSL version to 3.3 and use the OpenGL core profile #version 330 core out vec4 FragColor; -in vec4 vertexColor; + +uniform vec4 ourColor; void main() { - FragColor = vertexColor; + FragColor = ourColor; } )"; @@ -221,6 +221,13 @@ int main() { // Use the shaderProgram shader program for drawing verticies. glUseProgram(shaderProgram); + + // Change the uniform color. + float timeValue = glfwGetTime(); + float greenValue = (std::sin(timeValue) / 2.0f) + 0.5f; + int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); + glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); + if (!rectangleToggled) { // Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies. glBindVertexArray(triangleRenderObjs->vertexArrayObject);