static void init_shader_program() { GLuint vertex_shader, fragment_shader; GLint program_ok; vertex_shader = init_shader(GL_VERTEX_SHADER, "shaders/vertex.glsl"); fragment_shader = init_shader(GL_FRAGMENT_SHADER, "shaders/fragment.glsl"); if (vertex_shader == 0 || fragment_shader == 0) { Log("Could not init shaders\n"); } program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &program_ok); if (!program_ok) { Log("Failed to link shader program."); Log("Vertex shader log: %s", get_shader_log(vertex_shader)); Log("Fragment shader log: %s", get_shader_log(fragment_shader)); Engine_print_program_log(); glDeleteProgram(program); } glUseProgram(program); }
void shader_program::add_shader(const char* shader, const char* fname, GLenum type) { GLuint s = glCreateShader (type); glShaderSource(s, 1, &shader, NULL); glCompileShader(s); int result; glGetShaderiv(s, GL_COMPILE_STATUS, &result); if (result != GL_TRUE) { printf("Error compiling %s\n%s\n",fname, get_shader_log(s).c_str()); backtrace(); exit(1); } glAttachShader(program_id, s); attached.push_back(s); }
static GLuint init_shader(GLenum type, const char *filename) { GLchar *version; GLchar *source; GLuint shader; GLint length, shader_ok; // My linux laptop only has 300es support. #ifdef __APPLE__ version = "#version 330\n"; #elif __linux__ version = "#version 300 es\n"; #endif source = Loader_get_file_contents(filename, &length); if (!source) { Log("Failed to load shader file: %s\n", filename); return 0; } shader = glCreateShader(type); if (shader == 0) { Log("Could not create shader %d\n", type); return 0; } const char *sources[2] = { version, source }; GLint lengths[2] = { strlen(version), length }; glShaderSource(shader, 2, sources, lengths); free(source); glCompileShader(shader); glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok); if (!shader_ok) { char *log = get_shader_log(shader); Log("Compilation error in shader %s: %s\n", filename, log); glDeleteShader(shader); return 0; } return shader; }