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

Last change on this file since 2812 was 2803, checked in by ldelgass, 12 years ago

last commit didn't fix glew error, this one should

File size: 9.1 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#pragma once
3
4#include <vector>
5#include <queue>
6#include <string>
7#include <algorithm>
8#include <limits>
9
10#include <Cg/cg.h>
11
12#include <vr3d/vrTexture2D.h>
13#include <vr3d/vrTexture3D.h>
14
15#include "ParticleEmitter.h"
16#include "RenderVertexArray.h"
17
18#include "CircularQueue.h"
19#include "datatype.h"
20
21struct NewParticle {
22    int index;
23    float3 position;
24    float3 velocity;
25    float timeOfDeath;
26    float initTimeStep;
27};
28
29template <typename Type, typename Compare=std::less<Type> >
30class priority_queue_vector : public std::priority_queue<Type, std::vector<Type>, Compare>
31{
32public:
33    void reserve(typename priority_queue_vector<Type>::size_type count)
34    {
35#ifdef _WIN32
36        c.reserve(count);
37#else
38        //priority_queue<Type,vector<Type>, Compare>::reserve(count);
39#endif
40    }
41};
42
43struct ActiveParticle {
44    float timeOfDeath;
45    int index;
46};
47
48namespace std
49{
50    template <>
51    struct greater<ActiveParticle> {
52        bool operator() (const ActiveParticle& left, const ActiveParticle& right)
53        {
54            return left.timeOfDeath > right.timeOfDeath;
55        }
56    };
57}
58
59struct Particle {
60    float timeOfBirth;
61    float lifeTime;
62    float attributeType;
63    float index;
64};
65
66class ParticleSystem
67{
68public:
69    enum EnableEnum {
70        PS_SORT = 1,
71        PS_GLYPE = 2,
72        PS_DRAW_BBOX = 4,
73        PS_ADVECTION = 8,
74        PS_STREAMLINE = 16,
75    };
76
77    const int _width;
78    const int _height;
79    /**
80     * @brief The total number of particles (= _width * _height)
81     */
82    int _particleMaxCount;
83    /**
84     * @brief The total number of particles avaiable (1 <= _userDefinedParticleMaxCount <= _width * _height)
85     */
86    unsigned int _userDefinedParticleMaxCount;
87
88    int _currentSortPass;
89    int _maxSortPasses;
90    int _sortPassesPerFrame;
91
92    int _sortBegin;
93    int _sortEnd;
94
95    float _pointSize;
96
97    /**
98     * @brief The index for the source render target
99     */
100    int _currentSortIndex;
101
102    /**
103     * @brief The index for the destination render target
104     */
105    int _destSortIndex;
106
107    int _currentPosIndex;
108    int _destPosIndex;
109
110    std::vector<ParticleEmitter*> _emitters;
111
112    float _currentTime;
113
114    priority_queue_vector<int, std::greater<int> > _availableIndices;
115    priority_queue_vector<ActiveParticle, std::greater<ActiveParticle> > _activeParticles;
116    std::vector<NewParticle> _newParticles;
117
118    /**
119     * @brief frame buffer objects: two are defined, flip them as input output every step
120     */
121    unsigned int psys_fbo[2];   
122
123    /**
124     * @brief color textures attached to frame buffer objects
125     */
126    unsigned int psys_tex[2];   
127
128    unsigned int sort_fbo[2];
129    unsigned int sort_tex[2];
130
131    unsigned int velocity_fbo[2];
132    unsigned int velocity_tex[2];
133
134    // TEMP
135    ///////////////////////////////////////////////////
136    // TIME SERIES
137    std::vector<unsigned int> _vectorFieldIDs;
138    std::vector<vrTexture3D*> _vectorFields;
139    unsigned int _curVectorFieldID;
140    float _time_series_vel_mag_min;
141    float _time_series_vel_mag_max;
142    CircularFifo<float*, 10> _queue;
143    int _flowFileStartIndex;
144    int _flowFileEndIndex;
145    ///////////////////////////////////////////////////
146    int _skipByte;
147
148    float _maxVelocityScale;
149       
150    RenderVertexArray* _vertices;
151
152    Particle* _particles;
153    float3* _positionBuffer;
154    color4* _colorBuffer;
155    unsigned _colorBufferID;
156
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 _glypEnabled;
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
270    void advectStreamlines();
271    void renderStreamlines();
272
273    bool advect(float deltaT, float camx, float camy, float camz);
274    void enable(EnableEnum enabled);
275    void disable(EnableEnum enabled);
276    bool isEnabled(EnableEnum enabled);
277
278    ////////////////////////////////////////////
279    void addEmitter(ParticleEmitter* emitter);
280    ParticleEmitter* getEmitter(int index);
281    unsigned int getNumEmitters() const;
282    void removeEmitter(int index);
283    void removeAllEmitters();
284
285    int getMaxSize() const;
286
287    void render();
288
289    void reset();
290
291    void setDefaultPointSize(float size);
292
293    unsigned int getNumOfActiveParticles() const;
294
295    void setScreenSize(int sreenWidth, int screenHeight);
296
297    void setFOV(float fov);
298
299    void setUserDefinedNumOfParticles(int numParticles);
300
301    float getScaleX()const;
302    float getScaleY()const;
303    float getScaleZ()const;
304    unsigned int getVectorFieldGraphicsID() const;
305
306    bool isTimeVaryingField() const;
307    void setTimeVaryingField(bool timeVarying);
308
309    void setDepthCueEnabled(bool enabled);
310    bool getDepthCueEnabled() const;
311
312protected:
313    void mergeSort(int count);
314    void merge(int count, int step);
315    void sort();
316    void drawQuad();
317    void drawUnitBox();
318    void drawQuad(int x1, int y1, int x2, int y2);
319    void initShaders();
320    void passThoughPositions();
321    void initStreamlines(int width, int height);
322    void resetStreamlines();
323    void initInitPosTex();
324
325    void allocateParticle(const float3&, const float3&, float, float);
326    void initNewParticles();
327    void cleanUpParticles();
328};
329
330inline unsigned int ParticleSystem::getNumEmitters() const
331{
332    return _emitters.size();
333}
334
335inline int ParticleSystem::getMaxSize() const
336{
337    return _width * _height;
338}
339
340inline void ParticleSystem::setDefaultPointSize(float size)
341{
342    _pointSize = size;
343}
344
345inline unsigned int ParticleSystem::getNumOfActiveParticles() const
346{
347    return _activeParticles.size();
348}
349
350inline ParticleEmitter* ParticleSystem::getEmitter(int index)
351{
352    if ((unsigned int) index < _emitters.size())
353        return _emitters[index];
354    return 0;
355}
356
357inline void ParticleSystem::setScreenSize(int screenWidth, int screenHeight)
358{
359    _screenWidth = screenWidth;
360    _screenHeight = screenHeight;
361}
362
363inline void ParticleSystem::setFOV(float fov)
364{
365    _fov = fov;
366}
367
368inline float ParticleSystem::getScaleX()const
369{
370    return _scalex;
371}
372
373inline float ParticleSystem::getScaleY()const
374{
375    return _scaley;
376}
377
378inline float ParticleSystem::getScaleZ()const
379{
380    return _scalez;
381}
382
383inline unsigned int ParticleSystem::getVectorFieldGraphicsID()const
384{
385    return _curVectorFieldID;
386}
387
388inline bool ParticleSystem::isTimeVaryingField() const
389{
390    return _isTimeVaryingField;
391}
392
393inline void ParticleSystem::setTimeVaryingField(bool timeVarying)
394{
395    _isTimeVaryingField = timeVarying;
396}
397
398inline void ParticleSystem::setDepthCueEnabled(bool enabled)
399{
400    _depthCueEnabled = enabled;
401}
402
403inline bool ParticleSystem::getDepthCueEnabled() const
404{
405    return _depthCueEnabled;
406}
Note: See TracBrowser for help on using the repository browser.