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

Last change on this file since 3464 was 2860, checked in by ldelgass, 12 years ago

Put class member declarations in standard order

File size: 9.1 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 <vrmath/vrVector3f.h>
14
15#include "ParticleEmitter.h"
16#include "RenderVertexArray.h"
17#include "Texture2D.h"
18#include "Texture3D.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    ParticleSystem(int width, int height, const std::string& fileName,
79                   int fieldWidth, int fieldHeight, int fieldDepth,
80                   bool timeVaryingData, int flowFileStartIndex, int flowFileEndIndex);
81    ~ParticleSystem();
82    void createRenderTargets();
83    void createSortRenderTargets();
84    void createStreamlineRenderTargets();
85    void advectStreamlines();
86    void renderStreamlines();
87
88    bool advect(float deltaT, float camx, float camy, float camz);
89    void enable(EnableEnum enabled);
90    void disable(EnableEnum enabled);
91    bool isEnabled(EnableEnum enabled);
92
93    ////////////////////////////////////////////
94    void addEmitter(ParticleEmitter *emitter);
95    ParticleEmitter *getEmitter(int index);
96    unsigned int getNumEmitters() const;
97    void removeEmitter(int index);
98    void removeAllEmitters();
99
100    int getMaxSize() const;
101
102    void render();
103
104    void reset();
105
106    void setDefaultPointSize(float size);
107
108    unsigned int getNumOfActiveParticles() const;
109
110    void setScreenSize(int sreenWidth, int screenHeight);
111
112    void setFOV(float fov);
113
114    void setUserDefinedNumOfParticles(int numParticles);
115
116    float getScaleX() const;
117    float getScaleY() const;
118    float getScaleZ() const;
119    unsigned int getVectorFieldGraphicsID() const;
120
121    bool isTimeVaryingField() const;
122    void setTimeVaryingField(bool timeVarying);
123
124    void setDepthCueEnabled(bool enabled);
125    bool getDepthCueEnabled() const;
126
127    static void *dataLoadMain(void *data);
128    // TEMP
129    static void callbackForCgError();
130
131    const int _width;
132    const int _height;
133    /**
134     * @brief The total number of particles (= _width * _height)
135     */
136    int _particleMaxCount;
137    /**
138     * @brief The total number of particles avaiable (1 <= _userDefinedParticleMaxCount <= _width * _height)
139     */
140    unsigned int _userDefinedParticleMaxCount;
141
142    int _currentSortPass;
143    int _maxSortPasses;
144    int _sortPassesPerFrame;
145
146    int _sortBegin;
147    int _sortEnd;
148
149    float _pointSize;
150
151    /**
152     * @brief The index for the source render target
153     */
154    int _currentSortIndex;
155
156    /**
157     * @brief The index for the destination render target
158     */
159    int _destSortIndex;
160
161    int _currentPosIndex;
162    int _destPosIndex;
163
164    std::vector<ParticleEmitter *> _emitters;
165
166    float _currentTime;
167
168    priority_queue_vector<int, std::greater<int> > _availableIndices;
169    priority_queue_vector<ActiveParticle, std::greater<ActiveParticle> > _activeParticles;
170    std::vector<NewParticle> _newParticles;
171
172    /**
173     * @brief frame buffer objects: two are defined, flip them as input output every step
174     */
175    unsigned int psys_fbo[2];   
176
177    /**
178     * @brief color textures attached to frame buffer objects
179     */
180    unsigned int psys_tex[2];   
181
182    unsigned int sort_fbo[2];
183    unsigned int sort_tex[2];
184
185    unsigned int velocity_fbo[2];
186    unsigned int velocity_tex[2];
187
188    ///////////////////////////////////////////////////
189    // TIME SERIES
190    std::vector<unsigned int> _vectorFieldIDs;
191    std::vector<Texture3D *> _vectorFields;
192    unsigned int _curVectorFieldID;
193    float _time_series_vel_mag_min;
194    float _time_series_vel_mag_max;
195    CircularFifo<float *, 10> _queue;
196    int _flowFileStartIndex;
197    int _flowFileEndIndex;
198    ///////////////////////////////////////////////////
199    int _skipByte;
200
201    float _maxVelocityScale;
202       
203    RenderVertexArray* _vertices;
204
205    Particle *_particles;
206    vrVector3f *_positionBuffer;
207    color4 *_colorBuffer;
208    unsigned _colorBufferID;
209    //////////////////////////////////////////
210    Texture2D *_arrows;
211
212    float _camx;
213    float _camy;
214    float _camz;
215
216    float _scalex, _scaley, _scalez;
217
218    bool _sortEnabled;
219    bool _glyphEnabled;
220    bool _drawBBoxEnabled;
221    bool _advectionEnabled;
222    bool _streamlineEnabled;
223    bool _depthCueEnabled;
224
225    CGprogram _distanceInitFP;
226
227    CGprogram _distanceSortFP;
228    CGparameter _viewPosParam;
229
230    CGprogram _distanceSortLookupFP;
231
232    CGprogram _passthroughFP;
233    CGparameter _scaleParam;
234    CGparameter _biasParam;
235
236    CGprogram _sortRecursionFP;
237    CGparameter _srSizeParam;
238    CGparameter _srStepParam;
239    CGparameter _srCountParam;
240
241    CGprogram _sortEndFP;
242    CGparameter _seSizeParam;
243    CGparameter _seStepParam;
244
245    CGprogram _moveParticlesFP;
246    CGparameter _mpTimeScale;
247    CGparameter _mpVectorField;
248    CGparameter _mpUseInitTex;
249    CGparameter _mpCurrentTime;
250    CGparameter _mpMaxScale;
251    CGparameter _mpScale;
252
253    CGprogram _initParticlePosFP;
254    CGparameter _ipVectorFieldParam;
255
256    CGprogram _particleVP;
257    CGparameter _mvpParticleParam;
258    CGparameter _mvParticleParam;
259    CGparameter _mvTanHalfFOVParam;
260    CGparameter _mvCurrentTimeParam;
261       
262    CGprogram _particleFP;
263    CGparameter _vectorParticleParam;
264
265    //////////////////////////////////////////
266    // STREAMLINE
267    unsigned int _maxAtlasCount;
268    unsigned int _currentStreamlineIndex;
269    int _currentStreamlineOffset;
270    int _particleCount;
271    int _stepSizeStreamline;
272    int _curStepCount;
273    int _atlasWidth, _atlasHeight;
274    unsigned int _atlas_fbo;
275    unsigned int _atlas_tex;
276    RenderVertexArray *_streamVertices;
277    unsigned int _atlasIndexBufferID;
278
279    unsigned int _maxIndexBufferSize;
280    unsigned int *_indexBuffer;
281    int _atlas_x, _atlas_y;
282
283    //////////////////////////////////////////
284    // Initial Position
285    unsigned int _initPos_fbo;
286    unsigned int _initPosTex;
287
288    /////////////////////////////////////////
289    // Particle Info Vertex Buffer
290    unsigned int _particleInfoBufferID;
291
292    int _screenWidth;
293    int _screenHeight;
294    float _fov;
295
296    bool _isTimeVaryingField;
297
298    int _flowWidth;
299    int _flowHeight;
300    int _flowDepth;
301
302    std::string _fileNameFormat;
303
304    // INSOO
305    // TEST
306    std::vector<vrVector3f> *_criticalPoints;
307
308    // TEMP
309    static CGcontext _context;
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.