Use uniforms for color

This commit is contained in:
Phireh 2023-01-29 18:34:25 +01:00
commit 682a9b45e6
3 changed files with 27 additions and 7 deletions

26
main.c
View file

@ -12,7 +12,6 @@
#define log_err(str, ...) do { fprintf(stderr, "[ERROR] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0) #define log_err(str, ...) do { fprintf(stderr, "[ERROR] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
#define log_debug(str, ...) do { fprintf(stderr, "[DEBUG] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0) #define log_debug(str, ...) do { fprintf(stderr, "[DEBUG] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
char *read_entire_file(char *path) char *read_entire_file(char *path)
{ {
FILE *f = fopen(path, "rb"); FILE *f = fopen(path, "rb");
@ -38,7 +37,13 @@ static void glfw_error_callback(int errcode, const char *description)
fprintf(stderr, "[GLFW ERROR][ERRCODE %d] %s\n", errcode, description); fprintf(stderr, "[GLFW ERROR][ERRCODE %d] %s\n", errcode, description);
} }
int main() void set_uniform(GLuint program_id, const char *name, vec4f value)
{
GLint location = glGetUniformLocation(program_id, name);
glUniform4f(location, value.x, value.y, value.z, value.w);
}
int main(void)
{ {
GLFWwindow *window; GLFWwindow *window;
@ -61,7 +66,7 @@ int main()
char * vertexShaderSource = read_entire_file("shaders/vertex.glsl"); char * vertexShaderSource = read_entire_file("shaders/vertex.glsl");
char *fragmentShaderSource = read_entire_file("shaders/fragment.glsl"); char *fragmentShaderSource = read_entire_file("shaders/fragment.glsl");
// vertex shader // vertex shader
unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER); unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertexShaderSource, NULL); glShaderSource(vertex_shader, 1, &vertexShaderSource, NULL);
@ -106,8 +111,6 @@ int main()
background_color.b = 0.3f; background_color.b = 0.3f;
background_color.a = 1.0f; background_color.a = 1.0f;
hexgrid_t grid; hexgrid_t grid;
grid.rows = 2; grid.rows = 2;
grid.columns = 2; grid.columns = 2;
@ -181,6 +184,7 @@ int main()
//glBindBuffer(GL_ARRAY_BUFFER, 0); //glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindVertexArray(0); //glBindVertexArray(0);
int64_t framecount = 0;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
int width, height; int width, height;
@ -193,6 +197,15 @@ int main()
glUseProgram(shader_program); glUseProgram(shader_program);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// Set uniforms
vec4f hex_color;
hex_color.r = (float)(framecount % 1100)/1100;
hex_color.g = (float)(framecount % 300)/300;
hex_color.b = (float)(framecount % 1700)/1700;
hex_color.a = 1.0f;
set_uniform(shader_program, "uniform_color", hex_color);
for (size_t i = 0; i < grid.rows * grid.columns; ++i) for (size_t i = 0; i < grid.rows * grid.columns; ++i)
{ {
glBindVertexArray(grid.hexes[i].vao); glBindVertexArray(grid.hexes[i].vao);
@ -203,5 +216,8 @@ int main()
} }
glfwPollEvents(); glfwPollEvents();
glfwSwapBuffers(window); glfwSwapBuffers(window);
++framecount;
} }
return 0;
} }

View file

@ -1,6 +1,7 @@
#version 330 core #version 330 core
out vec4 color; in vec4 vertex_color;
out vec4 final_color;
void main() void main()
{ {
color = vec4(1.0f, 0.5f, 0.2f, 1.0f); final_color = vertex_color;
} }

View file

@ -1,6 +1,9 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 pos; layout (location = 0) in vec3 pos;
uniform vec4 uniform_color;
out vec4 vertex_color;
void main() void main()
{ {
gl_Position = vec4(pos, 1.0f); gl_Position = vec4(pos, 1.0f);
vertex_color = uniform_color;
} }