[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[3502] | 2 | /* |
---|
| 3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
| 4 | * |
---|
| 5 | */ |
---|
[4889] | 6 | #ifndef NV_PARTICLESYSTEM_H |
---|
| 7 | #define NV_PARTICLESYSTEM_H |
---|
[2096] | 8 | |
---|
| 9 | #include <vector> |
---|
[2800] | 10 | #include <queue> |
---|
[2096] | 11 | #include <string> |
---|
| 12 | #include <algorithm> |
---|
[2800] | 13 | #include <limits> |
---|
| 14 | |
---|
[2096] | 15 | #include <Cg/cg.h> |
---|
[2800] | 16 | |
---|
[3468] | 17 | #include <vrmath/Vector3f.h> |
---|
[2800] | 18 | |
---|
| 19 | #include "ParticleEmitter.h" |
---|
| 20 | #include "RenderVertexArray.h" |
---|
[2857] | 21 | #include "Texture2D.h" |
---|
| 22 | #include "Texture3D.h" |
---|
[2800] | 23 | |
---|
[2096] | 24 | #include "CircularQueue.h" |
---|
[2800] | 25 | |
---|
[4889] | 26 | namespace nv { |
---|
| 27 | |
---|
[2096] | 28 | struct NewParticle { |
---|
[2800] | 29 | int index; |
---|
[3492] | 30 | vrmath::Vector3f position; |
---|
| 31 | vrmath::Vector3f velocity; |
---|
[2800] | 32 | float timeOfDeath; |
---|
| 33 | float initTimeStep; |
---|
[2096] | 34 | }; |
---|
| 35 | |
---|
[2800] | 36 | template <typename Type, typename Compare=std::less<Type> > |
---|
| 37 | class priority_queue_vector : public std::priority_queue<Type, std::vector<Type>, Compare> |
---|
[2096] | 38 | { |
---|
| 39 | public: |
---|
[2800] | 40 | void reserve(typename priority_queue_vector<Type>::size_type count) |
---|
| 41 | { |
---|
[2817] | 42 | //priority_queue<Type, std::vector<Type>, Compare>::reserve(count); |
---|
[2800] | 43 | } |
---|
[2096] | 44 | }; |
---|
| 45 | |
---|
[2800] | 46 | struct ActiveParticle { |
---|
| 47 | float timeOfDeath; |
---|
| 48 | int index; |
---|
[2096] | 49 | }; |
---|
| 50 | |
---|
[4889] | 51 | struct particle_greater { |
---|
| 52 | bool operator() (const ActiveParticle& left, const ActiveParticle& right) |
---|
| 53 | { |
---|
| 54 | return (left.timeOfDeath > right.timeOfDeath); |
---|
| 55 | } |
---|
| 56 | }; |
---|
[2096] | 57 | |
---|
[2817] | 58 | struct color4 { |
---|
| 59 | float r, g, b, a; |
---|
| 60 | }; |
---|
| 61 | |
---|
[2800] | 62 | struct Particle { |
---|
| 63 | float timeOfBirth; |
---|
| 64 | float lifeTime; |
---|
| 65 | float attributeType; |
---|
| 66 | float index; |
---|
[2096] | 67 | }; |
---|
| 68 | |
---|
[2800] | 69 | class ParticleSystem |
---|
| 70 | { |
---|
| 71 | public: |
---|
| 72 | enum EnableEnum { |
---|
| 73 | PS_SORT = 1, |
---|
[2818] | 74 | PS_GLYPH = 2, |
---|
[2800] | 75 | PS_DRAW_BBOX = 4, |
---|
| 76 | PS_ADVECTION = 8, |
---|
| 77 | PS_STREAMLINE = 16, |
---|
| 78 | }; |
---|
[2096] | 79 | |
---|
[2860] | 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 | |
---|
[2800] | 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; |
---|
[2096] | 143 | |
---|
[2800] | 144 | int _currentSortPass; |
---|
| 145 | int _maxSortPasses; |
---|
| 146 | int _sortPassesPerFrame; |
---|
[2096] | 147 | |
---|
[2800] | 148 | int _sortBegin; |
---|
| 149 | int _sortEnd; |
---|
[2096] | 150 | |
---|
[2800] | 151 | float _pointSize; |
---|
[2096] | 152 | |
---|
[2800] | 153 | /** |
---|
| 154 | * @brief The index for the source render target |
---|
| 155 | */ |
---|
| 156 | int _currentSortIndex; |
---|
[2096] | 157 | |
---|
[2800] | 158 | /** |
---|
| 159 | * @brief The index for the destination render target |
---|
| 160 | */ |
---|
| 161 | int _destSortIndex; |
---|
[2096] | 162 | |
---|
[2800] | 163 | int _currentPosIndex; |
---|
| 164 | int _destPosIndex; |
---|
[2096] | 165 | |
---|
[2817] | 166 | std::vector<ParticleEmitter *> _emitters; |
---|
[2096] | 167 | |
---|
[2800] | 168 | float _currentTime; |
---|
[2096] | 169 | |
---|
[2800] | 170 | priority_queue_vector<int, std::greater<int> > _availableIndices; |
---|
[4889] | 171 | priority_queue_vector<ActiveParticle, particle_greater > _activeParticles; |
---|
[2800] | 172 | std::vector<NewParticle> _newParticles; |
---|
[2096] | 173 | |
---|
[2800] | 174 | /** |
---|
[2096] | 175 | * @brief frame buffer objects: two are defined, flip them as input output every step |
---|
| 176 | */ |
---|
[2800] | 177 | unsigned int psys_fbo[2]; |
---|
[2096] | 178 | |
---|
| 179 | /** |
---|
| 180 | * @brief color textures attached to frame buffer objects |
---|
| 181 | */ |
---|
| 182 | unsigned int psys_tex[2]; |
---|
| 183 | |
---|
[2800] | 184 | unsigned int sort_fbo[2]; |
---|
| 185 | unsigned int sort_tex[2]; |
---|
[2096] | 186 | |
---|
[2800] | 187 | unsigned int velocity_fbo[2]; |
---|
| 188 | unsigned int velocity_tex[2]; |
---|
[2096] | 189 | |
---|
[2800] | 190 | /////////////////////////////////////////////////// |
---|
| 191 | // TIME SERIES |
---|
| 192 | std::vector<unsigned int> _vectorFieldIDs; |
---|
[2857] | 193 | std::vector<Texture3D *> _vectorFields; |
---|
[2800] | 194 | unsigned int _curVectorFieldID; |
---|
| 195 | float _time_series_vel_mag_min; |
---|
| 196 | float _time_series_vel_mag_max; |
---|
[2817] | 197 | CircularFifo<float *, 10> _queue; |
---|
[2800] | 198 | int _flowFileStartIndex; |
---|
| 199 | int _flowFileEndIndex; |
---|
| 200 | /////////////////////////////////////////////////// |
---|
| 201 | int _skipByte; |
---|
[2096] | 202 | |
---|
[2800] | 203 | float _maxVelocityScale; |
---|
[2096] | 204 | |
---|
[2800] | 205 | RenderVertexArray* _vertices; |
---|
[2096] | 206 | |
---|
[2817] | 207 | Particle *_particles; |
---|
[3492] | 208 | vrmath::Vector3f *_positionBuffer; |
---|
[2817] | 209 | color4 *_colorBuffer; |
---|
[2800] | 210 | unsigned _colorBufferID; |
---|
| 211 | ////////////////////////////////////////// |
---|
[2857] | 212 | Texture2D *_arrows; |
---|
[2096] | 213 | |
---|
[2800] | 214 | float _camx; |
---|
| 215 | float _camy; |
---|
| 216 | float _camz; |
---|
[2096] | 217 | |
---|
[2800] | 218 | float _scalex, _scaley, _scalez; |
---|
[2096] | 219 | |
---|
[2800] | 220 | bool _sortEnabled; |
---|
[2818] | 221 | bool _glyphEnabled; |
---|
[2800] | 222 | bool _drawBBoxEnabled; |
---|
| 223 | bool _advectionEnabled; |
---|
| 224 | bool _streamlineEnabled; |
---|
| 225 | bool _depthCueEnabled; |
---|
[2096] | 226 | |
---|
[2800] | 227 | CGprogram _distanceInitFP; |
---|
[2096] | 228 | |
---|
[2800] | 229 | CGprogram _distanceSortFP; |
---|
| 230 | CGparameter _viewPosParam; |
---|
[2096] | 231 | |
---|
[2800] | 232 | CGprogram _distanceSortLookupFP; |
---|
[2096] | 233 | |
---|
[2800] | 234 | CGprogram _passthroughFP; |
---|
| 235 | CGparameter _scaleParam; |
---|
| 236 | CGparameter _biasParam; |
---|
[2096] | 237 | |
---|
[2800] | 238 | CGprogram _sortRecursionFP; |
---|
| 239 | CGparameter _srSizeParam; |
---|
| 240 | CGparameter _srStepParam; |
---|
| 241 | CGparameter _srCountParam; |
---|
[1704] | 242 | |
---|
[2800] | 243 | CGprogram _sortEndFP; |
---|
| 244 | CGparameter _seSizeParam; |
---|
| 245 | CGparameter _seStepParam; |
---|
[2096] | 246 | |
---|
[2800] | 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; |
---|
[2096] | 263 | |
---|
[2800] | 264 | CGprogram _particleFP; |
---|
| 265 | CGparameter _vectorParticleParam; |
---|
[2096] | 266 | |
---|
[2800] | 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; |
---|
[2817] | 278 | RenderVertexArray *_streamVertices; |
---|
[2800] | 279 | unsigned int _atlasIndexBufferID; |
---|
[2817] | 280 | |
---|
[2800] | 281 | unsigned int _maxIndexBufferSize; |
---|
[2817] | 282 | unsigned int *_indexBuffer; |
---|
[2800] | 283 | int _atlas_x, _atlas_y; |
---|
[2096] | 284 | |
---|
[2800] | 285 | ////////////////////////////////////////// |
---|
| 286 | // Initial Position |
---|
| 287 | unsigned int _initPos_fbo; |
---|
| 288 | unsigned int _initPosTex; |
---|
[2096] | 289 | |
---|
[2800] | 290 | ///////////////////////////////////////// |
---|
| 291 | // Particle Info Vertex Buffer |
---|
| 292 | unsigned int _particleInfoBufferID; |
---|
[2096] | 293 | |
---|
[2800] | 294 | int _screenWidth; |
---|
| 295 | int _screenHeight; |
---|
| 296 | float _fov; |
---|
[2096] | 297 | |
---|
[2800] | 298 | bool _isTimeVaryingField; |
---|
[2096] | 299 | |
---|
[2800] | 300 | int _flowWidth; |
---|
| 301 | int _flowHeight; |
---|
| 302 | int _flowDepth; |
---|
[2096] | 303 | |
---|
[2800] | 304 | std::string _fileNameFormat; |
---|
[2096] | 305 | |
---|
[2800] | 306 | // INSOO |
---|
| 307 | // TEST |
---|
[3492] | 308 | std::vector<vrmath::Vector3f> *_criticalPoints; |
---|
[2096] | 309 | |
---|
[2860] | 310 | // TEMP |
---|
| 311 | static CGcontext _context; |
---|
[2096] | 312 | |
---|
[2800] | 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 | |
---|
[3492] | 326 | void allocateParticle(const vrmath::Vector3f&, const vrmath::Vector3f&, float, float); |
---|
[2800] | 327 | void initNewParticles(); |
---|
| 328 | void cleanUpParticles(); |
---|
[2096] | 329 | }; |
---|
| 330 | |
---|
| 331 | inline unsigned int ParticleSystem::getNumEmitters() const |
---|
| 332 | { |
---|
[2800] | 333 | return _emitters.size(); |
---|
[1704] | 334 | } |
---|
| 335 | |
---|
[2096] | 336 | inline int ParticleSystem::getMaxSize() const |
---|
| 337 | { |
---|
[2800] | 338 | return _width * _height; |
---|
[2096] | 339 | } |
---|
| 340 | |
---|
| 341 | inline void ParticleSystem::setDefaultPointSize(float size) |
---|
| 342 | { |
---|
[2800] | 343 | _pointSize = size; |
---|
[2096] | 344 | } |
---|
| 345 | |
---|
| 346 | inline unsigned int ParticleSystem::getNumOfActiveParticles() const |
---|
| 347 | { |
---|
[2800] | 348 | return _activeParticles.size(); |
---|
[2096] | 349 | } |
---|
| 350 | |
---|
| 351 | inline ParticleEmitter* ParticleSystem::getEmitter(int index) |
---|
| 352 | { |
---|
[2800] | 353 | if ((unsigned int) index < _emitters.size()) |
---|
| 354 | return _emitters[index]; |
---|
| 355 | return 0; |
---|
[2096] | 356 | } |
---|
| 357 | |
---|
[1704] | 358 | inline void ParticleSystem::setScreenSize(int screenWidth, int screenHeight) |
---|
| 359 | { |
---|
[2800] | 360 | _screenWidth = screenWidth; |
---|
| 361 | _screenHeight = screenHeight; |
---|
[1704] | 362 | } |
---|
| 363 | |
---|
| 364 | inline void ParticleSystem::setFOV(float fov) |
---|
| 365 | { |
---|
[2800] | 366 | _fov = fov; |
---|
[1704] | 367 | } |
---|
| 368 | |
---|
| 369 | inline float ParticleSystem::getScaleX()const |
---|
| 370 | { |
---|
[2800] | 371 | return _scalex; |
---|
[1704] | 372 | } |
---|
| 373 | |
---|
| 374 | inline float ParticleSystem::getScaleY()const |
---|
| 375 | { |
---|
[2800] | 376 | return _scaley; |
---|
[1704] | 377 | } |
---|
[2800] | 378 | |
---|
[1704] | 379 | inline float ParticleSystem::getScaleZ()const |
---|
| 380 | { |
---|
[2800] | 381 | return _scalez; |
---|
| 382 | } |
---|
[1704] | 383 | |
---|
| 384 | inline unsigned int ParticleSystem::getVectorFieldGraphicsID()const |
---|
| 385 | { |
---|
[2800] | 386 | return _curVectorFieldID; |
---|
[1704] | 387 | } |
---|
| 388 | |
---|
| 389 | inline bool ParticleSystem::isTimeVaryingField() const |
---|
| 390 | { |
---|
[2800] | 391 | return _isTimeVaryingField; |
---|
[1704] | 392 | } |
---|
| 393 | |
---|
| 394 | inline void ParticleSystem::setTimeVaryingField(bool timeVarying) |
---|
| 395 | { |
---|
[2800] | 396 | _isTimeVaryingField = timeVarying; |
---|
[1704] | 397 | } |
---|
| 398 | |
---|
| 399 | inline void ParticleSystem::setDepthCueEnabled(bool enabled) |
---|
| 400 | { |
---|
[2800] | 401 | _depthCueEnabled = enabled; |
---|
[1704] | 402 | } |
---|
| 403 | |
---|
| 404 | inline bool ParticleSystem::getDepthCueEnabled() const |
---|
| 405 | { |
---|
[2800] | 406 | return _depthCueEnabled; |
---|
[1704] | 407 | } |
---|
[2817] | 408 | |
---|
[4889] | 409 | } |
---|
| 410 | |
---|
[2817] | 411 | #endif |
---|