qvolray

view src/volume.cc @ 18:3d05c261a2f4

demo metaballs crash & burn
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Apr 2012 06:08:59 +0300
parents 535762131d34
children 450d4c50470f
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <list>
6 #include <string>
7 #include <GL/glew.h>
8 #include <imago2.h>
9 #include "volume.h"
11 static void calc_gradients(float *voxels, int xsz, int ysz, int zsz);
12 static struct slice *read_voldesc(const char *fname, struct volume *vol);
13 static char *trim(char *str);
15 Volume::Volume()
16 {
17 sz[0] = sz[1] = sz[2] = 0;
18 zaspect = 1.0;
19 tex = 0;
20 }
22 Volume::~Volume()
23 {
24 if(tex) {
25 glDeleteTextures(1, &tex);
26 }
27 }
29 bool Volume::create(int xsz, int ysz, int zsz, float *data)
30 {
31 if(!tex)
32 glGenTextures(1, &tex);
33 glBindTexture(GL_TEXTURE_3D, tex);
34 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
35 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
36 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
37 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
38 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
39 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
40 }
42 bool Volume::load(const char *fname)
43 {
44 std::list<std::string> slist;
45 float *voxels = 0, *vptr;
46 struct img_pixmap img;
48 if(!(read_voldesc(fname, &slist))) {
49 return false;
50 }
52 /* load the first image to determine slice dimensions */
53 img_init(&img);
54 if(img_load(&img, slist.begin()->c_str()) == -1) {
55 fprintf(stderr, "failed to load volume slice: %s\n", slist.begin()->c_str());
56 return false;
57 }
59 sz[0] = img.width;
60 sz[1] = img.height;
61 printf("volume dimensions: %dx%dx%d\n", img.width, img.height, sz[2]);
63 /* allocate the whole volume at once */
64 voxels = new float[sz[0] * sz[1] * sz[2] * 4];
65 vptr = voxels;
66 img_destroy(&img);
68 /* put the volume data into the alpha component */
69 //for(auto slice : slist) { // fucking vs2010 doesn't support range-for
70 for(auto iter = slist.begin(); iter != slist.end(); iter++) {
71 auto slice = *iter;
73 int x, y, xsz, ysz;
74 float *pixels, *src;
76 if(!(pixels = (float*)img_load_pixels(slice.c_str(), &xsz, &ysz, IMG_FMT_GREYF))) {
77 fprintf(stderr, "failed to load volume slice: %s\n", slice.c_str());
78 delete [] voxels;
79 return false;
80 }
82 if(xsz != sz[0] || ysz != sz[1]) {
83 fprintf(stderr, "inconsistent dimensions for slice %s: %dx%d (%dx%d expected)\n",
84 slice.c_str(), xsz, ysz, sz[0], sz[1]);
85 delete [] voxels;
86 return false;
87 }
89 src = pixels;
90 for(y=0; y<ysz; y++) {
91 for(x=0; x<xsz; x++) {
92 vptr[0] = vptr[1] = vptr[2] = 0.0f;
93 vptr[3] = *src++;
94 vptr += 4;
95 }
96 }
97 img_free_pixels(pixels);
98 }
100 /* calculate gradients */
101 calc_gradients(voxels, sz[0], sz[1], sz[2]);
103 /* create the volume texture */
104 create(sz[0], sz[1], sz[2], voxels);
106 /* ... and destroy our copy */
107 delete [] voxels;
108 return true;
109 }
111 unsigned int Volume::get_texture() const
112 {
113 return tex;
114 }
116 void Volume::bind(int tex_unit) const
117 {
118 glActiveTexture(GL_TEXTURE0 + tex_unit);
119 glBindTexture(GL_TEXTURE_3D, tex);
120 }
122 struct vec4 { float x, y, z, w; };
124 static void calc_gradients(float *voxels, int xsz, int ysz, int zsz)
125 {
126 int x, y, z, slice_pixels = xsz * ysz;
127 vec4 *vptr = (vec4*)voxels;
129 for(z=0; z<zsz; z++) {
130 for(y=0; y<ysz; y++) {
131 for(x=0; x<xsz; x++) {
132 float x0, x1, y0, y1, z0, z1;
134 x0 = x > 0 ? (vptr - 1)->w : vptr->w;
135 y0 = y > 0 ? (vptr - xsz)->w : vptr->w;
136 z0 = z > 0 ? (vptr - slice_pixels)->w : vptr->w;
137 x1 = x < xsz - 1 ? (vptr + 1)->w : vptr->w;
138 y1 = y < ysz - 1 ? (vptr + xsz)->w : vptr->w;
139 z1 = z < zsz - 1 ? (vptr + slice_pixels)->w : vptr->w;
141 vptr->x = x1 - x0;
142 vptr->y = y1 - y0;
143 vptr->z = z1 - z0;
145 vptr++;
146 }
147 }
148 }
149 }
151 bool Volume::read_voldesc(const char *fname, std::list<std::string> *slist)
152 {
153 FILE *fp = 0;
154 char buf[512], *slash, *prefix = "";
155 int mode_slices = 0;
157 slist->clear();
159 if(!(fp = fopen(fname, "r"))) {
160 fprintf(stderr, "failed to open volume descriptor: %s\n", fname);
161 return false;
162 }
163 fgets(buf, sizeof buf, fp);
164 if(strstr(buf, "VOLDESC") != buf) {
165 fprintf(stderr, "invalid file format while trying to read volume descriptor: %s\n", fname);
166 fclose(fp);
167 return false;
168 }
170 if((slash = strrchr((char*)fname, '/'))) {
171 int len = slash - fname + 1;
172 prefix = (char*)alloca(len + 1);
173 memcpy(prefix, fname, len);
174 prefix[len] = 0;
175 }
177 while(fgets(buf, sizeof buf, fp)) {
178 char *line = trim(buf);
180 if(!*line || *line == '#')
181 continue;
183 if(mode_slices) {
184 /* we're in the part that contains filenames... append to the list */
185 slist->push_back(std::string(prefix) + std::string(line));
186 sz[2]++;
187 } else {
188 /* TODO we're in the options part... parse */
189 if(strcmp(line, "SLICES") == 0) {
190 mode_slices = 1;
191 }
192 }
193 }
194 fclose(fp);
196 if(slist->empty()) {
197 fprintf(stderr, "file: %s contains no slices\n", fname);
198 return false;
199 }
200 return true;
201 }
203 static char *trim(char *str)
204 {
205 char *tmp = str + strlen(str) - 1;
207 while(isspace(*tmp))
208 tmp--;
209 tmp[1] = 0;
211 tmp = str;
212 while(isspace(*tmp))
213 tmp++;
214 return tmp;
215 }