完整代码下载,看这里。
Tutorial02.fx
//顶点着色器
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 );
}
main.cpp
#pragma once
#include <Windows.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3Dcompiler.h>
#include <xnamath.h>
#include <tchar.h>
struct SimpleVertex //定义顶点结构体
{
XMFLOAT3 Pos;
};
//全局变量
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_HARDWARE;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
HINSTANCE g_hInstance = NULL;;
HWND g_hWnd = NULL;
ID3D11Device* g_device = NULL;
ID3D11DeviceContext* g_deviceContext = NULL;
IDXGISwapChain* g_swapChain = NULL;
ID3D11RenderTargetView* g_renderTargetView = NULL;
ID3D11VertexShader* g_vertexShader = NULL;
ID3D11PixelShader* g_pixelShader = NULL;
ID3D11InputLayout* g_inputLayout = NULL;
ID3D11Buffer* g_vertexBuffer = NULL;
//函数声明
bool InitWindow( HINSTANCE hInstance, int nCmdShow );
HRESULT InitDevice();
HRESULT CompileShaderFromFile( WCHAR* filename, LPCSTR entrypoint, LPCSTR shaderModel, ID3DBlob* pBlob );
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
void Render();
void ShutdownDevice();
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPreInstance, LPWSTR nCmdLine, int nCmdShow )
{
bool bResult;
g_hInstance = hInstance;
//初始化窗口
bResult = InitWindow( g_hInstance, nCmdShow );
if ( !bResult )
{
return 0;
}
//初始化设备(设备、设备上下文、渲染目标视图、顶点着色器、像素着色器、顶点缓存等)
HRESULT hResult;
hResult = InitDevice();
if ( FAILED(hResult) )
{
return 0;
}
//消息循环
MSG msg = { 0 };
while( msg.message != WM_QUIT )
{
if ( PeekMessage( &msg , NULL, 0, 0, PM_REMOVE ) )//处理键盘、鼠标等消息,给出响应
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();//图形图像渲染
}
}
//关闭设备
ShutdownDevice();