volmetrics

changeset 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 c22866ecb7ae
children 930c063ae346
files src/main.cc src/sdr.cc src/sdr.h src/volume.cc
diffstat 4 files changed, 143 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/src/main.cc	Tue Mar 25 00:17:39 2014 +0200
     1.2 +++ b/src/main.cc	Thu Apr 24 20:47:48 2014 +0300
     1.3 @@ -9,6 +9,7 @@
     1.4  #include <vector>
     1.5  
     1.6  #include "mesh.h"
     1.7 +#include "sdr.h"
     1.8  #include "volume.h"
     1.9  
    1.10  static bool init(void);
    1.11 @@ -34,17 +35,21 @@
    1.12  static std::vector<bool> key_state(256);
    1.13  
    1.14  static const char *vol_fname = "data/test1.vol";
    1.15 +static const char *vsdr_path = "data/shaders/transfer.v.glsl";
    1.16 +static const char *fsdr_path = "data/shaders/transfer.f.glsl";
    1.17  
    1.18  static Volume *vol;
    1.19  static Mesh *mesh;
    1.20  static float cur_z, thres = 0.5, thres2 = 1.0;
    1.21 -static float zeta;
    1.22 +static float slice_z;
    1.23  
    1.24  static int use_orig_vol_res = 1;
    1.25  static int vol_res[3]; // volume sampling resolution x/y/z
    1.26  
    1.27  static GLUI *ui;
    1.28  
    1.29 +static unsigned int sprog;
    1.30 +
    1.31  int main(int argc, char **argv)
    1.32  {
    1.33  	glutInit(&argc, argv);
    1.34 @@ -80,10 +85,24 @@
    1.35  	glEnable(GL_DEPTH_TEST);
    1.36  	glEnable(GL_NORMALIZE);
    1.37  
    1.38 -	glEnable(GL_LIGHTING); //TODO: shaders
    1.39 +	glEnable(GL_LIGHTING);
    1.40  	glEnable(GL_LIGHT0);
    1.41  	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
    1.42  
    1.43 +	//FIXME shaders setup
    1.44 +	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
    1.45 +	{
    1.46 +		fprintf(stderr, "Failed to create shader program!\n");
    1.47 +		return false;
    1.48 +	}
    1.49 +	glUseProgram(sprog);
    1.50 +	float t1 = glGetUniformLocation(sprog, "thres1");
    1.51 +	if(t1 != -1)
    1.52 +		glUniform1f(t1, 0);
    1.53 +	float t2 = glGetUniformLocation(sprog, "thres2");
    1.54 +		glUniform1f(t2, 1);
    1.55 +	glUseProgram(0);
    1.56 +
    1.57  	vol = new Volume;
    1.58  	if(!vol->load(vol_fname)) {
    1.59  		fprintf(stderr, "Failed to load %s", vol_fname);
    1.60 @@ -171,7 +190,7 @@
    1.61  
    1.62  	GLUI_Panel *preview_panel = ui->add_panel("volume preview");
    1.63  
    1.64 -	GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &zeta, 0);
    1.65 +	GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
    1.66  	preview_spin->set_float_limits(0, 1);
    1.67  
    1.68  	return ui;
    1.69 @@ -184,6 +203,8 @@
    1.70  	glDisable(GL_LIGHTING);
    1.71  	glDisable(GL_DEPTH_TEST);
    1.72  
    1.73 +	glUseProgram(sprog);
    1.74 +
    1.75  	glMatrixMode(GL_MODELVIEW);
    1.76  	glPushMatrix();
    1.77  	glLoadIdentity();
    1.78 @@ -197,10 +218,10 @@
    1.79  	glEnable(GL_TEXTURE_3D);
    1.80  	glBegin(GL_QUADS);
    1.81  	glColor3f(1.0, 1.0, 1.0);
    1.82 -	glTexCoord3f(0, 0, zeta); glVertex3f(-1.0, 1.0, 0.0);
    1.83 -	glTexCoord3f(0, 1, zeta); glVertex3f(-1.0, 0.5, 0.0);
    1.84 -	glTexCoord3f(1, 1, zeta); glVertex3f(-0.5, 0.5, 0.0);
    1.85 -	glTexCoord3f(1, 0, zeta); glVertex3f(-0.5, 1.0, 0.0);
    1.86 +	glTexCoord3f(0, 0, slice_z); glVertex3f(-1.0, 1.0, 0.0);
    1.87 +	glTexCoord3f(0, 1, slice_z); glVertex3f(-1.0, 0.5, 0.0);
    1.88 +	glTexCoord3f(1, 1, slice_z); glVertex3f(-0.5, 0.5, 0.0);
    1.89 +	glTexCoord3f(1, 0, slice_z); glVertex3f(-0.5, 1.0, 0.0);
    1.90  	glEnd();
    1.91  	glDisable(GL_TEXTURE_3D);
    1.92  
    1.93 @@ -219,6 +240,8 @@
    1.94  	glMatrixMode(GL_MODELVIEW);
    1.95  	glPopMatrix();
    1.96  
    1.97 +	glUseProgram(0);
    1.98 +
    1.99  	glEnable(GL_DEPTH_TEST);
   1.100  	glEnable(GL_LIGHTING);
   1.101  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/sdr.cc	Thu Apr 24 20:47:48 2014 +0300
     2.3 @@ -0,0 +1,104 @@
     2.4 +#include <GL/glew.h>
     2.5 +#include <GL/gl.h>
     2.6 +
     2.7 +#include <errno.h>
     2.8 +#include <stdio.h>
     2.9 +#include <stdlib.h>
    2.10 +#include <string.h>
    2.11 +
    2.12 +#include "sdr.h"
    2.13 +
    2.14 +char* sdr_load(const char* fname)
    2.15 +{
    2.16 +	if(!fname) {
    2.17 +		return 0;
    2.18 +	}
    2.19 +
    2.20 +	FILE *fp;
    2.21 +	int ctr = 0;
    2.22 +
    2.23 +	if(!(fp = fopen(fname, "rb"))) {
    2.24 +		fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno));
    2.25 +		return 0;
    2.26 +	}
    2.27 +	fseek(fp, 0, SEEK_END);
    2.28 +	ctr = ftell(fp);
    2.29 +	rewind(fp);
    2.30 +
    2.31 +	char *glsl;
    2.32 +	if(!(glsl = (char*) malloc(ctr + 1))) {
    2.33 +		perror("Failed to allocate memory");
    2.34 +		return 0;
    2.35 +	}
    2.36 +	fread(glsl, 1, ctr, fp);
    2.37 +	fclose(fp);
    2.38 +
    2.39 +	glsl[ctr] = 0;
    2.40 +
    2.41 +	return glsl;
    2.42 +}
    2.43 +
    2.44 +bool sdr_compile(unsigned int sdr)
    2.45 +{
    2.46 +	int status, loglen;
    2.47 +	char infolog[512];
    2.48 +
    2.49 +	glCompileShader(sdr);
    2.50 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
    2.51 +	glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
    2.52 +	if(status == GL_FALSE) {
    2.53 +		fprintf(stderr, "Failed to compile shader: %s\n", infolog);
    2.54 +		return false;
    2.55 +	} else if(loglen) {
    2.56 +		fprintf(stderr, "%s\n", infolog);
    2.57 +	}
    2.58 +	return true;
    2.59 +}
    2.60 +
    2.61 +unsigned int sdr_getprog(const char* vfname, const char* ffname)
    2.62 +{
    2.63 +	char *vsdr_glsl = NULL;
    2.64 +	char *fsdr_glsl = NULL;
    2.65 +
    2.66 +	GLuint vsdr; GLuint fsdr;
    2.67 +	vsdr = glCreateShader(GL_VERTEX_SHADER);
    2.68 +	fsdr = glCreateShader(GL_FRAGMENT_SHADER);
    2.69 +
    2.70 +	if(!(vsdr_glsl = sdr_load(vfname)))
    2.71 +		return 0;
    2.72 +	if(!(fsdr_glsl = sdr_load(ffname)))
    2.73 +		return 0;
    2.74 +
    2.75 +	glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
    2.76 +	glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
    2.77 +
    2.78 +	free(vsdr_glsl);
    2.79 +	free(fsdr_glsl);
    2.80 +
    2.81 +	if(!sdr_compile(vsdr)) {
    2.82 +		fprintf(stderr, "Shader %s failed to compile.\n", vfname);
    2.83 +		return 0;
    2.84 +	}
    2.85 +	if(!sdr_compile(fsdr)) {
    2.86 +		fprintf(stderr, "Shader %s failed to compile.\n", ffname);
    2.87 +		return 0;
    2.88 +	}
    2.89 +
    2.90 +	unsigned int sprog;
    2.91 +	int status, loglen;
    2.92 +	char infolog[512];
    2.93 +
    2.94 +	sprog = glCreateProgram();
    2.95 +	glAttachShader(sprog, vsdr);
    2.96 +	glAttachShader(sprog, fsdr);
    2.97 +		glLinkProgram(sprog);
    2.98 +	glGetProgramiv(sprog, GL_LINK_STATUS, &status);
    2.99 +	glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
   2.100 +	if(status == GL_FALSE) {
   2.101 +		fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
   2.102 +		return 0;
   2.103 +	} else if(loglen) {
   2.104 +		fprintf(stderr, "%s\n", infolog);
   2.105 +	}
   2.106 +	return sprog;
   2.107 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/sdr.h	Thu Apr 24 20:47:48 2014 +0300
     3.3 @@ -0,0 +1,8 @@
     3.4 +#ifndef SDR_H_
     3.5 +#define SDR_H_
     3.6 +
     3.7 +char *sdr_load(const char *fname);
     3.8 +bool sdr_compile(unsigned int sdr);
     3.9 +unsigned int sdr_getprog(const char *vfname, const char *ffname);
    3.10 +
    3.11 +#endif // SDR_H_
     4.1 --- a/src/volume.cc	Tue Mar 25 00:17:39 2014 +0200
     4.2 +++ b/src/volume.cc	Thu Apr 24 20:47:48 2014 +0300
     4.3 @@ -241,6 +241,7 @@
     4.4  
     4.5  void Volume::draw() const
     4.6  {
     4.7 +
     4.8  }
     4.9  
    4.10  static char *strip_whitespaces(char *buf)