Add a uniform color.

This commit is contained in:
Fries 2024-03-31 20:14:33 -07:00
parent f5f08ce669
commit 7215c6aba3

View file

@ -1,6 +1,7 @@
#include <iostream>
#include <epoxy/gl.h>
#include <GLFW/glfw3.h>
#include <cmath>
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);