invisible

annotate src/sdr.cc @ 25:96b022f1210e

'w' to switch to wireframe
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 17 Nov 2013 12:46:45 +0200
parents 38bf3eec0fac
children
rev   line source
eleni@19 1 #include <GL/glew.h>
eleni@19 2 #include <GL/gl.h>
eleni@19 3
eleni@19 4 #include <errno.h>
eleni@19 5 #include <stdio.h>
eleni@19 6 #include <stdlib.h>
eleni@19 7 #include <string.h>
eleni@19 8
eleni@19 9 #include "sdr.h"
eleni@19 10
eleni@19 11 char* sdr_load(const char* fname)
eleni@19 12 {
eleni@19 13 if(!fname) {
eleni@19 14 return 0;
eleni@19 15 }
eleni@19 16
eleni@19 17 FILE *fp;
eleni@19 18 int ctr = 0;
eleni@19 19
eleni@19 20 if(!(fp = fopen(fname, "rb"))) {
eleni@19 21 fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno));
eleni@19 22 return 0;
eleni@19 23 }
eleni@19 24 fseek(fp, 0, SEEK_END);
eleni@19 25 ctr = ftell(fp);
eleni@19 26 rewind(fp);
eleni@19 27
eleni@23 28 char *glsl;
eleni@19 29 if(!(glsl = (char*) malloc(ctr + 1))) {
eleni@19 30 perror("Failed to allocate memory");
eleni@19 31 return 0;
eleni@19 32 }
eleni@23 33 fread(glsl, 1, ctr, fp);
eleni@19 34 fclose(fp);
eleni@19 35
eleni@23 36 glsl[ctr] = 0;
eleni@23 37
eleni@19 38 return glsl;
eleni@19 39 }
eleni@19 40
eleni@19 41 bool sdr_compile(unsigned int sdr)
eleni@19 42 {
eleni@19 43 int status, loglen;
eleni@19 44 char infolog[512];
eleni@19 45
eleni@19 46 glCompileShader(sdr);
eleni@19 47 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
eleni@19 48 glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
eleni@19 49 if(status == GL_FALSE) {
eleni@19 50 fprintf(stderr, "Failed to compile shader: %s\n", infolog);
eleni@19 51 return false;
eleni@19 52 } else if(loglen) {
eleni@19 53 fprintf(stderr, "%s\n", infolog);
eleni@19 54 }
eleni@19 55 return true;
eleni@19 56 }
eleni@19 57
eleni@19 58 unsigned int sdr_getprog(const char* vfname, const char* ffname)
eleni@19 59 {
eleni@19 60 char *vsdr_glsl = NULL;
eleni@19 61 char *fsdr_glsl = NULL;
eleni@19 62
eleni@19 63 GLuint vsdr; GLuint fsdr;
eleni@19 64 vsdr = glCreateShader(GL_VERTEX_SHADER);
eleni@19 65 fsdr = glCreateShader(GL_FRAGMENT_SHADER);
eleni@19 66
eleni@19 67 if(!(vsdr_glsl = sdr_load(vfname)))
eleni@19 68 return 0;
eleni@19 69 if(!(fsdr_glsl = sdr_load(ffname)))
eleni@19 70 return 0;
eleni@19 71
eleni@19 72 glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
eleni@19 73 glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
eleni@19 74
eleni@19 75 free(vsdr_glsl);
eleni@19 76 free(fsdr_glsl);
eleni@19 77
eleni@19 78 if(!sdr_compile(vsdr)) {
eleni@19 79 fprintf(stderr, "Shader %s failed to compile.\n", vfname);
eleni@19 80 return 0;
eleni@19 81 }
eleni@19 82 if(!sdr_compile(fsdr)) {
eleni@19 83 fprintf(stderr, "Shader %s failed to compile.\n", ffname);
eleni@19 84 return 0;
eleni@19 85 }
eleni@19 86
eleni@19 87 unsigned int sprog;
eleni@19 88 int status, loglen;
eleni@19 89 char infolog[512];
eleni@19 90
eleni@19 91 sprog = glCreateProgram();
eleni@19 92 glAttachShader(sprog, vsdr);
eleni@19 93 glAttachShader(sprog, fsdr);
eleni@19 94 glLinkProgram(sprog);
eleni@19 95 glGetProgramiv(sprog, GL_LINK_STATUS, &status);
eleni@19 96 glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
eleni@19 97 if(status == GL_FALSE) {
eleni@19 98 fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
eleni@20 99 return 0;
eleni@19 100 } else if(loglen) {
eleni@19 101 fprintf(stderr, "%s\n", infolog);
eleni@19 102 }
eleni@19 103 return sprog;
eleni@19 104 }