invisible

diff src/sdr.cc @ 19:f756fc9fdd3e

quick backup
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 10 Nov 2013 14:02:36 +0200
parents
children 38bf3eec0fac
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sdr.cc	Sun Nov 10 14:02:36 2013 +0200
     1.3 @@ -0,0 +1,102 @@
     1.4 +#include <GL/glew.h>
     1.5 +#include <GL/gl.h>
     1.6 +
     1.7 +#include <errno.h>
     1.8 +#include <stdio.h>
     1.9 +#include <stdlib.h>
    1.10 +#include <string.h>
    1.11 +
    1.12 +#include "sdr.h"
    1.13 +
    1.14 +char* sdr_load(const char* fname)
    1.15 +{
    1.16 +	if(!fname) {
    1.17 +		return 0;
    1.18 +	}
    1.19 +
    1.20 +	FILE *fp;
    1.21 +	int ctr = 0;
    1.22 +
    1.23 +	if(!(fp = fopen(fname, "rb"))) {
    1.24 +		fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno));
    1.25 +		return 0;
    1.26 +	}
    1.27 +	fseek(fp, 0, SEEK_END);
    1.28 +	ctr = ftell(fp);
    1.29 +	rewind(fp);
    1.30 +
    1.31 +	char *glsl = NULL;
    1.32 +	if(!(glsl = (char*) malloc(ctr + 1))) {
    1.33 +		perror("Failed to allocate memory");
    1.34 +		return 0;
    1.35 +	}
    1.36 +	ctr = fread(glsl, 1, ctr, fp);
    1.37 +	fclose(fp);
    1.38 +
    1.39 +	return glsl;
    1.40 +}
    1.41 +
    1.42 +bool sdr_compile(unsigned int sdr)
    1.43 +{
    1.44 +	int status, loglen;
    1.45 +	char infolog[512];
    1.46 +
    1.47 +	glCompileShader(sdr);
    1.48 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
    1.49 +	glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
    1.50 +	if(status == GL_FALSE) {
    1.51 +		fprintf(stderr, "Failed to compile shader: %s\n", infolog);
    1.52 +		return false;
    1.53 +	} else if(loglen) {
    1.54 +		fprintf(stderr, "%s\n", infolog);
    1.55 +	}
    1.56 +	return true;
    1.57 +}
    1.58 +
    1.59 +unsigned int sdr_getprog(const char* vfname, const char* ffname)
    1.60 +{
    1.61 +	char *vsdr_glsl = NULL;
    1.62 +	char *fsdr_glsl = NULL;
    1.63 +
    1.64 +	GLuint vsdr; GLuint fsdr;
    1.65 +	vsdr = glCreateShader(GL_VERTEX_SHADER);
    1.66 +	fsdr = glCreateShader(GL_FRAGMENT_SHADER);
    1.67 +
    1.68 +	if(!(vsdr_glsl = sdr_load(vfname)))
    1.69 +		return 0;
    1.70 +	if(!(fsdr_glsl = sdr_load(ffname)))
    1.71 +		return 0;
    1.72 +
    1.73 +	glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
    1.74 +	glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
    1.75 +
    1.76 +	free(vsdr_glsl);
    1.77 +	free(fsdr_glsl);
    1.78 +
    1.79 +	if(!sdr_compile(vsdr)) {
    1.80 +		fprintf(stderr, "Shader %s failed to compile.\n", vfname);
    1.81 +		return 0;
    1.82 +	}
    1.83 +	if(!sdr_compile(fsdr)) {
    1.84 +		fprintf(stderr, "Shader %s failed to compile.\n", ffname);
    1.85 +		return 0;
    1.86 +	}
    1.87 +
    1.88 +	unsigned int sprog;
    1.89 +	int status, loglen;
    1.90 +	char infolog[512];
    1.91 +
    1.92 +	sprog = glCreateProgram();
    1.93 +	glAttachShader(sprog, vsdr);
    1.94 +	glAttachShader(sprog, fsdr);
    1.95 +		glLinkProgram(sprog);
    1.96 +	glGetProgramiv(sprog, GL_LINK_STATUS, &status);
    1.97 +	glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
    1.98 +	if(status == GL_FALSE) {
    1.99 +		fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
   1.100 +		return false;
   1.101 +	} else if(loglen) {
   1.102 +		fprintf(stderr, "%s\n", infolog);
   1.103 +	}
   1.104 +	return sprog;
   1.105 +}