source: nanovis/branches/1.1/ParticleSystem.h @ 4804

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

Add basic VTK structured points reader to nanovis, update copyright dates.

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