Second DirectX11 App
写的第二个DX11程序
//===================================================
// 第二个DirectX11程序,写于12年3月16日
//===================================================
#include <d3d11.h>
#include <D3DX11.h>
#include <xnamath.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
//===================================================
// 全局变量的声明
//===================================================
ID3D11Device *g_pD3dDevice;//设备
IDXGISwapChain *g_pSwapChain;//交换链
ID3D11RenderTargetView *g_pRenderTargetView;//渲染目标视点
ID3D11DeviceContext *g_pImContext;//立即环境
HWND g_hWnd;//窗口句柄
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_HARDWARE;//指定为硬件渲染
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;//特征等级
ID3D11VertexShader *g_pVShader = NULL;
ID3D11PixelShader *g_pPShader = NULL;
ID3D11InputLayout *g_pInputLayout = NULL;
ID3D11Buffer *g_pVertexBuffer = NULL;
struct SimpleVertex
{
XMFLOAT3 pos;
};
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
//=================================
//初始化窗口
//=================================
void InitWindow(HINSTANCE hInstance, int nShowCmd)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"TutorialWindowClass";
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
// Create window
RECT rc = { 0, 0, 640, 480 };
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
g_hWnd = CreateWindow( L"TutorialWindowClass", L"Second DX11 App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
NULL );
ShowWindow( g_hWnd, nShowCmd);
}
void InitD3D()
{
RECT rt;
GetWindowRect(g_hWnd, &rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0
};
DXGI_SWAP_CHAIN_DESC sd;//结构体初始化
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Height = height;
sd.BufferDesc.Width = width;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
//创建设备和交换链
HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL, g_driverType, 0, 0, featureLevels, 1, D3D11_SDK_VERSION,
&sd, &g_pSwapChain, &g_pD3dDevice, &g_featureLevel, &g_pImContext);
if(FAILED(hr))
{
MessageBox(NULL, L"A", L"A", 0);
}
//创建背景缓冲区
ID3D11Texture2D *pBackBuffer = NULL;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&pBackBuffer);
g_pD3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
pBackBuffer->Release();
g_pImContext->OMSetRenderTargets(1, &g_pRenderTargetView, 0);
//设置观察点
D3D11_VIEWPORT vp;
vp.Height = (FLOAT)height;
vp.Width = (FLOAT)width;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
g_pImContext->RSSetViewports(1, &vp);
//=================================================
// 以下为真正创建shader和vertex buffer
// 真正今天写的新的东西
//=================================================
//编译Vertex Shader
ID3DBlob *pVSBlob = NULL;
hr = D3DX11CompileFromFile(L"Shader.fx", NULL, NULL, "VS", "vs_4_0", 0, 0, NULL, &pVSBlob, 0, 0);
if(FAILED(hr))
{
MessageBox(NULL, L"B", L"B", 0);
}
//穿件VertexShader
g_pD3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVShader);
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
hr = g_pD3dDevice->CreateInputLayout(layout, 1, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &g_pInputLayout);
if(FAILED(hr))
MessageBox(NULL, L"E", L"E", 0);
g_pImContext->IASetInputLayout(g_pInputLayout);
ID3DBlob *pPSBlob = NULL;
hr = D3DX11CompileFromFile(L"Shader.fx", 0, 0, "PS", "ps_4_0", 0, 0, NULL, &pPSBlob, NULL, 0);
g_pD3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPShader);
if(FAILED(hr))
{
MessageBox(NULL, L"C", L"C", 0);
}
SimpleVertex vertices[] =
{
XMFLOAT3( 0.0f, 0.5f, 0.5f),
XMFLOAT3( 0.5f, -0.5f, 0.5f ),
XMFLOAT3( -0.5f, -0.5f, 0.5f ),
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = 3 * sizeof(SimpleVertex);
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices;
hr = g_pD3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer);
if(FAILED(hr))
MessageBox(NULL, L"D", L"D", 0);
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_pImContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
g_pImContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
void Clean()
{
if( g_pImContext ) g_pImContext->ClearState();
if( g_pVertexBuffer ) g_pVertexBuffer->Release();
if( g_pInputLayout ) g_pInputLayout->Release();
if( g_pVShader ) g_pVShader->Release();
if( g_pPShader ) g_pPShader->Release();
if( g_pRenderTargetView ) g_pRenderTargetView->Release();
if( g_pSwapChain ) g_pSwapChain->Release();
if( g_pImContext ) g_pImContext->Release();
if( g_pD3dDevice ) g_pD3dDevice->Release();
}
void Render()
{
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f};
g_pImContext->ClearRenderTargetView(g_pRenderTargetView, ClearColor);
g_pImContext->VSSetShader(g_pVShader, NULL, 0);
g_pImContext->PSSetShader(g_pPShader, NULL, 0);
g_pImContext->Draw(3, 0);
g_pSwapChain->Present(0, 0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nShowCmd)
{
InitWindow(hInstance, nShowCmd);
InitD3D();
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();
}
}
}
shader.fx
//================================================
// shader
//================================================
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
return Pos;
}
float4 PS( float4 Pos : SV_POSITION) : SV_Target
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f);
}
浙公网安备 33010602011771号