📄 ca_waterwaves.cpp
字号:
D3DXMatrixTranspose(&m_mWorldViewProj, &m_mWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// reset offsets to 0 (ie no offsets)
offset.x = 0.0f;
m_pD3DDev->SetVertexShaderConstant(CV_UV_OFFSET_TO_USE, &offset, 1);
m_pD3DDev->SetTexture(0, m_pTex_VelCurSrc );
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
break;
case FULLSCREEN_NEIGHBOR_CALC:
// Draw quad over full display
D3DXMatrixScaling(&matWorld, 2.0f, 2.0f, 1.0f);
D3DXMatrixMultiply(&m_mWorldViewProj, &matWorld, &matViewProj);
D3DXMatrixTranspose(&m_mWorldViewProj, &m_mWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// reset offsets to 0 (ie no offsets)
offset.x = 0.0f;
m_pD3DDev->SetVertexShaderConstant(CV_UV_OFFSET_TO_USE, &offset, 1);
m_pD3DDev->SetTexture(0, mpTextureFiltered[3] );
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
break;
case ALL_TOGETHER :
// draw quad in upper left corner: original texture
D3DXMatrixTranslation(&matWorld, -1.0f, 1.0f, 0.0f);
D3DXMatrixMultiply(&m_mWorldViewProj, &matWorld, &matViewProj);
D3DXMatrixTranspose(&m_mWorldViewProj, &m_mWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// reset offsets to 0 (ie no offsets)
offset.x = 0.0f;
m_pD3DDev->SetVertexShaderConstant(CV_UV_OFFSET_TO_USE, &offset, 1);
//m_pD3DDev->SetTexture(0, m_pInitialStateTexture->GetTexture()); // upper left corner
//m_pD3DDev->SetTexture(0, m_pTex_VelPrevSrc ); // upper left corner
m_pD3DDev->SetTexture(0, m_pTex_VelCurSrc ); // upper left corner
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
// draw quads in the other corners, use generated textures
for (int j = 0; j < 3; ++j)
{
D3DXMatrixTranslation(&matWorld, (j == 2) ? -1.0f : 1.0f,
(j == 0) ? 1.0f : -1.0,
0.0f);
D3DXMatrixMultiply(&m_mWorldViewProj, &matWorld, &matViewProj);
D3DXMatrixTranspose(&m_mWorldViewProj, &m_mWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// case for upper right display
pSrcTex = mpTextureFiltered[m_nTex_HeightTarg]; // upper right corner;
if( m_bFarther )
{
if( j==1 )
{
pSrcTex = m_pInt_FinalSrc; // low right corner
pSrcTex = mpTextureFiltered[3];
}
else if( j==2 )
{
pSrcTex = m_pTex_VelCurSrc; // low left corner
}
}
else
{
if( j==1 )
{
pSrcTex = mpTextureFiltered[3];
}
else if( j==2 )
{
pSrcTex = m_pTex_VelCurSrc;
}
}
if( pSrcTex != 0 )
{
m_pD3DDev->SetTexture(0, pSrcTex );
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
}
else
{
FDebug("bad texture pointer in result display!!\n");
}
}
break;
}; // switch for display config
}
else
{
// skip rendering this frame
// Set normal render target in case the app is doing more rendering
// to the window
hr = m_pD3DDev->SetRenderTarget(mpBackbufferColor, mpBackbufferDepth);
nSkip++;
}
// Reset bilinear filtering, as that is most common
for( i=0; i < 4; i++ )
{
m_pD3DDev->SetTextureStageState(i, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pD3DDev->SetTextureStageState(i, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pD3DDev->SetTextureStageState(i, D3DTSS_MIPFILTER, D3DTEXF_NONE );
}
return( hr );
}
void CA_Water::SetNormalMapSTScale( float STScale )
{
m_fNrmlSTScale = STScale;
}
void CA_Water::SetBlurDistance( float fac )
{
m_fBlurDist = fac;
UpdateBlurVertOffset();
}
void CA_Water::SetEqRestoreFac( float fac )
{
m_fEqRestore_factor = fac;
}
void CA_Water::SetVelocityApplyFac( float fac )
{
m_fVelFactor = fac;
}
void CA_Water::SetBlendFac( float fac )
{
m_fBlend = fac;
}
void CA_Water::UpdateBlurVertOffset()
{
float woff, hoff;
woff = m_fPerTexelWidth / 2.0f;
hoff = m_fPerTexelHeight / 2.0f;
float type3OffsetX[4] = { - m_fPerTexelWidth/2.0f + woff,
m_fPerTexelWidth + woff,
m_fPerTexelWidth/2.0f + woff,
- m_fPerTexelWidth + woff };
float type3OffsetY[4] = { - m_fPerTexelHeight + hoff,
- m_fPerTexelHeight/2.0f + hoff,
m_fPerTexelHeight + hoff,
m_fPerTexelHeight/2.0f + hoff };
float interp_x, interp_y;
for (int i = 0; i < 4; ++i)
{
interp_x = m_fBlurDist * ( type3OffsetX[i] - woff ) + woff;
interp_y = m_fBlurDist * ( type3OffsetY[i] - hoff ) + hoff;
D3DXVECTOR4 vec_type3Offset( interp_x, interp_y, 0.0f, 0.0f);
m_pD3DDev->SetVertexShaderConstant(CV_UV_T0_TYPE3 + 5*i, &vec_type3Offset, 1);
}
}
void CA_Water::DrawInteriorBoundaryObjects()
{
bool bInlineDroplet = true;
HRESULT hr;
assert( m_pD3DDev != NULL );
int i;
IDirect3DSurface8 *pBack_Color;
IDirect3DSurface8 *pBack_Depth;
/////////////////////////////
const int numobjects = 1;
int ind = 0;
float xmin[numobjects];
float xmax[numobjects];
float ymin[numobjects];
float ymax[numobjects];
DWORD color = 0x00000000; // ARGB
xmin[ind] = 0.0f;
xmax[ind] = 1.0f;
ymin[ind] = 0.0f;
ymax[ind] = 1.0f;
ind++;
// Could also draw a few fixed objects with
// preset positions, etc..
// Select a different texture or constant color
// as the source for these objects
/*
const int numobjects = 2;
int ind = 0;
float xmin[numobjects];
float xmax[numobjects];
float ymin[numobjects];
float ymax[numobjects];
DWORD color = 0x00000000; // ARGB
xmin[ind] = 0.485f;
xmax[ind] = 0.50f;
ymin[ind] = 0.15f;
ymax[ind] = 0.40f;
ind++; assert( ind < numobjects );
xmin[ind] = 0.485f;
xmax[ind] = 0.50f;
ymin[ind] = 0.45f;
ymax[ind] = 0.55f;
ind++;
*/
float x_center[ numobjects ];
float y_center[ numobjects ];
float x_size[ numobjects ];
float y_size[ numobjects ];
for( i=0; i < numobjects; i++ )
{
x_center[i] = (xmin[i] + xmax[i])/2.0f;
y_center[i] = (ymin[i] + ymax[i])/2.0f;
x_size[i] = (xmax[i] - xmin[i]);
y_size[i] = (ymax[i] - ymin[i]);
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// Draw into the velocity texture:
if( bInlineDroplet )
{
// Rendering is happening in middle of physics calc.
// Render to appropriate texture - this doesn't require
// any special work
}
else
{
// rendering happening outside physics calc
// Render to appropriate velocity texture for next frame,
// also save and restore current render targets
m_pD3DDev->GetRenderTarget( &pBack_Color );
m_pD3DDev->GetDepthStencilSurface( &pBack_Depth );
SetInitialRenderStates();
hr = m_pD3DDev->SetStreamSource(0, m_pVertexBuffer, sizeof(QuadVertex));
ASSERT_IF_FAILED(hr);
hr = m_pD3DDev->SetRenderTarget( mpFilterTarget[m_nTex_VelTarg], NULL);
ASSERT_IF_FAILED(hr);
}
hr = m_pD3DDev->SetVertexShader( m_dwVertexShader );
ASSERT_IF_FAILED(hr);
///////////////////////////////////////
// Set render mode to use source texture
// Alpha determines fixed vs. free areas
hr = m_pD3DDev->SetPixelShader( 0 );
ASSERT_IF_FAILED(hr);
m_pD3DDev->SetTexture(0, m_pInitialStateTexture->GetTexture() );
// Additive blend
m_pD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pD3DDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pD3DDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
// m_pD3DDev->SetRenderState( D3DRS_TEXTUREFACTOR, color ); // ARGB
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR ); // blue only
m_pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pD3DDev->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pD3DDev->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pD3DDev->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
float x,y;
for( i=0; i < numobjects; i++ )
{
// shift x,y into -1 to 1 range
x = (x_center[i] - 0.5f)*2.0f;
y = (y_center[i] - 0.5f)*2.0f;
// alter matrix to place the droplet
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProj;
D3DXMATRIX matViewProj;
D3DXMATRIX matWorldViewProj;
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( x, y, 0.0f );
D3DXVECTOR3 vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
// Set World, View, Projection, and combination matrices.
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUp);
// 2.0f cause translation extends from -1 to 1
D3DXMatrixOrthoLH(&matProj, 2.0f, 2.0f, 0.2f, 20.0f); // x,y, zmin zmax
D3DXMatrixMultiply(&matViewProj, &matView, &matProj);
// draw a single quad to texture:
// the quad covers the whole "screen" exactly
D3DXMatrixScaling(&matWorld, x_size[i], y_size[i], 1.0f);
D3DXMatrixMultiply(&matWorldViewProj, &matWorld, &matViewProj);
D3DXMatrixTranspose(&matWorldViewProj, &matWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &matWorldViewProj(0, 0), 4);
// Draw the square object
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
ASSERT_IF_FAILED(hr);
}
// reset world view proj matrix
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// Reset to original render target & depth
if( !bInlineDroplet )
{
hr = m_pD3DDev->SetRenderTarget( pBack_Color, pBack_Depth );
ASSERT_IF_FAILED(hr);
}
}
void CA_Water::DrawDroplet( float x, float y, bool bInlineDroplet )
{
//////////////////////////////////////////////////
// input x,y in range 0.0 to 1.0 to cover whole target
HRESULT hr;
assert( m_pD3DDev != NULL );
IDirect3DSurface8 *pBack_Color;
IDirect3DSurface8 *pBack_Depth;
/////////////////////////////
float scale = 10.0f;
scale += 25.0f * ((float)rand()/((float)RAND_MAX));
// shift x,y into -1 to 1 range
x = (x - 0.5f)*2.0f;
y = (y - 0.5f)*2.0f;
// FDebug("Rendering droplet at %f %f\n", x,y );
// Draw into the velocity texture:
if( bInlineDroplet )
{
// Rendering is happening in middle of physics calc.
// Render to appropriate velocity texture
hr = m_pD3DDev->SetRenderTarget( mpFilterTarget[m_nTex_VelTarg], NULL);
ASSERT_IF_FAILED(hr);
}
else
{
// rendering happening outside physics calc
// Render to appropriate velocity texture for next frame
scale = 14.0f;
m_pD3DDev->GetRenderTarget( &pBack_Color );
m_pD3DDev->GetDepthStencilSurface( &pBack_Depth );
SetInitialRenderStates();
hr = m_pD3DDev->SetStreamSource(0, m_pVertexBuffer, sizeof(QuadVertex));
ASSERT_IF_FAILED(hr);
hr = m_pD3DDev->SetRenderTarget( mpFilterTarget[m_nTex_VelTarg], NULL);
ASSERT_IF_FAILED(hr);
}
// alter matrix to place the droplet
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProj;
D3DXMATRIX matViewProj;
D3DXMATRIX matWorldViewProj;
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( x, y, 0.0f );
D3DXVECTOR3 vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
// Set World, View, Projection, and combination matrices.
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUp);
// 2.0f cause translation extends from -1 to 1
D3DXMatrixOrthoLH(&matProj, 2.0f, 2.0f, 0.2f, 20.0f); // x,y, zmin zmax
D3DXMatrixMultiply(&matViewProj, &matView, &matProj);
// draw a single quad to texture:
// the quad covers the whole "screen" exactly
D3DXMatrixScaling(&matWorld, 1.0f/scale, 1.0f/scale, 1.0f);
D3DXMatrixMultiply(&matWorldViewProj, &matWorld, &matViewProj);
D3DXMatrixTranspose(&matWorldViewProj, &matWorldViewProj);
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &matWorldViewProj(0, 0), 4);
///////////////////////////////////////
// Set render mode - additive
hr = m_pD3DDev->SetPixelShader( 0 );
ASSERT_IF_FAILED(hr);
hr = m_pD3DDev->SetVertexShader( m_dwVertexShader );
ASSERT_IF_FAILED(hr);
m_pD3DDev->SetTexture(0, m_pDropletTexture->GetTexture() );
// Additive blend
m_pD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
m_pD3DDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
m_pD3DDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR ); // blue only
m_pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pD3DDev->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
// Draw the droplet
hr = m_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
ASSERT_IF_FAILED(hr);
// reset world view proj matrix
m_pD3DDev->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &m_mWorldViewProj(0, 0), 4);
// Reset to original render target & depth
if( !bInlineDroplet )
{
hr = m_pD3DDev->SetRenderTarget( pBack_Color, pBack_Depth );
ASSERT_IF_FAILED(hr);
}
}
void CA_Water::Do_CreateNormalMapSteps()
{
HRESULT hr = S_OK;
D3DXVECTOR4 offset( 0.0f, 0.0f, 0.0f, 0.0f);
hr = m_pD3DDev->SetTexture( 0, mpTextureFiltered[m_nTex_HeightTarg] );
hr = m_pD3DDev->SetTexture( 1, mpTextureFiltered[m_nTex_HeightTarg] );
hr = m_pD3DDev->SetTexture( 2, mpTextureFiltered[m_nTex_HeightTarg] );
hr = m_pD3DDev->SetTexture( 3, mpTextureFiltered[m_nTex_HeightTarg] );
ASSERT_IF_FAILED(hr);
m_pD3DDev->SetRenderTarget( mpFilterTarget[ m_nTex_NrmlTarg ], NULL );
ASSERT_IF_FAILED( hr );
// if( m_bUseNormalMapMethod2 )
{
// Set constants for red & green scale factors (also essential color masks)
// Red mask first
float pix_masks[4] = { m_fNrmlSTScale, 0.0f, 0.0f, 0.0f };
hr = m_pD3DDev->SetPixelShaderConstant( PCN_RED_MASK, &pix_masks, 1 );
ASSERT_IF_FAILED(hr);
// Now green mask & scale:
pix_masks[0] = 0.0f;
pix_masks[1] = m_fNrmlSTScale;
hr = m_pD3DDev->SetPixelShaderConstant( PCN_GREEN_MASK, &pix_masks, 1 );
ASSERT_IF_FAILED(hr);
hr = m_pD3DDev->SetPixelShader( m_dwPSH_NormalMapCreate2_Scale );
ASSERT_IF_FAILED(hr);
}
// use nearest neighbor offsets
offset.x = 4.0f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -