source: trunk/packages/vizservers/nanovis/ParticleSystem.h @ 2852

Last change on this file since 2852 was 2818, checked in by ldelgass, 12 years ago

Fix spelling of some identifiers, formatting fixes

File size: 9.2 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#ifndef PARTICLESYSTEM_H
3#define PARTICLESYSTEM_H
4
5#include <vector>
6#include <queue>
7#include <string>
8#include <algorithm>
9#include <limits>
10
11#include <Cg/cg.h>
12
13#include <vr3d/vrTexture2D.h>
14#include <vr3d/vrTexture3D.h>
15#include <vrmath/vrVector3f.h>
16
17#include "ParticleEmitter.h"
18#include "RenderVertexArray.h"
19
20#include "CircularQueue.h"
21
22struct NewParticle {
23    int index;
24    vrVector3f position;
25    vrVector3f velocity;
26    float timeOfDeath;
27    float initTimeStep;
28};
29
30template <typename Type, typename Compare=std::less<Type> >
31class priority_queue_vector : public std::priority_queue<Type, std::vector<Type>, Compare>
32{
33public:
34    void reserve(typename priority_queue_vector<Type>::size_type count)
35    {
36        //priority_queue<Type, std::vector<Type>, Compare>::reserve(count);
37    }
38};
39
40struct ActiveParticle {
41    float timeOfDeath;
42    int index;
43};
44
45namespace std
46{
47    template <>
48    struct greater<ActiveParticle> {
49        bool operator() (const ActiveParticle& left, const ActiveParticle& right)
50        {
51            return left.timeOfDeath > right.timeOfDeath;
52        }
53    };
54}
55
56struct color4 {
57    float r, g, b, a;
58};
59
60struct Particle {
61    float timeOfBirth;
62    float lifeTime;
63    float attributeType;
64    float index;
65};
66
67class ParticleSystem
68{
69public:
70    enum EnableEnum {
71        PS_SORT = 1,
72        PS_GLYPH = 2,
73        PS_DRAW_BBOX = 4,
74        PS_ADVECTION = 8,
75        PS_STREAMLINE = 16,
76    };
77
78    const int _width;
79    const int _height;
80    /**
81     * @brief The total number of particles (= _width * _height)
82     */
83    int _particleMaxCount;
84    /**
85     * @brief The total number of particles avaiable (1 <= _userDefinedParticleMaxCount <= _width * _height)
86     */
87    unsigned int _userDefinedParticleMaxCount;
88
89    int _currentSortPass;
90    int _maxSortPasses;
91    int _sortPassesPerFrame;
92
93    int _sortBegin;
94    int _sortEnd;
95
96    float _pointSize;
97
98    /**
99     * @brief The index for the source render target
100     */
101    int _currentSortIndex;
102
103    /**
104     * @brief The index for the destination render target
105     */
106    int _destSortIndex;
107
108    int _currentPosIndex;
109    int _destPosIndex;
110
111    std::vector<ParticleEmitter *> _emitters;
112
113    float _currentTime;
114
115    priority_queue_vector<int, std::greater<int> > _availableIndices;
116    priority_queue_vector<ActiveParticle, std::greater<ActiveParticle> > _activeParticles;
117    std::vector<NewParticle> _newParticles;
118
119    /**
120     * @brief frame buffer objects: two are defined, flip them as input output every step
121     */
122    unsigned int psys_fbo[2];   
123
124    /**
125     * @brief color textures attached to frame buffer objects
126     */
127    unsigned int psys_tex[2];   
128
129    unsigned int sort_fbo[2];
130    unsigned int sort_tex[2];
131
132    unsigned int velocity_fbo[2];
133    unsigned int velocity_tex[2];
134
135    // TEMP
136    ///////////////////////////////////////////////////
137    // TIME SERIES
138    std::vector<unsigned int> _vectorFieldIDs;
139    std::vector<vrTexture3D *> _vectorFields;
140    unsigned int _curVectorFieldID;
141    float _time_series_vel_mag_min;
142    float _time_series_vel_mag_max;
143    CircularFifo<float *, 10> _queue;
144    int _flowFileStartIndex;
145    int _flowFileEndIndex;
146    ///////////////////////////////////////////////////
147    int _skipByte;
148
149    float _maxVelocityScale;
150       
151    RenderVertexArray* _vertices;
152
153    Particle *_particles;
154    vrVector3f *_positionBuffer;
155    color4 *_colorBuffer;
156    unsigned _colorBufferID;
157    //////////////////////////////////////////
158    vrTexture2D *_arrows;
159
160    float _camx;
161    float _camy;
162    float _camz;
163
164    float _scalex, _scaley, _scalez;
165
166    bool _sortEnabled;
167    bool _glyphEnabled;
168    bool _drawBBoxEnabled;
169    bool _advectionEnabled;
170    bool _streamlineEnabled;
171    bool _depthCueEnabled;
172
173    // TEMP
174    static void callbackForCgError();
175    static CGcontext _context;
176
177    CGprogram _distanceInitFP;
178
179    CGprogram _distanceSortFP;
180    CGparameter _viewPosParam;
181
182    CGprogram _distanceSortLookupFP;
183
184    CGprogram _passthroughFP;
185    CGparameter _scaleParam;
186    CGparameter _biasParam;
187
188    CGprogram _sortRecursionFP;
189    CGparameter _srSizeParam;
190    CGparameter _srStepParam;
191    CGparameter _srCountParam;
192
193    CGprogram _sortEndFP;
194    CGparameter _seSizeParam;
195    CGparameter _seStepParam;
196
197    CGprogram _moveParticlesFP;
198    CGparameter _mpTimeScale;
199    CGparameter _mpVectorField;
200    CGparameter _mpUseInitTex;
201    CGparameter _mpCurrentTime;
202    CGparameter _mpMaxScale;
203    CGparameter _mpScale;
204
205    CGprogram _initParticlePosFP;
206    CGparameter _ipVectorFieldParam;
207
208    CGprogram _particleVP;
209    CGparameter _mvpParticleParam;
210    CGparameter _mvParticleParam;
211    CGparameter _mvTanHalfFOVParam;
212    CGparameter _mvCurrentTimeParam;
213       
214    CGprogram _particleFP;
215    CGparameter _vectorParticleParam;
216
217    //////////////////////////////////////////
218    // STREAMLINE
219    unsigned int _maxAtlasCount;
220    unsigned int _currentStreamlineIndex;
221    int _currentStreamlineOffset;
222    int _particleCount;
223    int _stepSizeStreamline;
224    int _curStepCount;
225    int _atlasWidth, _atlasHeight;
226    unsigned int _atlas_fbo;
227    unsigned int _atlas_tex;
228    RenderVertexArray *_streamVertices;
229    unsigned int _atlasIndexBufferID;
230
231    unsigned int _maxIndexBufferSize;
232    unsigned int *_indexBuffer;
233    int _atlas_x, _atlas_y;
234
235    //////////////////////////////////////////
236    // Initial Position
237    unsigned int _initPos_fbo;
238    unsigned int _initPosTex;
239
240    /////////////////////////////////////////
241    // Particle Info Vertex Buffer
242    unsigned int _particleInfoBufferID;
243
244    int _screenWidth;
245    int _screenHeight;
246    float _fov;
247
248    bool _isTimeVaryingField;
249
250    int _flowWidth;
251    int _flowHeight;
252    int _flowDepth;
253
254    std::string _fileNameFormat;
255
256    // INSOO
257    // TEST
258    std::vector<vrVector3f> *_criticalPoints;
259
260    static void *dataLoadMain(void *data);
261
262    ParticleSystem(int width, int height, const std::string& fileName,
263                   int fieldWidth, int fieldHeight, int fieldDepth,
264                   bool timeVaryingData, int flowFileStartIndex, int flowFileEndIndex);
265    ~ParticleSystem();
266    void createRenderTargets();
267    void createSortRenderTargets();
268    void createStreamlineRenderTargets();
269    void advectStreamlines();
270    void renderStreamlines();
271
272    bool advect(float deltaT, float camx, float camy, float camz);
273    void enable(EnableEnum enabled);
274    void disable(EnableEnum enabled);
275    bool isEnabled(EnableEnum enabled);
276
277    ////////////////////////////////////////////
278    void addEmitter(ParticleEmitter *emitter);
279    ParticleEmitter *getEmitter(int index);
280    unsigned int getNumEmitters() const;
281    void removeEmitter(int index);
282    void removeAllEmitters();
283
284    int getMaxSize() const;
285
286    void render();
287
288    void reset();
289
290    void setDefaultPointSize(float size);
291
292    unsigned int getNumOfActiveParticles() const;
293
294    void setScreenSize(int sreenWidth, int screenHeight);
295
296    void setFOV(float fov);
297
298    void setUserDefinedNumOfParticles(int numParticles);
299
300    float getScaleX()const;
301    float getScaleY()const;
302    float getScaleZ()const;
303    unsigned int getVectorFieldGraphicsID() const;
304
305    bool isTimeVaryingField() const;
306    void setTimeVaryingField(bool timeVarying);
307
308    void setDepthCueEnabled(bool enabled);
309    bool getDepthCueEnabled() const;
310
311protected:
312    void mergeSort(int count);
313    void merge(int count, int step);
314    void sort();
315    void drawQuad();
316    void drawUnitBox();
317    void drawQuad(int x1, int y1, int x2, int y2);
318    void initShaders();
319    void passThoughPositions();
320    void initStreamlines(int width, int height);
321    void resetStreamlines();
322    void initInitPosTex();
323
324    void allocateParticle(const vrVector3f&, const vrVector3f&, float, float);
325    void initNewParticles();
326    void cleanUpParticles();
327};
328
329inline unsigned int ParticleSystem::getNumEmitters() const
330{
331    return _emitters.size();
332}
333
334inline int ParticleSystem::getMaxSize() const
335{
336    return _width * _height;
337}
338
339inline void ParticleSystem::setDefaultPointSize(float size)
340{
341    _pointSize = size;
342}
343
344inline unsigned int ParticleSystem::getNumOfActiveParticles() const
345{
346    return _activeParticles.size();
347}
348
349inline ParticleEmitter* ParticleSystem::getEmitter(int index)
350{
351    if ((unsigned int) index < _emitters.size())
352        return _emitters[index];
353    return 0;
354}
355
356inline void ParticleSystem::setScreenSize(int screenWidth, int screenHeight)
357{
358    _screenWidth = screenWidth;
359    _screenHeight = screenHeight;
360}
361
362inline void ParticleSystem::setFOV(float fov)
363{
364    _fov = fov;
365}
366
367inline float ParticleSystem::getScaleX()const
368{
369    return _scalex;
370}
371
372inline float ParticleSystem::getScaleY()const
373{
374    return _scaley;
375}
376
377inline float ParticleSystem::getScaleZ()const
378{
379    return _scalez;
380}
381
382inline unsigned int ParticleSystem::getVectorFieldGraphicsID()const
383{
384    return _curVectorFieldID;
385}
386
387inline bool ParticleSystem::isTimeVaryingField() const
388{
389    return _isTimeVaryingField;
390}
391
392inline void ParticleSystem::setTimeVaryingField(bool timeVarying)
393{
394    _isTimeVaryingField = timeVarying;
395}
396
397inline void ParticleSystem::setDepthCueEnabled(bool enabled)
398{
399    _depthCueEnabled = enabled;
400}
401
402inline bool ParticleSystem::getDepthCueEnabled() const
403{
404    return _depthCueEnabled;
405}
406
407#endif
Note: See TracBrowser for help on using the repository browser.