#include
#include
#include
#pragma warning( disable : 4996 ) // disable deprecated warning
#include
#pragma warning( default : 4996 )
#include
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;
LPDIRECT3DTEXTURE9 earthtexture = NULL;
LPDIRECT3DTEXTURE9 galaxytexture = NULL;
LPDIRECT3DPIXELSHADER9 MultiPS = 0;
LPD3DXCONSTANTTABLE MultiCT = 0;
D3DXHANDLE EatrhHandle = 0;
D3DXHANDLE GalaxyHandle = 0;
D3DXCONSTANT_DESC earthDesc;
D3DXCONSTANT_DESC galaxyDesc;
struct CUSTOMVERTEX
{
FLOAT x, y, z;
FLOAT u0,v0;
FLOAT u1,v1;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEX2)
HRESULT InitD3D(HWND hWnd)
{
// Create the D3D object.
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
{
return E_FAIL;
}
return S_OK;
}
void CreateShader()
{
ID3DXBuffer* shader = 0;
ID3DXBuffer* errorBuffer = 0;
//对着色器程序进行编译
HRESULT hr = D3DXCompileShaderFromFile(
L"ps_multitex.txt",
0, 0,
"Main",
"ps_1_1",
D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY,
&shader,
&errorBuffer,
&MultiCT);
//创建像素着色器
hr = g_pd3dDevice->CreatePixelShader(
(DWORD*)shader->GetBufferPointer(),
&MultiPS);
shader->Release();
}
void GetHandles()
{
EatrhHandle = MultiCT->GetConstantByName(0, "EarthTex");
GalaxyHandle = MultiCT->GetConstantByName(0, "GalaxyTex");
UINT count;
MultiCT->GetConstantDesc(EatrhHandle, &earthDesc, &count);
MultiCT->GetConstantDesc(GalaxyHandle, &galaxyDesc, &count);
MultiCT->SetDefaults(g_pd3dDevice);
}
bool CreateTecture()
{
if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice,L"galaxy.jpg",&galaxytexture)))
return false;
if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice,L"earth.jpg",&earthtexture)))
return false;
return true;
}
HRESULT CreateBuffer()
{
CUSTOMVERTEX vertices[] =
{
{ -10.0f, -10.0f, 0.0f, 0,1,0,1},
{ -10.0f, 10.0f, 0.0f, 0,0,0,0 },
{ 10.0f, 10.0f, 0.0f, 1,0,1,0},
{ 10.0f, -10.0f, 0.0f, 1,1,1,1},
};
// 创建顶点缓存
if (FAILED(g_pd3dDevice->CreateVertexBuffer(sizeof(vertices),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL)))
{
return E_FAIL;
}
VOID* pVertices;
if (FAILED(g_pVB->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
return E_FAIL;
memcpy(pVertices, (void*)vertices, sizeof(vertices));
g_pVB->Unlock();
// 创建索引缓存
WORD indices[] = {0,1,2,0,2,3};
if( FAILED( g_pd3dDevice->CreateIndexBuffer( sizeof(indices),
0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &g_pIB, NULL ) ) )
{
return E_FAIL;
}
VOID* pIndices = NULL;
if(FAILED(g_pIB->Lock(0, sizeof(indices), (void**)&pIndices,0)))
return E_FAIL;
memcpy(pIndices,(VOID*)indices, sizeof(indices));
g_pIB->Unlock();
return S_OK;
}
VOID SetupMatrices()
{
D3DXVECTOR3 position( 0, 0, -20);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &V);
//D3DXMatrixPerspectiveFovLH()函数中的最远、最近距离为相对于视点的距离(即vEyePt中的距离)
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
VOID Cleanup()
{
if( earthtexture != NULL )
earthtexture->Release();
if( galaxytexture != NULL )
galaxytexture->Release();
if (g_pVB != NULL)
g_pVB->Release();
if(g_pIB != NULL)
g_pIB->Release();
if (g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if (g_pD3D != NULL)
g_pD3D->Release();
if(MultiPS != NULL)
MultiPS->Release();
if(MultiCT != NULL)
MultiCT->Release();
}
VOID Render()
{
CreateShader();
GetHandles();
CreateTecture();
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
// Begin the scene
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
SetupMatrices();
g_pd3dDevice->SetPixelShader(MultiPS);
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->SetIndices(g_pIB);
g_pd3dDevice->SetTexture(earthDesc.RegisterIndex, earthtexture);
g_pd3dDevice->SetSamplerState(earthDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(earthDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(earthDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetTexture(galaxyDesc.RegisterIndex, galaxytexture);
g_pd3dDevice->SetSamplerState(galaxyDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(galaxyDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(galaxyDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Cleanup();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT)
{
UNREFERENCED_PARAMETER(hInst);
// Register the window class
WNDCLASSEX wc =
{
sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow(L"D3D Tutorial", L"D3D Tutorial 03: MultiTexture",
WS_OVERLAPPEDWINDOW, 100, 100, 700, 700,
NULL, NULL, wc.hInstance, NULL);
// Initialize Direct3D
if (SUCCEEDED(InitD3D(hWnd)))
{
// Create the scene geometry
if (SUCCEEDED(CreateBuffer()))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Enter the message loop
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(L"D3D Tutorial", wc.hInstance);
return 0;
}