GteFluid3.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <Physics/GteFluid3.h>
10 using namespace gte;
11 
12 Fluid3::Fluid3(std::shared_ptr<GraphicsEngine> const& engine,
13  std::shared_ptr<ProgramFactory> const& factory,
14  int xSize, int ySize, int zSize, float dt)
15  :
16  mEngine(engine),
17  mXSize(xSize),
18  mYSize(ySize),
19  mZSize(zSize),
20  mDt(dt),
21  mTime(0.0f)
22 {
23  // Create the shared parameters for many of the simulation shaders.
24  float dx = 1.0f/static_cast<float>(mXSize);
25  float dy = 1.0f/static_cast<float>(mYSize);
26  float dz = 1.0f/static_cast<float>(mZSize);
27  float dtDivDxDx = (dt/dx)/dx;
28  float dtDivDyDy = (dt/dy)/dy;
29  float dtDivDzDz = (dt/dz)/dz;
30  float ratio0 = dx/dy;
31  float ratio1 = dx/dz;
32  float ratio0Sqr = ratio0*ratio0;
33  float ratio1Sqr = ratio1*ratio1;
34  float factor = 0.5f/(1.0f + ratio0Sqr + ratio1Sqr);
35  float epsilonX = factor;
36  float epsilonY = ratio0Sqr*factor;
37  float epsilonZ = ratio1Sqr*factor;
38  float epsilon0 = dx*dx*factor;
39  float const denViscosity = 0.0001f;
40  float const velViscosity = 0.0001f;
41  float denVX = denViscosity*dtDivDxDx;
42  float denVY = denViscosity*dtDivDyDy;
43  float denVZ = denViscosity*dtDivDzDz;
44  float velVX = velViscosity*dtDivDxDx;
45  float velVY = velViscosity*dtDivDyDy;
46  float velVZ = velViscosity*dtDivDzDz;
47 
48  mParameters = std::make_shared<ConstantBuffer>(sizeof(Fluid3Parameters), false);
50  p.spaceDelta = { dx, dy, dz, 0.0f };
51  p.halfDivDelta = { 0.5f / dx, 0.5f / dy, 0.5f / dz, 0.0f };
52  p.timeDelta = { dt / dx, dt / dy, dt / dz, dt };
53  p.viscosityX = { velVX, velVX, velVX, denVX };
54  p.viscosityY = { velVY, velVY, velVY, denVY };
55  p.viscosityZ = { velVZ, velVZ, velVZ, denVZ };
56  p.epsilon = { epsilonX, epsilonY, epsilonZ, epsilon0 };
57 
58  // Create the compute shaders and textures for the simulation.
59  mInitializeSource = std::make_shared<Fluid3InitializeSource>(factory,
60  mXSize, mYSize, mZSize, 8, 8, 8, mParameters);
61  mSourceTexture = mInitializeSource->GetSource();
62 
63  mInitializeState = std::make_shared<Fluid3InitializeState>(factory,
64  mXSize, mYSize, mZSize, 8, 8, 8);
65  mStateTm1Texture = mInitializeState->GetStateTm1();
66  mStateTTexture = mInitializeState->GetStateT();
67 
68  mEnforceStateBoundary = std::make_shared<Fluid3EnforceStateBoundary>(
69  factory, mXSize, mYSize, mZSize, 8, 8, 8);
70 
71  mUpdateState = std::make_shared<Fluid3UpdateState>(factory, mXSize,
72  mYSize, mZSize, 8, 8, 8, mParameters);
73  mStateTp1Texture = mUpdateState->GetUpdateState();
74 
75  mComputeDivergence = std::make_shared<Fluid3ComputeDivergence>(factory,
76  mXSize, mYSize, mZSize, 8, 8, 8, mParameters);
77  mDivergenceTexture = mComputeDivergence->GetDivergence();
78 
79  mSolvePoisson = std::make_shared<Fluid3SolvePoisson>(factory, mXSize,
80  mYSize, mZSize, 8, 8, 8, mParameters, 32);
81  mPoissonTexture = mSolvePoisson->GetPoisson();
82 
83  mAdjustVelocity = std::make_shared<Fluid3AdjustVelocity>(factory, mXSize,
84  mYSize, mZSize, 8, 8, 8, mParameters);
85 }
86 
88 {
89  mInitializeSource->Execute(mEngine);
90  mInitializeState->Execute(mEngine);
93 }
94 
96 {
105  std::swap(mStateTm1Texture, mStateTTexture);
106 
107  mTime += mDt;
108 }
std::shared_ptr< Fluid3InitializeSource > mInitializeSource
Definition: GteFluid3.h:46
Vector4< float > epsilon
std::shared_ptr< Fluid3AdjustVelocity > mAdjustVelocity
Definition: GteFluid3.h:52
Vector4< float > timeDelta
std::shared_ptr< GraphicsEngine > mEngine
Definition: GteFluid3.h:38
std::shared_ptr< Fluid3UpdateState > mUpdateState
Definition: GteFluid3.h:49
float mTime
Definition: GteFluid3.h:43
Vector4< float > halfDivDelta
int mYSize
Definition: GteFluid3.h:39
Vector4< float > viscosityX
std::shared_ptr< ConstantBuffer > mParameters
Definition: GteFluid3.h:45
std::shared_ptr< Texture3 > mDivergenceTexture
Definition: GteFluid3.h:58
int mXSize
Definition: GteFluid3.h:39
std::shared_ptr< Texture3 > mStateTTexture
Definition: GteFluid3.h:56
std::shared_ptr< Fluid3EnforceStateBoundary > mEnforceStateBoundary
Definition: GteFluid3.h:48
void Initialize()
Definition: GteFluid3.cpp:87
Vector4< float > viscosityY
std::shared_ptr< Fluid3InitializeState > mInitializeState
Definition: GteFluid3.h:47
Vector4< float > spaceDelta
std::shared_ptr< Texture3 > mPoissonTexture
Definition: GteFluid3.h:59
int mZSize
Definition: GteFluid3.h:39
void DoSimulationStep()
Definition: GteFluid3.cpp:95
Vector4< float > viscosityZ
std::shared_ptr< Texture3 > mStateTp1Texture
Definition: GteFluid3.h:57
GLfloat f
Definition: glcorearb.h:1921
std::shared_ptr< Texture3 > mStateTm1Texture
Definition: GteFluid3.h:55
float mDt
Definition: GteFluid3.h:40
std::shared_ptr< Fluid3ComputeDivergence > mComputeDivergence
Definition: GteFluid3.h:50
std::shared_ptr< Texture3 > mSourceTexture
Definition: GteFluid3.h:54
GLfloat GLfloat p
Definition: glext.h:11668
std::shared_ptr< Fluid3SolvePoisson > mSolvePoisson
Definition: GteFluid3.h:51
Fluid3(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int zSize, float dt)
Definition: GteFluid3.cpp:12


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59