1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * VolumeRenderer.cpp : VolumeRenderer class for volume visualization |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
8 | * |
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | |
---|
16 | #include <R2/R2string.h> |
---|
17 | #include <R2/R2FilePath.h> |
---|
18 | #include "VolumeRenderer.h" |
---|
19 | #include "NvStdVertexShader.h" |
---|
20 | |
---|
21 | |
---|
22 | VolumeRenderer::VolumeRenderer(): |
---|
23 | n_volumes(0), |
---|
24 | slice_mode(false), |
---|
25 | volume_mode(true) |
---|
26 | { |
---|
27 | volume.clear(); |
---|
28 | tf.clear(); |
---|
29 | |
---|
30 | init_shaders(); |
---|
31 | |
---|
32 | R2string path = R2FilePath::getInstance()->getPath("Font.bmp"); |
---|
33 | init_font((const char*) path); |
---|
34 | } |
---|
35 | |
---|
36 | VolumeRenderer::~VolumeRenderer() |
---|
37 | { |
---|
38 | delete _zincBlendeShader; |
---|
39 | delete _regularVolumeShader; |
---|
40 | delete _stdVertexShader; |
---|
41 | } |
---|
42 | |
---|
43 | //initialize the volume shaders |
---|
44 | void VolumeRenderer::init_shaders(){ |
---|
45 | |
---|
46 | //standard vertex program |
---|
47 | _stdVertexShader = new NvStdVertexShader(); |
---|
48 | |
---|
49 | //volume rendering shader: one cubic volume |
---|
50 | _regularVolumeShader = new NvRegularVolumeShader(); |
---|
51 | |
---|
52 | //volume rendering shader: one zincblende orbital volume. |
---|
53 | //This shader renders one orbital of the simulation. |
---|
54 | //A sim has S, P, D, SS orbitals. thus a full rendering requires 4 zincblende orbital volumes. |
---|
55 | //A zincblende orbital volume is decomposed into 2 "interlocking" cubic 4-component volumes and passed to the shader. |
---|
56 | //We render each orbital with a independent transfer functions then blend the result. |
---|
57 | // |
---|
58 | //The engine is already capable of rendering multiple volumes and combine them. Thus, we just invoke this shader on |
---|
59 | //S, P, D and SS orbitals with different transfor functions. The result is a multi-orbital rendering. |
---|
60 | _zincBlendeShader = new NvZincBlendeVolumeShader(); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | int VolumeRenderer::add_volume(Volume* _vol, TransferFunction* _tf){ |
---|
65 | |
---|
66 | int ret = n_volumes; |
---|
67 | |
---|
68 | volume.push_back(_vol); |
---|
69 | tf.push_back(_tf); |
---|
70 | |
---|
71 | n_volumes++; |
---|
72 | |
---|
73 | return ret; |
---|
74 | } |
---|
75 | |
---|
76 | void |
---|
77 | VolumeRenderer::shade_volume(Volume* _vol, TransferFunction* _tf) |
---|
78 | { |
---|
79 | for (int i=0; i < volume.size(); i++) { |
---|
80 | if (volume[i] == _vol) { |
---|
81 | tf[i] = _tf; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | TransferFunction* |
---|
87 | VolumeRenderer::get_volume_shading(Volume* _vol) |
---|
88 | { |
---|
89 | for (int i=0; i < volume.size(); i++) { |
---|
90 | if (volume[i] == _vol) { |
---|
91 | return tf[i]; |
---|
92 | } |
---|
93 | } |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | typedef struct SortElement{ |
---|
98 | float z; |
---|
99 | int volume_id; |
---|
100 | int slice_id; |
---|
101 | SortElement(float _z, int _v, int _s): |
---|
102 | z(_z), volume_id(_v), slice_id(_s){} |
---|
103 | }; |
---|
104 | |
---|
105 | int slice_sort(const void* a, const void* b){ |
---|
106 | if((*((SortElement*)a)).z > (*((SortElement*)b)).z) |
---|
107 | return 1; |
---|
108 | else |
---|
109 | return -1; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void VolumeRenderer::render_all() |
---|
114 | { |
---|
115 | int total_rendered_slices = 0; |
---|
116 | |
---|
117 | ConvexPolygon*** polys = new ConvexPolygon**[n_volumes]; //two dimension pointer array |
---|
118 | //storing the slices |
---|
119 | int* actual_slices = new int[n_volumes]; //number of actual slices for each volume |
---|
120 | |
---|
121 | for(int i=0; i<n_volumes; i++){ |
---|
122 | polys[i] = NULL; |
---|
123 | actual_slices[i] = 0; |
---|
124 | |
---|
125 | if(!volume[i]->is_enabled()) |
---|
126 | continue; //skip this volume |
---|
127 | |
---|
128 | int volume_index = i; |
---|
129 | int n_slices = volume[volume_index]->get_n_slice(); |
---|
130 | |
---|
131 | //volume start location |
---|
132 | Vector3* location = volume[volume_index]->get_location(); |
---|
133 | Vector4 shift_4d(location->x, location->y, location->z, 0); |
---|
134 | |
---|
135 | double x0 = 0; |
---|
136 | double y0 = 0; |
---|
137 | double z0 = 0; |
---|
138 | |
---|
139 | Mat4x4 model_view_no_trans, model_view_trans; |
---|
140 | Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse; |
---|
141 | |
---|
142 | double zNear, zFar; |
---|
143 | |
---|
144 | //initialize volume plane with world coordinates |
---|
145 | Plane volume_planes[6]; |
---|
146 | volume_planes[0].set_coeffs(1, 0, 0, -x0); |
---|
147 | volume_planes[1].set_coeffs(-1, 0, 0, x0+1); |
---|
148 | volume_planes[2].set_coeffs(0, 1, 0, -y0); |
---|
149 | volume_planes[3].set_coeffs(0, -1, 0, y0+1); |
---|
150 | volume_planes[4].set_coeffs(0, 0, 1, -z0); |
---|
151 | volume_planes[5].set_coeffs(0, 0, -1, z0+1); |
---|
152 | |
---|
153 | //get modelview matrix with no translation |
---|
154 | glPushMatrix(); |
---|
155 | glScalef(volume[volume_index]->aspect_ratio_width, |
---|
156 | volume[volume_index]->aspect_ratio_height, |
---|
157 | volume[volume_index]->aspect_ratio_depth); |
---|
158 | |
---|
159 | glEnable(GL_DEPTH_TEST); |
---|
160 | |
---|
161 | GLfloat mv_no_trans[16]; |
---|
162 | glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans); |
---|
163 | |
---|
164 | model_view_no_trans = Mat4x4(mv_no_trans); |
---|
165 | model_view_no_trans_inverse = model_view_no_trans.inverse(); |
---|
166 | |
---|
167 | glPopMatrix(); |
---|
168 | |
---|
169 | //get modelview matrix with translation |
---|
170 | glPushMatrix(); |
---|
171 | glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z); |
---|
172 | glScalef(volume[volume_index]->aspect_ratio_width, |
---|
173 | volume[volume_index]->aspect_ratio_height, |
---|
174 | volume[volume_index]->aspect_ratio_depth); |
---|
175 | GLfloat mv_trans[16]; |
---|
176 | glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans); |
---|
177 | |
---|
178 | model_view_trans = Mat4x4(mv_trans); |
---|
179 | model_view_trans_inverse = model_view_trans.inverse(); |
---|
180 | |
---|
181 | //draw volume bounding box with translation (the correct location in space) |
---|
182 | if (volume[volume_index]->outline_is_enabled()) { |
---|
183 | float olcolor[3]; |
---|
184 | volume[volume_index]->get_outline_color(olcolor); |
---|
185 | draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1, |
---|
186 | (double)olcolor[0], (double)olcolor[1], (double)olcolor[2], |
---|
187 | 1.5); |
---|
188 | } |
---|
189 | glPopMatrix(); |
---|
190 | |
---|
191 | //draw labels |
---|
192 | glPushMatrix(); |
---|
193 | glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z); |
---|
194 | if(volume[i]->outline_is_enabled()) { |
---|
195 | draw_label(i); |
---|
196 | } |
---|
197 | glPopMatrix(); |
---|
198 | |
---|
199 | //transform volume_planes to eye coordinates. |
---|
200 | for(int i=0; i<6; i++) |
---|
201 | volume_planes[i].transform(model_view_no_trans); |
---|
202 | get_near_far_z(mv_no_trans, zNear, zFar); |
---|
203 | |
---|
204 | //compute actual rendering slices |
---|
205 | float z_step = fabs(zNear-zFar)/n_slices; |
---|
206 | int n_actual_slices; |
---|
207 | |
---|
208 | if (volume[volume_index]->data_is_enabled()) { |
---|
209 | n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1); |
---|
210 | polys[volume_index] = new ConvexPolygon*[n_actual_slices]; |
---|
211 | } else { |
---|
212 | n_actual_slices = 0; |
---|
213 | polys[volume_index] = NULL; |
---|
214 | } |
---|
215 | actual_slices[volume_index] = n_actual_slices; |
---|
216 | |
---|
217 | Vector4 vert1 = (Vector4(-10, -10, -0.5, 1)); |
---|
218 | Vector4 vert2 = (Vector4(-10, +10, -0.5, 1)); |
---|
219 | Vector4 vert3 = (Vector4(+10, +10, -0.5, 1)); |
---|
220 | Vector4 vert4 = (Vector4(+10, -10, -0.5, 1)); |
---|
221 | |
---|
222 | |
---|
223 | //Render cutplanes first with depth test enabled. |
---|
224 | //They will mark the image with their depth values. Then we render other volume slices. |
---|
225 | //These volume slices will be occluded correctly by the cutplanes and vice versa. |
---|
226 | |
---|
227 | ConvexPolygon static_poly; |
---|
228 | for(int i=0; i<volume[volume_index]->get_cutplane_count(); i++) |
---|
229 | { |
---|
230 | if(!volume[volume_index]->cutplane_is_enabled(i)) |
---|
231 | continue; |
---|
232 | |
---|
233 | float offset = volume[volume_index]->get_cutplane(i)->offset; |
---|
234 | int axis = volume[volume_index]->get_cutplane(i)->orient; |
---|
235 | |
---|
236 | if(axis==3){ |
---|
237 | vert1 = Vector4(-10, -10, offset, 1); |
---|
238 | vert2 = Vector4(-10, +10, offset, 1); |
---|
239 | vert3 = Vector4(+10, +10, offset, 1); |
---|
240 | vert4 = Vector4(+10, -10, offset, 1); |
---|
241 | //continue; |
---|
242 | } |
---|
243 | else if(axis==1){ |
---|
244 | vert1 = Vector4(offset, -10, -10, 1); |
---|
245 | vert2 = Vector4(offset, +10, -10, 1); |
---|
246 | vert3 = Vector4(offset, +10, +10, 1); |
---|
247 | vert4 = Vector4(offset, -10, +10, 1); |
---|
248 | //continue; |
---|
249 | } |
---|
250 | else if(axis==2){ |
---|
251 | vert1 = Vector4(-10, offset, -10, 1); |
---|
252 | vert2 = Vector4(+10, offset, -10, 1); |
---|
253 | vert3 = Vector4(+10, offset, +10, 1); |
---|
254 | vert4 = Vector4(-10, offset, +10, 1); |
---|
255 | //continue; |
---|
256 | } |
---|
257 | |
---|
258 | vert1 = model_view_no_trans.transform(vert1); |
---|
259 | vert2 = model_view_no_trans.transform(vert2); |
---|
260 | vert3 = model_view_no_trans.transform(vert3); |
---|
261 | vert4 = model_view_no_trans.transform(vert4); |
---|
262 | |
---|
263 | ConvexPolygon* p = &static_poly; |
---|
264 | p->vertices.clear(); |
---|
265 | |
---|
266 | p->append_vertex(vert1); |
---|
267 | p->append_vertex(vert2); |
---|
268 | p->append_vertex(vert3); |
---|
269 | p->append_vertex(vert4); |
---|
270 | |
---|
271 | for(int k=0; k<6; k++){ |
---|
272 | p->clip(volume_planes[k], true); |
---|
273 | } |
---|
274 | |
---|
275 | p->transform(model_view_no_trans_inverse); |
---|
276 | p->transform(model_view_trans); |
---|
277 | |
---|
278 | glPushMatrix(); |
---|
279 | glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth); |
---|
280 | |
---|
281 | activate_volume_shader(volume_index, true); |
---|
282 | glPopMatrix(); |
---|
283 | |
---|
284 | glEnable(GL_DEPTH_TEST); |
---|
285 | glDisable(GL_BLEND); |
---|
286 | |
---|
287 | glBegin(GL_POLYGON); |
---|
288 | p->Emit(true); |
---|
289 | glEnd(); |
---|
290 | glDisable(GL_DEPTH_TEST); |
---|
291 | |
---|
292 | deactivate_volume_shader(); |
---|
293 | } //done cutplanes |
---|
294 | |
---|
295 | |
---|
296 | //Now do volume rendering |
---|
297 | |
---|
298 | vert1 = (Vector4(-10, -10, -0.5, 1)); |
---|
299 | vert2 = (Vector4(-10, +10, -0.5, 1)); |
---|
300 | vert3 = (Vector4(+10, +10, -0.5, 1)); |
---|
301 | vert4 = (Vector4(+10, -10, -0.5, 1)); |
---|
302 | |
---|
303 | int counter = 0; |
---|
304 | |
---|
305 | //transform slices and store them |
---|
306 | float slice_z; |
---|
307 | for (int i=0; i<n_actual_slices; i++){ |
---|
308 | slice_z = zFar + i * z_step; //back to front |
---|
309 | |
---|
310 | ConvexPolygon *poly = new ConvexPolygon(); |
---|
311 | polys[volume_index][counter] = poly; |
---|
312 | counter++; |
---|
313 | |
---|
314 | poly->vertices.clear(); |
---|
315 | poly->set_id(volume_index); |
---|
316 | |
---|
317 | //Setting Z-coordinate |
---|
318 | vert1.z = slice_z; |
---|
319 | vert2.z = slice_z; |
---|
320 | vert3.z = slice_z; |
---|
321 | vert4.z = slice_z; |
---|
322 | |
---|
323 | poly->append_vertex(vert1); |
---|
324 | poly->append_vertex(vert2); |
---|
325 | poly->append_vertex(vert3); |
---|
326 | poly->append_vertex(vert4); |
---|
327 | |
---|
328 | for(int k=0; k<6; k++){ |
---|
329 | poly->clip(volume_planes[k], true); |
---|
330 | } |
---|
331 | |
---|
332 | poly->transform(model_view_no_trans_inverse); |
---|
333 | poly->transform(model_view_trans); |
---|
334 | |
---|
335 | if(poly->vertices.size()>=3) |
---|
336 | total_rendered_slices++; |
---|
337 | } |
---|
338 | |
---|
339 | } //iterate all volumes |
---|
340 | //fprintf(stderr, "total slices: %d\n", total_rendered_slices); |
---|
341 | |
---|
342 | //We sort all the polygons according to their eye-space depth, from farthest to the closest. |
---|
343 | //This step is critical for correct blending |
---|
344 | |
---|
345 | SortElement* slices = (SortElement*) malloc(sizeof(SortElement)*total_rendered_slices); |
---|
346 | |
---|
347 | int counter = 0; |
---|
348 | for(int i=0; i<n_volumes; i++){ |
---|
349 | for(int j=0; j<actual_slices[i]; j++){ |
---|
350 | if(polys[i][j]->vertices.size() >= 3){ |
---|
351 | slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j); |
---|
352 | counter++; |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | //sort them |
---|
358 | qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort); |
---|
359 | |
---|
360 | /* |
---|
361 | //debug |
---|
362 | for(int i=0; i<total_rendered_slices; i++){ |
---|
363 | fprintf(stderr, "%f ", slices[i].z); |
---|
364 | } |
---|
365 | fprintf(stderr, "\n\n"); |
---|
366 | */ |
---|
367 | |
---|
368 | //Now we are ready to render all the slices from back to front |
---|
369 | glEnable(GL_DEPTH_TEST); |
---|
370 | glEnable(GL_BLEND); |
---|
371 | |
---|
372 | for(int i=0; i<total_rendered_slices; i++){ |
---|
373 | int volume_index = slices[i].volume_id; |
---|
374 | int slice_index = slices[i].slice_id; |
---|
375 | ConvexPolygon* cur = polys[volume_index][slice_index]; |
---|
376 | |
---|
377 | glPushMatrix(); |
---|
378 | glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth); |
---|
379 | |
---|
380 | activate_volume_shader(volume_index, false); |
---|
381 | glPopMatrix(); |
---|
382 | |
---|
383 | glBegin(GL_POLYGON); |
---|
384 | cur->Emit(true); |
---|
385 | glEnd(); |
---|
386 | |
---|
387 | deactivate_volume_shader(); |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | glDisable(GL_DEPTH_TEST); |
---|
392 | glDisable(GL_BLEND); |
---|
393 | |
---|
394 | //Deallocate all the memory used |
---|
395 | for(int i=0; i<n_volumes; i++){ |
---|
396 | for(int j=0; j<actual_slices[i]; j++){ |
---|
397 | delete polys[i][j]; |
---|
398 | } |
---|
399 | if (polys[i]) { |
---|
400 | delete[] polys[i]; |
---|
401 | } |
---|
402 | } |
---|
403 | delete[] polys; |
---|
404 | delete[] actual_slices; |
---|
405 | free(slices); |
---|
406 | } |
---|
407 | |
---|
408 | |
---|
409 | void VolumeRenderer::render(int volume_index){ |
---|
410 | int n_slices = volume[volume_index]->get_n_slice(); |
---|
411 | |
---|
412 | //volume start location |
---|
413 | Vector4 shift_4d(volume[volume_index]->location.x, volume[volume_index]->location.y, volume[volume_index]->location.z, 0); |
---|
414 | |
---|
415 | double x0 = 0; |
---|
416 | double y0 = 0; |
---|
417 | double z0 = 0; |
---|
418 | |
---|
419 | Mat4x4 model_view_no_trans, model_view_trans; |
---|
420 | Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse; |
---|
421 | |
---|
422 | double zNear, zFar; |
---|
423 | |
---|
424 | //initialize volume plane with world coordinates |
---|
425 | Plane volume_planes[6]; |
---|
426 | volume_planes[0].set_coeffs(1, 0, 0, -x0); |
---|
427 | volume_planes[1].set_coeffs(-1, 0, 0, x0+1); |
---|
428 | volume_planes[2].set_coeffs(0, 1, 0, -y0); |
---|
429 | volume_planes[3].set_coeffs(0, -1, 0, y0+1); |
---|
430 | volume_planes[4].set_coeffs(0, 0, 1, -z0); |
---|
431 | volume_planes[5].set_coeffs(0, 0, -1, z0+1); |
---|
432 | |
---|
433 | glPushMatrix(); |
---|
434 | |
---|
435 | glScalef(volume[volume_index]->aspect_ratio_width, |
---|
436 | volume[volume_index]->aspect_ratio_height, |
---|
437 | volume[volume_index]->aspect_ratio_depth); |
---|
438 | |
---|
439 | glEnable(GL_DEPTH_TEST); |
---|
440 | |
---|
441 | GLfloat mv_no_trans[16]; |
---|
442 | glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans); |
---|
443 | |
---|
444 | model_view_no_trans = Mat4x4(mv_no_trans); |
---|
445 | model_view_no_trans_inverse = model_view_no_trans.inverse(); |
---|
446 | |
---|
447 | glPopMatrix(); |
---|
448 | |
---|
449 | //get modelview matrix with translation |
---|
450 | glPushMatrix(); |
---|
451 | glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z); |
---|
452 | glScalef(volume[volume_index]->aspect_ratio_width, |
---|
453 | volume[volume_index]->aspect_ratio_height, |
---|
454 | volume[volume_index]->aspect_ratio_depth); |
---|
455 | GLfloat mv_trans[16]; |
---|
456 | glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans); |
---|
457 | |
---|
458 | model_view_trans = Mat4x4(mv_trans); |
---|
459 | model_view_trans_inverse = model_view_trans.inverse(); |
---|
460 | |
---|
461 | //draw volume bounding box |
---|
462 | draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1, 0.8, 0.1, 0.1, 1.5); |
---|
463 | glPopMatrix(); |
---|
464 | |
---|
465 | //transform volume_planes to eye coordinates. |
---|
466 | for(int i=0; i<6; i++) |
---|
467 | volume_planes[i].transform(model_view_no_trans); |
---|
468 | |
---|
469 | get_near_far_z(mv_no_trans, zNear, zFar); |
---|
470 | //fprintf(stderr, "zNear:%f, zFar:%f\n", zNear, zFar); |
---|
471 | //fflush(stderr); |
---|
472 | |
---|
473 | //compute actual rendering slices |
---|
474 | float z_step = fabs(zNear-zFar)/n_slices; |
---|
475 | int n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1); |
---|
476 | //fprintf(stderr, "slices: %d\n", n_actual_slices); |
---|
477 | //fflush(stderr); |
---|
478 | |
---|
479 | static ConvexPolygon staticPoly; |
---|
480 | float slice_z; |
---|
481 | |
---|
482 | Vector4 vert1 = (Vector4(-10, -10, -0.5, 1)); |
---|
483 | Vector4 vert2 = (Vector4(-10, +10, -0.5, 1)); |
---|
484 | Vector4 vert3 = (Vector4(+10, +10, -0.5, 1)); |
---|
485 | Vector4 vert4 = (Vector4(+10, -10, -0.5, 1)); |
---|
486 | |
---|
487 | glEnable(GL_BLEND); |
---|
488 | |
---|
489 | if(slice_mode){ |
---|
490 | glEnable(GL_DEPTH_TEST); |
---|
491 | |
---|
492 | //render the cut planes |
---|
493 | for(int i=0; i<volume[volume_index]->get_cutplane_count(); i++){ |
---|
494 | float offset = volume[volume_index]->get_cutplane(i)->offset; |
---|
495 | int axis = volume[volume_index]->get_cutplane(i)->orient; |
---|
496 | |
---|
497 | if(axis==1){ |
---|
498 | vert1 = Vector4(-10, -10, offset, 1); |
---|
499 | vert2 = Vector4(-10, +10, offset, 1); |
---|
500 | vert3 = Vector4(+10, +10, offset, 1); |
---|
501 | vert4 = Vector4(+10, -10, offset, 1); |
---|
502 | } |
---|
503 | else if(axis==2){ |
---|
504 | vert1 = Vector4(offset, -10, -10, 1); |
---|
505 | vert2 = Vector4(offset, +10, -10, 1); |
---|
506 | vert3 = Vector4(offset, +10, +10, 1); |
---|
507 | vert4 = Vector4(offset, -10, +10, 1); |
---|
508 | } |
---|
509 | else if(axis==3){ |
---|
510 | vert1 = Vector4(-10, offset, -10, 1); |
---|
511 | vert2 = Vector4(+10, offset, -10, 1); |
---|
512 | vert3 = Vector4(+10, offset, +10, 1); |
---|
513 | vert4 = Vector4(-10, offset, +10, 1); |
---|
514 | } |
---|
515 | |
---|
516 | vert1 = model_view_no_trans.transform(vert1); |
---|
517 | vert2 = model_view_no_trans.transform(vert2); |
---|
518 | vert3 = model_view_no_trans.transform(vert3); |
---|
519 | vert4 = model_view_no_trans.transform(vert4); |
---|
520 | |
---|
521 | ConvexPolygon *poly; |
---|
522 | poly = &staticPoly; |
---|
523 | poly->vertices.clear(); |
---|
524 | |
---|
525 | poly->append_vertex(vert1); |
---|
526 | poly->append_vertex(vert2); |
---|
527 | poly->append_vertex(vert3); |
---|
528 | poly->append_vertex(vert4); |
---|
529 | |
---|
530 | for(int k=0; k<6; k++){ |
---|
531 | poly->clip(volume_planes[k], true); |
---|
532 | } |
---|
533 | |
---|
534 | //poly->transform(model_view_inverse); |
---|
535 | //poly->translate(shift_4d); |
---|
536 | //poly->transform(model_view); |
---|
537 | poly->transform(model_view_no_trans_inverse); |
---|
538 | poly->transform(model_view_trans); |
---|
539 | |
---|
540 | glPushMatrix(); |
---|
541 | glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth); |
---|
542 | |
---|
543 | activate_volume_shader(volume_index, true); |
---|
544 | glPopMatrix(); |
---|
545 | |
---|
546 | glBegin(GL_POLYGON); |
---|
547 | poly->Emit(true); |
---|
548 | glEnd(); |
---|
549 | |
---|
550 | deactivate_volume_shader(); |
---|
551 | } |
---|
552 | } //slice_mode |
---|
553 | |
---|
554 | |
---|
555 | if(volume_mode){ |
---|
556 | glEnable(GL_DEPTH_TEST); |
---|
557 | |
---|
558 | for (int i=0; i<n_actual_slices; i++){ |
---|
559 | slice_z = zFar + i * z_step; //back to front |
---|
560 | |
---|
561 | ConvexPolygon *poly; |
---|
562 | poly = &staticPoly; |
---|
563 | poly->vertices.clear(); |
---|
564 | |
---|
565 | //Setting Z-coordinate |
---|
566 | vert1.z = slice_z; |
---|
567 | vert2.z = slice_z; |
---|
568 | vert3.z = slice_z; |
---|
569 | vert4.z = slice_z; |
---|
570 | |
---|
571 | poly->append_vertex(vert1); |
---|
572 | poly->append_vertex(vert2); |
---|
573 | poly->append_vertex(vert3); |
---|
574 | poly->append_vertex(vert4); |
---|
575 | |
---|
576 | for(int k=0; k<6; k++){ |
---|
577 | poly->clip(volume_planes[k], true); |
---|
578 | } |
---|
579 | |
---|
580 | //move the volume to the proper location |
---|
581 | //poly->transform(model_view_inverse); |
---|
582 | //poly->translate(shift_4d); |
---|
583 | //poly->transform(model_view); |
---|
584 | |
---|
585 | poly->transform(model_view_no_trans_inverse); |
---|
586 | poly->transform(model_view_trans); |
---|
587 | |
---|
588 | glPushMatrix(); |
---|
589 | glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth); |
---|
590 | |
---|
591 | /* |
---|
592 | //draw slice lines only |
---|
593 | glDisable(GL_BLEND); |
---|
594 | glDisable(GL_TEXTURE_3D); |
---|
595 | glDisable(GL_TEXTURE_2D); |
---|
596 | glLineWidth(1.0); |
---|
597 | glColor3f(1,1,1); |
---|
598 | glBegin(GL_LINE_LOOP); |
---|
599 | poly->Emit(false); |
---|
600 | glEnd(); |
---|
601 | */ |
---|
602 | |
---|
603 | activate_volume_shader(volume_index, false); |
---|
604 | glPopMatrix(); |
---|
605 | |
---|
606 | glBegin(GL_POLYGON); |
---|
607 | poly->Emit(true); |
---|
608 | glEnd(); |
---|
609 | |
---|
610 | deactivate_volume_shader(); |
---|
611 | |
---|
612 | } |
---|
613 | } //volume_mode |
---|
614 | |
---|
615 | glDisable(GL_BLEND); |
---|
616 | glDisable(GL_DEPTH_TEST); |
---|
617 | } |
---|
618 | |
---|
619 | void VolumeRenderer::draw_bounding_box(float x0, float y0, float z0, |
---|
620 | float x1, float y1, float z1, |
---|
621 | float r, float g, float b, float line_width) |
---|
622 | { |
---|
623 | glDisable(GL_TEXTURE_2D); |
---|
624 | |
---|
625 | glColor4d(r, g, b, 1.0); |
---|
626 | glLineWidth(line_width); |
---|
627 | |
---|
628 | glBegin(GL_LINE_LOOP); |
---|
629 | |
---|
630 | glVertex3d(x0, y0, z0); |
---|
631 | glVertex3d(x1, y0, z0); |
---|
632 | glVertex3d(x1, y1, z0); |
---|
633 | glVertex3d(x0, y1, z0); |
---|
634 | |
---|
635 | glEnd(); |
---|
636 | |
---|
637 | glBegin(GL_LINE_LOOP); |
---|
638 | |
---|
639 | glVertex3d(x0, y0, z1); |
---|
640 | glVertex3d(x1, y0, z1); |
---|
641 | glVertex3d(x1, y1, z1); |
---|
642 | glVertex3d(x0, y1, z1); |
---|
643 | |
---|
644 | glEnd(); |
---|
645 | |
---|
646 | |
---|
647 | glBegin(GL_LINE_LOOP); |
---|
648 | |
---|
649 | glVertex3d(x0, y0, z0); |
---|
650 | glVertex3d(x0, y0, z1); |
---|
651 | glVertex3d(x0, y1, z1); |
---|
652 | glVertex3d(x0, y1, z0); |
---|
653 | |
---|
654 | glEnd(); |
---|
655 | |
---|
656 | glBegin(GL_LINE_LOOP); |
---|
657 | |
---|
658 | glVertex3d(x1, y0, z0); |
---|
659 | glVertex3d(x1, y0, z1); |
---|
660 | glVertex3d(x1, y1, z1); |
---|
661 | glVertex3d(x1, y1, z0); |
---|
662 | |
---|
663 | glEnd(); |
---|
664 | |
---|
665 | glEnable(GL_TEXTURE_2D); |
---|
666 | } |
---|
667 | |
---|
668 | |
---|
669 | |
---|
670 | void VolumeRenderer::activate_volume_shader(int volume_index, bool slice_mode) |
---|
671 | { |
---|
672 | //vertex shader |
---|
673 | _stdVertexShader->bind(); |
---|
674 | |
---|
675 | if (volume[volume_index]->volume_type == CUBIC) |
---|
676 | { |
---|
677 | //regular cubic volume |
---|
678 | _regularVolumeShader->bind(tf[volume_index]->id, volume[volume_index], slice_mode); |
---|
679 | } |
---|
680 | else if (volume[volume_index]->volume_type == ZINCBLENDE) |
---|
681 | { |
---|
682 | _zincBlendeShader->bind(tf[volume_index]->id, volume[volume_index], slice_mode); |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | |
---|
687 | void VolumeRenderer::deactivate_volume_shader() |
---|
688 | { |
---|
689 | _stdVertexShader->unbind(); |
---|
690 | _regularVolumeShader->unbind(); |
---|
691 | _zincBlendeShader->unbind(); |
---|
692 | } |
---|
693 | |
---|
694 | |
---|
695 | void VolumeRenderer::get_near_far_z(Mat4x4 mv, double &zNear, double &zFar) |
---|
696 | { |
---|
697 | |
---|
698 | double x0 = 0; |
---|
699 | double y0 = 0; |
---|
700 | double z0 = 0; |
---|
701 | double x1 = 1; |
---|
702 | double y1 = 1; |
---|
703 | double z1 = 1; |
---|
704 | |
---|
705 | double zMin, zMax; |
---|
706 | zMin = 10000; |
---|
707 | zMax = -10000; |
---|
708 | |
---|
709 | double vertex[8][4]; |
---|
710 | |
---|
711 | vertex[0][0]=x0; vertex[0][1]=y0; vertex[0][2]=z0; vertex[0][3]=1.0; |
---|
712 | vertex[1][0]=x1; vertex[1][1]=y0; vertex[1][2]=z0; vertex[1][3]=1.0; |
---|
713 | vertex[2][0]=x0; vertex[2][1]=y1; vertex[2][2]=z0; vertex[2][3]=1.0; |
---|
714 | vertex[3][0]=x0; vertex[3][1]=y0; vertex[3][2]=z1; vertex[3][3]=1.0; |
---|
715 | vertex[4][0]=x1; vertex[4][1]=y1; vertex[4][2]=z0; vertex[4][3]=1.0; |
---|
716 | vertex[5][0]=x1; vertex[5][1]=y0; vertex[5][2]=z1; vertex[5][3]=1.0; |
---|
717 | vertex[6][0]=x0; vertex[6][1]=y1; vertex[6][2]=z1; vertex[6][3]=1.0; |
---|
718 | vertex[7][0]=x1; vertex[7][1]=y1; vertex[7][2]=z1; vertex[7][3]=1.0; |
---|
719 | |
---|
720 | for(int i=0;i<8;i++) |
---|
721 | { |
---|
722 | Vector4 tmp = mv.transform(Vector4(vertex[i][0], vertex[i][1], vertex[i][2], vertex[i][3])); |
---|
723 | tmp.perspective_devide(); |
---|
724 | vertex[i][2] = tmp.z; |
---|
725 | if (vertex[i][2]<zMin) zMin = vertex[i][2]; |
---|
726 | if (vertex[i][2]>zMax) zMax = vertex[i][2]; |
---|
727 | } |
---|
728 | |
---|
729 | zNear = zMax; |
---|
730 | zFar = zMin; |
---|
731 | } |
---|
732 | |
---|
733 | void VolumeRenderer::set_slice_mode(bool val) { slice_mode = val; } |
---|
734 | void VolumeRenderer::set_volume_mode(bool val) { volume_mode = val; } |
---|
735 | void VolumeRenderer::switch_slice_mode() { slice_mode = (!slice_mode); } |
---|
736 | void VolumeRenderer::switch_volume_mode() { volume_mode = (!volume_mode); } |
---|
737 | |
---|
738 | void VolumeRenderer::enable_volume(int index){ |
---|
739 | volume[index]->enable(); |
---|
740 | } |
---|
741 | |
---|
742 | void VolumeRenderer::disable_volume(int index){ |
---|
743 | volume[index]->disable(); |
---|
744 | } |
---|
745 | |
---|
746 | |
---|
747 | void VolumeRenderer::init_font(const char* filename) { |
---|
748 | |
---|
749 | FILE *file; |
---|
750 | unsigned short int bfType; |
---|
751 | long int bfOffBits; |
---|
752 | short int biPlanes; |
---|
753 | short int biBitCount; |
---|
754 | long int biSizeImage; |
---|
755 | int width, height; |
---|
756 | int i; |
---|
757 | unsigned char temp; |
---|
758 | |
---|
759 | |
---|
760 | /* make sure the file is there and open it read-only (binary) */ |
---|
761 | if ((file = fopen(filename, "rb")) == NULL) |
---|
762 | { |
---|
763 | assert(false); |
---|
764 | } |
---|
765 | |
---|
766 | if(!fread(&bfType, sizeof(short int), 1, file)) |
---|
767 | { |
---|
768 | assert(false); |
---|
769 | //printf("Error reading file!\n"); |
---|
770 | } |
---|
771 | |
---|
772 | /* check if file is a bitmap */ |
---|
773 | if (bfType != 19778) |
---|
774 | { |
---|
775 | assert(false); |
---|
776 | //printf("Not a Bitmap-File!\n"); |
---|
777 | } |
---|
778 | |
---|
779 | /* get the file size */ |
---|
780 | /* skip file size and reserved fields of bitmap file header */ |
---|
781 | fseek(file, 8, SEEK_CUR); |
---|
782 | |
---|
783 | /* get the position of the actual bitmap data */ |
---|
784 | if (!fread(&bfOffBits, sizeof(long int), 1, file)) |
---|
785 | { |
---|
786 | assert(false); |
---|
787 | //printf("Error reading file!\n"); |
---|
788 | } |
---|
789 | //printf("Data at Offset: %ld\n", bfOffBits); |
---|
790 | |
---|
791 | /* skip size of bitmap info header */ |
---|
792 | fseek(file, 4, SEEK_CUR); |
---|
793 | |
---|
794 | /* get the width of the bitmap */ |
---|
795 | fread(&width, sizeof(int), 1, file); |
---|
796 | //printf("Width of Bitmap: %d\n", texture->width); |
---|
797 | |
---|
798 | /* get the height of the bitmap */ |
---|
799 | fread(&height, sizeof(int), 1, file); |
---|
800 | //printf("Height of Bitmap: %d\n", texture->height); |
---|
801 | |
---|
802 | /* get the number of planes (must be set to 1) */ |
---|
803 | fread(&biPlanes, sizeof(short int), 1, file); |
---|
804 | if (biPlanes != 1) |
---|
805 | { |
---|
806 | assert(false); |
---|
807 | //printf("Error: number of Planes not 1!\n"); |
---|
808 | } |
---|
809 | |
---|
810 | /* get the number of bits per pixel */ |
---|
811 | if (!fread(&biBitCount, sizeof(short int), 1, file)) |
---|
812 | { |
---|
813 | assert(false); |
---|
814 | //printf("Error reading file!\n"); |
---|
815 | //return 0; |
---|
816 | } |
---|
817 | |
---|
818 | //printf("Bits per Pixel: %d\n", biBitCount); |
---|
819 | if (biBitCount != 24) |
---|
820 | { |
---|
821 | assert(false); |
---|
822 | //printf("Bits per Pixel not 24\n"); |
---|
823 | //return 0; |
---|
824 | } |
---|
825 | |
---|
826 | |
---|
827 | /* calculate the size of the image in bytes */ |
---|
828 | biSizeImage = width * height * 3 * sizeof(unsigned char); |
---|
829 | unsigned char* data = (unsigned char*) malloc(biSizeImage); |
---|
830 | |
---|
831 | |
---|
832 | /* seek to the actual data */ |
---|
833 | fseek(file, bfOffBits, SEEK_SET); |
---|
834 | if (!fread(data, biSizeImage, 1, file)) |
---|
835 | { |
---|
836 | assert(false); |
---|
837 | //printf("Error loading file!\n"); |
---|
838 | } |
---|
839 | |
---|
840 | /* swap red and blue (bgr -> rgb) */ |
---|
841 | for (i = 0; i < biSizeImage; i += 3) |
---|
842 | { |
---|
843 | temp = data[i]; |
---|
844 | data[i] = data[i + 2]; |
---|
845 | data[i + 2] = temp; |
---|
846 | } |
---|
847 | |
---|
848 | //insert alpha channel |
---|
849 | unsigned char* data_with_alpha = (unsigned char*) malloc(width*height*4*sizeof(unsigned char)); |
---|
850 | for(int i=0; i<height; i++){ |
---|
851 | for(int j=0; j<width; j++){ |
---|
852 | unsigned char r, g, b, a; |
---|
853 | r = data[3*(i*width+j)]; |
---|
854 | g = data[3*(i*width+j)+1]; |
---|
855 | b = data[3*(i*width+j)+2]; |
---|
856 | |
---|
857 | if(r==0 && g==0 && b==0) |
---|
858 | a = 0; |
---|
859 | else |
---|
860 | a = 255; |
---|
861 | |
---|
862 | data_with_alpha[4*(i*width+j)] = r; |
---|
863 | data_with_alpha[4*(i*width+j) + 1] = g; |
---|
864 | data_with_alpha[4*(i*width+j) + 2] = b; |
---|
865 | data_with_alpha[4*(i*width+j) + 3] = a; |
---|
866 | |
---|
867 | } |
---|
868 | } |
---|
869 | free(data); |
---|
870 | |
---|
871 | //create opengl texture |
---|
872 | glGenTextures(1, &font_texture); |
---|
873 | glBindTexture(GL_TEXTURE_2D, font_texture); |
---|
874 | //glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); |
---|
875 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data_with_alpha); |
---|
876 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
877 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
878 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
---|
879 | |
---|
880 | free(data_with_alpha); |
---|
881 | |
---|
882 | build_font(); |
---|
883 | assert(glGetError()==0); |
---|
884 | } |
---|
885 | |
---|
886 | |
---|
887 | |
---|
888 | void VolumeRenderer::draw_label(int volume_index){ |
---|
889 | |
---|
890 | Volume* vol = volume[volume_index]; |
---|
891 | |
---|
892 | //glEnable(GL_TEXTURE_2D); |
---|
893 | glDisable(GL_TEXTURE_2D); |
---|
894 | glEnable(GL_DEPTH_TEST); |
---|
895 | |
---|
896 | //x |
---|
897 | glColor3f(0.5, 0.5, 0.5); |
---|
898 | |
---|
899 | int length = vol->label[0].size(); |
---|
900 | glPushMatrix(); |
---|
901 | |
---|
902 | glTranslatef(.5*vol->aspect_ratio_width, vol->aspect_ratio_height, -0.1*vol->aspect_ratio_depth); |
---|
903 | glRotatef(180, 0, 0, 1); |
---|
904 | glRotatef(90, 1, 0, 0); |
---|
905 | |
---|
906 | glScalef(0.0008, 0.0008, 0.0008); |
---|
907 | for(int i=0; i<length; i++){ |
---|
908 | glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[0].c_str()[i]); |
---|
909 | glTranslatef(0.04, 0., 0.); |
---|
910 | } |
---|
911 | glPopMatrix(); |
---|
912 | |
---|
913 | //y |
---|
914 | length = vol->label[1].size(); |
---|
915 | glPushMatrix(); |
---|
916 | glTranslatef(vol->aspect_ratio_width, 0.5*vol->aspect_ratio_height, -0.1*vol->aspect_ratio_depth); |
---|
917 | glRotatef(90, 0, 1, 0); |
---|
918 | glRotatef(90, 0, 0, 1); |
---|
919 | |
---|
920 | glScalef(0.0008, 0.0008, 0.0008); |
---|
921 | for(int i=0; i<length; i++){ |
---|
922 | glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[1].c_str()[i]); |
---|
923 | glTranslatef(0.04, 0., 0.); |
---|
924 | } |
---|
925 | glPopMatrix(); |
---|
926 | |
---|
927 | |
---|
928 | //z |
---|
929 | length = vol->label[2].size(); |
---|
930 | glPushMatrix(); |
---|
931 | glTranslatef(0., 1.*vol->aspect_ratio_height, 0.5*vol->aspect_ratio_depth); |
---|
932 | glRotatef(90, 0, 1, 0); |
---|
933 | |
---|
934 | glScalef(0.0008, 0.0008, 0.0008); |
---|
935 | for(int i=0; i<length; i++){ |
---|
936 | glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[2].c_str()[i]); |
---|
937 | glTranslatef(0.04, 0., 0.); |
---|
938 | } |
---|
939 | glPopMatrix(); |
---|
940 | |
---|
941 | glDisable(GL_TEXTURE_2D); |
---|
942 | } |
---|
943 | |
---|
944 | |
---|
945 | |
---|
946 | void VolumeRenderer::build_font() { |
---|
947 | |
---|
948 | GLfloat cx, cy; /* the character coordinates in our texture */ |
---|
949 | font_base = glGenLists(256); |
---|
950 | glBindTexture(GL_TEXTURE_2D, font_texture); |
---|
951 | for (int loop = 0; loop < 256; loop++) |
---|
952 | { |
---|
953 | cx = (float) (loop % 16) / 16.0f; |
---|
954 | cy = (float) (loop / 16) / 16.0f; |
---|
955 | glNewList(font_base + loop, GL_COMPILE); |
---|
956 | glBegin(GL_QUADS); |
---|
957 | glTexCoord2f(cx, 1 - cy - 0.0625f); |
---|
958 | glVertex3f(0, 0, 0); |
---|
959 | glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f); |
---|
960 | glVertex3f(0.04, 0, 0); |
---|
961 | glTexCoord2f(cx + 0.0625f, 1 - cy); |
---|
962 | glVertex3f(0.04, 0.04, 0); |
---|
963 | glTexCoord2f(cx, 1 - cy); |
---|
964 | glVertex3f(0, 0.04, 0); |
---|
965 | glEnd(); |
---|
966 | glTranslated(0.04, 0, 0); |
---|
967 | glEndList(); |
---|
968 | } |
---|
969 | } |
---|
970 | |
---|
971 | |
---|
972 | |
---|
973 | void VolumeRenderer::glPrint(char* string, int set){ |
---|
974 | |
---|
975 | if(set>1) set=1; |
---|
976 | |
---|
977 | glBindTexture(GL_TEXTURE_2D, font_texture); |
---|
978 | |
---|
979 | glListBase(font_base - 32 + (128 * set)); |
---|
980 | glCallLists(strlen(string), GL_BYTE, string); |
---|
981 | |
---|
982 | } |
---|
983 | |
---|