volray

changeset 3:6f275934717b

foon
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 14:42:03 +0300
parents 0b73aa7317e1
children 3e53a16d4667
files Makefile src/volray.c
diffstat 2 files changed, 64 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Apr 02 14:00:28 2012 +0300
     1.2 +++ b/Makefile	Mon Apr 02 14:42:03 2012 +0300
     1.3 @@ -4,11 +4,13 @@
     1.4  bin = volray
     1.5  
     1.6  CC = gcc
     1.7 -CFLAGS = -pedantic -Wall -g
     1.8 -LDFLAGS = $(libgl) -limago -lvmath -lm
     1.9 +CFLAGS = -pedantic -Wall -g $(incdir)
    1.10 +LDFLAGS = $(libdir) $(libgl) -limago -lvmath -lm
    1.11  
    1.12  ifeq ($(shell uname -s), Darwin)
    1.13 -	libgl = -framework OpenGL -framework GLUT
    1.14 +	libgl = -framework OpenGL -framework GLUT -lGLEW
    1.15 +	incdir = -I/opt/local/include
    1.16 +	libdir = -L/opt/local/lib
    1.17  else
    1.18  	libgl = -lGL -lglut -lGLEW
    1.19  endif
     2.1 --- a/src/volray.c	Mon Apr 02 14:00:28 2012 +0300
     2.2 +++ b/src/volray.c	Mon Apr 02 14:42:03 2012 +0300
     2.3 @@ -13,6 +13,8 @@
     2.4  #include <imago2.h>
     2.5  #include "sdr.h"
     2.6  
     2.7 +#define XFER_MAP_SZ	512
     2.8 +
     2.9  struct slice_file {
    2.10  	char *name;
    2.11  	struct slice_file *next;
    2.12 @@ -29,6 +31,7 @@
    2.13  static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
    2.14  static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
    2.15  static int round_pow2(int x);
    2.16 +static void create_transfer_map(float mean, float sdev);
    2.17  
    2.18  float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    2.19  float cam_x, cam_y, cam_z;
    2.20 @@ -40,6 +43,10 @@
    2.21  int win_xsz, win_ysz;
    2.22  int raytex_needs_recalc = 1;
    2.23  
    2.24 +unsigned int xfer_tex;
    2.25 +float xfer_mean = 0.5, xfer_sdev = 1.0;
    2.26 +int xfertex_needs_recalc = 1;
    2.27 +
    2.28  int main(int argc, char **argv)
    2.29  {
    2.30  	glutInit(&argc, argv);
    2.31 @@ -77,6 +84,7 @@
    2.32  	}
    2.33  	set_uniform_int(sdr, "volume", 0);
    2.34  	set_uniform_int(sdr, "ray_tex", 1);
    2.35 +	set_uniform_int(sdr, "xfer_tex", 2);
    2.36  
    2.37  	glGenTextures(1, &vol_tex);
    2.38  	glBindTexture(GL_TEXTURE_3D, vol_tex);
    2.39 @@ -125,6 +133,9 @@
    2.40  	if(raytex_needs_recalc) {
    2.41  		create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
    2.42  	}
    2.43 +	if(xfertex_needs_recalc) {
    2.44 +		create_transfer_map(xfer_mean, xfer_sdev);
    2.45 +	}
    2.46  
    2.47  	glMatrixMode(GL_MODELVIEW);
    2.48  	glLoadIdentity();
    2.49 @@ -146,6 +157,10 @@
    2.50  	glBindTexture(GL_TEXTURE_2D, ray_tex);
    2.51  	glEnable(GL_TEXTURE_2D);
    2.52  
    2.53 +	glActiveTexture(GL_TEXTURE2);
    2.54 +	glBindTexture(GL_TEXTURE_1D, xfer_tex);
    2.55 +	glEnable(GL_TEXTURE_1D);
    2.56 +
    2.57  	bind_program(sdr);
    2.58  	glBegin(GL_QUADS);
    2.59  	glColor3f(1, 1, 1);
    2.60 @@ -156,6 +171,8 @@
    2.61  	glEnd();
    2.62  	bind_program(0);
    2.63  
    2.64 +	glActiveTexture(GL_TEXTURE2);
    2.65 +	glDisable(GL_TEXTURE_1D);
    2.66  	glActiveTexture(GL_TEXTURE1);
    2.67  	glDisable(GL_TEXTURE_2D);
    2.68  	glActiveTexture(GL_TEXTURE0);
    2.69 @@ -234,12 +251,30 @@
    2.70  {
    2.71  	int i;
    2.72  	struct slice_file *tail;
    2.73 +	char *endp;
    2.74  
    2.75  	for(i=1; i<argc; i++) {
    2.76  		if(argv[i][0] == '-' && argv[i][2] == 0) {
    2.77  			switch(argv[i][1]) {
    2.78 -			case 'r':
    2.79 +			case 'm':
    2.80 +				xfer_mean = strtod(argv[++i], &endp);
    2.81 +				if(endp == argv[i]) {
    2.82 +					fprintf(stderr, "-m must be followed by the transfer function mean\n");
    2.83 +					return -1;
    2.84 +				}
    2.85  				break;
    2.86 +
    2.87 +			case 'v':
    2.88 +				xfer_sdev = strtod(argv[++i], &endp);
    2.89 +				if(endp == argv[i]) {
    2.90 +					fprintf(stderr, "-v must be followed by the transfer function sdeviance\n");
    2.91 +					return -1;
    2.92 +				}
    2.93 +				break;
    2.94 +
    2.95 +			default:
    2.96 +				fprintf(stderr, "unrecognized option: %s\n", argv[i]);
    2.97 +				return -1;
    2.98  			}
    2.99  		} else {
   2.100  			struct slice_file *sfile;
   2.101 @@ -265,7 +300,6 @@
   2.102  		fprintf(stderr, "pass the slice filenames\n");
   2.103  		return -1;
   2.104  	}
   2.105 -
   2.106  	return 0;
   2.107  }
   2.108  
   2.109 @@ -346,3 +380,26 @@
   2.110  	return x + 1;
   2.111  }
   2.112  
   2.113 +static void create_transfer_map(float mean, float sdev)
   2.114 +{
   2.115 +	static float map[XFER_MAP_SZ];
   2.116 +	int i;
   2.117 +
   2.118 +	if(!xfer_tex) {
   2.119 +		glGenTextures(1, &xfer_tex);
   2.120 +		glBindTexture(GL_TEXTURE_1D, xfer_tex);
   2.121 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   2.122 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   2.123 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   2.124 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   2.125 +		glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
   2.126 +	}
   2.127 +
   2.128 +	for(i=0; i<XFER_MAP_SZ; i++) {
   2.129 +		float x = (float)i / (float)XFER_MAP_SZ;
   2.130 +		map[i] = gaussian(x, mean, sdev);
   2.131 +	}
   2.132 +
   2.133 +	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, XFER_MAP_SZ, 1, GL_LUMINANCE, GL_FLOAT, map);
   2.134 +	xfertex_needs_recalc = 0;
   2.135 +}