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

Last change on this file since 455 was 455, checked in by mmc, 18 years ago

Added a new "legend" command, which can be used to request the
legend strip for a transfer function.

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