# HG changeset patch # User John Tsiombikas # Date 1333804032 -10800 # Node ID ae10631bb11b45b2c0b515437f75d132059ad21d # Parent f40e4edfee7e1ee1c2f8192e9e3f707d88c57c82 cleaning up a bit to expand diff -r f40e4edfee7e -r ae10631bb11b .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sat Apr 07 16:07:12 2012 +0300 @@ -0,0 +1,5 @@ +^volray$ +\.o$ +\.d$ +\.swp$ +^data diff -r f40e4edfee7e -r ae10631bb11b src/volume.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/volume.c Sat Apr 07 16:07:12 2012 +0300 @@ -0,0 +1,162 @@ +#include +#include +#include +#include + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include + +struct slice { + char *name; + struct slice *next; +}; + +static struct slice *read_voldesc(const char *fname, struct volume *vol); +static char *trim(char *str); + +struct volume *load_volume(const char *fname) +{ + struct slice *slist; + struct volume *vol = 0; + + if(!(vol = malloc(sizeof *vol))) { + perror("failed to allocate volume"); + return 0; + } + memset(vol, 0, sizeof *vol); + vol->zaspect = 1; + + if(!(slist = read_voldesc(fname, vol))) { + goto err; + } + + glGenTextures(1, &vol->tex_vol); + glBindTexture(GL_TEXTURE_3D, vol->tex_vol); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + for(i=0; isz[2]; i++) { + struct img_pixmap img; + struct slice *slice = head; + head = head->next; + + img_init(&img); + if(img_load(&img, slice->name) == -1) { + fprintf(stderr, "failed to load volume slice: %s\n", slice->name); + goto err; + } + } + + glGenTextures(1, &vol->tex_grad); + glBindTexture(GL_TEXTURE_3D, vol->tex_grad); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + +err: + while(slist) { + struct slice *tmp = slist; + slist = slist->next; + free(tmp->name); + free(tmp); + } + if(vol) { + if(vol->tex_vol) + glDeleteTextures(1, &vol->tex_vol); + if(vol->tex_grad) + glDeleteTextures(1, &vol->tex_grad); + free(vol); + } + return 0; +} + +static struct slice *read_voldesc(const char *fname, struct volume *vol) +{ + FILE *fp = 0; + char buf[512]; + int mode_slices = 0; + struct slice *head = 0, *tail; + + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "failed to open volume descriptor: %s\n", fname); + return 0; + } + fgets(buf, sizeof buf, fp); + if(strstr(buf, "VOLDESC") != buf) { + fprintf(stderr, "invalid file format while trying to read volume descriptor: %s\n", fname); + goto err; + } + + while(fgets(buf, sizeof buf, fp)) { + char *line = trim(buf); + + if(!*line || *line == '#') + continue; + + if(mode_slices) { + /* we're in the part that contains filenames... append to the list */ + struct slice *node = malloc(sizeof *node); + if(!node || !(node->name = malloc(strlen(line) + 1))) { + perror("failed to allocate list node"); + free(node); + goto err; + } + strcpy(node->name, line); + node->next = 0; + + if(head) { + tail->next = node; + tail = node; + } else { + head = tail = node; + } + vol->sz[2]++; + } else { + /* TODO we're in the options part... parse */ + } + } + fclose(fp); + + if(!vol->sz[2]) { + fprintf(stderr, "file: %s contains no slices\n", fname); + goto err; + } + + return head; + +err: + while(head) { + struct slice *tmp = head; + head = head->next; + free(tmp->name); + free(tmp); + } + if(fp) { + fclose(fp); + } + return 0; +} + +static char *trim(char *str) +{ + char *tmp = str + strlen(str) - 1; + + while(isspace(*tmp)) + tmp--; + tmp[1] = 0; + + tmp = str; + while(isspace(*tmp)) + tmp++; + return tmp; +} diff -r f40e4edfee7e -r ae10631bb11b src/volume.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/volume.h Sat Apr 07 16:07:12 2012 +0300 @@ -0,0 +1,15 @@ +#ifndef VOLUME_H_ +#define VOLUME_H_ + +#include + +struct volume { + int sz[3]; + float zaspect; + unsigned int tex_vol, tex_grad; +}; + +struct volume *load_volume(const char *fname); +void free_volume(struct volume *vol); + +#endif /* VOLUME_H_ */ diff -r f40e4edfee7e -r ae10631bb11b volray.p.glsl --- a/volray.p.glsl Thu Apr 05 00:52:11 2012 +0300 +++ b/volray.p.glsl Sat Apr 07 16:07:12 2012 +0300 @@ -63,15 +63,14 @@ vec3 pos = ray.origin + ray.dir * t; t += ray_step; - float val = eval(pos) * energy; + float val = eval(pos); vec3 norm; norm.x = eval(pos + vec3(OFFS, 0.0, 0.0)) - val; norm.y = eval(pos + vec3(0.0, OFFS, 0.0)) - val; norm.z = eval(pos + vec3(0.0, 0.0, OFFS)) - val; - norm = normalize(norm); - col += shade(ray, pos, norm) * val; + col += shade(ray, pos, normalize(norm)) * val * energy; energy -= val; if(energy < 0.001) { break; @@ -84,15 +83,15 @@ vec3 shade(Ray ray, vec3 pos, vec3 norm) { - vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - pos); + vec3 ldir = -pos;//normalize(vec3(10.0, 10.0, -10.0) - pos); vec3 vdir = -ray.dir; vec3 hdir = normalize(ldir + vdir); float ndotl = dot(ldir, norm); float ndoth = dot(hdir, norm); - vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0); - vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0); + vec3 dcol = vec3(0.9, 0.9, 0.9) * max(ndotl, 0.0); + vec3 scol = vec3(0.5, 0.5, 0.5) * pow(max(ndoth, 0.0), 50.0); return vec3(0.01, 0.01, 0.01) + dcol + scol; }