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

Last change on this file since 3613 was 3613, checked in by ldelgass, 11 years ago

Include namespace in header guard defines

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