Ignore:
Timestamp:
Feb 13, 2008 7:38:58 PM (16 years ago)
Author:
vrinside
Message:

Adding Volume animation

Location:
trunk/vizservers/nanovis
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/vizservers/nanovis/Command.cpp

    r881 r884  
    11281128            }
    11291129        }
    1130     } else if ((c == 't') && (strcmp(argv[1],"test2") == 0)) {
     1130    }
     1131    else if (strcmp(argv[1],"animation") == 0) {
     1132       
     1133        if (strcmp(argv[2],"volumes") == 0) {
     1134            vector<unsigned int> ivol;
     1135            if (GetVolumeIndices(interp, argc-4, argv+4, &ivol) != TCL_OK) {
     1136                return TCL_ERROR;
     1137            }
     1138
     1139            NanoVis::vol_renderer->clearAnimatedVolumeInfo();
     1140
     1141            vector<unsigned int>::iterator iter;
     1142            for (iter = ivol.begin(); iter != ivol.end(); iter++) {
     1143                NanoVis::vol_renderer->addAnimatedVolume(NanoVis::volume[*iter], *iter);
     1144            }
     1145        }
     1146        else if (strcmp(argv[2],"start") == 0) {
     1147                NanoVis::vol_renderer->startVolumeAnimation();
     1148        }
     1149        else if (strcmp(argv[2],"stop") == 0) {
     1150                NanoVis::vol_renderer->stopVolumeAnimation();
     1151        }
     1152        else if (strcmp(argv[2],"clear") == 0) {
     1153                NanoVis::vol_renderer->clearAnimatedVolumeInfo();
     1154        }
     1155
     1156    }
     1157    else if ((c == 't') && (strcmp(argv[1],"test2") == 0)) {
    11311158        NanoVis::volume[1]->disable_data();
    11321159        NanoVis::volume[1]->disable();
  • trunk/vizservers/nanovis/Makefile.in

    r881 r884  
    5252        ConvexPolygon.o \
    5353        Event.o \
     54        GradientFilter.o \
    5455        Grid.o \
    5556        HeightMap.o \
     
    105106        Vector4.o \
    106107        Volume.o \
     108        VolumeInterpolator.o \
    107109        VolumeRenderer.o \
    108110        ZincBlendeVolume.o \
     
    303305
    304306RenderContext.o: RenderContext.cpp RenderContext.h
     307VolumeInterpolator.o: VolumeInterpolator.cpp VolumeInterpolator.h
     308GradientFilter.o: GradientFilter.cpp GradientFilter.h
    305309
    306310Util.o: Util.cpp Util.h
  • trunk/vizservers/nanovis/Texture3D.cpp

    r418 r884  
    5252}
    5353
    54 GLuint Texture3D::initialize(float *data)
     54void Texture3D::update(float* data)
    5555{
    5656        //load texture with 16 bit half floating point precision if card is 6 series NV40
     
    5959        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    6060
    61         glGenTextures(1, &id);
    6261        glBindTexture(GL_TEXTURE_3D, id);
    6362        assert(id!=-1);
     
    115114       
    116115        gl_resource_allocated = true;
     116
     117}
     118
     119GLuint Texture3D::initialize(float *data)
     120{
     121        if (id != 0) glDeleteTextures(1, &id);
     122
     123        //load texture with 16 bit half floating point precision if card is 6 series NV40
     124        //half float with linear interpolation is only supported by 6 series and up cards
     125        //If NV40 not defined, data is quantized to 8-bit from 32-bit.
     126        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     127
     128        glGenTextures(1, &id);
     129        glBindTexture(GL_TEXTURE_3D, id);
     130        assert(id!=-1);
     131
     132        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     133        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     134        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
     135
     136        if(interp_type==GL_LINEAR){
     137                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     138                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     139        }
     140        else{
     141                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     142                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     143        }
     144
     145        //to do: add handling to more formats
     146        if(type==GL_FLOAT){
     147          switch(n_components){
     148            #ifdef NV40
     149                case 1:
     150                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
     151                        break;
     152                case 2:
     153                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
     154                        break;
     155                case 3:
     156                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
     157                        break;
     158                case 4:
     159                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
     160                        break;
     161            #else
     162                case 1:
     163                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
     164                        break;
     165                case 2:
     166                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
     167                        break;
     168                case 3:
     169                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
     170                        break;
     171                case 4:
     172                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
     173                        break;
     174            #endif
     175                default:
     176                        break;
     177          }
     178        }
     179
     180
     181        assert(glGetError()==0);
     182       
     183        gl_resource_allocated = true;
    117184        return id;
    118185}
  • trunk/vizservers/nanovis/Texture3D.h

    r273 r884  
    4949        static void check_max_unit();
    5050
     51    void update(float* data);
     52
    5153};
    5254
  • trunk/vizservers/nanovis/Volume.cpp

    r870 r884  
    1616#include <assert.h>
    1717#include "Volume.h"
     18#include "Trace.h"
    1819
    1920
     
    4142    iso_surface(0)
    4243{
     44    Trace("Volume constructor\n");
    4345
    4446    tex = new Texture3D(w, h, d, NVIS_FLOAT, NVIS_LINEAR_INTERP, n);
    45     tex->initialize(data);
     47
     48    _data = new float[width * height * depth * n_components];
     49    if (data)
     50    {
     51        Trace("data is copied\n");
     52        memcpy(_data, data, sizeof(width *height* depth * n_components *sizeof(float)));
     53        tex->initialize(data);
     54    }
     55    else
     56    {
     57        Trace("data is null\n");
     58        memset(_data, 0x00, sizeof(width *height* depth * n_components *sizeof(float)));
     59        tex->initialize(_data);
     60    }
     61
    4662    id = tex->id;
    4763   
     
    6379    label[1] = "Y Label";
    6480    label[2] = "Z Label";
     81
     82    Trace("End -- Volume constructor\n");
    6583}
    66 
    6784
    6885Volume::~Volume()
     
    7289        // TBD...
    7390    }
     91
     92    delete [] _data;
     93
    7494    delete tex;
    7595}
  • trunk/vizservers/nanovis/Volume.h

    r870 r884  
    6464
    6565    Texture3D* tex;             // OpenGL texture storing the volume
     66
     67    float* _data;
    6668   
    6769    int pointsetIndex;
     
    100102           float size, int n_component, float* data, double vmin, double vmax,
    101103           double nonzero_min);
     104
     105 
    102106    ~Volume();
    103107       
  • trunk/vizservers/nanovis/VolumeRenderer.cpp

    r871 r884  
    1818#include "VolumeRenderer.h"
    1919#include "NvStdVertexShader.h"
     20#include <time.h>
     21#include <sys/time.h>
     22#include "Trace.h"
    2023
    2124
     
    3235    R2string path = R2FilePath::getInstance()->getPath("Font.bmp");
    3336    init_font((const char*) path);
     37
     38    _volumeInterpolator = new VolumeInterpolator();
    3439}
    3540
     
    3944    delete _regularVolumeShader;
    4045    delete _stdVertexShader;
     46    delete _volumeInterpolator;
    4147}
    4248
     
    141147}
    142148
     149#if 0
     150void VolumeRenderer::render_all()
     151{
     152    Volume* vol = 0;
     153    Volume* ani_vol = 0;
     154    TransferFunction* cur_tf = 0;
     155    TransferFunction* ani_tf = 0;
     156
     157  int total_rendered_slices = 0;
     158  int num_volumes = n_volumes;
     159
     160// TEST
     161/*
     162  if (_volumeInterpolator->is_started())
     163  {
     164    Trace("get ani... TEST\n");
     165    ++ num_volumes;
     166    ani_tf = tf[_volumeInterpolator->getReferenceVolumeID()];
     167    ani_vol = _volumeInterpolator->getVolume();
     168  }
     169  */
     170
     171  ConvexPolygon*** polys = new ConvexPolygon**[num_volumes];    //two dimension pointer array
     172                                                                        //storing the slices
     173  int* actual_slices = new int[num_volumes]; //number of actual slices for each volume
     174
     175  for(int i=0; i < num_volumes; i++) {
     176    int volume_index = i;
     177
     178    if (volume_index != n_volumes)
     179    {
     180        vol = volume[volume_index];
     181        cur_tf = tf[volume_index];
     182    }
     183    else
     184    {
     185        vol = ani_vol;
     186        cur_tf = ani_tf;
     187    }
     188
     189    polys[volume_index] = NULL;
     190    actual_slices[volume_index] = 0;
     191
     192    if(!vol->is_enabled())
     193      continue; //skip this volume
     194
     195    // TEST
     196    //if (i != n_volumes) continue;
     197
     198    int n_slices = vol->get_n_slice();
     199
     200    Trace("SLICES : %d\n", n_slices);
     201
     202    if (vol->get_isosurface())
     203    {
     204                // double the number of slices
     205        n_slices <<= 1;
     206        }
     207
     208    //volume start location
     209    Vector3* location = vol->get_location();
     210    Vector4 shift_4d(location->x, location->y, location->z, 0);
     211
     212    double x0 = 0;
     213    double y0 = 0;
     214    double z0 = 0;
     215
     216    Mat4x4 model_view_no_trans, model_view_trans;
     217    Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse;
     218
     219    double zNear, zFar;
     220
     221    //initialize volume plane with world coordinates
     222    Plane volume_planes[6];
     223    volume_planes[0].set_coeffs(1, 0, 0, -x0);
     224    volume_planes[1].set_coeffs(-1, 0, 0, x0+1);
     225    volume_planes[2].set_coeffs(0, 1, 0, -y0);
     226    volume_planes[3].set_coeffs(0, -1, 0, y0+1);
     227    volume_planes[4].set_coeffs(0, 0, 1, -z0);
     228    volume_planes[5].set_coeffs(0, 0, -1, z0+1);
     229 
     230    //get modelview matrix with no translation
     231    glPushMatrix();
     232    glScalef(vol->aspect_ratio_width,
     233          vol->aspect_ratio_height,
     234          vol->aspect_ratio_depth);
     235
     236    glEnable(GL_DEPTH_TEST);
     237
     238    GLfloat mv_no_trans[16];
     239    glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans);
     240
     241    model_view_no_trans = Mat4x4(mv_no_trans);
     242    model_view_no_trans_inverse = model_view_no_trans.inverse();
     243
     244    glPopMatrix();
     245
     246    //get modelview matrix with translation
     247    glPushMatrix();
     248    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     249    glScalef(vol->aspect_ratio_width,
     250          vol->aspect_ratio_height,
     251          vol->aspect_ratio_depth);
     252    GLfloat mv_trans[16];
     253    glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans);
     254
     255    model_view_trans = Mat4x4(mv_trans);
     256    model_view_trans_inverse = model_view_trans.inverse();
     257
     258    //draw volume bounding box with translation (the correct location in space)
     259    if (vol->outline_is_enabled()) {
     260        float olcolor[3];
     261        vol->get_outline_color(olcolor);
     262        draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1,
     263            (double)olcolor[0], (double)olcolor[1], (double)olcolor[2],
     264            1.5);
     265    }
     266    glPopMatrix();
     267
     268    //draw labels
     269    glPushMatrix();
     270    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     271    if(vol->outline_is_enabled())
     272    {
     273       draw_label(vol);
     274    }
     275    glPopMatrix();
     276
     277    //transform volume_planes to eye coordinates.
     278    for(int j=0; j<6; j++)
     279      volume_planes[i].transform(model_view_no_trans);
     280
     281    get_near_far_z(mv_no_trans, zNear, zFar);
     282
     283    //compute actual rendering slices
     284    float z_step = fabs(zNear-zFar)/n_slices;           
     285    int n_actual_slices;
     286
     287    if (vol->data_is_enabled())
     288    {
     289        n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1);
     290        polys[volume_index] = new ConvexPolygon*[n_actual_slices];
     291    } else {
     292        n_actual_slices = 0;
     293        polys[volume_index] = NULL;
     294    }
     295    actual_slices[volume_index] = n_actual_slices;
     296
     297    Vector4 vert1 = (Vector4(-10, -10, -0.5, 1));
     298    Vector4 vert2 = (Vector4(-10, +10, -0.5, 1));
     299    Vector4 vert3 = (Vector4(+10, +10, -0.5, 1));
     300    Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
     301
     302   
     303    //Render cutplanes first with depth test enabled.
     304    //They will mark the image with their depth values. Then we render other volume slices.
     305    //These volume slices will be occluded correctly by the cutplanes and vice versa.
     306
     307    ConvexPolygon static_poly;
     308    for(int i=0; i<vol->get_cutplane_count(); i++)
     309    {
     310      if(!vol->cutplane_is_enabled(i))
     311        continue;
     312
     313      float offset = vol->get_cutplane(i)->offset;
     314      int axis = vol->get_cutplane(i)->orient;
     315
     316      if(axis==3){
     317        vert1 = Vector4(-10, -10, offset, 1);
     318        vert2 = Vector4(-10, +10, offset, 1);
     319        vert3 = Vector4(+10, +10, offset, 1);
     320        vert4 = Vector4(+10, -10, offset, 1);
     321        //continue;
     322      }
     323      else if(axis==1){
     324        vert1 = Vector4(offset, -10, -10, 1);
     325        vert2 = Vector4(offset, +10, -10, 1);
     326        vert3 = Vector4(offset, +10, +10, 1);
     327        vert4 = Vector4(offset, -10, +10, 1);
     328        //continue;
     329      }
     330      else if(axis==2){
     331        vert1 = Vector4(-10, offset, -10, 1);
     332        vert2 = Vector4(+10, offset, -10, 1);
     333        vert3 = Vector4(+10, offset, +10, 1);
     334        vert4 = Vector4(-10, offset, +10, 1);
     335        //continue;
     336      }
     337
     338      vert1 = model_view_no_trans.transform(vert1);
     339      vert2 = model_view_no_trans.transform(vert2);
     340      vert3 = model_view_no_trans.transform(vert3);
     341      vert4 = model_view_no_trans.transform(vert4);
     342
     343      ConvexPolygon* p = &static_poly;
     344      p->vertices.clear();
     345
     346      p->append_vertex(vert1);
     347      p->append_vertex(vert2);
     348      p->append_vertex(vert3);
     349      p->append_vertex(vert4);
     350
     351      for(int k=0; k<6; k++){
     352        p->clip(volume_planes[k], true);
     353      }
     354
     355      p->transform(model_view_no_trans_inverse);
     356      p->transform(model_view_trans);
     357
     358      glPushMatrix();
     359      glScalef(vol->aspect_ratio_width, vol->aspect_ratio_height, vol->aspect_ratio_depth);
     360
     361      activate_volume_shader(vol, cur_tf, true);
     362      glPopMatrix();
     363
     364      glEnable(GL_DEPTH_TEST);
     365      glDisable(GL_BLEND);
     366
     367      glBegin(GL_POLYGON);
     368        p->Emit(true);
     369      glEnd();
     370      glDisable(GL_DEPTH_TEST);
     371
     372      deactivate_volume_shader();
     373    } //done cutplanes
     374
     375   
     376    //Now do volume rendering
     377
     378    vert1 = (Vector4(-10, -10, -0.5, 1));
     379    vert2 = (Vector4(-10, +10, -0.5, 1));
     380    vert3 = (Vector4(+10, +10, -0.5, 1));
     381    vert4 = (Vector4(+10, -10, -0.5, 1));
     382
     383    int counter = 0;
     384   
     385    //transform slices and store them
     386    float slice_z;
     387    for (int i=0; i<n_actual_slices; i++){
     388      slice_z = zFar + i * z_step;      //back to front
     389
     390      ConvexPolygon *poly = new ConvexPolygon();
     391      polys[volume_index][counter] = poly;
     392      counter++;
     393
     394      poly->vertices.clear();
     395      poly->set_id(volume_index);
     396
     397      //Setting Z-coordinate
     398      vert1.z = slice_z;
     399      vert2.z = slice_z;
     400      vert3.z = slice_z;
     401      vert4.z = slice_z;
     402               
     403      poly->append_vertex(vert1);
     404      poly->append_vertex(vert2);
     405      poly->append_vertex(vert3);
     406      poly->append_vertex(vert4);
     407       
     408      for(int k=0; k<6; k++){
     409        poly->clip(volume_planes[k], true);
     410      }
     411
     412      poly->transform(model_view_no_trans_inverse);
     413      poly->transform(model_view_trans);
     414
     415      if(poly->vertices.size()>=3)
     416        total_rendered_slices++;
     417    }
     418   
     419  } //iterate all volumes
     420  //fprintf(stderr, "total slices: %d\n", total_rendered_slices);
     421
     422  //We sort all the polygons according to their eye-space depth, from farthest to the closest.
     423  //This step is critical for correct blending
     424
     425  SortElement* slices = (SortElement*) malloc(sizeof(SortElement)*total_rendered_slices);
     426
     427  int counter = 0;
     428  for(int i=0; i<num_volumes; i++)
     429  {
     430    for(int j=0; j<actual_slices[i]; j++){
     431      if(polys[i][j]->vertices.size() >= 3){
     432        slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j);
     433        counter++;
     434      }
     435    }
     436  }
     437
     438  //sort them
     439  qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort);
     440
     441  /*
     442  //debug
     443  for(int i=0; i<total_rendered_slices; i++){
     444    fprintf(stderr, "%f ", slices[i].z);
     445  }
     446  fprintf(stderr, "\n\n");
     447  */
     448
     449  //Now we are ready to render all the slices from back to front
     450  glEnable(GL_DEPTH_TEST);
     451  glEnable(GL_BLEND);
     452
     453  for(int i=0; i<total_rendered_slices; i++)
     454  {
     455    int volume_index = slices[i].volume_id;
     456    int slice_index = slices[i].slice_id;
     457    ConvexPolygon* cur = polys[volume_index][slice_index];
     458    /*
     459    if (volume_index == n_volumes)
     460    {
     461        // TEST
     462        vol = ani_vol;
     463        //vol = volume[0];
     464        cur_tf = ani_tf;
     465    }
     466    else
     467    {
     468    */
     469        vol = volume[volume_index];
     470        cur_tf = tf[volume_index];
     471    //}
     472
     473    glPushMatrix();
     474    glScalef(vol->aspect_ratio_width, vol->aspect_ratio_height, vol->aspect_ratio_depth);
     475   
     476    activate_volume_shader(vol, cur_tf, false);
     477    glPopMatrix();
     478
     479    glBegin(GL_POLYGON);
     480      cur->Emit(true);
     481    glEnd();
     482
     483    deactivate_volume_shader();
     484  }
     485
     486  glDisable(GL_DEPTH_TEST);
     487  glDisable(GL_BLEND);
     488
     489  //Deallocate all the memory used
     490  for(int i=0; i<num_volumes; i++)
     491  {
     492    for(int j=0; j<actual_slices[i]; j++){
     493      delete polys[i][j];
     494    }
     495    if (polys[i]) {
     496      delete[] polys[i];
     497    }
     498  }
     499  delete[] polys;
     500  delete[] actual_slices;
     501  free(slices);
     502
     503}
     504#endif
     505void VolumeRenderer::render_all()
     506{
     507
     508    Volume* cur_vol = 0;
     509    Volume* ani_vol = 0;
     510    TransferFunction* cur_tf = 0;
     511    TransferFunction* ani_tf = 0;
     512    int total_rendered_slices = 0;
     513    int num_volumes = n_volumes;
     514/*
     515    if (_volumeInterpolator->is_started())
     516    {
     517        Trace("get ani... TEST\n");
     518        ++ num_volumes;
     519        ani_tf = tf[_volumeInterpolator->getReferenceVolumeID()];
     520        ani_vol = _volumeInterpolator->getVolume();
     521    }
     522    */
     523
     524  ConvexPolygon*** polys = new ConvexPolygon**[num_volumes];    //two dimension pointer array
     525  int* actual_slices = new int[num_volumes]; //number of actual slices for each volume
     526
     527  for(int i=0; i<n_volumes; i++){
     528    int vol_index= i;
     529    if (vol_index != n_volumes)
     530    {
     531        cur_vol = volume[vol_index];
     532        cur_tf = tf[vol_index];
     533    }
     534    else
     535    {
     536        cur_vol = ani_vol;
     537        cur_tf = ani_tf;
     538    }
     539   
     540    polys[i] = NULL;
     541    actual_slices[i] = 0;
     542
     543    if(!cur_vol->is_enabled())
     544      continue; //skip this volume
     545
     546    int n_slices = cur_vol->get_n_slice();
     547    if (cur_vol->get_isosurface())
     548    {
     549                // double the number of slices
     550        n_slices <<= 1;
     551        }
     552
     553    //volume start location
     554    Vector3* location = cur_vol->get_location();
     555    Vector4 shift_4d(location->x, location->y, location->z, 0);
     556
     557    double x0 = 0;
     558    double y0 = 0;
     559    double z0 = 0;
     560
     561    Mat4x4 model_view_no_trans, model_view_trans;
     562    Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse;
     563
     564    double zNear, zFar;
     565
     566    //initialize volume plane with world coordinates
     567    Plane volume_planes[6];
     568    volume_planes[0].set_coeffs(1, 0, 0, -x0);
     569    volume_planes[1].set_coeffs(-1, 0, 0, x0+1);
     570    volume_planes[2].set_coeffs(0, 1, 0, -y0);
     571    volume_planes[3].set_coeffs(0, -1, 0, y0+1);
     572    volume_planes[4].set_coeffs(0, 0, 1, -z0);
     573    volume_planes[5].set_coeffs(0, 0, -1, z0+1);
     574 
     575    //get modelview matrix with no translation
     576    glPushMatrix();
     577    glScalef(cur_vol->aspect_ratio_width,
     578          cur_vol->aspect_ratio_height,
     579          cur_vol->aspect_ratio_depth);
     580
     581    glEnable(GL_DEPTH_TEST);
     582
     583    GLfloat mv_no_trans[16];
     584    glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans);
     585
     586    model_view_no_trans = Mat4x4(mv_no_trans);
     587    model_view_no_trans_inverse = model_view_no_trans.inverse();
     588
     589    glPopMatrix();
     590
     591    //get modelview matrix with translation
     592    glPushMatrix();
     593    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     594    glScalef(cur_vol->aspect_ratio_width,
     595          cur_vol->aspect_ratio_height,
     596          cur_vol->aspect_ratio_depth);
     597    GLfloat mv_trans[16];
     598    glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans);
     599
     600    model_view_trans = Mat4x4(mv_trans);
     601    model_view_trans_inverse = model_view_trans.inverse();
     602
     603    //draw volume bounding box with translation (the correct location in space)
     604    if (cur_vol->outline_is_enabled()) {
     605        float olcolor[3];
     606        cur_vol->get_outline_color(olcolor);
     607        draw_bounding_box(x0, y0, z0, x0+1, y0+1, z0+1,
     608            (double)olcolor[0], (double)olcolor[1], (double)olcolor[2],
     609            1.5);
     610    }
     611    glPopMatrix();
     612
     613    //draw labels
     614    glPushMatrix();
     615    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
     616    if(cur_vol->outline_is_enabled()) {
     617       //draw_label(i);
     618    }
     619    glPopMatrix();
     620
     621    //transform volume_planes to eye coordinates.
     622    for(int i=0; i<6; i++)
     623      volume_planes[i].transform(model_view_no_trans);
     624    get_near_far_z(mv_no_trans, zNear, zFar);
     625
     626    //compute actual rendering slices
     627    float z_step = fabs(zNear-zFar)/n_slices;           
     628    int n_actual_slices;
     629
     630    if (cur_vol->data_is_enabled()) {
     631        n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1);
     632        polys[vol_index] = new ConvexPolygon*[n_actual_slices];
     633    } else {
     634        n_actual_slices = 0;
     635        polys[vol_index] = NULL;
     636    }
     637    actual_slices[vol_index] = n_actual_slices;
     638
     639    Vector4 vert1 = (Vector4(-10, -10, -0.5, 1));
     640    Vector4 vert2 = (Vector4(-10, +10, -0.5, 1));
     641    Vector4 vert3 = (Vector4(+10, +10, -0.5, 1));
     642    Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
     643
     644   
     645    //Render cutplanes first with depth test enabled.
     646    //They will mark the image with their depth values. Then we render other volume slices.
     647    //These volume slices will be occluded correctly by the cutplanes and vice versa.
     648
     649    ConvexPolygon static_poly;
     650    for(int i=0; i<cur_vol->get_cutplane_count(); i++)
     651    {
     652      if(!cur_vol->cutplane_is_enabled(i))
     653        continue;
     654
     655      float offset = cur_vol->get_cutplane(i)->offset;
     656      int axis = cur_vol->get_cutplane(i)->orient;
     657
     658      if(axis==3){
     659        vert1 = Vector4(-10, -10, offset, 1);
     660        vert2 = Vector4(-10, +10, offset, 1);
     661        vert3 = Vector4(+10, +10, offset, 1);
     662        vert4 = Vector4(+10, -10, offset, 1);
     663        //continue;
     664      }
     665      else if(axis==1){
     666        vert1 = Vector4(offset, -10, -10, 1);
     667        vert2 = Vector4(offset, +10, -10, 1);
     668        vert3 = Vector4(offset, +10, +10, 1);
     669        vert4 = Vector4(offset, -10, +10, 1);
     670        //continue;
     671      }
     672      else if(axis==2){
     673        vert1 = Vector4(-10, offset, -10, 1);
     674        vert2 = Vector4(+10, offset, -10, 1);
     675        vert3 = Vector4(+10, offset, +10, 1);
     676        vert4 = Vector4(-10, offset, +10, 1);
     677        //continue;
     678      }
     679
     680      vert1 = model_view_no_trans.transform(vert1);
     681      vert2 = model_view_no_trans.transform(vert2);
     682      vert3 = model_view_no_trans.transform(vert3);
     683      vert4 = model_view_no_trans.transform(vert4);
     684
     685      ConvexPolygon* p = &static_poly;
     686      p->vertices.clear();
     687
     688      p->append_vertex(vert1);
     689      p->append_vertex(vert2);
     690      p->append_vertex(vert3);
     691      p->append_vertex(vert4);
     692
     693      for(int k=0; k<6; k++){
     694        p->clip(volume_planes[k], true);
     695      }
     696
     697      p->transform(model_view_no_trans_inverse);
     698      p->transform(model_view_trans);
     699
     700      glPushMatrix();
     701      glScalef(cur_vol->aspect_ratio_width, cur_vol->aspect_ratio_height, cur_vol->aspect_ratio_depth);
     702
     703      activate_volume_shader(cur_vol, cur_tf, true);
     704      glPopMatrix();
     705
     706      glEnable(GL_DEPTH_TEST);
     707      glDisable(GL_BLEND);
     708
     709      glBegin(GL_POLYGON);
     710        p->Emit(true);
     711      glEnd();
     712      glDisable(GL_DEPTH_TEST);
     713
     714      deactivate_volume_shader();
     715    } //done cutplanes
     716
     717   
     718    //Now do volume rendering
     719
     720    vert1 = (Vector4(-10, -10, -0.5, 1));
     721    vert2 = (Vector4(-10, +10, -0.5, 1));
     722    vert3 = (Vector4(+10, +10, -0.5, 1));
     723    vert4 = (Vector4(+10, -10, -0.5, 1));
     724
     725    int counter = 0;
     726   
     727    //transform slices and store them
     728    float slice_z;
     729    for (int i=0; i<n_actual_slices; i++){
     730      slice_z = zFar + i * z_step;      //back to front
     731
     732      ConvexPolygon *poly = new ConvexPolygon();
     733      polys[vol_index][counter] = poly;
     734      counter++;
     735
     736      poly->vertices.clear();
     737      poly->set_id(vol_index);
     738
     739      //Setting Z-coordinate
     740      vert1.z = slice_z;
     741      vert2.z = slice_z;
     742      vert3.z = slice_z;
     743      vert4.z = slice_z;
     744               
     745      poly->append_vertex(vert1);
     746      poly->append_vertex(vert2);
     747      poly->append_vertex(vert3);
     748      poly->append_vertex(vert4);
     749       
     750      for(int k=0; k<6; k++){
     751        poly->clip(volume_planes[k], true);
     752      }
     753
     754      poly->transform(model_view_no_trans_inverse);
     755      poly->transform(model_view_trans);
     756
     757      if(poly->vertices.size()>=3)
     758        total_rendered_slices++;
     759    }
     760   
     761  } //iterate all volumes
     762  //fprintf(stderr, "total slices: %d\n", total_rendered_slices);
     763
     764  //We sort all the polygons according to their eye-space depth, from farthest to the closest.
     765  //This step is critical for correct blending
     766
     767  SortElement* slices = (SortElement*) malloc(sizeof(SortElement)*total_rendered_slices);
     768
     769  int counter = 0;
     770  for(int i=0; i<n_volumes; i++){
     771    for(int j=0; j<actual_slices[i]; j++){
     772      if(polys[i][j]->vertices.size() >= 3){
     773        slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j);
     774        counter++;
     775      }
     776    }
     777  }
     778
     779  //sort them
     780  qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort);
     781
     782  /*
     783  //debug
     784  for(int i=0; i<total_rendered_slices; i++){
     785    fprintf(stderr, "%f ", slices[i].z);
     786  }
     787  fprintf(stderr, "\n\n");
     788  */
     789
     790  //Now we are ready to render all the slices from back to front
     791  glEnable(GL_DEPTH_TEST);
     792  glEnable(GL_BLEND);
     793
     794  for(int i=0; i<total_rendered_slices; i++){
     795    int volume_index = slices[i].volume_id;
     796    int slice_index = slices[i].slice_id;
     797    ConvexPolygon* cur = polys[volume_index][slice_index];
     798
     799    if (volume_index == n_volumes)
     800    {
     801        cur_vol = ani_vol;
     802        cur_tf = ani_tf;
     803    }
     804    else
     805    {
     806        cur_vol = volume[volume_index];
     807        cur_tf = tf[volume_index];
     808    }
     809
     810
     811    glPushMatrix();
     812    glScalef(cur_vol->aspect_ratio_width, cur_vol->aspect_ratio_height, cur_vol->aspect_ratio_depth);
     813   
     814    activate_volume_shader(cur_vol, cur_tf, false);
     815    glPopMatrix();
     816
     817    glBegin(GL_POLYGON);
     818      cur->Emit(true);
     819    glEnd();
     820
     821    deactivate_volume_shader();
     822  }
     823
     824
     825  glDisable(GL_DEPTH_TEST);
     826  glDisable(GL_BLEND);
     827
     828  //Deallocate all the memory used
     829  for(int i=0; i<n_volumes; i++){
     830    for(int j=0; j<actual_slices[i]; j++){
     831      delete polys[i][j];
     832    }
     833    if (polys[i]) {
     834      delete[] polys[i];
     835    }
     836  }
     837  delete[] polys;
     838  delete[] actual_slices;
     839  free(slices);
     840}
     841#if 0
    143842void VolumeRenderer::render_all()
    144843{
     
    229928    glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);
    230929    if(volume[i]->outline_is_enabled()) {
    231        draw_label(i);
     930       draw_label(volume[i]);
    232931    }
    233932    glPopMatrix();
     
    3151014      glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    3161015
    317       activate_volume_shader(volume_index, true);
     1016      activate_volume_shader(volume[volume_index], tf[volume_index],true);
    3181017      glPopMatrix();
    3191018
     
    4141113    glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    4151114   
    416     activate_volume_shader(volume_index, false);
     1115    activate_volume_shader(volume[volume_index], tf[volume_index],false);
    4171116    glPopMatrix();
    4181117
     
    4411140  free(slices);
    4421141}
    443 
    444 
    445 void VolumeRenderer::render(int volume_index){
     1142#endif
     1143
     1144void VolumeRenderer::render(int volume_index)
     1145{
    4461146  int n_slices = volume[volume_index]->get_n_slice();
    4471147
     
    5771277    glScalef(volume[volume_index]->aspect_ratio_width, volume[volume_index]->aspect_ratio_height, volume[volume_index]->aspect_ratio_depth);
    5781278
    579     activate_volume_shader(volume_index, true);
     1279    activate_volume_shader(volume[volume_index], tf[volume_index], true);
    5801280    glPopMatrix();
    5811281
     
    6371337    */
    6381338   
    639     activate_volume_shader(volume_index, false);
     1339    activate_volume_shader(volume[volume_index], tf[volume_index], true);
    6401340    glPopMatrix();
    6411341
     
    7041404
    7051405
    706 void VolumeRenderer::activate_volume_shader(int volume_index, bool slice_mode)
     1406void VolumeRenderer::activate_volume_shader(Volume* vol, TransferFunction* tf, bool slice_mode)
    7071407{
    7081408  //vertex shader
    7091409  _stdVertexShader->bind();
    7101410
    711   if (volume[volume_index]->volume_type == CUBIC)
     1411  if (vol->volume_type == CUBIC)
    7121412  {
    7131413    //regular cubic volume
    714     _regularVolumeShader->bind(tf[volume_index]->id, volume[volume_index], slice_mode);
    715   }
    716   else if (volume[volume_index]->volume_type == ZINCBLENDE)
     1414    _regularVolumeShader->bind(tf->id, vol, slice_mode);
     1415  }
     1416  else if (vol->volume_type == ZINCBLENDE)
    7171417  {
    718     _zincBlendeShader->bind(tf[volume_index]->id, volume[volume_index], slice_mode);
     1418    _zincBlendeShader->bind(tf->id, vol, slice_mode);
    7191419  }
    7201420}
     
    9221622
    9231623
    924 void VolumeRenderer::draw_label(int volume_index){
    925 
    926   Volume* vol = volume[volume_index];
    927  
     1624void VolumeRenderer::draw_label(Volume* vol){
     1625
    9281626  //glEnable(GL_TEXTURE_2D);
    9291627  glDisable(GL_TEXTURE_2D);
  • trunk/vizservers/nanovis/VolumeRenderer.h

    r821 r884  
    3737#include "NvZincBlendeVolumeShader.h"
    3838#include "NvStdVertexShader.h"
     39#include "VolumeInterpolator.h"
    3940
    4041class VolumeRenderer {
    4142
     43    friend class NanoVis;
    4244private:
    4345    std::vector <Volume*> volume;           //!<- array of volumes
    4446    std::vector <TransferFunction*> tf;    //!<- array of corresponding transfer functions
     47    VolumeInterpolator* _volumeInterpolator;
     48
    4549    int n_volumes;
    4650
     
    7882
    7983  void init_shaders();
    80   void activate_volume_shader(int volume_index, bool slice_mode);
     84  void activate_volume_shader(Volume* vol, TransferFunction* tf, bool slice_mode);
    8185  void deactivate_volume_shader();
    8286
     
    9195  GLuint font_texture;                          //the id of the font texture
    9296  void glPrint(char* string, int set);          //there are two sets of font in the texture. 0, 1
    93   void draw_label(int volume_index);            //draw label using bitmap texture
     97  void draw_label(Volume* vol);         //draw label using bitmap texture
    9498  GLuint font_base;                             //the base of the font display list
    9599  void build_font();                            //register the location of each alphabet in the texture
     
    117121  void enable_volume(int index); //enable a volume
    118122  void disable_volume(int index); //disable a volume
     123
     124    void clearAnimatedVolumeInfo();
     125    void addAnimatedVolume(Volume* volume, unsigned int volumeId);
     126    void startVolumeAnimation();
     127    void stopVolumeAnimation();
    119128};
    120129
     130inline void VolumeRenderer::clearAnimatedVolumeInfo()
     131{
     132    _volumeInterpolator->clearAll();
     133}
     134
     135inline void VolumeRenderer::addAnimatedVolume(Volume* volume, unsigned int volumeId)
     136{
     137    _volumeInterpolator->addVolume(volume, volumeId);
     138}
     139
     140inline void VolumeRenderer::startVolumeAnimation()
     141{
     142    _volumeInterpolator->start();
     143}
     144
     145inline void VolumeRenderer::stopVolumeAnimation()
     146{
     147    _volumeInterpolator->stop();
     148}
     149
     150
    121151#endif
  • trunk/vizservers/nanovis/nanovis.cpp

    r881 r884  
    2323#include <sstream>
    2424#include <string>
     25#include <time.h>
    2526#include <sys/time.h>
    2627#include <sys/types.h>
     
    2829#include <fcntl.h>
    2930#include <signal.h>
     31#include <stdlib.h>
    3032
    3133#include "Nv.h"
     
    5658#include "HeightMap.h"
    5759#include "Grid.h"
     60#include "VolumeInterpolator.h"
    5861#include <RenderContext.h>
    5962
     
    13641367#endif
    13651368
     1369void NanoVis::update()
     1370{
     1371    if (vol_renderer->_volumeInterpolator->is_started())
     1372    {
     1373    struct timeval clock;
     1374    gettimeofday(&clock, NULL);
     1375    double cur_time = clock.tv_sec + clock.tv_usec/1000000.0;
     1376
     1377    float fraction;
     1378    float f = fmod(cur_time - vol_renderer->_volumeInterpolator->getStartTime(),
     1379                    vol_renderer->_volumeInterpolator->getInterval());
     1380
     1381    if (f == 0.0f) fraction = 0.0f;
     1382    else fraction = f / vol_renderer->_volumeInterpolator->getInterval();
     1383
     1384    vol_renderer->_volumeInterpolator->update(fraction);
     1385  }
     1386}
    13661387
    13671388/*----------------------------------------------------*/
  • trunk/vizservers/nanovis/nanovis.h

    r881 r884  
    152152    static void bmp_write_to_file(int frame_number);
    153153    static void display(void);
     154    static void update(void);
    154155    static void display_offscreen_buffer();
    155156    static void read_screen();
  • trunk/vizservers/nanovis/shaders/one_volume.cg

    r870 r884  
    6262    //float normal_dot_half = abs(dot(normal, half_vector));
    6363
     64
    6465    float ambient = 0.8;
    6566    float diffuse = normal_dot_light * renderParameters.z;
Note: See TracChangeset for help on using the changeset viewer.