source: trunk/vizservers/nanovis/NvParticleRenderer.cpp @ 851

Last change on this file since 851 was 851, checked in by vrinside, 13 years ago

Got particle system and LIC (Line integral convolution) run.

File size: 8.6 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 * NvParticleRenderer.cpp: particle system 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#include <stdio.h>
17#include <assert.h>
18#include <malloc.h>
19#include <string.h>
20
21#include <R2/R2FilePath.h>
22#include "NvParticleRenderer.h"
23#include <Trace.h>
24
25
26#define NV_32
27
28NvParticleRenderer::NvParticleRenderer(int w, int h, CGcontext context)
29: scale(1, 1, 1), _activate(false)
30{
31  psys_width = w;
32  psys_height = h;
33
34  psys_frame = 0;
35  reborn = true;
36  flip = true;
37  max_life = 500;
38
39  data = (Particle*) malloc(w*h*sizeof(Particle));
40
41  m_vertex_array = new RenderVertexArray(psys_width*psys_height, 3, GL_FLOAT);
42
43  assert(glGetError()==0);
44
45  glGenFramebuffersEXT(2, psys_fbo);
46  glGenTextures(2, psys_tex);
47
48  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[0]);
49
50  glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
51  glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
52  glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
53
54#ifdef NV_32
55  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
56#else
57  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGBA, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
58#endif
59  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, psys_tex[0], 0);
60
61  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[1]);
62
63  glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[1]);
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#ifdef NV_32
67  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
68#else
69  glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGBA, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, NULL);
70#endif
71  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, psys_tex[1], 0);
72
73  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
74
75  CHECK_FRAMEBUFFER_STATUS();
76
77  //load related shaders
78  /*
79  m_g_context = context;
80
81  R2string path = R2FilePath::getInstance()->getPath("update_pos.cg");
82  m_pos_fprog = loadProgram(m_g_context, CG_PROFILE_FP30, CG_SOURCE, path);
83  m_pos_timestep_param  = cgGetNamedParameter(m_pos_fprog, "timestep");
84  m_vel_tex_param = cgGetNamedParameter(m_pos_fprog, "vel_tex");
85  m_pos_tex_param = cgGetNamedParameter(m_pos_fprog, "pos_tex");
86  m_scale_param = cgGetNamedParameter(m_pos_fprog, "scale");
87  cgGLSetTextureParameter(m_vel_tex_param, volume);
88  cgGLSetParameter3f(m_scale_param, scale_x, scale_y, scale_z);
89  */
90
91  _advectionShader = new NvParticleAdvectionShader();
92
93}
94
95NvParticleRenderer::~NvParticleRenderer()
96{
97    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[0]);
98    glDeleteTextures(1, psys_tex);
99
100    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[1]);
101    glDeleteTextures(1, psys_tex+1);
102
103    glDeleteFramebuffersEXT(2, psys_fbo);
104
105    free(data);
106}
107
108void NvParticleRenderer::initialize(Particle* p)
109{
110    //also store the data on main memory for next initialization
111    memcpy(data, p, psys_width*psys_height*sizeof(Particle));
112
113    glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
114    // I need to find out why GL_FLOAT_RGBA32_NV doesn't work
115#ifdef NV_32
116    glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)p);
117#else
118    glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGBA, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)p);
119#endif
120    glBindTexture(GL_TEXTURE_RECTANGLE_NV, 0);
121 
122    flip = true;
123    reborn = false;
124
125    //fprintf(stderr, "init particles\n");
126}
127
128void NvParticleRenderer::reset()
129{
130    glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
131
132#ifdef NV_32
133    glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)data);
134#else
135    glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGBA, psys_width, psys_height, 0, GL_RGBA, GL_FLOAT, (float*)data);#
136#endif
137    glBindTexture(GL_TEXTURE_RECTANGLE_NV, 0);
138 
139    flip = true;
140    reborn = false;
141    psys_frame = 0;
142}
143
144void NvParticleRenderer::advect()
145{
146    if (reborn)
147        reset();
148
149    glDisable(GL_BLEND);
150    glDisable(GL_DEPTH_TEST);
151   
152    if (flip)
153    {
154        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[1]);
155        glEnable(GL_TEXTURE_RECTANGLE_NV);
156        glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[0]);
157
158        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159        //glClear(GL_COLOR_BUFFER_BIT);
160
161        glViewport(0, 0, psys_width, psys_height);
162        glMatrixMode(GL_PROJECTION);
163        glLoadIdentity();
164        //gluOrtho2D(0, psys_width, 0, psys_height);
165        glOrtho(0, psys_width, 0, psys_height, -10.0f, 10.0f);
166        glMatrixMode(GL_MODELVIEW);
167        glLoadIdentity();
168
169        _advectionShader->bind(psys_tex[0]);
170     
171     //cgGLBindProgram(m_pos_fprog);
172     //cgGLSetParameter1f(m_pos_timestep_param, 0.05);
173     //cgGLEnableTextureParameter(m_vel_tex_param);
174     //cgGLSetTextureParameter(m_pos_tex_param, psys_tex[0]);
175     //cgGLEnableTextureParameter(m_pos_tex_param);
176     //cgGLEnableProfile(CG_PROFILE_FP30);
177     
178     draw_quad(psys_width, psys_height, psys_width, psys_height);
179
180     //cgGLDisableProfile(CG_PROFILE_FP30);
181     //cgGLDisableTextureParameter(m_vel_tex_param);
182     //cgGLDisableTextureParameter(m_pos_tex_param);
183        glDisable(GL_TEXTURE_RECTANGLE_NV);
184   }
185   else
186   {
187        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, psys_fbo[0]);
188        glBindTexture(GL_TEXTURE_RECTANGLE_NV, psys_tex[1]);
189
190        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
191        //glClear(GL_COLOR_BUFFER_BIT);
192
193        glViewport(0, 0, psys_width, psys_height);
194        glMatrixMode(GL_PROJECTION);
195        glLoadIdentity();
196        //gluOrtho2D(0, psys_width, 0, psys_height);
197        glOrtho(0, psys_width, 0, psys_height, -10.0f, 10.0f);
198        glMatrixMode(GL_MODELVIEW);
199        glLoadIdentity();
200
201        _advectionShader->bind(psys_tex[1]);
202
203     //cgGLBindProgram(m_pos_fprog);
204     //cgGLSetParameter1f(m_pos_timestep_param, 0.05);
205     //cgGLEnableTextureParameter(m_vel_tex_param);
206     //cgGLSetTextureParameter(m_pos_tex_param, psys_tex[1]);
207     //cgGLEnableTextureParameter(m_pos_tex_param);
208     //cgGLEnableProfile(CG_PROFILE_FP30);
209
210        draw_quad(psys_width, psys_height, psys_width, psys_height);
211        //draw_quad(psys_width, psys_height, 1.0f, 1.0f);
212
213     //cgGLDisableProfile(CG_PROFILE_FP30);
214     //cgGLDisableTextureParameter(m_vel_tex_param);
215     //cgGLDisableTextureParameter(m_pos_tex_param);
216    }
217
218    _advectionShader->unbind();
219
220   //soft_read_verts();
221   
222   update_vertex_buffer();
223
224    flip = (!flip);
225
226    psys_frame++;
227    if(psys_frame==max_life)
228    {
229        psys_frame=0;
230        reborn = true;
231    }
232
233    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
234}
235
236void NvParticleRenderer::update_vertex_buffer()
237{
238    m_vertex_array->Read(psys_width, psys_height);
239
240    //m_vertex_array->LoadData(vert);     //does not work??
241    //assert(glGetError()==0);
242}
243
244void NvParticleRenderer::render()
245{
246    display_vertices();
247}
248
249void NvParticleRenderer::display_vertices()
250{
251    glDisable(GL_TEXTURE_2D);
252    glDisable(GL_BLEND);
253    glEnable(GL_DEPTH_TEST);
254
255    glEnable(GL_COLOR_MATERIAL);
256
257    glPointSize(1.2);
258    glColor4f(.2,.2,.8,1.);
259 
260    glPushMatrix();
261
262    glScaled(scale.x, scale.y, scale.z);
263
264    glEnableClientState(GL_VERTEX_ARRAY);
265    m_vertex_array->SetPointer(0);
266    glDrawArrays(GL_POINTS, 0, psys_width*psys_height);
267    glDisableClientState(GL_VERTEX_ARRAY);
268
269    glPopMatrix();
270 
271    glDisable(GL_DEPTH_TEST);
272
273    //assert(glGetError()==0);
274}
275
276void NvParticleRenderer::setVectorField(unsigned int texID, float scaleX, float scaleY, float scaleZ, float max)
277{
278    scale.set(scaleX, scaleY, scaleZ);
279  _advectionShader->setScale(scale);
280  _advectionShader->setVelocityVolume(texID, max);
281}
Note: See TracBrowser for help on using the repository browser.