source: branches/blt4/packages/vizservers/nanovis/ParticleSystem.h @ 2742

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