volmetrics

diff src/sdr.cc @ 22:4e120dcd55ec

added shaders that just draw
author Eleni Maria Stea <elene.mst@gmail.com>
date Thu, 24 Apr 2014 20:47:48 +0300
parents
children df4a277adb82
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sdr.cc	Thu Apr 24 20:47:48 2014 +0300
     1.3 @@ -0,0 +1,104 @@
     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;
    1.32 +	if(!(glsl = (char*) malloc(ctr + 1))) {
    1.33 +		perror("Failed to allocate memory");
    1.34 +		return 0;
    1.35 +	}
    1.36 +	fread(glsl, 1, ctr, fp);
    1.37 +	fclose(fp);
    1.38 +
    1.39 +	glsl[ctr] = 0;
    1.40 +
    1.41 +	return glsl;
    1.42 +}
    1.43 +
    1.44 +bool sdr_compile(unsigned int sdr)
    1.45 +{
    1.46 +	int status, loglen;
    1.47 +	char infolog[512];
    1.48 +
    1.49 +	glCompileShader(sdr);
    1.50 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
    1.51 +	glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
    1.52 +	if(status == GL_FALSE) {
    1.53 +		fprintf(stderr, "Failed to compile shader: %s\n", infolog);
    1.54 +		return false;
    1.55 +	} else if(loglen) {
    1.56 +		fprintf(stderr, "%s\n", infolog);
    1.57 +	}
    1.58 +	return true;
    1.59 +}
    1.60 +
    1.61 +unsigned int sdr_getprog(const char* vfname, const char* ffname)
    1.62 +{
    1.63 +	char *vsdr_glsl = NULL;
    1.64 +	char *fsdr_glsl = NULL;
    1.65 +
    1.66 +	GLuint vsdr; GLuint fsdr;
    1.67 +	vsdr = glCreateShader(GL_VERTEX_SHADER);
    1.68 +	fsdr = glCreateShader(GL_FRAGMENT_SHADER);
    1.69 +
    1.70 +	if(!(vsdr_glsl = sdr_load(vfname)))
    1.71 +		return 0;
    1.72 +	if(!(fsdr_glsl = sdr_load(ffname)))
    1.73 +		return 0;
    1.74 +
    1.75 +	glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
    1.76 +	glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
    1.77 +
    1.78 +	free(vsdr_glsl);
    1.79 +	free(fsdr_glsl);
    1.80 +
    1.81 +	if(!sdr_compile(vsdr)) {
    1.82 +		fprintf(stderr, "Shader %s failed to compile.\n", vfname);
    1.83 +		return 0;
    1.84 +	}
    1.85 +	if(!sdr_compile(fsdr)) {
    1.86 +		fprintf(stderr, "Shader %s failed to compile.\n", ffname);
    1.87 +		return 0;
    1.88 +	}
    1.89 +
    1.90 +	unsigned int sprog;
    1.91 +	int status, loglen;
    1.92 +	char infolog[512];
    1.93 +
    1.94 +	sprog = glCreateProgram();
    1.95 +	glAttachShader(sprog, vsdr);
    1.96 +	glAttachShader(sprog, fsdr);
    1.97 +		glLinkProgram(sprog);
    1.98 +	glGetProgramiv(sprog, GL_LINK_STATUS, &status);
    1.99 +	glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
   1.100 +	if(status == GL_FALSE) {
   1.101 +		fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
   1.102 +		return 0;
   1.103 +	} else if(loglen) {
   1.104 +		fprintf(stderr, "%s\n", infolog);
   1.105 +	}
   1.106 +	return sprog;
   1.107 +}