volray

diff src/volray.c @ 3:6f275934717b

foon
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 14:42:03 +0300
parents 0b73aa7317e1
children 3e53a16d4667
line diff
     1.1 --- a/src/volray.c	Mon Apr 02 14:00:28 2012 +0300
     1.2 +++ b/src/volray.c	Mon Apr 02 14:42:03 2012 +0300
     1.3 @@ -13,6 +13,8 @@
     1.4  #include <imago2.h>
     1.5  #include "sdr.h"
     1.6  
     1.7 +#define XFER_MAP_SZ	512
     1.8 +
     1.9  struct slice_file {
    1.10  	char *name;
    1.11  	struct slice_file *next;
    1.12 @@ -29,6 +31,7 @@
    1.13  static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
    1.14  static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
    1.15  static int round_pow2(int x);
    1.16 +static void create_transfer_map(float mean, float sdev);
    1.17  
    1.18  float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    1.19  float cam_x, cam_y, cam_z;
    1.20 @@ -40,6 +43,10 @@
    1.21  int win_xsz, win_ysz;
    1.22  int raytex_needs_recalc = 1;
    1.23  
    1.24 +unsigned int xfer_tex;
    1.25 +float xfer_mean = 0.5, xfer_sdev = 1.0;
    1.26 +int xfertex_needs_recalc = 1;
    1.27 +
    1.28  int main(int argc, char **argv)
    1.29  {
    1.30  	glutInit(&argc, argv);
    1.31 @@ -77,6 +84,7 @@
    1.32  	}
    1.33  	set_uniform_int(sdr, "volume", 0);
    1.34  	set_uniform_int(sdr, "ray_tex", 1);
    1.35 +	set_uniform_int(sdr, "xfer_tex", 2);
    1.36  
    1.37  	glGenTextures(1, &vol_tex);
    1.38  	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.39 @@ -125,6 +133,9 @@
    1.40  	if(raytex_needs_recalc) {
    1.41  		create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
    1.42  	}
    1.43 +	if(xfertex_needs_recalc) {
    1.44 +		create_transfer_map(xfer_mean, xfer_sdev);
    1.45 +	}
    1.46  
    1.47  	glMatrixMode(GL_MODELVIEW);
    1.48  	glLoadIdentity();
    1.49 @@ -146,6 +157,10 @@
    1.50  	glBindTexture(GL_TEXTURE_2D, ray_tex);
    1.51  	glEnable(GL_TEXTURE_2D);
    1.52  
    1.53 +	glActiveTexture(GL_TEXTURE2);
    1.54 +	glBindTexture(GL_TEXTURE_1D, xfer_tex);
    1.55 +	glEnable(GL_TEXTURE_1D);
    1.56 +
    1.57  	bind_program(sdr);
    1.58  	glBegin(GL_QUADS);
    1.59  	glColor3f(1, 1, 1);
    1.60 @@ -156,6 +171,8 @@
    1.61  	glEnd();
    1.62  	bind_program(0);
    1.63  
    1.64 +	glActiveTexture(GL_TEXTURE2);
    1.65 +	glDisable(GL_TEXTURE_1D);
    1.66  	glActiveTexture(GL_TEXTURE1);
    1.67  	glDisable(GL_TEXTURE_2D);
    1.68  	glActiveTexture(GL_TEXTURE0);
    1.69 @@ -234,12 +251,30 @@
    1.70  {
    1.71  	int i;
    1.72  	struct slice_file *tail;
    1.73 +	char *endp;
    1.74  
    1.75  	for(i=1; i<argc; i++) {
    1.76  		if(argv[i][0] == '-' && argv[i][2] == 0) {
    1.77  			switch(argv[i][1]) {
    1.78 -			case 'r':
    1.79 +			case 'm':
    1.80 +				xfer_mean = strtod(argv[++i], &endp);
    1.81 +				if(endp == argv[i]) {
    1.82 +					fprintf(stderr, "-m must be followed by the transfer function mean\n");
    1.83 +					return -1;
    1.84 +				}
    1.85  				break;
    1.86 +
    1.87 +			case 'v':
    1.88 +				xfer_sdev = strtod(argv[++i], &endp);
    1.89 +				if(endp == argv[i]) {
    1.90 +					fprintf(stderr, "-v must be followed by the transfer function sdeviance\n");
    1.91 +					return -1;
    1.92 +				}
    1.93 +				break;
    1.94 +
    1.95 +			default:
    1.96 +				fprintf(stderr, "unrecognized option: %s\n", argv[i]);
    1.97 +				return -1;
    1.98  			}
    1.99  		} else {
   1.100  			struct slice_file *sfile;
   1.101 @@ -265,7 +300,6 @@
   1.102  		fprintf(stderr, "pass the slice filenames\n");
   1.103  		return -1;
   1.104  	}
   1.105 -
   1.106  	return 0;
   1.107  }
   1.108  
   1.109 @@ -346,3 +380,26 @@
   1.110  	return x + 1;
   1.111  }
   1.112  
   1.113 +static void create_transfer_map(float mean, float sdev)
   1.114 +{
   1.115 +	static float map[XFER_MAP_SZ];
   1.116 +	int i;
   1.117 +
   1.118 +	if(!xfer_tex) {
   1.119 +		glGenTextures(1, &xfer_tex);
   1.120 +		glBindTexture(GL_TEXTURE_1D, xfer_tex);
   1.121 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   1.122 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   1.123 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   1.124 +		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   1.125 +		glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
   1.126 +	}
   1.127 +
   1.128 +	for(i=0; i<XFER_MAP_SZ; i++) {
   1.129 +		float x = (float)i / (float)XFER_MAP_SZ;
   1.130 +		map[i] = gaussian(x, mean, sdev);
   1.131 +	}
   1.132 +
   1.133 +	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, XFER_MAP_SZ, 1, GL_LUMINANCE, GL_FLOAT, map);
   1.134 +	xfertex_needs_recalc = 0;
   1.135 +}