source: trunk/gui/vizservers/nanovis/VolumeRenderer.cpp @ 580

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