5 #include "client/shader.h"
7 static GLuint compile_and_attach_shader(GLenum type, const char *path, const char *name, GLuint program, const char *definitions)
9 char full_path[strlen(path) + 1 + strlen(name) + 1 + 4 + 1];
10 sprintf(full_path, "%s/%s.glsl", path, name);
12 FILE *file = fopen(full_path, "r");
18 if (fseek(file, 0, SEEK_END) == -1) {
24 long size = ftell(file);
32 if (fseek(file, 0, SEEK_SET) == -1) {
40 if (fread(code, 1, size, file) != (size_t) size) {
48 GLuint id = glCreateShader(type);
50 const char *version = "#version 460 core\n";
52 const char *code_list[3] = {
64 glShaderSource(id, 3, code_list, size_list);
69 glGetShaderiv(id, GL_COMPILE_STATUS, &success);
72 glGetShaderInfoLog(id, BUFSIZ, NULL, errbuf);
73 fprintf(stderr, "Failed to compile %s shader: %s", name, errbuf);
78 glAttachShader(program, id);
83 bool shader_program_create(const char *path, GLuint *idptr, const char *definitions)
85 GLuint id = glCreateProgram();
92 if (! (vert = compile_and_attach_shader(GL_VERTEX_SHADER, path, "vertex", id, definitions))) {
97 if (! (frag = compile_and_attach_shader(GL_FRAGMENT_SHADER, path, "fragment", id, definitions))) {
104 glDeleteShader(vert);
105 glDeleteShader(frag);
108 glGetProgramiv(id, GL_LINK_STATUS, &success);
111 glGetProgramInfoLog(id, BUFSIZ, NULL, errbuf);
112 fprintf(stderr, "Failed to link shader program: %s\n", errbuf);