#include <iostream>
#include "DxStdAfx.h"
#include "RenderSystem.h"
#include "CTextureTarget.h"
#include "CRound.h"
#include "CPanel.h"
#define WinW 512.0f
#define WinH 512.0f
//-----------------------------------
enum RenderTargetType
{
TEX_HEIGHT0 = 0,
TEX_HEIGHT1,//
TEX_HEIGHT2,
TEX_NORMAL,
TEX_MAx,
};
int g_nTexTargetId[TEX_MAx] = {0};
CPanel *g_pCPanel[TEX_MAx] = {0};
//-----------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
bool Init(HINSTANCE hInstance, HWND hWnd);
void Render();
void Cleanup();
// 接触点
void AddImpulse();
// 下一次的高度
void WaterSimulation();
// 法线
void ConvertNormal();
// 背景
void Water();
// 交换
void SwapHeightmaps();
//-----------------------------------
CRenderSystem *g_pCRenderSystem = NULL;
//Direct3D设备对象
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
// 效果对象
LPD3DXEFFECT g_pEffect = NULL;
// 水面背景纹理
LPDIRECT3DTEXTURE9 g_pWater = NULL;
//
D3DXMATRIX g_mWorld;
D3DMATERIAL9 g_matre;
Round *g_pRound = NULL;
CPanel *g_pMainCPanel = NULL;
CPanel *g_pMeshPanel = NULL;
//-----------------------------------
INT WINAPI WinMain( __in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in_opt LPSTR lpCmdLine,
__in int nShowCmd )
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"ClassName", NULL };
RegisterClassEx( &wc );
//创建窗口
HWND hWnd = CreateWindow( "ClassName", "水波模拟",
WS_OVERLAPPED| WS_CAPTION |WS_SYSMENU, 200, 100, (DWORD)WinW, (DWORD)WinH,
NULL, NULL, wc.hInstance, NULL );
if( Init(hInstance,hWnd) == false )
return 0;
//显示主窗口
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//进入消息循环
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render(); //渲染图形
}
}
UnregisterClass( "ClassName", wc.hInstance );
return 0;
return 0;
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_LBUTTONUP:
{
int iMouseX = (int)LOWORD(lParam);
int iMouseY = (int)HIWORD(lParam);
if(g_pRound != NULL)
{
g_pRound->SetPos(D3DXVECTOR2(iMouseX,iMouseY));
}
}break;
case WM_MOUSEMOVE:
{
int iMouseX = (int)LOWORD(lParam);
int iMouseY = (int)HIWORD(lParam);
if(g_pRound != NULL)
{
g_pRound->SetPos(D3DXVECTOR2(iMouseX,iMouseY));
}/**/
}break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
};
bool Init(HINSTANCE hInstance, HWND hWnd)
{
g_pCRenderSystem = new CRenderSystem;
if(!g_pCRenderSystem->createRenderSystem(hInstance,hWnd))
return false;
g_pd3dDevice = g_pCRenderSystem->getDirect3DDecice9();
if(!g_pCRenderSystem->loadingEffectFile("Header.fx",&g_pEffect))
{
return false;
};
D3DXCreateTextureFromFile( g_pd3dDevice, "lena.tga", &g_pWater);
D3DVIEWPORT9 Viewport;
Viewport.Height = (DWORD)WinH;
Viewport.Width = (DWORD)WinW;
Viewport.MaxZ = 1;
Viewport.MinZ = 0;
Viewport.X = 0;
Viewport.Y = 0;
g_pd3dDevice->SetViewport(&Viewport);
D3DXMatrixIdentity(&g_mWorld);
/////////////////////////////////////////////////////////////////////////////////
int x = 40;
for(int i=0;i<TEX_MAx;++i)
{
g_nTexTargetId[i] = g_pCRenderSystem->createRenderTarget_In_texture(WinW,WinH);
CRenderTarget *pCRenderTarget = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[i]);
pCRenderTarget->setUseDepthSurface(false);
g_pCPanel[i] = new CPanel();
g_pCPanel[i]->create(g_pd3dDevice,D3DXVECTOR2(x,40),40,40);
x += 45*2;
}
g_pRound = new Round;
g_pRound->Create(g_pd3dDevice,D3DXVECTOR2(WinW/2,WinH/2),7.0f);
////////////////////////////////////////////////////////////////////////////////
g_pMainCPanel = new CPanel;
g_pMainCPanel->create(g_pd3dDevice,D3DXVECTOR2(WinW/2,WinH/2),WinW/2,WinH/2);
g_pMeshPanel = new CPanel;
g_pMeshPanel->create(g_pd3dDevice,D3DXVECTOR2(WinW/2,WinH/2),30,30);
// 初始化一些状态
// 灯光
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
return true;
};
void Render()
{
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0,0,0,0),1.0f,0);
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
AddImpulse();
WaterSimulation();
ConvertNormal();
g_pCRenderSystem->setRenderTargetToMain();
//g_pd3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0x0, 1.0f, 0);
Water();
SwapHeightmaps();
//g_pRound->render(g_pd3dDevice);
for(int i=0;i<TEX_MAx;++i)
{
if(i == TEX_HEIGHT2)
{
g_pEffect->SetTechnique("Heightmap");
g_pEffect->Begin(NULL,0);
g_pEffect->BeginPass(0);
CTextureTarget *pCTextureTarget = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[i]);
g_pCPanel[i]->render(g_pd3dDevice,pCTextureTarget->getTexture());
g_pEffect->EndPass();
g_pEffect->End();
}else
{
CTextureTarget *pCTextureTarget = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[i]);
g_pCPanel[i]->render(g_pd3dDevice,pCTextureTarget->getTexture());
}
}
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(0,0,0,0);
}
};
void Cleanup()
{
//释放Direct3D设备对象
if( g_pCRenderSystem != NULL)
{
delete g_pCRenderSystem;
g_pCRenderSystem = NULL;
}
if(g_pEffect != NULL)
{
g_pEffect->Release();
g_pEffect = NULL;
}
if(g_pMainCPanel != NULL)
{
delete g_pMainCPanel;
g_pMainCPanel = NULL;
}
if(g_pRound != NULL)
{
delete g_pRound;
g_pRound = NULL;
}
for (int i = 0; i < TEX_MAx; i++)
{
delete g_pCPanel[i];
g_pCPanel[i] = NULL;
}
};
// 接触点
void AddImpulse()
{
static D3DXVECTOR2 vPositive = g_pRound->GetCurPos();
D3DXVECTOR2 v = g_pRound->GetCurPos() - vPositive;
if (D3DXVec2Length(&v) < 25.0f)
{
return;
}
vPositive = g_pRound->GetCurPos();
g_pCRenderSystem->setRenderTarget(g_nTexTargetId[TEX_HEIGHT1]);
g_pEffect->SetTechnique("AddImpulse");
g_pEffect->SetFloat("fForce",-5);
UINT nPassNum = 0;
g_pEffect->Begin(&nPassNum,0);
for (int i = 0; i < nPassNum; i++)
{
g_pEffect->BeginPass(i);
g_pRound->render(g_pd3dDevice);
g_pEffect->EndPass();
}
g_pEffect->End();
};
// 下一次的高度
void WaterSimulation()
{
g_pCRenderSystem->setRenderTarget(g_nTexTargetId[TEX_HEIGHT2]);
CTextureTarget *pCTextureTarget0 = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[TEX_HEIGHT0]);
CTextureTarget *pCTextureTarget1 = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[TEX_HEIGHT1]);
D3DXVECTOR4 texturesize;
texturesize.x = 1.0f/WinW;
texturesize.y = 1.0f/WinH;
texturesize.z = WinW;
texturesize.w = WinH;
g_pEffect->SetTechnique("WaterSimulation");
g_pEffect->SetTexture("heightmap_prev" ,pCTextureTarget0->getTexture());
g_pEffect->SetTexture("heightmap_current",pCTextureTarget1->getTexture());
g_pEffect->SetVector("texture_size",&texturesize);
g_pEffect->SetFloat("fDamping",0.95f);
UINT nPassNum = 0;
g_pEffect->Begin(&nPassNum,0);
for (int i = 0; i < nPassNum; i++)
{
g_pEffect->BeginPass(i);
g_pEffect->CommitChanges();
g_pMainCPanel->render(g_pd3dDevice,NULL);
g_pEffect->EndPass();
}
g_pEffect->End();
};
// 法线
void ConvertNormal()
{
g_pCRenderSystem->setRenderTarget(g_nTexTargetId[TEX_NORMAL]);
CTextureTarget *pCTextureTarget2 = g_pCRenderSystem->getRenderTarget(g_nTexTargetId[TEX_HEIGHT2]);
g_pEffect->SetTechnique
- 1
- 2
- 3
前往页