www.pudn.com > fluid.rar > Scene.cpp, change:2006-10-29,size:4306b
#include "stableHeader.h"
#include "Scene.h"
#include "DTKTextureViewer.h"
#include "DTKUtil.h"
#include "DTKTextureViewer.h"
#include "FluidAnimator2D.h"
#include "Substance2D.h"
//------------------------------------------------------------------------------
//global variables
//------------------------------------------------------------------------------
DTK::FluidAnimator2D g_animator(128,128);
DTK::Substance2D g_substance(128,128);
float g_fTimeScale =1.0f;
bool g_bShowQuiverVelocityField = false;
DTK::uint g_uCurDisplayMode = 0;
float g_forceRadius = 0.026f;
float g_forceStrength = 500.0f;
float g_sourceRadius = 0.01f;
DTK::ColorValue g_sourceStrength;
namespace Scene
{
//------------------------------------------------------------------------------
//file scope variable
//------------------------------------------------------------------------------
static IDirect3DDevice9* gs_pD3DDevice9 = NULL;
static IDirect3DTexture9* gs_vecFieldTex = NULL;
void init()
{
DTK::registerDeviceListener(&g_animator);
DTK::registerDeviceListener(&g_substance);
}
void reset()
{
setFluidType(FT_SMOKE);
clearScene();
}
void clearScene()
{
g_animator.reset();
g_substance.reset();
}
void setFluidType(DTK::uint fluid)
{
switch(fluid)
{
case FT_SMOKE:
g_forceRadius = 0.07f;
g_forceStrength =1000.0f;
g_animator.setDissipation(0.9995f);
g_animator.setParticleDistance(0.5f);
g_animator.setIterateNumber(40);
g_animator.setViscous(0.31f);
g_animator.setVorticityConfinementCoefficient(10.0);
g_substance.setUint(DTK::Vector2(0.0125f,0.0125f));
g_sourceRadius = 0.02f;
g_sourceStrength = DTK::ColorValue(1.0,0.64f,0.145f,1.0f);
g_substance.setDissipation(0.997f);
g_fTimeScale = 1.0f;
break;
default:
break;
}
}
void onCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc ,
void* pUserContext )
{
gs_pD3DDevice9 = pd3dDevice;
}
void onResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc , void* pUserContext )
{
HRESULT hr;
SAFE_RELEASE(gs_vecFieldTex);
//V(D3DXCreateTextureFromFileEx(gs_pD3DDevice9,L"../texture/normal.jpg",16,16,D3DX_DEFAULT,
// 0,D3DFMT_A32B32G32R32F,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,
// 0,NULL,NULL,&gs_vecFieldTex));
V(D3DXCreateTextureFromFile(gs_pD3DDevice9,L"../texture/normal.jpg",&gs_vecFieldTex));
reset();
}
void onLostDevice( void* pUserContext )
{
SAFE_RELEASE(gs_vecFieldTex);
}
void onDestroyDevice( void* pUserContext )
{
}
void render(double fTime, float fElapsedTime)
{
//g_animator.timeStep(1.0f);
g_animator.timeStep(fElapsedTime*g_fTimeScale);
//DTK::Vector2 unit = g_animator.getSize();
//unit = DTK::Vector2(1.0/unit.x,1.0/unit.y);
//unit =1.0f/g_animator.getParticleDistance()*unit;
//g_substance.setUint(unit);
g_substance.timeStep(g_animator.getVelocityField(),fElapsedTime*g_fTimeScale);
//DTK::Vector2 velMagScale = g_animator.getSize()*g_animator.getParticleDistance()/2.0f;
DTK::Vector2 velMagScale = g_animator.getSize()/2.0f;
velMagScale= DTK::Vector2(1.0f/velMagScale.x,1.0f/velMagScale.y);
DTK::TextureViewer& texViewer = DTK::TextureViewer::instance();
switch(g_uCurDisplayMode)
{
case DM_SOURCE:
texViewer.viewColor(g_substance.getSubstance(),true);
break;
case DM_VELOCITY:
texViewer.colorVectorField(g_animator.getVelocityField());
break;
case DM_PRESSURE:
texViewer.colorScalarField(g_animator.getPressureField());
break;
case DM_VORTICITY:
texViewer.colorScalarField(g_animator.getVorticityField());
break;
case DM_FORCE:
texViewer.colorVectorField(g_animator.getForceField());
}
if (g_bShowQuiverVelocityField)
{
texViewer.quiverVectorField(g_animator.getVelocityField(),velMagScale);
}
//DTK::TextureViewer::instance().viewColor(gs_vecFieldTex);
//DTK::TextureViewer::instance().quiverVectorField(gs_vecFieldTex,1.0f);
}
}