source: trunk/vizservers/nanovis/Lic.cpp @ 828

Last change on this file since 828 was 755, checked in by dkearney, 17 years ago

added -p option to nanovis so we can add paths in a colon separated string.
removed all references to hard coded paths in /opt/nanovis
updated start_vis.sh script to include nanovis' -p flag and what a call to pymol might look like

File size: 9.5 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 * Lic.h: line integral convolution class
4 *
5 * ======================================================================
6 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
7 *           Purdue Rendering and Perceptualization Lab (PURPL)
8 *
9 *  Copyright (c) 2004-2006  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15
16
17#include <stdlib.h>
18#include <math.h>
19#include <assert.h>
20
21#include <R2/R2FilePath.h>
22#include "Lic.h"
23
24Lic::Lic(int _size, int _width, int _height, float _offset,
25                CGcontext _context, NVISid _vector_field,
26                float scalex, float scaley, float scalez):
27        Renderable(Vector3(0.,0.,0.)),
28        size(_size),
29        offset(_offset),
30        m_g_context(_context),
31        width(_width),
32        height(_height),
33        iframe(0),
34        Npat(64),
35        alpha(0.12*255),
36        tmax(NPIX/(SCALE*NPN)),
37        dmax(SCALE/NPIX)
38{
39  scale = Vector3(scalex, scaley, scalez);
40  slice_vector = new float[size*size*4];
41
42  //initialize the pattern texture
43  glGenTextures(1, &pattern_tex);
44  glBindTexture(GL_TEXTURE_2D, pattern_tex);
45  glTexParameteri(GL_TEXTURE_2D,
46                  GL_TEXTURE_WRAP_S, GL_REPEAT);
47  glTexParameteri(GL_TEXTURE_2D,
48                  GL_TEXTURE_WRAP_T, GL_REPEAT);
49  glTexParameteri(GL_TEXTURE_2D,
50                  GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51  glTexParameteri(GL_TEXTURE_2D,
52                  GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
54
55  //initialize frame buffer objects
56
57  //render buffer for projecting 3D velocity onto a 2D plane
58  glGenFramebuffersEXT(1, &vel_fbo);
59  glGenTextures(1, &slice_vector_tex);
60
61  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
62
63  glBindTexture(GL_TEXTURE_RECTANGLE_NV, slice_vector_tex);
64  glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
65  glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
66  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0,
67                  GL_FLOAT_RGBA32_NV, size, size, 0, GL_RGBA, GL_FLOAT, NULL);
68  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
69                  GL_TEXTURE_RECTANGLE_NV, slice_vector_tex, 0);
70
71
72  //render buffer for the convolution
73  glGenFramebuffersEXT(1, &fbo);
74  glGenTextures(1, &color_tex);
75
76  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
77
78  //initialize color texture for lic
79  glBindTexture(GL_TEXTURE_2D, color_tex);
80  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0,
83               GL_RGB, GL_INT, NULL);
84  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
85                            GL_COLOR_ATTACHMENT0_EXT,
86                            GL_TEXTURE_2D, color_tex, 0);
87
88  // Check framebuffer completeness at the end of initialization.
89  CHECK_FRAMEBUFFER_STATUS();
90  assert(glGetError()==0);
91
92  R2string path = R2FilePath::getInstance()->getPath("render_vel.cg");
93  m_render_vel_fprog = loadProgram(m_g_context, CG_PROFILE_FP30, CG_SOURCE, path);
94  m_vel_tex_param_render_vel = cgGetNamedParameter(m_render_vel_fprog, "vel_tex");
95  m_plane_normal_param_render_vel = cgGetNamedParameter(m_render_vel_fprog, "plane_normal");
96  cgGLSetTextureParameter(m_vel_tex_param_render_vel, _vector_field);
97
98  get_slice();
99  make_patterns();
100
101  fprintf(stderr, "initialize lic ...\n");
102}
103
104Lic::~Lic(){
105  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
106  glDeleteTextures(1, &slice_vector_tex);
107
108  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
109  glDeleteTextures(1, &color_tex);
110
111  NVISid buffers[2] = {vel_fbo, fbo};
112  glDeleteFramebuffersEXT(2, buffers);
113
114  delete slice_vector;
115}
116
117void Lic::make_patterns()
118{
119   int lut[256];
120   int phase[NPN][NPN];
121   GLubyte pat[NPN][NPN][4];
122   int i, j, k, t;
123   
124   for (i = 0; i < 256; i++) lut[i] = i < 127 ? 0 : 255;
125   for (i = 0; i < NPN; i++)
126   for (j = 0; j < NPN; j++) phase[i][j] = rand() % 256;
127
128   for (k = 0; k < Npat; k++) {
129     t = k*256/Npat;
130     for (i = 0; i < NPN; i++)
131     for (j = 0; j < NPN; j++) {
132       pat[i][j][0] =
133       pat[i][j][1] =
134       pat[i][j][2] = lut[(t + phase[i][j]) % 255];
135       pat[i][j][3] = alpha;
136     }
137
138     glNewList(k + 1, GL_COMPILE);
139     glBindTexture(GL_TEXTURE_2D, pattern_tex);
140     glTexImage2D(GL_TEXTURE_2D, 0, 4, NPN, NPN, 0,
141                     GL_RGBA, GL_UNSIGNED_BYTE, pat);
142     glEndList();
143   }
144}
145
146
147void Lic::make_magnitudes()
148{
149
150  GLubyte mag[NMESH][NMESH][4];
151
152  //read vector filed
153  for(int i=0; i<NMESH; i++){
154    for(int j=0; j<NMESH; j++){
155
156      float x=DM*i;
157      float y=DM*j;
158
159      float magnitude = sqrt(x*x+y*y)/1.414;
160     
161      //from green to red
162      GLubyte r = floor(magnitude*255);
163      GLubyte g = 0;
164      GLubyte b = 255 - r;
165      GLubyte a = 122;
166
167      mag[i][j][0] = r;
168      mag[i][j][1] = g;
169      mag[i][j][2] = b;
170      mag[i][j][3] = a;
171    }
172  }
173
174  glGenTextures(1, &mag_tex);
175  glBindTexture(GL_TEXTURE_2D, mag_tex);
176  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
177  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
178  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
180  //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
181  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, NMESH, NMESH, 0, GL_RGBA, GL_UNSIGNED_BYTE, mag);
182
183}
184
185//project 3D vectors to a 2D slice for line integral convolution
186void Lic::get_slice(){
187  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, vel_fbo);
188  glBindTexture(GL_TEXTURE_RECTANGLE_NV, slice_vector_tex);
189
190  glClear(GL_COLOR_BUFFER_BIT);
191
192  glViewport(0, 0, NMESH, NMESH);
193  glMatrixMode(GL_PROJECTION);
194  glLoadIdentity();
195  gluOrtho2D(0, NMESH, 0, NMESH);
196  glMatrixMode(GL_MODELVIEW);
197  glLoadIdentity();
198
199  cgGLBindProgram(m_render_vel_fprog);
200  cgGLEnableTextureParameter(m_vel_tex_param_render_vel);
201  cgGLSetParameter4f(m_plane_normal_param_render_vel, 1., 1., 0., 0);
202
203  cgGLEnableProfile(CG_PROFILE_FP30);
204  glBegin(GL_QUADS);
205    glTexCoord3f(0., 0., offset); glVertex2f(0.,   0.);
206    glTexCoord3f(1., 0., offset); glVertex2f(size, 0.);
207    glTexCoord3f(1., 1., offset); glVertex2f(size, size);
208    glTexCoord3f(0., 1., offset); glVertex2f(0.,   size);
209  glEnd();
210  cgGLDisableProfile(CG_PROFILE_FP30);
211   
212  cgGLDisableTextureParameter(m_vel_tex_param_render_vel);
213
214  //read the vectors
215  glReadPixels(0, 0, NMESH, NMESH, GL_RGBA, GL_FLOAT, slice_vector);
216  /*
217  for(int i=0; i<NMESH*NMESH; i++){
218    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]);
219  }
220  */
221  assert(glGetError()==0);
222}
223
224
225//line integral convolution
226void Lic::convolve(){
227
228   int   i, j;
229   float x1, x2, y, px, py;
230
231   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
232
233   glViewport(0, 0, (GLsizei) NPIX, (GLsizei) NPIX);
234   glMatrixMode(GL_PROJECTION);
235   glLoadIdentity();
236   glTranslatef(-1.0, -1.0, 0.0);
237   glScalef(2.0, 2.0, 1.0);
238
239   //sa = 0.010*cos(iframe*2.0*M_PI/200.0);
240   glBindTexture(GL_TEXTURE_2D, pattern_tex);
241   glEnable(GL_TEXTURE_2D);
242   sa = 0.01;
243   for (i = 0; i < NMESH-1; i++) {
244      x1 = DM*i; x2 = x1 + DM;
245      glBegin(GL_QUAD_STRIP);
246      for (j = 0; j < NMESH-1; j++) {
247          y = DM*j;
248          glTexCoord2f(x1, y);
249          get_velocity(x1, y, &px, &py);
250          glVertex2f(px, py);
251
252          glTexCoord2f(x2, y);
253          get_velocity(x2, y, &px, &py);
254          glVertex2f(px, py);
255      }
256      glEnd();
257   }
258   iframe = iframe + 1;
259
260   glEnable(GL_BLEND);
261   glCallList(iframe % Npat + 1);
262   glBegin(GL_QUAD_STRIP);
263      glTexCoord2f(0.0,  0.0);  glVertex2f(0.0, 0.0);
264      glTexCoord2f(0.0,  tmax); glVertex2f(0.0, 1.0);
265      glTexCoord2f(tmax, 0.0);  glVertex2f(1.0, 0.0);
266      glTexCoord2f(tmax, tmax); glVertex2f(1.0, 1.0);
267   glEnd();
268
269   /*
270   //inject dye
271   glDisable(GL_TEXTURE_2D);
272   glColor4f(1.,0.8,0.,1.);
273   glBegin(GL_QUADS);
274     glVertex2d(0.6, 0.6);
275     glVertex2d(0.6, 0.62);
276     glVertex2d(0.62, 0.62);
277     glVertex2d(0.62, 0.6);
278   glEnd();
279   */
280
281   glDisable(GL_BLEND);
282   glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, NPIX, NPIX, 0);
283   
284   /*
285   //blend magnitude texture
286   glBindTexture(GL_TEXTURE_2D, mag_tex);
287   glEnable(GL_TEXTURE_2D);
288   glEnable(GL_BLEND);
289   glBegin(GL_QUADS);
290      glTexCoord2f(0.0,  0.0);  glVertex2f(0.0, 0.0);
291      glTexCoord2f(0.0,  1.0); glVertex2f(0.0, 1.);
292      glTexCoord2f(1.0, 1.0);  glVertex2f(1., 1.);
293      glTexCoord2f(1.0, 0.0); glVertex2f(1., 0.0);
294   glEnd();
295   */
296}
297
298void Lic::render(){ display(); }
299
300
301void Lic::display(){
302
303  glBindTexture(GL_TEXTURE_2D, color_tex);
304  glEnable(GL_TEXTURE_2D);
305
306  //draw line integral convolution quad
307  glEnable(GL_DEPTH_TEST);
308  glPushMatrix();
309  glScalef(scale.x, scale.y, scale.z);
310
311  glBegin(GL_QUADS);
312    glTexCoord2f(0, 0); glVertex3f(0, 0, offset);
313    glTexCoord2f(1, 0); glVertex3f(1, 0, offset);
314    glTexCoord2f(1, 1); glVertex3f(1, 1, offset);
315    glTexCoord2f(0, 1); glVertex3f(0, 1, offset);
316  glEnd();
317
318  glPopMatrix();
319
320  glDisable(GL_DEPTH_TEST);
321  glDisable(GL_TEXTURE_2D);
322}
323
324
325void Lic::get_velocity(float x, float y, float *px, float *py)
326{
327   float dx, dy, vx, vy, r;
328
329   int xi = x*NMESH;
330   int yi = y*NMESH;
331
332   vx = slice_vector[4*(xi+yi*NMESH)];
333   vy = slice_vector[4*(xi+yi*NMESH)+1];
334   r  = vx*vx + vy*vy;
335   if (r > dmax*dmax) {
336      r  = sqrt(r);
337      vx *= dmax/r;
338      vy *= dmax/r;
339   }
340   *px = x + vx;         
341   *py = y + vy;
342}
343
344void Lic::set_offset(float v)
345{
346  offset = v;
347  get_slice();
348}
Note: See TracBrowser for help on using the repository browser.