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