volmetrics

annotate src/sdr.cc @ 31:cfeecd093730

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