Ignore:
Timestamp:
Apr 4, 2006 4:01:47 PM (18 years ago)
Author:
qiaow
Message:

Lic class working!!
OpenGL scaling calls now are inside of Lic and ParticleSystem? classes.

File:
1 edited

Legend:

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

    r396 r397  
    1515
    1616
     17#include <stdlib.h>
     18#include <math.h>
     19#include <assert.h>
    1720
    1821#include "Lic.h"
    1922
    20 
    21 Lic::Lic(int _size, int _width, int _height):
     23Lic::Lic(int _size, int _width, int _height, float _offset,
     24                CGcontext _context, NVISid _vector_field,
     25                float scalex, float scaley, float scalez):
    2226        size(_size),
     27        offset(_offset),
     28        m_g_context(_context),
    2329        display_width(_width),
    24         display_height(_height)
     30        display_height(_height),
     31        iframe(0),
     32        Npat(64),
     33        alpha(0.12*255),
     34        tmax(NPIX/(SCALE*NPN)),
     35        dmax(SCALE/NPIX)
    2536{
     37  scale = Vector3(scalex, scaley, scalez);
     38  slice_vector = new float[NMESH*NMESH*4];
    2639
    2740  //initialize the pattern texture
     
    3952
    4053  //initialize frame buffer objects
     54
     55  //render buffer for projecting 3D velocity onto a 2D plane
    4156  glGenFramebuffersEXT(1, &vel_fbo);
    4257  glGenTextures(1, &slice_vector_tex);
     
    4459  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
    4560
    46   //initialize texture storing per slice vectors
    4761  glBindTexture(GL_TEXTURE_RECTANGLE_NV, slice_vector_tex);
    4862  glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    5468
    5569
    56   //lic result fbo
     70  //render buffer for the convolution
    5771  glGenFramebuffersEXT(1, &fbo);
    5872  glGenTextures(1, &color_tex);
     
    7488  assert(glGetError()==0);
    7589
    76 }
    77 
    78 
    79 void Lic::MakePatterns(void)
     90  m_render_vel_fprog = loadProgram(m_g_context, CG_PROFILE_FP30, CG_SOURCE, "./shaders/render_vel.cg");
     91  m_vel_tex_param_render_vel = cgGetNamedParameter(m_render_vel_fprog, "vel_tex");
     92  m_plane_normal_param_render_vel = cgGetNamedParameter(m_render_vel_fprog, "plane_normal");
     93  cgGLSetTextureParameter(m_vel_tex_param_render_vel, _vector_field);
     94
     95  get_slice();
     96  make_patterns();
     97
     98  fprintf(stderr, "initialize lic ...\n");
     99}
     100
     101Lic::~Lic(){
     102  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
     103  glDeleteTextures(1, &slice_vector_tex);
     104
     105  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
     106  glDeleteTextures(1, &color_tex);
     107
     108  NVISid buffers[2] = {vel_fbo, fbo};
     109  glDeleteFramebuffersEXT(2, buffers);
     110
     111  delete slice_vector;
     112}
     113
     114void Lic::make_patterns()
    80115{
    81116   int lut[256];
     
    107142
    108143
    109 void Lic::MakeMagnitudes()
     144void Lic::make_magnitudes()
    110145{
    111146
     
    145180}
    146181
     182//project 3D vectors to a 2D slice for line integral convolution
     183void Lic::get_slice(){
     184  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
     185  glBindTexture(GL_TEXTURE_RECTANGLE_NV, slice_vector_tex);
     186
     187  glClear(GL_COLOR_BUFFER_BIT);
     188
     189  glViewport(0, 0, NMESH, NMESH);
     190  glMatrixMode(GL_PROJECTION);
     191  glLoadIdentity();
     192  gluOrtho2D(0, NMESH, 0, NMESH);
     193  glMatrixMode(GL_MODELVIEW);
     194  glLoadIdentity();
     195
     196  cgGLBindProgram(m_render_vel_fprog);
     197  cgGLEnableTextureParameter(m_vel_tex_param_render_vel);
     198  cgGLSetParameter4f(m_plane_normal_param_render_vel, 1., 1., 0., 0);
     199
     200  cgGLEnableProfile(CG_PROFILE_FP30);
     201  glBegin(GL_QUADS);
     202    glTexCoord3f(0., 0., offset); glVertex2f(0.,   0.);
     203    glTexCoord3f(1., 0., offset); glVertex2f(size, 0.);
     204    glTexCoord3f(1., 1., offset); glVertex2f(size, size);
     205    glTexCoord3f(0., 1., offset); glVertex2f(0.,   size);
     206  glEnd();
     207  cgGLDisableProfile(CG_PROFILE_FP30);
     208   
     209  cgGLDisableTextureParameter(m_vel_tex_param_render_vel);
     210
     211  //read the vectors
     212  glReadPixels(0, 0, NMESH, NMESH, GL_RGBA, GL_FLOAT, slice_vector);
     213  /*
     214  for(int i=0; i<NMESH*NMESH; i++){
     215    fprintf(stderr, "%f,%f,%f,%f", slice_vector[4*i], slice_vector[4*i+1], slice_vector[4*i+2], slice_vector[4*i+3]);
     216  }
     217  */
     218  assert(glGetError()==0);
     219}
     220
    147221
    148222//line integral convolution
    149223void Lic::convolve(){
     224
    150225   int   i, j;
    151226   float x1, x2, y, px, py;
     227
     228   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
    152229
    153230   glViewport(0, 0, (GLsizei) NPIX, (GLsizei) NPIX);
     
    167244          y = DM*j;
    168245          glTexCoord2f(x1, y);
    169           getDP(x1, y, &px, &py);
     246          get_velocity(x1, y, &px, &py);
    170247          glVertex2f(px, py);
    171248
    172249          glTexCoord2f(x2, y);
    173           getDP(x2, y, &px, &py);
     250          get_velocity(x2, y, &px, &py);
    174251          glVertex2f(px, py);
    175252      }
     
    201278   glDisable(GL_BLEND);
    202279   glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, NPIX, NPIX, 0);
    203 }
    204 
    205 void display(){
    206 
    207 
    208 }
    209 
     280   
     281   /*
     282   //blend magnitude texture
     283   glBindTexture(GL_TEXTURE_2D, mag_tex);
     284   glEnable(GL_TEXTURE_2D);
     285   glEnable(GL_BLEND);
     286   glBegin(GL_QUADS);
     287      glTexCoord2f(0.0,  0.0);  glVertex2f(0.0, 0.0);
     288      glTexCoord2f(0.0,  1.0); glVertex2f(0.0, 1.);
     289      glTexCoord2f(1.0, 1.0);  glVertex2f(1., 1.);
     290      glTexCoord2f(1.0, 0.0); glVertex2f(1., 0.0);
     291   glEnd();
     292   */
     293}
     294
     295void Lic::display(){
     296
     297  glBindTexture(GL_TEXTURE_2D, color_tex);
     298  glEnable(GL_TEXTURE_2D);
     299
     300  //draw line integral convolution quad
     301  glEnable(GL_DEPTH_TEST);
     302  glPushMatrix();
     303  glScalef(scale.x, scale.y, scale.z);
     304
     305  glBegin(GL_QUADS);
     306    glTexCoord2f(0, 0); glVertex3f(0, 0, offset);
     307    glTexCoord2f(1, 0); glVertex3f(1, 0, offset);
     308    glTexCoord2f(1, 1); glVertex3f(1, 1, offset);
     309    glTexCoord2f(0, 1); glVertex3f(0, 1, offset);
     310  glEnd();
     311
     312  glPopMatrix();
     313
     314  glDisable(GL_DEPTH_TEST);
     315  glDisable(GL_TEXTURE_2D);
     316}
     317
     318
     319void Lic::get_velocity(float x, float y, float *px, float *py)
     320{
     321   float dx, dy, vx, vy, r;
     322
     323   int xi = x*NMESH;
     324   int yi = y*NMESH;
     325
     326   vx = slice_vector[4*(xi+yi*NMESH)];
     327   vy = slice_vector[4*(xi+yi*NMESH)+1];
     328   r  = vx*vx + vy*vy;
     329   if (r > dmax*dmax) {
     330      r  = sqrt(r);
     331      vx *= dmax/r;
     332      vy *= dmax/r;
     333   }
     334   *px = x + vx;         
     335   *py = y + vy;
     336}
     337
     338void Lic::set_offset(float v){
     339  offset = v;
     340  get_slice();
     341}
Note: See TracChangeset for help on using the changeset viewer.