commit 42d41d50dd210a281749fe5aa9781290bab7a053
parent a1f6a78399ecd2d2d59590475a6ac3cd3a4e15de
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 17 Nov 2021 22:23:03 -0800
Added functions to set uniforms on shaders
Diffstat:
4 files changed, 48 insertions(+), 40 deletions(-)
diff --git a/include/glh/shader.h b/include/glh/shader.h
@@ -1,16 +0,0 @@
-#ifndef GLH_SHADER_H
-#define GLH_SHADER_H
-
-#include "glad/glad.h"
-
-typedef struct {
- GLuint program;
- const char* vert_filename;
- const char* frag_filename;
-} Shader;
-
-Shader Shader_create(const char* vert_filename, const char* frag_filename);
-
-void Shader_reload(Shader* shader);
-
-#endif
diff --git a/include/vec.h b/include/vec.h
@@ -8,5 +8,6 @@ typedef int IVec3[3];
bool IVec3_equal(const IVec3 a, const IVec3 b);
typedef float Vec3[3];
+typedef float Mat4[4][4];
#endif
diff --git a/src/main.c b/src/main.c
@@ -1,8 +1,8 @@
#include "app.h"
#include "camera.h"
#include "chunk_manager.h"
-#include "glh/shader.h"
#include "perlin.h"
+#include "shader.h"
#include "glad/glad.h"
@@ -44,15 +44,8 @@ int main(int argc, char** argv)
perlinSDF, 0.0f);
ChunkManager_drawChunks(&chunk_manager);
- Shader shader = Shader_create("shaders/basic.vs", "shaders/basic.fs");
- glUseProgram(shader.program);
-
- GLint view_pos_loc = glGetUniformLocation(shader.program, "view_pos");
-
- GLint pointlight_pos_loc = glGetUniformLocation(shader.program,
- "pointlight_pos");
-
- GLint cam_loc = glGetUniformLocation(shader.program, "camera");
+ Shader *shader = Shader_make("shaders/basic.vs", "shaders/basic.fs");
+ Shader_use(shader);
SDL_Event window_event;
while (true)
@@ -72,9 +65,8 @@ int main(int argc, char** argv)
App_hideCursor(app);
break;
case SDLK_r:
- Shader_reload(&shader);
- glUseProgram(shader.program);
- cam_loc = glGetUniformLocation(shader.program, "camera");
+ Shader_reload(shader);
+ Shader_use(shader);
break;
}
}
@@ -82,10 +74,9 @@ int main(int argc, char** argv)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Camera_move(camera);
- glUniformMatrix4fv(cam_loc, 1, GL_FALSE, camera->matrix[0]);
-
- glUniform3fv(view_pos_loc, 1, camera->position);
- glUniform3fv(pointlight_pos_loc, 1, camera->position);
+ Shader_setMat4(shader, "camera", camera->matrix);
+ Shader_setVec3(shader, "view_pos", camera->position);
+ Shader_setVec3(shader, "pointlight_pos", camera->position);
ChunkManager_recenter(&chunk_manager, camera->position);
ChunkManager_drawChunks(&chunk_manager);
diff --git a/src/shader.c b/src/shader.c
@@ -1,4 +1,4 @@
-#include "glh/shader.h"
+#include "shader.h"
#include <stdio.h>
#include <stdlib.h>
@@ -107,18 +107,37 @@ static GLuint createProgramFromFiles(const char* vertex_shader_filename,
return program;
}
-Shader Shader_create(const char* vertex_shader_filename,
- const char* fragment_shader_filename)
+struct Shader {
+ GLuint program;
+ const char *vert_filename;
+ const char *frag_filename;
+};
+
+Shader *Shader_make(const char* vert_filename, const char* frag_filename)
{
- GLuint program = createProgramFromFiles(vertex_shader_filename,
- fragment_shader_filename);
+ GLuint program = createProgramFromFiles(vert_filename, frag_filename);
- Shader shader = { program, vertex_shader_filename,
- fragment_shader_filename };
+ Shader *shader = malloc(sizeof *shader);
+ shader->program = program;
+ shader->vert_filename = vert_filename;
+ shader->frag_filename = frag_filename;
return shader;
}
+void Shader_free(Shader *shader)
+{
+ if (shader)
+ {
+ free(shader);
+ }
+}
+
+void Shader_use(Shader *shader)
+{
+ glUseProgram(shader->program);
+}
+
void Shader_reload(Shader* shader)
{
GLuint reloaded_program = createProgramFromFiles(shader->vert_filename,
@@ -130,3 +149,15 @@ void Shader_reload(Shader* shader)
shader->program = reloaded_program;
}
}
+
+void Shader_setVec3(const Shader *shader, const char *name, Vec3 v)
+{
+ GLint uniform_loc = glGetUniformLocation(shader->program, name);
+ glUniform3fv(uniform_loc, 1, v);
+}
+
+void Shader_setMat4(const Shader *shader, const char *name, Mat4 m)
+{
+ GLint uniform_loc = glGetUniformLocation(shader->program, name);
+ glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, m[0]);
+}
+\ No newline at end of file