5 #include "client/client_config.h"
6 #include "client/opengl.h"
7 #include "client/shader.h"
9 static GLuint compile_shader(GLenum type, const char *path, const char *name, GLuint program, const char *def)
11 char full_path[strlen(path) + 1 + strlen(name) + 1 + 4 + 1];
12 sprintf(full_path, "%s/%s.glsl", path, name);
14 FILE *file = fopen(full_path, "r");
20 if (fseek(file, 0, SEEK_END) == -1) {
26 long size = ftell(file);
34 if (fseek(file, 0, SEEK_SET) == -1) {
42 if (fread(code, 1, size, file) != (size_t) size) {
50 GLuint id = glCreateShader(type); GL_DEBUG
52 const char *version = client_config.texture_batching
53 ? "#version 400 core\n"
54 : "#version 330 core\n";
56 const char *code_list[3] = {
68 glShaderSource(id, 3, code_list, size_list); GL_DEBUG
70 glCompileShader(id); GL_DEBUG
73 glGetShaderiv(id, GL_COMPILE_STATUS, &success); GL_DEBUG
76 glGetShaderInfoLog(id, BUFSIZ, NULL, errbuf); GL_DEBUG
77 fprintf(stderr, "[error] failed to compile shader %s: %s", full_path, errbuf);
78 glDeleteShader(id); GL_DEBUG
82 glAttachShader(program, id); GL_DEBUG
87 GLuint shader_program_create(const char *path, const char *def)
89 GLuint id = glCreateProgram(); GL_DEBUG
96 if (!(vert = compile_shader(GL_VERTEX_SHADER, path, "vertex", id, def))) {
97 glDeleteProgram(id); GL_DEBUG
101 if (!(frag = compile_shader(GL_FRAGMENT_SHADER, path, "fragment", id, def))) {
102 glDeleteShader(vert); GL_DEBUG
103 glDeleteProgram(id); GL_DEBUG
107 glLinkProgram(id); GL_DEBUG
108 glDeleteShader(vert); GL_DEBUG
109 glDeleteShader(frag); GL_DEBUG
112 glGetProgramiv(id, GL_LINK_STATUS, &success); GL_DEBUG
115 glGetProgramInfoLog(id, BUFSIZ, NULL, errbuf); GL_DEBUG
116 fprintf(stderr, "[error] failed to link shader program %s: %s\n", path, errbuf);
117 glDeleteProgram(id); GL_DEBUG