qvolray

diff src/volray.c @ 9:a6765984e057

moved the volume loading to volume.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 08 Apr 2012 07:11:54 +0300
parents f40e4edfee7e
children
line diff
     1.1 --- a/src/volray.c	Sat Apr 07 16:07:12 2012 +0300
     1.2 +++ b/src/volray.c	Sun Apr 08 07:11:54 2012 +0300
     1.3 @@ -12,14 +12,10 @@
     1.4  #include <vmath/vmath.h>
     1.5  #include <imago2.h>
     1.6  #include "sdr.h"
     1.7 +#include "volume.h"
     1.8  
     1.9  #define XFER_MAP_SZ	512
    1.10  
    1.11 -struct slice_file {
    1.12 -	char *name;
    1.13 -	struct slice_file *next;
    1.14 -};
    1.15 -
    1.16  enum {
    1.17  	UIMODE_DEFAULT,
    1.18  	UIMODE_XFER,
    1.19 @@ -43,24 +39,26 @@
    1.20  static int round_pow2(int x);
    1.21  static void create_transfer_map(float mean, float sdev);
    1.22  
    1.23 -float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    1.24 -float cam_x, cam_y, cam_z;
    1.25 +static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    1.26 +static float cam_x, cam_y, cam_z;
    1.27  
    1.28 -vec2_t tex_scale;
    1.29 -struct slice_file *flist;
    1.30 -int nslices;
    1.31 -unsigned int vol_sdr, slice_sdr, vol_tex, ray_tex;
    1.32 -int win_xsz, win_ysz;
    1.33 -int raytex_needs_recalc = 1;
    1.34 +static vec2_t tex_scale;
    1.35 +static struct volume *volume;
    1.36 +static unsigned int vol_sdr, slice_sdr, ray_tex;
    1.37 +static int win_xsz, win_ysz;
    1.38 +static int raytex_needs_recalc = 1;
    1.39  
    1.40 -unsigned int xfer_tex;
    1.41 -float xfer_mean = 0.7, xfer_sdev = 0.1;
    1.42 -int xfertex_needs_recalc = 1;
    1.43 +static unsigned int xfer_tex;
    1.44 +static float xfer_mean = 0.7, xfer_sdev = 0.1;
    1.45 +static int xfertex_needs_recalc = 1;
    1.46  
    1.47  static int uimode;
    1.48  static float cur_z = 0.0;
    1.49  static float ray_step = 0.01;
    1.50  
    1.51 +static const char *fname;
    1.52 +
    1.53 +
    1.54  int main(int argc, char **argv)
    1.55  {
    1.56  	glutInit(&argc, argv);
    1.57 @@ -92,9 +90,7 @@
    1.58  
    1.59  int init(void)
    1.60  {
    1.61 -	int i, vol_xsz, vol_ysz;
    1.62 -
    1.63 -	if(!(vol_sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) {
    1.64 +	if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
    1.65  		return -1;
    1.66  	}
    1.67  	set_uniform_int(vol_sdr, "volume", 0);
    1.68 @@ -103,51 +99,16 @@
    1.69  	set_uniform_float(vol_sdr, "ray_step", ray_step);
    1.70  	set_uniform_float(vol_sdr, "zclip", cur_z);
    1.71  
    1.72 -	if(!(slice_sdr = create_program_load(0, "slice.p.glsl"))) {
    1.73 +	if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
    1.74  		return -1;
    1.75  	}
    1.76  	set_uniform_int(slice_sdr, "volume", 0);
    1.77  	set_uniform_int(slice_sdr, "xfer_tex", 1);
    1.78  
    1.79 -	glGenTextures(1, &vol_tex);
    1.80 -	glBindTexture(GL_TEXTURE_3D, vol_tex);
    1.81 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.82 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.83 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    1.84 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    1.85 -	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    1.86 +	if(!(volume = load_volume(fname))) {
    1.87 +		return -1;
    1.88 +	}
    1.89  
    1.90 -	for(i=0; i<nslices; i++) {
    1.91 -		int xsz, ysz;
    1.92 -		void *pix;
    1.93 -		struct slice_file *sfile = flist;
    1.94 -		flist = flist->next;
    1.95 -
    1.96 -		if(!(pix = img_load_pixels(sfile->name, &xsz, &ysz, IMG_FMT_RGBA32))) {
    1.97 -			fprintf(stderr, "failed to load image: %s\n", sfile->name);
    1.98 -			return -1;
    1.99 -		}
   1.100 -
   1.101 -		if(i == 0) {
   1.102 -			/* allocate storage for the texture */
   1.103 -			glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F, xsz, ysz, nslices, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
   1.104 -
   1.105 -			vol_xsz = xsz;
   1.106 -			vol_ysz = ysz;
   1.107 -
   1.108 -		} else {
   1.109 -			if(xsz != vol_xsz || ysz != vol_ysz) {
   1.110 -				fprintf(stderr, "%s: inconsistent slice size: %dx%d. expected: %dx%d\n",
   1.111 -						sfile->name, xsz, ysz, vol_xsz, vol_ysz);
   1.112 -				img_free_pixels(pix);
   1.113 -				return -1;
   1.114 -			}
   1.115 -		}
   1.116 -		free(sfile);
   1.117 -
   1.118 -		glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
   1.119 -		img_free_pixels(pix);
   1.120 -	}
   1.121  	return 0;
   1.122  }
   1.123  
   1.124 @@ -190,7 +151,7 @@
   1.125  
   1.126  	/* tex unit0: volume data 3D texture */
   1.127  	glActiveTexture(GL_TEXTURE0);
   1.128 -	glBindTexture(GL_TEXTURE_3D, vol_tex);
   1.129 +	glBindTexture(GL_TEXTURE_3D, volume->tex_vol);
   1.130  	glEnable(GL_TEXTURE_3D);
   1.131  
   1.132  	/* tex unit1: primary rays in view space */
   1.133 @@ -235,7 +196,7 @@
   1.134  	glTranslatef(-1, -1, 0);
   1.135  
   1.136  	glActiveTexture(GL_TEXTURE0);
   1.137 -	glBindTexture(GL_TEXTURE_3D, vol_tex);
   1.138 +	glBindTexture(GL_TEXTURE_3D, volume->tex_vol);
   1.139  	glEnable(GL_TEXTURE_3D);
   1.140  
   1.141  	glActiveTexture(GL_TEXTURE1);
   1.142 @@ -426,7 +387,6 @@
   1.143  int parse_args(int argc, char **argv)
   1.144  {
   1.145  	int i;
   1.146 -	struct slice_file *tail;
   1.147  	char *endp;
   1.148  
   1.149  	for(i=1; i<argc; i++) {
   1.150 @@ -453,27 +413,16 @@
   1.151  				return -1;
   1.152  			}
   1.153  		} else {
   1.154 -			struct slice_file *sfile;
   1.155 -
   1.156 -			if(!(sfile = malloc(sizeof *sfile))) {
   1.157 -				perror("failed to allocate memory");
   1.158 +			if(fname) {
   1.159 +				fprintf(stderr, "unexpected argument: %s\n", argv[i]);
   1.160  				return -1;
   1.161  			}
   1.162 -			sfile->name = argv[i];
   1.163 -			sfile->next = 0;
   1.164 -
   1.165 -			if(!flist) {
   1.166 -				flist = tail = sfile;
   1.167 -			} else {
   1.168 -				tail->next = sfile;
   1.169 -				tail = sfile;
   1.170 -			}
   1.171 -			nslices++;
   1.172 +			fname = argv[i];
   1.173  		}
   1.174  	}
   1.175  
   1.176 -	if(!nslices) {
   1.177 -		fprintf(stderr, "pass the slice filenames\n");
   1.178 +	if(!fname) {
   1.179 +		fprintf(stderr, "pass the volume descriptor filename\n");
   1.180  		return -1;
   1.181  	}
   1.182  	return 0;