Changeset 274 for trunk/gui/vizservers


Ignore:
Timestamp:
Mar 3, 2006, 2:03:43 PM (19 years ago)
Author:
qiaow
Message:
 
Location:
trunk/gui/vizservers/nanovis
Files:
3 edited

Legend:

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

    r273 r274  
    1616#include <stdio.h>
    1717#include <assert.h>
     18#include <malloc.h>
     19#include <string.h>
    1820
    1921#include "ParticleSystem.h"
     
    2931  flip = true;
    3032  max_life = 30;
     33
     34  data = (Particle*) malloc(w*h*sizeof(Particle));
     35
     36  m_vertex_array = new RenderVertexArray(psys_width*psys_height, 3, GL_FLOAT);
     37  assert(glGetError()==0);
    3138
    3239  glGenFramebuffersEXT(2, psys_fbo);
     
    7380
    7481  glDeleteFramebuffersEXT(2, psys_fbo);
    75 }
    76 
    77 
    78 void ParticleSystem::initialize(Particle* data){
     82
     83  free(data);
     84}
     85
     86
     87void ParticleSystem::initialize(Particle* p){
     88  //also store the data on main memory for next initialization
     89  memcpy(data, p, psys_width*psys_height*sizeof(Particle));
     90
     91  glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
     92  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)p);
     93 
     94  assert(glGetError()==0);
     95
     96  flip = true;
     97  reborn = false;
     98
     99  fprintf(stderr, "init particles\n");
     100}
     101
     102void ParticleSystem::reset(){
    79103  glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
    80104  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)data);
     
    84108  flip = true;
    85109  reborn = false;
    86 
    87   fprintf(stderr, "init particles\n");
    88 }
     110}
     111
    89112
    90113void ParticleSystem::advect(){
     114   if(reborn) reset();
     115
    91116   glDisable(GL_BLEND);
    92117   
     
    150175
    151176   //soft_read_verts();
    152 
    153    //hard_read_verts();
     177   update_vertex_buffer();
    154178
    155179   flip = (!flip);
     
    163187   fprintf(stderr, "advect: %d ", psys_frame);
    164188}
     189
     190
     191void ParticleSystem::update_vertex_buffer(){
     192  m_vertex_array->Read(psys_width, psys_height);
     193  //m_vertex_array->LoadData(vert);     //does not work??
     194  assert(glGetError()==0);
     195}
     196
     197
     198void ParticleSystem::display_vertices(){
     199  glDisable(GL_TEXTURE_2D);
     200  glDisable(GL_BLEND);
     201
     202  glPointSize(0.5);
     203  glColor4f(.8,.8,.8,1.);
     204
     205  m_vertex_array->SetPointer(0);
     206  //glEnableVertexAttribArray(0);
     207  glEnableClientState(GL_VERTEX_ARRAY);
     208  glDrawArrays(GL_POINTS, 0, psys_width*psys_height);
     209  //glDisableVertexAttribArray(0);
     210  glDisableClientState(GL_VERTEX_ARRAY);
     211 
     212  assert(glGetError()==0);
     213}
     214
     215
  • trunk/gui/vizservers/nanovis/ParticleSystem.h

    r273 r274  
    2525#include "global.h"
    2626
     27#include "RenderVertexArray.h"
     28
    2729typedef struct Particle{
    2830  float x;
     
    4244  NVISid psys_tex[2];   //color textures attached to frame buffer objects
    4345
    44   int psys_width;       //the storage of particles is implemented as a 2D array.
    45   int psys_height;
    4646  Particle* data;
    4747
     
    5050  bool flip;                    //flip the source and destination render targets
    5151  float max_life;
     52
     53  RenderVertexArray* m_vertex_array;    //vertex array for display particles
    5254
    5355  //Nvidia CG shaders and their parameters
     
    5860
    5961public:
     62  int psys_width;       //the storage of particles is implemented as a 2D array.
     63  int psys_height;
     64
    6065  ParticleSystem(int w, int h, CGcontext context, NVISid vector_field);
    6166  ~ParticleSystem();
    6267  void initialize(Particle* data);
    6368  void advect();
     69  void update_vertex_buffer();
     70  void display_vertices();
     71  void reset();
    6472
    6573};
  • trunk/gui/vizservers/nanovis/nanovis.cpp

    r273 r274  
    1717
    1818ParticleSystem* psys;
    19 
    20 
    21 //particle system related variables
    22 NVISid psys_fbo[2];
    23 NVISid psys_tex[2];
    24 int psys_width = NMESH;                 //particle system size
    25 int psys_height = NMESH;
    26 int psys_frame = 0;                     //count the frame number of particle system iteration
    27 float psys_x =0.4, psys_y=0, psys_z=0;  //particle initialization coordinates
    28 int life = 30;                          //particle lifespan
    29 bool reborn = true;                     //reinitiate particles
    30 bool flip = true;                       //flip the source and destination render targets
    31 
     19float psys_x=0.4, psys_y=0, psys_z=0;
    3220
    3321char* screen_buffer = new char[4*NPIX*NPIX+1];          //buffer to store data read from the screen
     
    7058CGparameter m_mvp_vert_std_param;
    7159CGparameter m_mvi_vert_std_param;
    72 
    73 
    74 float m_pointsize = 1.0;
    75 float m_point_alpha;
    76 
    77 //RenderVertexArray
    78 RenderVertexArray* m_vertex_array;
    7960
    8061
     
    125106
    126107
    127 
    128 //initialize particle system
    129 void init_psys(){
    130 
    131   glGenFramebuffersEXT(2, psys_fbo);
    132   glGenTextures(2, psys_tex);
    133 
    134   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[0]);
    135 
    136   glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
    137   glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    138   glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    139   glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
    140   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, psys_tex[0], 0);
    141 
    142   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[1]);
    143 
    144   glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[1]);
    145   glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    146   glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    147   glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
    148   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, psys_tex[1], 0);
    149 
    150   CHECK_FRAMEBUFFER_STATUS();
    151   assert(glGetError()==0);
    152   fprintf(stderr, "init_psys\n");
    153 }
    154 
    155 
    156108void load_volume(int index, int width, int height, int depth, int n_component, float* data);
    157 
    158109
    159110
     
    301252
    302253
    303 //initialize the vertex buffer object
    304 void init_vbo(){
    305   m_vertex_array = new RenderVertexArray(psys_width*psys_height, 3, GL_FLOAT);
    306   assert(glGetError()==0);
    307 }
    308 
    309 
    310254void makeMagnitudes(){
    311255  GLubyte mag[NMESH][NMESH][4];
     
    399343void init_particles(){
    400344  //random placement on a slice
    401   float* data = new float[psys_width*psys_height*4];
    402   bzero(data, sizeof(float)*4*psys_width*psys_height);
    403   for (int i=0; i<psys_width; i++){
    404     for (int j=0; j<psys_height; j++){
    405       int index = i + psys_height*j;
     345  float* data = new float[psys->psys_width * psys->psys_height * 4];
     346  bzero(data, sizeof(float)*4* psys->psys_width * psys->psys_height);
     347
     348  for (int i=0; i<psys->psys_width; i++){
     349    for (int j=0; j<psys->psys_height; j++){
     350      int index = i + psys->psys_height*j;
    406351      bool particle = rand() % 256 > 100;
    407352      if(particle)
    408353      {
    409354        data[4*index] = psys_x;
    410         data[4*index+1]= i/float(psys_width);
    411         data[4*index+2]= j/float(psys_height);
    412         data[4*index+3]= life
     355        data[4*index+1]= i/float(psys->psys_width);
     356        data[4*index+2]= j/float(psys->psys_height);
     357        data[4*index+3]= 30;   
    413358      }
    414359      else
     
    423368
    424369  psys->initialize((Particle*)data);
    425 
    426   glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
    427   glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, data);
    428  
    429   flip = true;
    430   assert(glGetError()==0);
    431 
    432370  delete[] data;
    433   reborn = false;
    434371
    435372  fprintf(stderr, "init particles\n");
    436373}
    437 
    438374
    439375
     
    482418   init_vector_field(); //3d vector field
    483419   init_fbo();  //frame buffer objects
    484    init_psys(); //particle system
    485    init_cg();           //init cg shaders
    486    init_vbo();          //init vertex buffer object
     420   init_cg();   //init cg shaders
    487421
    488422   psys = new ParticleSystem(NMESH, NMESH, g_context, volume[0]->id);
    489 
    490423   init_particles();    //fill initial particles
     424
    491425   get_slice_vectors();
    492426}
     
    790724
    791725void soft_read_verts(){
    792   glReadPixels(0, 0, psys_width, psys_height, GL_RGB, GL_FLOAT, vert);
     726  glReadPixels(0, 0, psys->psys_width, psys->psys_height, GL_RGB, GL_FLOAT, vert);
    793727  //fprintf(stderr, "soft_read_vert");
    794728
    795729  //cpu sort the distance 
    796   Particle* p = (Particle*) malloc(sizeof(Particle)*psys_width*psys_height);
    797   for(int i=0; i<psys_width*psys_height; i++){
     730  Particle* p = (Particle*) malloc(sizeof(Particle)* psys->psys_width * psys->psys_height);
     731  for(int i=0; i<psys->psys_width * psys->psys_height; i++){
    798732    float x = vert[3*i];
    799733    float y = vert[3*i+1];
     
    807741  }
    808742
    809   qsort(p, psys_width*psys_height, sizeof(Particle), particle_distance_sort);
    810 
    811   for(int i=0; i<psys_width*psys_height; i++){
     743  qsort(p, psys->psys_width * psys->psys_height, sizeof(Particle), particle_distance_sort);
     744
     745  for(int i=0; i<psys->psys_width * psys->psys_height; i++){
    812746    vert[3*i] = p[i].x;
    813747    vert[3*i+1] = p[i].y;
     
    816750
    817751  free(p);
    818 }
    819 
    820 
    821 void hard_read_verts(){
    822   m_vertex_array->Read(psys_width, psys_height);
    823   //m_vertex_array->LoadData(vert);     //does not work
    824   assert(glGetError()==0);
    825 }
    826 
    827 
    828 void sortstep();
    829 
    830 void advect_particles(){
    831  
    832    glDisable(GL_BLEND);
    833    
    834    if(flip)
    835    {
    836 
    837    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[1]);
    838    glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
    839 
    840    glClear(GL_COLOR_BUFFER_BIT);
    841 
    842    glViewport(0, 0, psys_width, psys_height);
    843    glMatrixMode(GL_PROJECTION);
    844    glLoadIdentity();
    845    gluOrtho2D(0, psys_width, 0, psys_height);
    846    glMatrixMode(GL_MODELVIEW);
    847    glLoadIdentity();
    848 
    849    cgGLBindProgram(m_pos_fprog);
    850    cgGLSetParameter1f(m_pos_timestep_param, 0.1);
    851    cgGLEnableTextureParameter(m_vel_tex_param);
    852    cgGLSetTextureParameter(m_pos_tex_param, psys_tex[0]);
    853    cgGLEnableTextureParameter(m_pos_tex_param);
    854 
    855    cgGLEnableProfile(CG_PROFILE_FP30);
    856    draw_quad(psys_width, psys_height, psys_width, psys_height);
    857    cgGLDisableProfile(CG_PROFILE_FP30);
    858    
    859    cgGLDisableTextureParameter(m_vel_tex_param);
    860    cgGLDisableTextureParameter(m_pos_tex_param);
    861 
    862   /*
    863    cgGLBindProgram(m_passthru_fprog);
    864    cgGLEnableProfile(CG_PROFILE_FP30);
    865 
    866    cgGLSetParameter4f(m_passthru_scale_param, 1.0, 1.0, 1.0, 1.0);
    867    cgGLSetParameter4f(m_passthru_bias_param, 0.0, 0.0, 0.0, 0.0);
    868    
    869    draw_quad(psys_width, psys_height, psys_width, psys_height);
    870    cgGLDisableProfile(CG_PROFILE_FP30);
    871    */
    872    
    873    }
    874    else
    875    {
    876 
    877    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[0]);
    878    glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[1]);
    879 
    880    glClear(GL_COLOR_BUFFER_BIT);
    881 
    882    glViewport(0, 0, psys_width, psys_height);
    883    glMatrixMode(GL_PROJECTION);
    884    glLoadIdentity();
    885    gluOrtho2D(0, psys_width, 0, psys_height);
    886    glMatrixMode(GL_MODELVIEW);
    887    glLoadIdentity();
    888 
    889    cgGLBindProgram(m_pos_fprog);
    890    cgGLSetParameter1f(m_pos_timestep_param, 0.1);
    891    cgGLEnableTextureParameter(m_vel_tex_param);
    892    cgGLSetTextureParameter(m_pos_tex_param, psys_tex[1]);
    893    cgGLEnableTextureParameter(m_pos_tex_param);
    894 
    895    cgGLEnableProfile(CG_PROFILE_FP30);
    896    draw_quad(psys_width, psys_height, psys_width, psys_height);
    897    cgGLDisableProfile(CG_PROFILE_FP30);
    898    
    899    cgGLDisableTextureParameter(m_vel_tex_param);
    900    cgGLDisableTextureParameter(m_pos_tex_param);
    901 
    902   /*
    903    cgGLBindProgram(m_passthru_fprog);
    904    cgGLEnableProfile(CG_PROFILE_FP30);
    905 
    906    cgGLSetParameter4f(m_passthru_scale_param, 1.0, 1.0, 1.0, 1.0);
    907    cgGLSetParameter4f(m_passthru_bias_param, 0.0, 0.0, 0.0, 0.0);
    908    
    909    draw_quad(psys_width, psys_height, psys_width, psys_height);
    910    cgGLDisableProfile(CG_PROFILE_FP30);
    911   */
    912 
    913    }
    914 
    915    assert(glGetError()==0);
    916 
    917    //soft_read_verts();
    918 
    919    hard_read_verts();
    920 
    921    flip = (!flip);
    922 
    923    psys_frame++;
    924    if(psys_frame==life){
    925      psys_frame=0;
    926      reborn = true;
    927    }
    928 
    929    fprintf(stderr, "advect: %d ", psys_frame);
    930752}
    931753
     
    960782
    961783
    962 //draw vertices in the onboard vertex buffer object
    963 void hard_display_verts(){
    964   glDisable(GL_TEXTURE_2D);
    965   glDisable(GL_BLEND);
    966 
    967   glPointSize(m_pointsize);
    968   glColor4f(.8,.8,.8,1.);
    969 
    970   m_vertex_array->SetPointer(0);
    971   //glEnableVertexAttribArray(0);
    972   glEnableClientState(GL_VERTEX_ARRAY);
    973   glDrawArrays(GL_POINTS, 0, psys_width*psys_height);
    974   //glDisableVertexAttribArray(0);
    975   glDisableClientState(GL_VERTEX_ARRAY);
    976  
    977   assert(glGetError()==0);
    978 }
    979 
    980 
    981784//draw vertices in the main memory
    982785void soft_display_verts(){
     
    984787  glEnable(GL_BLEND);
    985788
    986   glPointSize(m_pointsize);
     789  glPointSize(0.5);
    987790  glColor4f(0,0.8,0.8,0.6);
    988791  glBegin(GL_POINTS);
    989   for(int i=0; i<psys_width*psys_height; i++){
     792  for(int i=0; i<psys->psys_width * psys->psys_height; i++){
    990793    glVertex3f(vert[3*i], vert[3*i+1], vert[3*i+2]);
    991794  }
     
    13311134   glEnd();
    13321135   
    1333    
    1334    advect_particles();
     1136   //advect particles
     1137   psys->advect();
    13351138
    13361139   final_fbo_capture();
    13371140
    1338    /*
    1339    if(flip)
    1340      display_texture(psys_tex[1], psys_width, psys_height);
    1341    else
    1342      display_texture(psys_tex[0], psys_width, psys_height);
    1343    */
    1344  
    13451141   //display_texture(slice_vector_tex, NMESH, NMESH);
    13461142
     
    13951191
    13961192   //soft_display_verts();
    1397    hard_display_verts();
     1193   psys->display_vertices();
    13981194
    13991195   //render volume
     
    14051201   display_final_fbo();
    14061202
    1407    //rebirth
    1408    if(reborn){
    1409     init_particles();
    1410    }
    14111203   glutSwapBuffers();
    14121204}
Note: See TracChangeset for help on using the changeset viewer.