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