正好在看,转载一下。
#include "assert.h"
#include "d3dx9.h"
#pragma warning(disable : 4996)
#include "strsafe.h"
#pragma warning(disable : 4996)
LPDIRECT3D9 g_pD3DObj = NULL;
LPDIRECT3DDEVICE9 g_pD3DDev = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVerBuf = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
TCHAR g_AppName[] = TEXT("D3D");
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)//灵活顶点格式
struct CUSTOMVERTEX
{
D3DXVECTOR3 position;
D3DCOLOR color;
float tu, tv;
};
HRESULT InitD3D(HWND hWnd)
{
if((g_pD3DObj = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
assert(false);
return E_FAIL;
}
int numAdapter = g_pD3DObj->GetAdapterCount();
if (numAdapter <= 0)
{
assert(false);
return E_FAIL;
}
D3DDISPLAYMODE devMode;
if (FAILED(g_pD3DObj->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &devMode)))
{
assert(false);
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(g_pD3DObj->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3DDev
)))
{
assert(false);
return E_FAIL;
}
g_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pD3DDev->SetRenderState(D3DRS_ZENABLE, true);
g_pD3DDev->SetRenderState(D3DRS_LIGHTING, false);
return S_OK;
}
HRESULT InitVBuf()
{
if (FAILED(D3DXCreateTextureFromFile(g_pD3DDev, TEXT("banana.bmp"), &g_pTexture)))
{
return E_FAIL;
}
if (FAILED(g_pD3DDev->CreateVertexBuffer(
4*sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pVerBuf,
NULL
)))
{
return E_FAIL;
}
CUSTOMVERTEX *vertices;
if (FAILED(g_pVerBuf->Lock(0, 0, (void**)&vertices, 0)))
{
return E_FAIL;
}
vertices[0].position = D3DXVECTOR3(-1.0f,-1.0f, 0.0f);
vertices[0].color = 0xffff0000;
vertices[0].tu = 0;
vertices[0].tv = 0.5;
vertices[1].position = D3DXVECTOR3( 1.0f,-1.0f, 0.0f);
vertices[1].color = 0xff0000ff;
vertices[1].tu = 0.5;
vertices[1].tv = 0.5;
vertices[2].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f);
vertices[2].color = 0xffffffff;
vertices[2].tu = 0.5;
vertices[2].tv = 0;
vertices[3].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
vertices[3].color = 0xff000fff;
vertices[3].tu = 0;
vertices[3].tv = 0;
g_pVerBuf->Unlock();
return S_OK;
}
HRESULT SetUpMatrices()
{
D3DXMATRIXA16 matView;
D3DXMatrixIdentity(&matView);
D3DXVECTOR3 vEyeAt(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 vEyeLook(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vEyeUp(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &vEyeAt, &vEyeLook, &vEyeUp);
g_pD3DDev->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIXA16 matProj;
D3DXMatrixIdentity(&matProj);
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
g_pD3DDev->SetTransform(D3DTS_PROJECTION, &matProj);
return S_OK;
}
HRESULT SetUpWorld()
{
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity(&matWorld);
g_pD3DDev->SetTransform(D3DTS_WORLD, &matWorld);
return S_OK;
}
VOID Cleanup()
{
if (g_pVerBuf != NULL)
{
g_pVerBuf->Release();
}
if (g_pD3DDev != NULL)
{
g_pD3DDev->Release();
}
if (g_pD3DObj != NULL)
{
g_pD3DObj->Release();
}
}
VOID Render()
{
if (g_pD3DDev == NULL)
{
return ;
}
g_pD3DDev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255),1.0f, 0);
if (SUCCEEDED(g_pD3DDev->BeginScene()))
{
SetUpWorld();
g_pD3DDev->SetTexture( 0, g_pTexture );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pD3DDev->SetStreamSource(0, g_pVerBuf, 0, sizeof(CUSTOMVERTEX));
g_pD3DDev->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
g_pD3DDev->EndScene();
}
g_pD3DDev->Present(NULL, NULL, NULL, NULL);
}
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);
}
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_CLASSDC,
MsgProc,
0L,
0L,
GetModuleHandle(NULL),
NULL,
NULL,
NULL,
NULL,
g_AppName,
NULL
};
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(
g_AppName,
TEXT("D3D Tutorial 1"),
WS_OVERLAPPEDWINDOW,
100,
100,
300,
300,
NULL,
NULL,
wc.hInstance,
NULL
);
if (hWnd == NULL)
{
return -1;
}
if(SUCCEEDED(InitD3D(hWnd)))
{
if(SUCCEEDED(InitVBuf()) && SUCCEEDED(SetUpMatrices()))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
}
}
UnregisterClass(g_AppName, wc.hInstance);
return 0;
}
#include "assert.h"
#include "d3dx9.h"
#pragma warning(disable : 4996)
#include "strsafe.h"
#pragma warning(disable : 4996)
LPDIRECT3D9 g_pD3DObj = NULL;
LPDIRECT3DDEVICE9 g_pD3DDev = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVerBuf = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
TCHAR g_AppName[] = TEXT("D3D");
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)//灵活顶点格式
struct CUSTOMVERTEX
{
D3DXVECTOR3 position;
D3DCOLOR color;
float tu, tv;
};
HRESULT InitD3D(HWND hWnd)
{
if((g_pD3DObj = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
assert(false);
return E_FAIL;
}
int numAdapter = g_pD3DObj->GetAdapterCount();
if (numAdapter <= 0)
{
assert(false);
return E_FAIL;
}
D3DDISPLAYMODE devMode;
if (FAILED(g_pD3DObj->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &devMode)))
{
assert(false);
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(g_pD3DObj->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3DDev
)))
{
assert(false);
return E_FAIL;
}
g_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pD3DDev->SetRenderState(D3DRS_ZENABLE, true);
g_pD3DDev->SetRenderState(D3DRS_LIGHTING, false);
return S_OK;
}
HRESULT InitVBuf()
{
if (FAILED(D3DXCreateTextureFromFile(g_pD3DDev, TEXT("banana.bmp"), &g_pTexture)))
{
return E_FAIL;
}
if (FAILED(g_pD3DDev->CreateVertexBuffer(
4*sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pVerBuf,
NULL
)))
{
return E_FAIL;
}
CUSTOMVERTEX *vertices;
if (FAILED(g_pVerBuf->Lock(0, 0, (void**)&vertices, 0)))
{
return E_FAIL;
}
vertices[0].position = D3DXVECTOR3(-1.0f,-1.0f, 0.0f);
vertices[0].color = 0xffff0000;
vertices[0].tu = 0;
vertices[0].tv = 0.5;
vertices[1].position = D3DXVECTOR3( 1.0f,-1.0f, 0.0f);
vertices[1].color = 0xff0000ff;
vertices[1].tu = 0.5;
vertices[1].tv = 0.5;
vertices[2].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f);
vertices[2].color = 0xffffffff;
vertices[2].tu = 0.5;
vertices[2].tv = 0;
vertices[3].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
vertices[3].color = 0xff000fff;
vertices[3].tu = 0;
vertices[3].tv = 0;
g_pVerBuf->Unlock();
return S_OK;
}
HRESULT SetUpMatrices()
{
D3DXMATRIXA16 matView;
D3DXMatrixIdentity(&matView);
D3DXVECTOR3 vEyeAt(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 vEyeLook(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vEyeUp(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &vEyeAt, &vEyeLook, &vEyeUp);
g_pD3DDev->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIXA16 matProj;
D3DXMatrixIdentity(&matProj);
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
g_pD3DDev->SetTransform(D3DTS_PROJECTION, &matProj);
return S_OK;
}
HRESULT SetUpWorld()
{
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity(&matWorld);
g_pD3DDev->SetTransform(D3DTS_WORLD, &matWorld);
return S_OK;
}
VOID Cleanup()
{
if (g_pVerBuf != NULL)
{
g_pVerBuf->Release();
}
if (g_pD3DDev != NULL)
{
g_pD3DDev->Release();
}
if (g_pD3DObj != NULL)
{
g_pD3DObj->Release();
}
}
VOID Render()
{
if (g_pD3DDev == NULL)
{
return ;
}
g_pD3DDev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255),1.0f, 0);
if (SUCCEEDED(g_pD3DDev->BeginScene()))
{
SetUpWorld();
g_pD3DDev->SetTexture( 0, g_pTexture );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pD3DDev->SetStreamSource(0, g_pVerBuf, 0, sizeof(CUSTOMVERTEX));
g_pD3DDev->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
g_pD3DDev->EndScene();
}
g_pD3DDev->Present(NULL, NULL, NULL, NULL);
}
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);
}
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_CLASSDC,
MsgProc,
0L,
0L,
GetModuleHandle(NULL),
NULL,
NULL,
NULL,
NULL,
g_AppName,
NULL
};
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(
g_AppName,
TEXT("D3D Tutorial 1"),
WS_OVERLAPPEDWINDOW,
100,
100,
300,
300,
NULL,
NULL,
wc.hInstance,
NULL
);
if (hWnd == NULL)
{
return -1;
}
if(SUCCEEDED(InitD3D(hWnd)))
{
if(SUCCEEDED(InitVBuf()) && SUCCEEDED(SetUpMatrices()))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
}
}
UnregisterClass(g_AppName, wc.hInstance);
return 0;
}