invisible

annotate src/sdr.cc @ 20:38bf3eec0fac

fixed false return
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 10 Nov 2013 14:03:37 +0200
parents f756fc9fdd3e
children b50ad2711f5f
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@19 28 char *glsl = NULL;
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@19 33 ctr = fread(glsl, 1, ctr, fp);
eleni@19 34 fclose(fp);
eleni@19 35
eleni@19 36 return glsl;
eleni@19 37 }
eleni@19 38
eleni@19 39 bool sdr_compile(unsigned int sdr)
eleni@19 40 {
eleni@19 41 int status, loglen;
eleni@19 42 char infolog[512];
eleni@19 43
eleni@19 44 glCompileShader(sdr);
eleni@19 45 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
eleni@19 46 glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
eleni@19 47 if(status == GL_FALSE) {
eleni@19 48 fprintf(stderr, "Failed to compile shader: %s\n", infolog);
eleni@19 49 return false;
eleni@19 50 } else if(loglen) {
eleni@19 51 fprintf(stderr, "%s\n", infolog);
eleni@19 52 }
eleni@19 53 return true;
eleni@19 54 }
eleni@19 55
eleni@19 56 unsigned int sdr_getprog(const char* vfname, const char* ffname)
eleni@19 57 {
eleni@19 58 char *vsdr_glsl = NULL;
eleni@19 59 char *fsdr_glsl = NULL;
eleni@19 60
eleni@19 61 GLuint vsdr; GLuint fsdr;
eleni@19 62 vsdr = glCreateShader(GL_VERTEX_SHADER);
eleni@19 63 fsdr = glCreateShader(GL_FRAGMENT_SHADER);
eleni@19 64
eleni@19 65 if(!(vsdr_glsl = sdr_load(vfname)))
eleni@19 66 return 0;
eleni@19 67 if(!(fsdr_glsl = sdr_load(ffname)))
eleni@19 68 return 0;
eleni@19 69
eleni@19 70 glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
eleni@19 71 glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
eleni@19 72
eleni@19 73 free(vsdr_glsl);
eleni@19 74 free(fsdr_glsl);
eleni@19 75
eleni@19 76 if(!sdr_compile(vsdr)) {
eleni@19 77 fprintf(stderr, "Shader %s failed to compile.\n", vfname);
eleni@19 78 return 0;
eleni@19 79 }
eleni@19 80 if(!sdr_compile(fsdr)) {
eleni@19 81 fprintf(stderr, "Shader %s failed to compile.\n", ffname);
eleni@19 82 return 0;
eleni@19 83 }
eleni@19 84
eleni@19 85 unsigned int sprog;
eleni@19 86 int status, loglen;
eleni@19 87 char infolog[512];
eleni@19 88
eleni@19 89 sprog = glCreateProgram();
eleni@19 90 glAttachShader(sprog, vsdr);
eleni@19 91 glAttachShader(sprog, fsdr);
eleni@19 92 glLinkProgram(sprog);
eleni@19 93 glGetProgramiv(sprog, GL_LINK_STATUS, &status);
eleni@19 94 glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
eleni@19 95 if(status == GL_FALSE) {
eleni@19 96 fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
eleni@20 97 return 0;
eleni@19 98 } else if(loglen) {
eleni@19 99 fprintf(stderr, "%s\n", infolog);
eleni@19 100 }
eleni@19 101 return sprog;
eleni@19 102 }