source: trunk/packages/vizservers/nanovis/NvParticleRenderer.cpp @ 1028

Last change on this file since 1028 was 1028, checked in by gah, 13 years ago

various cleanups

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