#include <d3d9.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 )
#include <strsafe.h>
#pragma warning( default : 4996 )
#include <d3dx9core.h>
wchar_t *g_pClassName = L"Transform";
wchar_t *g_pWindowName = L"空间坐标变换实例";
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3D9 g_pD3D = NULL;
LPD3DXFONT g_pFont = 0;
WCHAR* strText = L"鼠标左键控制着色模式";
RECT clientRect;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuf = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIndexBuf = NULL;
struct CUSTOMVERTEX
{
FLOAT _x,_y,_z;
DWORD _color;
CUSTOMVERTEX(FLOAT x,FLOAT y,FLOAT z,DWORD color)
:_x(x),_y(y),_z(z),_color(color){}
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
HRESULT InitDirect3D(HWND hWnd)
{
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
if( FAILED(D3DXCreateFont(g_pd3dDevice,0,0,0,0,0,0,0,0,0,L"Arial",&g_pFont)))
return E_FAIL;
GetClientRect(hWnd,&clientRect);
g_pd3dDevice->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX), 0 ,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,
&g_pVertexBuf,NULL
);
CUSTOMVERTEX *pVertices = NULL;
g_pVertexBuf->Lock(0,0,(void**)&pVertices,0);
pVertices[0] = CUSTOMVERTEX(-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0));
pVertices[1] = CUSTOMVERTEX(-5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,255,0));
pVertices[2] = CUSTOMVERTEX(5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,0,255));
pVertices[3] = CUSTOMVERTEX(5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,255,0));
pVertices[4] = CUSTOMVERTEX(-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,255,255));
pVertices[5] = CUSTOMVERTEX(-5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,255));
pVertices[6] = CUSTOMVERTEX(5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(0,255,255));
pVertices[7] = CUSTOMVERTEX(5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(0,0,0));
g_pVertexBuf->Unlock();
g_pd3dDevice->CreateIndexBuffer(36 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndexBuf,NULL);
WORD * pIndices = NULL;
g_pIndexBuf->Lock(0,0,(void**)&pIndices,0);
pIndices[0] = 0;pIndices[1] = 1;pIndices[2]=2;
pIndices[3] = 0;pIndices[4] = 2;pIndices[5]=3;
pIndices[6] = 0;pIndices[7] = 3;pIndices[8]=7;
pIndices[9] = 0;pIndices[10] = 7;pIndices[11]=4;
pIndices[12] = 0;pIndices[13] = 4;pIndices[14]=5;
pIndices[15] = 0;pIndices[16] = 5;pIndices[17]=1;
pIndices[18] = 2;pIndices[19] = 6;pIndices[20]=7;
pIndices[21] = 2;pIndices[22] = 7;pIndices[23]=3;
pIndices[24] = 2;pIndices[25] = 5;pIndices[26]=6;
pIndices[27] = 2;pIndices[28] = 1;pIndices[29]=5;
pIndices[30] = 4;pIndices[31] = 6;pIndices[32]=5;
pIndices[33] = 4;pIndices[34] = 7;pIndices[35]=6;
g_pIndexBuf->Unlock();
return S_OK;
}
VOID Direct3DRender()
{
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
g_pd3dDevice->BeginScene();
D3DXMATRIX matWorld,Rx,Ry,Rz;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationX(&Rx,::timeGetTime() / 1000.0f);
D3DXMatrixRotationY(&Ry,::timeGetTime() / 1000.0f);
D3DXMatrixRotationZ(&Rz,::timeGetTime() / 1000.0f);
matWorld = Rx * Ry * Rz * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld);
D3DXMATRIX matView;
D3DXVECTOR3 vEye(0.0f,0.0f,-30.0f);
D3DXVECTOR3 vAt(0.0f,0.0f,0.0f);
D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH( &matView,&vEye,&vAt,&vUp);
g_pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI / 4.0f,1.0f,1.0f,1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
g_pd3dDevice->SetStreamSource(0,g_pVertexBuf,0,sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->SetIndices(g_pIndexBuf);
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
VOID Cleanup()
{
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
if(g_pIndexBuf != NULL)
{
g_pIndexBuf->Release();
}
if(g_pVertexBuf != NULL)
g_pVertexBuf->Release();
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
Direct3DRender();
return 0;
case WM_LBUTTONDOWN:
Direct3DRender();
return 0;
case WM_RBUTTONDOWN:
Direct3DRender();
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
UNREFERENCED_PARAMETER( hInst );
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"DirectX游戏编程一", NULL
};
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( L"DirectX游戏编程一", L"DirectX游戏编程:空间坐标变换实例",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
if( SUCCEEDED( InitDirect3D( hWnd ) ) )
{
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
UnregisterClass( L"DirectX游戏编程一", wc.hInstance );
return 0;
}
}