00001 #ifndef SMOKESYSTEM_H 00002 #define SMOKESYSTEM_H 00003 00004 00005 #include <VoxelGrid.h> 00006 #include <Emitter.h> 00007 #include <iostream> 00008 #include <ngl/Texture.h> 00009 #include <libnoise/noise.h> 00010 #include <Obstacle.h> 00011 00012 00013 //---------------------------------------------------------------------------------------------------------------------- 00023 00024 struct Sprite 00025 { 00026 GLfloat x; 00027 GLfloat y; 00028 GLfloat z; 00029 GLfloat transparency; 00030 GLfloat shadow; 00031 GLfloat temperature; 00032 GLboolean showTemperature; 00033 }; 00034 00035 class SmokeSystem 00036 { 00037 public: 00038 //---------------------------------------------------------------------------------------------------------------------- 00045 //---------------------------------------------------------------------------------------------------------------------- 00046 SmokeSystem( 00047 const float _gridLengthX = 10.0, 00048 const float _gridLengthY = 10.0, 00049 const float _gridLengthZ = 10.0, 00050 const int _gridBaseRes = 10 00051 ); 00052 //---------------------------------------------------------------------------------------------------------------------- 00055 //---------------------------------------------------------------------------------------------------------------------- 00056 ~SmokeSystem(); 00057 //---------------------------------------------------------------------------------------------------------------------- 00061 //---------------------------------------------------------------------------------------------------------------------- 00062 void advectVelocity( 00063 float _dt 00064 ); 00065 //---------------------------------------------------------------------------------------------------------------------- 00076 //---------------------------------------------------------------------------------------------------------------------- 00077 ngl::Vec3 advectVelocityFunction( 00078 float _i, 00079 float _j, 00080 float _k, 00081 float _h, 00082 float _currU, 00083 float _currV, 00084 float _currW 00085 ); 00086 //---------------------------------------------------------------------------------------------------------------------- 00092 //---------------------------------------------------------------------------------------------------------------------- 00093 void applyBuoyancyAndTurbulenceForces( 00094 float _dt, 00095 float _time 00096 ); 00097 //---------------------------------------------------------------------------------------------------------------------- 00100 //---------------------------------------------------------------------------------------------------------------------- 00101 void applyPressure( 00102 float _dt 00103 ); 00104 //---------------------------------------------------------------------------------------------------------------------- 00107 //---------------------------------------------------------------------------------------------------------------------- 00108 void calcVorticityConfinement( 00109 float _dt 00110 ); 00111 //---------------------------------------------------------------------------------------------------------------------- 00114 //---------------------------------------------------------------------------------------------------------------------- 00115 void calcPressure(); 00116 //---------------------------------------------------------------------------------------------------------------------- 00122 //---------------------------------------------------------------------------------------------------------------------- 00123 void convectDensity( 00124 float _i, 00125 float _j, 00126 float _k, 00127 float _dt 00128 ); 00129 //---------------------------------------------------------------------------------------------------------------------- 00132 //---------------------------------------------------------------------------------------------------------------------- 00133 void convectEnergy( 00134 float _dt 00135 ); 00136 //---------------------------------------------------------------------------------------------------------------------- 00149 //---------------------------------------------------------------------------------------------------------------------- 00150 float convectEnergyFunction( 00151 float _i, 00152 float _j, 00153 float _k, 00154 float _curr, 00155 float _next_inX, 00156 float _prev_inX, 00157 float _next_inY, 00158 float _prev_inY, 00159 float _next_inZ, 00160 float _prev_inZ 00161 ); 00162 //---------------------------------------------------------------------------------------------------------------------- 00168 //---------------------------------------------------------------------------------------------------------------------- 00169 void convectTemperature( 00170 float _i, 00171 float _j, 00172 float _k, 00173 float _dt 00174 ); 00175 //---------------------------------------------------------------------------------------------------------------------- 00177 //---------------------------------------------------------------------------------------------------------------------- 00178 void createSpriteVAO(); 00179 //---------------------------------------------------------------------------------------------------------------------- 00181 //---------------------------------------------------------------------------------------------------------------------- 00182 void createVelocityVAO(); 00183 //---------------------------------------------------------------------------------------------------------------------- 00185 //---------------------------------------------------------------------------------------------------------------------- 00186 void drawSmokeSprites(); 00187 //---------------------------------------------------------------------------------------------------------------------- 00189 //---------------------------------------------------------------------------------------------------------------------- 00190 void drawVelocityArrows(); 00191 //---------------------------------------------------------------------------------------------------------------------- 00196 //---------------------------------------------------------------------------------------------------------------------- 00197 void emitEnergy( 00198 float _dt 00199 ); 00200 //---------------------------------------------------------------------------------------------------------------------- 00203 //---------------------------------------------------------------------------------------------------------------------- 00204 inline float getAbsoluteTolerance() const { return m_absoluteTolerance; } 00205 //---------------------------------------------------------------------------------------------------------------------- 00208 //---------------------------------------------------------------------------------------------------------------------- 00209 inline std::vector <Sprite>* getData() { return &m_spriteData;} 00210 //---------------------------------------------------------------------------------------------------------------------- 00213 //---------------------------------------------------------------------------------------------------------------------- 00214 inline Emitter* getEmitter() { return m_emitter; } 00215 //---------------------------------------------------------------------------------------------------------------------- 00218 //---------------------------------------------------------------------------------------------------------------------- 00219 inline float getGravity() const { return m_gravity.m_y; } 00220 //---------------------------------------------------------------------------------------------------------------------- 00223 //---------------------------------------------------------------------------------------------------------------------- 00224 inline VoxelGrid* getGrid() { return m_grid; } 00225 //---------------------------------------------------------------------------------------------------------------------- 00228 //---------------------------------------------------------------------------------------------------------------------- 00229 inline float getKDensityDiss() const { return m_kDensityDiss; } 00230 //---------------------------------------------------------------------------------------------------------------------- 00233 //---------------------------------------------------------------------------------------------------------------------- 00234 inline float getKFall() const { return m_kFall; } 00235 //---------------------------------------------------------------------------------------------------------------------- 00238 //---------------------------------------------------------------------------------------------------------------------- 00239 inline float getKRise() const { return m_kRise; } 00240 //---------------------------------------------------------------------------------------------------------------------- 00243 //---------------------------------------------------------------------------------------------------------------------- 00244 inline float getKTemperDiss() const { return m_kTemperDiss; } 00245 //---------------------------------------------------------------------------------------------------------------------- 00248 //---------------------------------------------------------------------------------------------------------------------- 00249 inline float getKVorticity() const { return m_kVorticity; } 00250 //---------------------------------------------------------------------------------------------------------------------- 00253 //---------------------------------------------------------------------------------------------------------------------- 00254 inline float getNoiseMagnitude() const { return m_noiseMagnitude; } 00255 //---------------------------------------------------------------------------------------------------------------------- 00258 //---------------------------------------------------------------------------------------------------------------------- 00259 inline noise::module::Perlin* getNoiseModule() { return &m_noiseModule; } 00260 //---------------------------------------------------------------------------------------------------------------------- 00263 //---------------------------------------------------------------------------------------------------------------------- 00264 inline float getNoiseSpeed() const { return m_noiseSpeed; } 00265 //---------------------------------------------------------------------------------------------------------------------- 00268 //---------------------------------------------------------------------------------------------------------------------- 00269 inline Obstacle* getObstacle() { return m_obstacle; } 00270 //---------------------------------------------------------------------------------------------------------------------- 00273 //---------------------------------------------------------------------------------------------------------------------- 00274 inline float getOpacityStrength() const { return m_opacityStrength; } 00275 //---------------------------------------------------------------------------------------------------------------------- 00278 //---------------------------------------------------------------------------------------------------------------------- 00279 inline int getPrecisionX() const { return m_precisionX; } 00280 //---------------------------------------------------------------------------------------------------------------------- 00283 //---------------------------------------------------------------------------------------------------------------------- 00284 inline int getPrecisionY() const { return m_precisionY; } 00285 //---------------------------------------------------------------------------------------------------------------------- 00288 //---------------------------------------------------------------------------------------------------------------------- 00289 inline int getPrecisionZ() const { return m_precisionZ; } 00290 //---------------------------------------------------------------------------------------------------------------------- 00293 //---------------------------------------------------------------------------------------------------------------------- 00294 inline int getPressureIters() const { return m_pressureIters; } 00295 //---------------------------------------------------------------------------------------------------------------------- 00298 //---------------------------------------------------------------------------------------------------------------------- 00299 inline float getRelaxationRate() const { return m_relaxationRate; } 00300 //---------------------------------------------------------------------------------------------------------------------- 00303 //---------------------------------------------------------------------------------------------------------------------- 00304 inline float getShadowStrength() const { return m_shadowStrength; } 00305 //---------------------------------------------------------------------------------------------------------------------- 00308 //---------------------------------------------------------------------------------------------------------------------- 00309 inline GLuint getTexID() const { return m_texID; } 00310 //---------------------------------------------------------------------------------------------------------------------- 00313 //---------------------------------------------------------------------------------------------------------------------- 00314 inline float getViscosity() const { return m_viscosity; } 00315 //---------------------------------------------------------------------------------------------------------------------- 00317 //---------------------------------------------------------------------------------------------------------------------- 00318 void initGridValues(); 00319 //---------------------------------------------------------------------------------------------------------------------- 00321 //---------------------------------------------------------------------------------------------------------------------- 00322 void initSolidVoxels(); 00323 //---------------------------------------------------------------------------------------------------------------------- 00325 //---------------------------------------------------------------------------------------------------------------------- 00326 void initSmokeSpritePoints(); 00327 //---------------------------------------------------------------------------------------------------------------------- 00334 //---------------------------------------------------------------------------------------------------------------------- 00335 void initValues( 00336 const float _gridLengthX = 10.0, 00337 const float _gridLengthY = 10.0, 00338 const float _gridLengthZ = 10.0, 00339 const int _gridBaseRes = 10 00340 ); 00341 //---------------------------------------------------------------------------------------------------------------------- 00343 //---------------------------------------------------------------------------------------------------------------------- 00344 void initVelocityField(); 00345 //---------------------------------------------------------------------------------------------------------------------- 00348 //---------------------------------------------------------------------------------------------------------------------- 00349 inline bool isObstacle() const { return m_isObstacle; } 00350 //---------------------------------------------------------------------------------------------------------------------- 00352 //---------------------------------------------------------------------------------------------------------------------- 00353 void reset(); 00354 //--------------------------------------------------------------------------------------------------------------------- 00357 //---------------------------------------------------------------------------------------------------------------------- 00358 inline void setAbsoluteTolerance( 00359 double _val 00360 ) { m_absoluteTolerance = _val; } 00361 //---------------------------------------------------------------------------------------------------------------------- 00364 //---------------------------------------------------------------------------------------------------------------------- 00365 inline void setGravity( 00366 double _val 00367 ) { m_gravity.m_y = _val; } 00368 //---------------------------------------------------------------------------------------------------------------------- 00371 //---------------------------------------------------------------------------------------------------------------------- 00372 void setGridBaseRes( 00373 int _res 00374 ); 00375 //---------------------------------------------------------------------------------------------------------------------- 00378 //---------------------------------------------------------------------------------------------------------------------- 00379 void setGridLengthX( 00380 float _length 00381 ); 00382 //---------------------------------------------------------------------------------------------------------------------- 00385 //---------------------------------------------------------------------------------------------------------------------- 00386 void setGridLengthY( 00387 float _length 00388 ); 00389 //---------------------------------------------------------------------------------------------------------------------- 00392 //---------------------------------------------------------------------------------------------------------------------- 00393 void setGridLengthZ( 00394 float _length 00395 ); 00396 //---------------------------------------------------------------------------------------------------------------------- 00399 //---------------------------------------------------------------------------------------------------------------------- 00400 void setH( 00401 float _val 00402 ); 00403 //---------------------------------------------------------------------------------------------------------------------- 00406 //---------------------------------------------------------------------------------------------------------------------- 00407 inline void setIsObstacle( 00408 bool _val 00409 ) { m_isObstacle = _val; } 00410 //---------------------------------------------------------------------------------------------------------------------- 00413 //---------------------------------------------------------------------------------------------------------------------- 00414 inline void setKDensityDiss( 00415 double _val 00416 ) { m_kDensityDiss = _val; } 00417 //---------------------------------------------------------------------------------------------------------------------- 00420 //---------------------------------------------------------------------------------------------------------------------- 00421 inline void setKFall( 00422 double _val 00423 ) { m_kFall = _val; } 00424 //---------------------------------------------------------------------------------------------------------------------- 00427 //---------------------------------------------------------------------------------------------------------------------- 00428 inline void setKRise( 00429 double _val 00430 ) { m_kRise = _val; } 00431 //---------------------------------------------------------------------------------------------------------------------- 00434 //---------------------------------------------------------------------------------------------------------------------- 00435 inline void setKTemperDiss( 00436 float _val 00437 ) { m_kTemperDiss = _val; } 00438 //---------------------------------------------------------------------------------------------------------------------- 00441 //---------------------------------------------------------------------------------------------------------------------- 00442 inline void setKVorticity( 00443 double _val 00444 ) { m_kVorticity = _val; } 00445 //---------------------------------------------------------------------------------------------------------------------- 00448 //---------------------------------------------------------------------------------------------------------------------- 00449 inline void setNoiseMagnitude( 00450 float _val 00451 ) { m_noiseMagnitude = _val; } 00452 //---------------------------------------------------------------------------------------------------------------------- 00455 //---------------------------------------------------------------------------------------------------------------------- 00456 inline void setNoiseSpeed( 00457 float _val 00458 ) { m_noiseSpeed = _val; } 00459 //---------------------------------------------------------------------------------------------------------------------- 00462 //---------------------------------------------------------------------------------------------------------------------- 00463 inline void setOpacityStrength( 00464 float _val 00465 ) { m_opacityStrength = _val / 100.0; } 00466 //---------------------------------------------------------------------------------------------------------------------- 00469 //---------------------------------------------------------------------------------------------------------------------- 00470 void setPrecisionX( 00471 int _precision 00472 ); 00473 //---------------------------------------------------------------------------------------------------------------------- 00476 //---------------------------------------------------------------------------------------------------------------------- 00477 void setPrecisionY( 00478 int _precision 00479 ); 00480 //---------------------------------------------------------------------------------------------------------------------- 00483 //---------------------------------------------------------------------------------------------------------------------- 00484 void setPrecisionZ( 00485 int _precision 00486 ); 00487 //--------------------------------------------------------------------------------------------------------------------- 00490 //---------------------------------------------------------------------------------------------------------------------- 00491 inline void setRelaxationRate( 00492 double _val 00493 ) { m_relaxationRate = _val; } 00494 //---------------------------------------------------------------------------------------------------------------------- 00497 //---------------------------------------------------------------------------------------------------------------------- 00498 inline void setShadowStrength( 00499 float _val 00500 ) { m_shadowStrength = _val / 100.0; } 00501 //---------------------------------------------------------------------------------------------------------------------- 00504 //---------------------------------------------------------------------------------------------------------------------- 00505 inline void setViscosity( 00506 float _val 00507 ) { m_viscosity = _val; } 00508 //---------------------------------------------------------------------------------------------------------------------- 00513 //---------------------------------------------------------------------------------------------------------------------- 00514 void update( 00515 float _dt, 00516 float _time, 00517 bool _showVelocityField 00518 ); 00519 //---------------------------------------------------------------------------------------------------------------------- 00521 //---------------------------------------------------------------------------------------------------------------------- 00522 void updateSpriteVAO(); 00523 //---------------------------------------------------------------------------------------------------------------------- 00525 //---------------------------------------------------------------------------------------------------------------------- 00526 void updateVelocityVAOData(); 00527 //---------------------------------------------------------------------------------------------------------------------- 00529 //---------------------------------------------------------------------------------------------------------------------- 00530 void updateVelocityVAO(); 00531 00532 private: 00533 //---------------------------------------------------------------------------------------------------------------------- 00536 //---------------------------------------------------------------------------------------------------------------------- 00537 float m_absoluteTolerance; 00538 //---------------------------------------------------------------------------------------------------------------------- 00540 //---------------------------------------------------------------------------------------------------------------------- 00541 Emitter* m_emitter; 00542 //---------------------------------------------------------------------------------------------------------------------- 00544 //---------------------------------------------------------------------------------------------------------------------- 00545 ngl::Vec3 m_gravity; 00546 //---------------------------------------------------------------------------------------------------------------------- 00548 //---------------------------------------------------------------------------------------------------------------------- 00549 VoxelGrid* m_grid; 00550 //---------------------------------------------------------------------------------------------------------------------- 00552 //---------------------------------------------------------------------------------------------------------------------- 00553 int m_gridBaseRes; 00554 //---------------------------------------------------------------------------------------------------------------------- 00556 //---------------------------------------------------------------------------------------------------------------------- 00557 float m_gridLengthX; 00558 //---------------------------------------------------------------------------------------------------------------------- 00560 //---------------------------------------------------------------------------------------------------------------------- 00561 float m_gridLengthY; 00562 //---------------------------------------------------------------------------------------------------------------------- 00564 //---------------------------------------------------------------------------------------------------------------------- 00565 float m_gridLengthZ; 00566 //---------------------------------------------------------------------------------------------------------------------- 00568 //---------------------------------------------------------------------------------------------------------------------- 00569 float m_h; 00570 //---------------------------------------------------------------------------------------------------------------------- 00572 //---------------------------------------------------------------------------------------------------------------------- 00573 float m_hX; 00574 //---------------------------------------------------------------------------------------------------------------------- 00576 //---------------------------------------------------------------------------------------------------------------------- 00577 float m_hY; 00578 //---------------------------------------------------------------------------------------------------------------------- 00580 //---------------------------------------------------------------------------------------------------------------------- 00581 float m_hZ; 00582 //---------------------------------------------------------------------------------------------------------------------- 00584 //---------------------------------------------------------------------------------------------------------------------- 00585 bool m_isObstacle; 00586 //---------------------------------------------------------------------------------------------------------------------- 00588 //---------------------------------------------------------------------------------------------------------------------- 00589 float m_kDensityDiss; 00590 //---------------------------------------------------------------------------------------------------------------------- 00592 //---------------------------------------------------------------------------------------------------------------------- 00593 float m_kFall; 00594 //---------------------------------------------------------------------------------------------------------------------- 00596 //---------------------------------------------------------------------------------------------------------------------- 00597 float m_kRise; 00598 //---------------------------------------------------------------------------------------------------------------------- 00600 //---------------------------------------------------------------------------------------------------------------------- 00601 float m_kTemperDiss; 00602 //---------------------------------------------------------------------------------------------------------------------- 00604 //---------------------------------------------------------------------------------------------------------------------- 00605 float m_kVorticity; 00606 //---------------------------------------------------------------------------------------------------------------------- 00608 //---------------------------------------------------------------------------------------------------------------------- 00609 float m_noiseMagnitude; 00610 //---------------------------------------------------------------------------------------------------------------------- 00612 //---------------------------------------------------------------------------------------------------------------------- 00613 noise::module::Perlin m_noiseModule; 00614 //---------------------------------------------------------------------------------------------------------------------- 00616 //---------------------------------------------------------------------------------------------------------------------- 00617 noise::module::Turbulence m_noiseTurbulenceModule; 00618 //---------------------------------------------------------------------------------------------------------------------- 00620 //---------------------------------------------------------------------------------------------------------------------- 00621 float m_noiseSpeed; 00622 //---------------------------------------------------------------------------------------------------------------------- 00624 //---------------------------------------------------------------------------------------------------------------------- 00625 Obstacle* m_obstacle; 00626 //---------------------------------------------------------------------------------------------------------------------- 00628 //---------------------------------------------------------------------------------------------------------------------- 00629 float m_opacityStrength; 00630 //---------------------------------------------------------------------------------------------------------------------- 00632 //---------------------------------------------------------------------------------------------------------------------- 00633 int m_precisionX; 00634 //---------------------------------------------------------------------------------------------------------------------- 00636 //---------------------------------------------------------------------------------------------------------------------- 00637 int m_precisionY; 00638 //---------------------------------------------------------------------------------------------------------------------- 00640 //---------------------------------------------------------------------------------------------------------------------- 00641 int m_precisionZ; 00642 //---------------------------------------------------------------------------------------------------------------------- 00644 //---------------------------------------------------------------------------------------------------------------------- 00645 int m_pressureIters; 00646 //---------------------------------------------------------------------------------------------------------------------- 00648 //---------------------------------------------------------------------------------------------------------------------- 00649 float m_relaxationRate; 00650 //---------------------------------------------------------------------------------------------------------------------- 00652 //---------------------------------------------------------------------------------------------------------------------- 00653 float m_shadowStrength; 00654 //---------------------------------------------------------------------------------------------------------------------- 00656 //---------------------------------------------------------------------------------------------------------------------- 00657 GLuint m_texID; 00658 //---------------------------------------------------------------------------------------------------------------------- 00660 //---------------------------------------------------------------------------------------------------------------------- 00661 std::vector <Sprite> m_spriteData; 00662 //---------------------------------------------------------------------------------------------------------------------- 00664 //---------------------------------------------------------------------------------------------------------------------- 00665 ngl::VertexArrayObject *m_spriteVAO; 00666 //---------------------------------------------------------------------------------------------------------------------- 00668 //---------------------------------------------------------------------------------------------------------------------- 00669 std::vector <ngl::vertData> m_velocityFieldData; 00670 //---------------------------------------------------------------------------------------------------------------------- 00672 //---------------------------------------------------------------------------------------------------------------------- 00673 ngl::VertexArrayObject *m_velocityFieldVAO; 00674 //---------------------------------------------------------------------------------------------------------------------- 00676 //---------------------------------------------------------------------------------------------------------------------- 00677 float m_viscosity; 00678 00679 00680 00681 }; 00682 00683 00684 #endif // SMOKESYSTEM_H