summaryrefslogtreecommitdiff
path: root/src/shader.c
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2026-01-25 15:10:37 -0600
committerAaditya Dhruv <[email protected]>2026-01-25 15:10:37 -0600
commit118980e02e59ff31871df59dce257075394f3533 (patch)
tree26fba4492bb4b561d21bf49b35d892a821d54fab /src/shader.c
parent0e6e1245b70df4dfcba135d50e1b53d1a8ef7eb8 (diff)
wip
Diffstat (limited to 'src/shader.c')
-rw-r--r--src/shader.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/shader.c b/src/shader.c
new file mode 100644
index 0000000..c78a7e5
--- /dev/null
+++ b/src/shader.c
@@ -0,0 +1,63 @@
+#include "shader.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+
+
+/**
+ * Simple method to read shaders from path and return a string
+ * containing the code. Function allocates memory which needs
+ * to be cleaned up
+ * @param path Path to shader GLSL file
+ * @return buf Contents of shader file as a string
+ */
+char* read_shader(char* path) {
+ FILE* file = fopen(path, "r");
+ fseek(file, 0, SEEK_END);
+ int file_size = ftell(file);
+ rewind(file);
+ char* buf = malloc(file_size);
+ fread(buf, file_size, 1, file);
+ return buf;
+}
+
+/**
+ * Compile some given shader code
+ * @param type The type of shader (GL_VERTEX_SHADER, GL_FRAGMENT_SHADER etc)
+ * @param code The string corresponding to the shader code
+ * @return shader The GLuint pointer to the compiled shader
+ */
+GLuint compile_shader(GLuint type, const char* code) {
+ GLuint shader = glCreateShader(type);
+ glShaderSource(shader, 1, &code, NULL);
+ glCompileShader(shader);
+ return shader;
+}
+
+/*
+ * Given a vertex and fragment shader, link and compile a shader program,
+ * returning the GLuint pointing to that program
+ * @param vs The vertex shader string, usually returend from read_shader
+ * @param fs The fragment shader string, usually returend from read_shader
+ * @return shader_program The pointer to the compiled shader
+ */
+GLuint create_shader_program(const char* vs, const char* fs) {
+ GLuint shader_program = glCreateProgram();
+ GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vs);
+ GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fs);
+ glAttachShader(shader_program, fragment_shader);
+ glAttachShader(shader_program, vertex_shader);
+ glLinkProgram(shader_program);
+ return shader_program;
+}
+
+
+GLuint shader_init() {
+ char* shaders[2] = { "shaders/fragment.glsl", "shaders/vertex.glsl" };
+ char* fragment_shader = read_shader(shaders[0]);
+ char* vertex_shader = read_shader(shaders[1]);
+ GLuint shader_program = create_shader_program(vertex_shader, fragment_shader);
+ free(vertex_shader);
+ free(fragment_shader);
+ return shader_program;
+}