5 #include "client/gl_debug.h"
6 #include "client/shader.h"
8 static GLuint compile_shader(GLenum type, const char *path, const char *name, GLuint program, const char *definitions)
10 char full_path[strlen(path) + 1 + strlen(name) + 1 + 4 + 1];
11 sprintf(full_path, "%s/%s.glsl", path, name);
13 FILE *file = fopen(full_path, "r");
19 if (fseek(file, 0, SEEK_END) == -1) {
25 long size = ftell(file);
33 if (fseek(file, 0, SEEK_SET) == -1) {
41 if (fread(code, 1, size, file) != (size_t) size) {
49 GLuint id = glCreateShader(type); GL_DEBUG
51 // Minimum OpenGL version is 4.2.0 (idk some shader feature from that version is required)
52 const char *version = "#version 420 core\n"; // 420 blaze it
54 const char *code_list[3] = {
66 glShaderSource(id, 3, code_list, size_list); GL_DEBUG
68 glCompileShader(id); GL_DEBUG
71 glGetShaderiv(id, GL_COMPILE_STATUS, &success); GL_DEBUG
74 glGetShaderInfoLog(id, BUFSIZ, NULL, errbuf); GL_DEBUG
75 fprintf(stderr, "[error] failed to compile %s shader: %s", name, errbuf);
76 glDeleteShader(id); GL_DEBUG
80 glAttachShader(program, id); GL_DEBUG
85 bool shader_program_create(const char *path, GLuint *idptr, const char *definitions)
87 GLuint id = glCreateProgram(); GL_DEBUG
94 if (!(vert = compile_shader(GL_VERTEX_SHADER, path, "vertex", id, definitions))) {
95 glDeleteProgram(id); GL_DEBUG
99 if (!(frag = compile_shader(GL_FRAGMENT_SHADER, path, "fragment", id, definitions))) {
100 glDeleteShader(vert); GL_DEBUG
101 glDeleteProgram(id); GL_DEBUG
105 glLinkProgram(id); GL_DEBUG
106 glDeleteShader(vert); GL_DEBUG
107 glDeleteShader(frag); GL_DEBUG
110 glGetProgramiv(id, GL_LINK_STATUS, &success); GL_DEBUG
113 glGetProgramInfoLog(id, BUFSIZ, NULL, errbuf); GL_DEBUG
114 fprintf(stderr, "[error] failed to link shader program: %s\n", errbuf);
115 glDeleteProgram(id); GL_DEBUG