Changeset 415


Ignore:
Timestamp:
Apr 27, 2006 10:55:14 AM (18 years ago)
Author:
qiaow
Message:

Now can load arbitary number of volumes (16 per application due to hardware limitation). The VolumeRenderer? render them with correct slice order!!

Location:
trunk/gui/vizservers/nanovis
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/gui/vizservers/nanovis/ConvexPolygon.cpp

    r226 r415  
    2525}
    2626
     27void ConvexPolygon::set_id(int v_id) { volume_id; };
     28
    2729void ConvexPolygon::append_vertex(Vector4 vert){
    2830    vertices.push_back(vert);
     
    105107}
    106108
    107 void ConvexPolygon::clip(Plane &clipPlane) {
     109void ConvexPolygon::clip(Plane &clipPlane, bool copy_to_texcoord) {
    108110        if (vertices.size() == 0) {
    109111                //fprintf(stderr, "ConvexPolygon: polygon has no vertices\n"); 
     
    159161                    clippedVerts.begin(),
    160162                    clippedVerts.end());
     163
     164    if(copy_to_texcoord)
     165      copy_vertices_to_texcoords();
    161166}
    162167
  • trunk/gui/vizservers/nanovis/ConvexPolygon.h

    r226 r415  
    2727        VertexVector vertices;
    2828        TexVector texcoords;
     29        int volume_id;  //which volume this polygon slice belongs to
    2930
    3031        ConvexPolygon();
     
    3738
    3839        // Clips the polygon, retaining the portion where ax + by + cz + d >= 0
    39         void clip(Plane &clipPlane);
     40        void clip(Plane &clipPlane, bool copy_to_texcoords);
    4041        void Emit(bool use_texture);
    4142        void Emit(bool use_texture, Vector3& shift, Vector3& scale);
    4243        void copy_vertices_to_texcoords();
     44        void set_id(int v_id);
    4345};
    4446
  • trunk/gui/vizservers/nanovis/Makefile

    r407 r415  
    2020CFLAG = -g -c -Wall $(INCLUDES)
    2121
    22 all: nanovis simclient
     22all: nanovis
     23#  simclient
    2324
    2425clean:
     
    5859        gcc -g -o client $(OBJ_CLIENT) $(LIB_CLIENT)
    5960
    60 simclient: Event.o nanoscale/EventPlayer.cpp
    61         gcc -g $(INCLUDES) $(NANOSCALESRC)/EventPlayer.cpp $(NANOSCALESRC)/clientlib.cpp -o simclient Event.o $(LIB_CLIENT)
    62         #g++ -g $(INCLUDES) $(NANOSCALESRC)/EventPlayer.cpp $(NANOSCALESRC)/clientlib.cpp -o simclient Event.o
     61#simclient: Event.o $(NANOSCALESRC)/EventPlayer.cpp
     62#       gcc -g $(INCLUDES) $(NANOSCALESRC)/EventPlayer.cpp $(NANOSCALESRC)/clientlib.cpp -o simclient Event.o $(LIB_CLIENT)
     63#       g++ -g $(INCLUDES) $(NANOSCALESRC)/EventPlayer.cpp $(NANOSCALESRC)/clientlib.cpp -o simclient Event.o
    6364
    6465Color.o: Color.cpp Color.h
  • trunk/gui/vizservers/nanovis/Plane.h

    r406 r415  
    2121class Plane {
    2222public:
    23         float a, b, c, d;
     23    float a, b, c, d;
    2424       
    2525    Plane(float a, float b, float c, float d);
  • trunk/gui/vizservers/nanovis/VolumeRenderer.cpp

    r407 r415  
    2323  cam(_cam),
    2424  n_volumes(0),
    25   g_context(_context)
     25  g_context(_context),
     26  slice_mode(false),
     27  volume_mode(true)
    2628{
    2729  //init the vectors for volumes and transfer functions
     
    5456VolumeRenderer::~VolumeRenderer(){}
    5557
    56 void VolumeRenderer::add_volume(Volume* _vol, TransferFunction* _tf, int _slice){
     58int VolumeRenderer::add_volume(Volume* _vol, TransferFunction* _tf, int _slice){
     59  int ret = n_volumes;
     60
    5761  volume.push_back(_vol);
    5862  tf.push_back(_tf);
     
    6165
    6266  n_volumes++;
    63 }
     67
     68  return ret;
     69}
     70
     71typedef struct SortElement{
     72  float z;
     73  int volume_id;
     74  int slice_id;
     75  SortElement(float _z, int _v, int _s):
     76          z(_z), volume_id(_v), slice_id(_s){}
     77};
     78
     79int slice_sort(const void* a, const void* b){
     80  if((*((SortElement*)a)).z > (*((SortElement*)b)).z)
     81      return 1;
     82   else
     83      return -1;
     84}
     85
     86
     87void VolumeRenderer::render_all(){
     88  int total_enabled_volumes = 0;
     89  int total_rendered_slices = 0;
     90
     91  for(int i=0; i<n_volumes; i++){
     92    if(render_bit[i])
     93      total_enabled_volumes ++;
     94  }
     95  fprintf(stderr, "total volumes rendered: %d\n", total_enabled_volumes);
     96
     97  ConvexPolygon*** polys = new ConvexPolygon**[total_enabled_volumes];
     98  int* actual_slices = new int[total_enabled_volumes];
     99
     100  int cur_index = -1;
     101  for(int i=0; i<n_volumes; i++){
     102    if(!render_bit[i])
     103      continue;
     104    cur_index++;
     105
     106    int volume_index = i;
     107    int n_slices = slice[volume_index];
     108
     109    //volume start location
     110    Vector4 shift_4d(volume[volume_index]->location.x, volume[volume_index]->location.y, volume[volume_index]->location.z, 0);
     111
     112    double x0 = 0;
     113    double y0 = 0;
     114    double z0 = 0;
     115
     116    Mat4x4 model_view_no_trans, model_view_trans;
     117    Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse;
     118
     119    double zNear, zFar;
     120
     121    //initialize volume plane with world coordinates
     122    Plane volume_planes[6];
     123    volume_planes[0].set_coeffs(1, 0, 0, -x0);
     124    volume_planes[1].set_coeffs(-1, 0, 0, x0+1);
     125    volume_planes[2].set_coeffs(0, 1, 0, -y0);
     126    volume_planes[3].set_coeffs(0, -1, 0, y0+1);
     127    volume_planes[4].set_coeffs(0, 0, 1, -z0);
     128    volume_planes[5].set_coeffs(0, 0, -1, z0+1);
     129 
     130    glPushMatrix();
     131
     132    glScalef(volume[volume_index]->aspect_ratio_width,
     133          volume[volume_index]->aspect_ratio_height,
     134          volume[volume_index]->aspect_ratio_depth);
     135
     136    glEnable(GL_DEPTH_TEST);
     137
     138    GLfloat mv_no_trans[16];
     139    glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans);
     140
     141    model_view_no_trans = Mat4x4(mv_no_trans);
     142    model_view_no_trans_inverse = model_view_no_trans.inverse();
     143
     144    glPopMatrix();
     145
     146    //get modelview matrix with translation
     147    glPushMatrix();
     148    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     149    glScalef(volume[volume_index]->aspect_ratio_width,
     150          volume[volume_index]->aspect_ratio_height,
     151          volume[volume_index]->aspect_ratio_depth);
     152    GLfloat mv_trans[16];
     153    glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans);
     154
     155    model_view_trans = Mat4x4(mv_trans);
     156    model_view_trans_inverse = model_view_trans.inverse();
     157
     158    //draw volume bounding box
     159    draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1, 0.8, 0.1, 0.1, 1.5);
     160    glPopMatrix();
     161
     162    //transform volume_planes to eye coordinates.
     163    for(int i=0; i<6; i++)
     164      volume_planes[i].transform(model_view_no_trans);
     165
     166    get_near_far_z(mv_no_trans, zNear, zFar);
     167    //fprintf(stderr, "zNear:%f, zFar:%f\n", zNear, zFar);
     168    //fflush(stderr);
     169
     170    //compute actual rendering slices
     171    float z_step = fabs(zNear-zFar)/n_slices;           
     172    int n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1);
     173    actual_slices[cur_index] = n_actual_slices;
     174    polys[cur_index] = new ConvexPolygon*[n_actual_slices];
     175
     176    //fprintf(stderr, "slices: %d\n", n_actual_slices);
     177    //fflush(stderr);
     178
     179    float slice_z;
     180
     181    Vector4 vert1 = (Vector4(-10, -10, -0.5, 1));
     182    Vector4 vert2 = (Vector4(-10, +10, -0.5, 1));
     183    Vector4 vert3 = (Vector4(+10, +10, -0.5, 1));
     184    Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
     185
     186    int counter = 0;
     187    //transform slices and store them
     188    for (int i=0; i<n_actual_slices; i++){
     189      slice_z = zFar + i * z_step;      //back to front
     190
     191      ConvexPolygon *poly = new ConvexPolygon();
     192      polys[cur_index][counter] = poly;
     193      counter++;
     194
     195      poly->vertices.clear();
     196      poly->set_id(volume_index);
     197
     198      //Setting Z-coordinate
     199      vert1.z = slice_z;
     200      vert2.z = slice_z;
     201      vert3.z = slice_z;
     202      vert4.z = slice_z;
     203               
     204      poly->append_vertex(vert1);
     205      poly->append_vertex(vert2);
     206      poly->append_vertex(vert3);
     207      poly->append_vertex(vert4);
     208       
     209      for(int k=0; k<6; k++){
     210        poly->clip(volume_planes[k], true);
     211      }
     212
     213      poly->transform(model_view_no_trans_inverse);
     214      poly->transform(model_view_trans);
     215
     216      if(poly->vertices.size()>=3)
     217        total_rendered_slices++;
     218    }
     219  } //iterate all volumes
     220
     221
     222  //We sort all the polygons according to their eye-space depth, from farthest to the closest.
     223  //This step is critical for correct blending
     224
     225  SortElement* slices = (SortElement*) malloc(sizeof(SortElement)*total_rendered_slices);
     226
     227  int counter = 0;
     228  for(int i=0; i<total_enabled_volumes; i++){
     229    for(int j=0; j<actual_slices[i]; j++){
     230      if(polys[i][j]->vertices.size() >= 3){
     231        slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j);
     232        counter++;
     233      }
     234    }
     235  }
     236
     237  //sort them
     238  qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort);
     239
     240  //debug
     241  for(int i=0; i<total_rendered_slices; i++){
     242    fprintf(stderr, "%f ", slices[i].z);
     243  }
     244  fprintf(stderr, "\n\n");
     245
     246
     247  //Now we are ready to render all the slices from back to front
     248
     249  glEnable(GL_DEPTH_TEST);
     250  glEnable(GL_BLEND);
     251
     252  for(int i=0; i<total_rendered_slices; i++){
     253    int volume_index = slices[i].volume_id;
     254    int slice_index = slices[i].slice_id;
     255    ConvexPolygon* cur = polys[volume_index][slice_index];
     256
     257    glPushMatrix();
     258    glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
     259   
     260    activate_one_volume_shader(volume_index, 1, 1);
     261    glPopMatrix();
     262
     263    glEnable(GL_DEPTH_TEST);
     264    glBegin(GL_POLYGON);
     265      cur->Emit(true);
     266    glEnd();
     267
     268    deactivate_one_volume_shader();
     269  }
     270
     271  glDisable(GL_DEPTH_TEST);
     272  glDisable(GL_BLEND);
     273
     274  //Deallocate all the memory used
     275  for(int i=0; i<total_enabled_volumes; i++){
     276    for(int j=0; j<actual_slices[i]; j++){
     277      delete polys[i][j];
     278    }
     279    delete[] polys[i];
     280  }
     281  delete[] polys;
     282  delete[] actual_slices;
     283  free(slices);
     284}
     285
    64286
    65287void VolumeRenderer::render(int volume_index){
     
    73295  double z0 = 0;
    74296
    75   Mat4x4 model_view;
    76   Mat4x4 model_view_inverse;
     297  Mat4x4 model_view_no_trans, model_view_trans;
     298  Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse;
    77299
    78300  double zNear, zFar;
     
    80302  //initialize volume plane with world coordinates
    81303  Plane volume_planes[6];
    82 
    83304  volume_planes[0].set_coeffs(1, 0, 0, -x0);
    84305  volume_planes[1].set_coeffs(-1, 0, 0, x0+1);
     
    87308  volume_planes[4].set_coeffs(0, 0, 1, -z0);
    88309  volume_planes[5].set_coeffs(0, 0, -1, z0+1);
    89 
     310 
    90311  glPushMatrix();
    91312
     
    96317  glEnable(GL_DEPTH_TEST);
    97318
     319  GLfloat mv_no_trans[16];
     320  glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans);
     321
     322  model_view_no_trans = Mat4x4(mv_no_trans);
     323  model_view_no_trans_inverse = model_view_no_trans.inverse();
     324
     325  glPopMatrix();
     326
     327  //get modelview matrix with translation
     328  glPushMatrix();
     329  glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     330  glScalef(volume[volume_index]->aspect_ratio_width,
     331          volume[volume_index]->aspect_ratio_height,
     332          volume[volume_index]->aspect_ratio_depth);
     333  GLfloat mv_trans[16];
     334  glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans);
     335
     336  model_view_trans = Mat4x4(mv_trans);
     337  model_view_trans_inverse = model_view_trans.inverse();
     338
    98339  //draw volume bounding box
    99   draw_bounding_box(x0+shift_4d.x, y0+shift_4d.y, z0+shift_4d.z, x0+shift_4d.x+1, y0+shift_4d.y+1, z0+shift_4d.z+1, 0.8, 0.1, 0.1, 1.5);
    100 
    101   GLfloat mv[16];
    102   glGetFloatv(GL_MODELVIEW_MATRIX, mv);
    103 
    104   model_view = Mat4x4(mv);
    105   model_view_inverse = model_view.inverse();
    106 
     340  draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1, 0.8, 0.1, 0.1, 1.5);
    107341  glPopMatrix();
    108342
    109343  //transform volume_planes to eye coordinates.
    110344  for(int i=0; i<6; i++)
    111     volume_planes[i].transform(model_view);
    112 
    113   get_near_far_z(mv, zNear, zFar);
     345    volume_planes[i].transform(model_view_no_trans);
     346
     347  get_near_far_z(mv_no_trans, zNear, zFar);
    114348  //fprintf(stderr, "zNear:%f, zFar:%f\n", zNear, zFar);
    115349  //fflush(stderr);
     
    129363  Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
    130364
    131   glEnable(GL_DEPTH_TEST);
    132365  glEnable(GL_BLEND);
     366
     367#if 0
     368  if(slice_mode){
     369    glEnable(GL_DEPTH_TEST);
    133370
    134371  //render the cut planes
     
    171408
    172409    for(int k=0; k<6; k++){
    173       poly->clip(volume_planes[k]);
    174     }
    175 
    176     poly->copy_vertices_to_texcoords();
     410      poly->clip(volume_planes[k], true);
     411    }
     412
    177413    poly->transform(model_view_inverse);
    178414    poly->translate(shift_4d);
     
    182418    glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    183419
    184     activate_one_volume_shader(volume_index, 1, 1);
     420    //activate_one_volume_shader(volume_index, 1, 1);
    185421    glPopMatrix();
    186422
    187     glBegin(GL_POLYGON);
    188       poly->Emit(true);
     423    glDisable(GL_TEXTURE_3D);
     424    glDisable(GL_TEXTURE_2D);
     425    glDisable(GL_BLEND);
     426    glColor3f(1,1,1);
     427
     428    //glBegin(GL_POLYGON);
     429    glBegin(GL_LINE_LOOP);
     430      //poly->Emit(true);
     431      poly->Emit(false);
    189432    glEnd();
    190433
    191     deactivate_one_volume_shader();
    192   }
    193 
    194 #if 1
     434    //deactivate_one_volume_shader();
     435  }
     436  } //slice_mode
     437
     438#endif
     439
     440  if(volume_mode){
     441  glEnable(GL_DEPTH_TEST);
     442
    195443  for (int i=0; i<n_actual_slices; i++){
    196444    slice_z = zFar + i * z_step;        //back to front
     
    212460       
    213461    for(int k=0; k<6; k++){
    214       poly->clip(volume_planes[k]);
    215     }
    216 
    217     poly->copy_vertices_to_texcoords();
     462      poly->clip(volume_planes[k], true);
     463    }
     464
    218465    //move the volume to the proper location
    219 
    220     poly->transform(model_view_inverse);
    221     poly->translate(shift_4d);
    222     poly->transform(model_view);
     466    //poly->transform(model_view_inverse);
     467    //poly->translate(shift_4d);
     468    //poly->transform(model_view);
     469
     470    poly->transform(model_view_no_trans_inverse);
     471    poly->transform(model_view_trans);
    223472
    224473    glPushMatrix();
    225474    glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    226475   
    227     //glPushMatrix();
    228     //glMatrixMode(GL_MODELVIEW);
    229     //glLoadIdentity();
    230     //glScaled(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    231 
    232     /*
     476   /*
    233477    //draw slice lines only
    234478    glDisable(GL_BLEND);
     
    252496               
    253497  }
    254 #endif
     498  } //volume_mode
    255499
    256500  glDisable(GL_BLEND);
     
    378622void VolumeRenderer::set_diffuse(float val){ live_diffuse = val; }
    379623
    380 
     624void VolumeRenderer::set_slice_mode(bool val) { slice_mode = val; }
     625void VolumeRenderer::set_volume_mode(bool val) { volume_mode = val; }
     626void VolumeRenderer::switch_slice_mode() { slice_mode = (!slice_mode); }
     627void VolumeRenderer::switch_volume_mode() { volume_mode = (!volume_mode); }
     628
  • trunk/gui/vizservers/nanovis/VolumeRenderer.h

    r407 r415  
    5050  float live_specular;
    5151
     52  bool slice_mode;      //enable cut planes
     53  bool volume_mode;     //enable full volume rendering
     54
    5255  //cg related
    5356  CGcontext g_context;          //the Nvidia cg context
     
    7881  ~VolumeRenderer();
    7982
    80   void add_volume(Volume* _vol, TransferFunction* _tf, int _slice);
     83  int add_volume(Volume* _vol, TransferFunction* _tf, int _slice);
    8184                                                //add a volume and its transfer function
    8285                                                //we require a transfer function when a
    8386                                                //volume is added.
    8487  void render(int volume_index);
     88  void render_all();    //render all enabled volumes;
    8589  void set_specular(float val);
    8690  void set_diffuse(float val);
     91  void set_slice_mode(bool val); //control independently.
     92  void set_volume_mode(bool val);
     93  void switch_slice_mode(); //switch_cutplane_mode
     94  void switch_volume_mode();
    8795};
    8896
  • trunk/gui/vizservers/nanovis/nanovis.cpp

    r414 r415  
    862862   load_vector_file(0, "./data/J-wire-vec.dx");
    863863   load_volume_file(1, "./data/mu-wire-3d.dx");
     864   load_volume_file(2, "./data/mu-wire-3d.dx");
     865   load_volume_file(3, "./data/mu-wire-3d.dx");
    864866
    865867   init_tf();   //initialize transfer function
     
    870872   //create volume renderer
    871873   vol_render = new VolumeRenderer(cam, volume[1], tf[0], g_context);
     874   vol_render->add_volume(volume[2], tf[0], 256);
     875   volume[2]->location =Vector3(0.42, 0.1, 0.1);
     876   vol_render->add_volume(volume[3], tf[0], 256);
     877   volume[3]->location =Vector3(0.2, -0.1, -0.1);
     878
    872879   
    873880   psys = new ParticleSystem(NMESH, NMESH, g_context, volume[0]->id,
     
    14701477
    14711478   perf->enable();
    1472      //render volume :0
    1473      //volume[0]->location =Vector3(0.,0.,0.);
    1474      //render_volume(0, 256);
    1475 
    1476      //render volume :1
    1477      volume[1]->location =Vector3(0., 0., 0.);
    1478      //render_volume(1, 256);
    1479      vol_render->render(0);
     1479     //vol_render->render(0);
     1480     vol_render->render_all();
    14801481
    14811482     //fprintf(stderr, "%lf\n", get_time_interval());
     
    16391640                vol_render->set_diffuse(live_diffuse);
    16401641                break;
     1642        case 'v':
     1643                vol_render->switch_volume_mode();
     1644                break;
     1645        case 'b':
     1646                vol_render->switch_slice_mode();
     1647                break;
    16411648
    16421649        default:
  • trunk/gui/vizservers/nanovis/shaders/one_volume.cg

    r407 r415  
    1818
    1919
    20 #define PHONG_SHADING
     20//#define PHONG_SHADING
    2121
    2222PixelOut main(v2f IN, /* uniform sampler1D tf,*/
     
    4949      //sample the transfer function texture
    5050      float4 color = tex1D(tf, sample.x);
    51       color.w = renderParameters.y*color.w/renderParameters.x;
     51      //color.w = renderParameters.y*color.w/renderParameters.x;
     52      //color.w = 0.5;
    5253
    5354      //float4 color = float4(sample.x, 0, 0, 1);
Note: See TracChangeset for help on using the changeset viewer.