Index: trunk/gui/vizservers/nanovis/VolumeRenderer.cpp
===================================================================
--- trunk/gui/vizservers/nanovis/VolumeRenderer.cpp	(revision 415)
+++ trunk/gui/vizservers/nanovis/VolumeRenderer.cpp	(revision 416)
@@ -62,5 +62,4 @@
   tf.push_back(_tf);
   slice.push_back(_slice);
-  render_bit.push_back(true);
 
   n_volumes++;
@@ -89,18 +88,21 @@
   int total_rendered_slices = 0;
 
+  //compute total enabled volumes
   for(int i=0; i<n_volumes; i++){
-    if(render_bit[i])
+    if(volume[i]->is_enabled())
       total_enabled_volumes ++;
   }
   fprintf(stderr, "total volumes rendered: %d\n", total_enabled_volumes);
 
-  ConvexPolygon*** polys = new ConvexPolygon**[total_enabled_volumes];
-  int* actual_slices = new int[total_enabled_volumes];
-
-  int cur_index = -1;
+  ConvexPolygon*** polys = new ConvexPolygon**[total_enabled_volumes];	//two dimension pointer array
+  									//storing the slices
+  int* actual_slices = new int[total_enabled_volumes]; //number of actual slices for each volume
+
+  int cur_vol = -1;
   for(int i=0; i<n_volumes; i++){
-    if(!render_bit[i])
-      continue;
-    cur_index++;
+    if(!volume[i]->is_enabled())
+      continue; //skip this volume
+
+    cur_vol++;
 
     int volume_index = i;
@@ -108,5 +110,6 @@
 
     //volume start location
-    Vector4 shift_4d(volume[volume_index]->location.x, volume[volume_index]->location.y, volume[volume_index]->location.z, 0);
+    Vector3* location = volume[volume_index]->get_location();
+    Vector4 shift_4d(location->x, location->y, location->z, 0);
 
     double x0 = 0;
@@ -128,6 +131,6 @@
     volume_planes[5].set_coeffs(0, 0, -1, z0+1);
   
+    //get modelview matrix with no translation
     glPushMatrix();
-
     glScalef(volume[volume_index]->aspect_ratio_width, 
 	  volume[volume_index]->aspect_ratio_height, 
@@ -156,5 +159,5 @@
     model_view_trans_inverse = model_view_trans.inverse();
 
-    //draw volume bounding box
+    //draw volume bounding box with translation (the correct location in space)
     draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1, 0.8, 0.1, 0.1, 1.5);
     glPopMatrix();
@@ -163,17 +166,11 @@
     for(int i=0; i<6; i++)
       volume_planes[i].transform(model_view_no_trans);
-
     get_near_far_z(mv_no_trans, zNear, zFar);
-    //fprintf(stderr, "zNear:%f, zFar:%f\n", zNear, zFar);
-    //fflush(stderr);
 
     //compute actual rendering slices
     float z_step = fabs(zNear-zFar)/n_slices;		
     int n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1);
-    actual_slices[cur_index] = n_actual_slices;
-    polys[cur_index] = new ConvexPolygon*[n_actual_slices];
-
-    //fprintf(stderr, "slices: %d\n", n_actual_slices);
-    //fflush(stderr);
+    actual_slices[cur_vol] = n_actual_slices;
+    polys[cur_vol] = new ConvexPolygon*[n_actual_slices];
 
     float slice_z;
@@ -184,4 +181,75 @@
     Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
 
+    //Render cutplanes first with depth test enabled.
+    //They will mark the image with their depth values. Then we render other volume slices.
+    //These volume slices will be occluded correctly by the cutplanes and vice versa.
+    glEnable(GL_DEPTH_TEST);
+    glDisable(GL_BLEND);
+
+    for(int i=0; i<volume[volume_index]->get_cutplane_count(); i++){
+      if(!volume[volume_index]->cutplane_is_enabled(i))
+        continue;
+
+      float offset = volume[volume_index]->get_cutplane(i)->offset;
+      int axis = volume[volume_index]->get_cutplane(i)->orient;
+
+      if(axis==1){
+        vert1 = Vector4(-10, -10, offset, 1);
+        vert2 = Vector4(-10, +10, offset, 1);
+        vert3 = Vector4(+10, +10, offset, 1);
+        vert4 = Vector4(+10, -10, offset, 1);
+      }
+      else if(axis==2){
+        vert1 = Vector4(offset, -10, -10, 1);
+        vert2 = Vector4(offset, +10, -10, 1);
+        vert3 = Vector4(offset, +10, +10, 1);
+        vert4 = Vector4(offset, -10, +10, 1);
+      }
+      else if(axis==3){
+        vert1 = Vector4(-10, offset, -10, 1);
+        vert2 = Vector4(+10, offset, -10, 1);
+        vert3 = Vector4(+10, offset, +10, 1);
+        vert4 = Vector4(-10, offset, +10, 1);
+      }
+
+      vert1 = model_view_no_trans.transform(vert1);
+      vert2 = model_view_no_trans.transform(vert2);
+      vert3 = model_view_no_trans.transform(vert3);
+      vert4 = model_view_no_trans.transform(vert4);
+
+      ConvexPolygon poly;
+      poly.vertices.clear();
+
+      poly.append_vertex(vert1);
+      poly.append_vertex(vert2);
+      poly.append_vertex(vert3);
+      poly.append_vertex(vert4);
+
+      for(int k=0; k<6; k++){
+        poly.clip(volume_planes[k], true);
+      }
+
+      //poly->transform(model_view_inverse);
+      //poly->translate(shift_4d);
+      //poly->transform(model_view);
+      poly.transform(model_view_no_trans_inverse);
+      poly.transform(model_view_trans);
+
+      glPushMatrix();
+      glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
+
+      activate_one_volume_shader(volume_index, 0, 1);
+      glPopMatrix();
+
+      glBegin(GL_POLYGON);
+        poly.Emit(true); 
+      glEnd();
+
+      deactivate_one_volume_shader();
+    } //done cutplanes
+
+   
+    //Now do volume rendering
+
     int counter = 0;
     //transform slices and store them
@@ -190,5 +258,5 @@
 
       ConvexPolygon *poly = new ConvexPolygon();
-      polys[cur_index][counter] = poly;
+      polys[cur_vol][counter] = poly;
       counter++;
 
@@ -238,4 +306,5 @@
   qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort);
 
+  /*
   //debug
   for(int i=0; i<total_rendered_slices; i++){
@@ -243,8 +312,7 @@
   }
   fprintf(stderr, "\n\n");
-
+  */
 
   //Now we are ready to render all the slices from back to front
-
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_BLEND);
@@ -261,5 +329,4 @@
     glPopMatrix();
 
-    glEnable(GL_DEPTH_TEST);
     glBegin(GL_POLYGON);
       cur->Emit(true); 
@@ -365,12 +432,11 @@
   glEnable(GL_BLEND);
 
-#if 0
   if(slice_mode){
     glEnable(GL_DEPTH_TEST);
 
   //render the cut planes
-  for(int i=0; i<volume[volume_index]->plane.size(); i++){
-    float offset = volume[volume_index]->plane[i].offset;
-    int axis = volume[volume_index]->plane[i].orient;
+  for(int i=0; i<volume[volume_index]->get_cutplane_count(); i++){
+    float offset = volume[volume_index]->get_cutplane(i)->offset;
+    int axis = volume[volume_index]->get_cutplane(i)->orient;
 
     if(axis==1){
@@ -393,8 +459,8 @@
     }
 
-    vert1 = model_view.transform(vert1);
-    vert2 = model_view.transform(vert2);
-    vert3 = model_view.transform(vert3);
-    vert4 = model_view.transform(vert4);
+    vert1 = model_view_no_trans.transform(vert1);
+    vert2 = model_view_no_trans.transform(vert2);
+    vert3 = model_view_no_trans.transform(vert3);
+    vert4 = model_view_no_trans.transform(vert4);
 
     ConvexPolygon *poly;
@@ -411,30 +477,24 @@
     }
 
-    poly->transform(model_view_inverse);
-    poly->translate(shift_4d);
-    poly->transform(model_view);
+    //poly->transform(model_view_inverse);
+    //poly->translate(shift_4d);
+    //poly->transform(model_view);
+    poly->transform(model_view_no_trans_inverse);
+    poly->transform(model_view_trans);
 
     glPushMatrix();
     glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
 
-    //activate_one_volume_shader(volume_index, 1, 1);
+    activate_one_volume_shader(volume_index, 1, 1);
     glPopMatrix();
 
-    glDisable(GL_TEXTURE_3D);
-    glDisable(GL_TEXTURE_2D);
-    glDisable(GL_BLEND);
-    glColor3f(1,1,1);
-
-    //glBegin(GL_POLYGON);
-    glBegin(GL_LINE_LOOP);
-      //poly->Emit(true); 
-      poly->Emit(false); 
+    glBegin(GL_POLYGON);
+      poly->Emit(true); 
     glEnd();
 
-    //deactivate_one_volume_shader();
+    deactivate_one_volume_shader();
   }
   } //slice_mode
 
-#endif
 
   if(volume_mode){ 
@@ -500,5 +560,4 @@
   glDisable(GL_BLEND);
   glDisable(GL_DEPTH_TEST);
-  //glPopMatrix();
 }
 
