#include #include char* file_read(const char* filename) { FILE* input = fopen(filename, "rb"); if (input == NULL) return NULL; if (fseek(input, 0, SEEK_END) == -1) return NULL; long size = ftell(input); if (size == -1) return NULL; if (fseek(input, 0, SEEK_SET) == -1) return NULL; char *content = malloc( (size_t) size +1 ); if (content == NULL) return NULL; fread(content, 1, (size_t)size, input); if (ferror(input)) { free(content); return NULL; } fclose(input); content[size] = '\0'; return content; } void print_log(GLuint object) { GLint log_length = 0; if (glIsShader(object)) glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length); else if (glIsProgram(object)) glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length); else { fprintf(stderr, "printlog: Not a shader or a program\n"); return; } char* log = (char*)malloc(log_length); if (glIsShader(object)) glGetShaderInfoLog(object, log_length, NULL, log); else if (glIsProgram(object)) glGetProgramInfoLog(object, log_length, NULL, log); fprintf(stderr, "%s", log); free(log); } GLuint create_shader(const char* filename, GLenum type) { const GLchar* source = file_read(filename); if (source == NULL) { fprintf(stderr, "Error opening %s: ", filename); perror(""); return 0; } GLuint res = glCreateShader(type); glShaderSource(res, 1, &source, NULL); free((void*)source); glCompileShader(res); GLint compile_ok = GL_FALSE; glGetShaderiv(res, GL_COMPILE_STATUS, &compile_ok); if (compile_ok == GL_FALSE) { fprintf(stderr, "%s:", filename); print_log(res); glDeleteShader(res); return 0; } return res; } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(800, 600); glutInitContextVersion(3, 3); glutInitContextProfile(GLUT_CORE_PROFILE); glutCreateWindow("test"); GLuint vs, fs; if ((vs = create_shader("raycast.vert", GL_VERTEX_SHADER)) == 0) return 1; if ((fs = create_shader("raycast.frag", GL_FRAGMENT_SHADER)) == 0) return 1; /* prog <- loadProgram "raycast.vert" "raycast.frag" currentProgram $= Just prog */ GLuint program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); GLint link_ok = GL_FALSE; glGetProgramiv(program, GL_LINK_STATUS, &link_ok); if (!link_ok) { fprintf(stderr, "glLinkProgram:"); print_log(program); } glUseProgram(program); glClear(GL_COLOR_BUFFER_BIT); }