DirectX11 学习笔记1 - 第一个程序
Posted lytwajue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DirectX11 学习笔记1 - 第一个程序相关的知识,希望对你有一定的参考价值。
为了加快学习速度,和使程序更加easy理解。 我把sampler tutorial里面的一个样例 的固定代码和常常修改的代码经过简单的类的封装了一下。
以后学习的时候就能够仅仅在还有一个文件写render渲染代码和fx着色器代码了
D3DBase.h 基本框架代码
#include <windows.h> #include <d3d11.h> #include <d3dx11.h> #include <d3dcompiler.h> #include <xnamath.h> #include "resource.h" //-------------------------------------------------------------------------------------- // Global Variables //-------------------------------------------------------------------------------------- HINSTANCE g_hInst = NULL; HWND g_hWnd = NULL; D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL; D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0; ID3D11Device* g_pd3dDevice = NULL; ID3D11DeviceContext* g_pImmediateContext = NULL; IDXGISwapChain* g_pSwapChain = NULL; ID3D11RenderTargetView* g_pRenderTargetView = NULL; ID3D11VertexShader* g_pVertexShader = NULL; ID3D11PixelShader* g_pPixelShader = NULL; ID3D11InputLayout* g_pVertexLayout = NULL; ID3D11Buffer* g_pVertexBuffer = NULL; //顶点缓冲 ID3D11Buffer* g_pIndexBuffer = NULL; //顶点索引 ID3D11Buffer* g_pConstantBuffer = NULL; //转换矩阵 //转换矩阵 XMMATRIX g_World; XMMATRIX g_View; XMMATRIX g_Projection; //-------------------------------------------------------------------------------------- // Structures //-------------------------------------------------------------------------------------- struct SimpleVertex { XMFLOAT3 Pos;//位置 XMFLOAT4 Color;//颜色 }; struct ConstantBuffer { XMMATRIX mWorld;//世界矩阵 XMMATRIX mView;//观察矩阵 XMMATRIX mProjection;//投影矩阵 }; class D3DBase { public: //-------------------------------------------------------------------------------------- // Forward declarations //-------------------------------------------------------------------------------------- HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow,float w,float h ); virtual HRESULT InitDevice(); virtual void Render()=0; virtual HRESULT VertexIndex()=0; virtual HRESULT VS_Shader(); virtual HRESULT InputLayout(); virtual HRESULT PS_Shader(); static int BeginD3D(D3DBase &d3d,HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow); virtual void CleanupDevice(); virtual HRESULT CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut ); public: ID3DBlob* pVSBlob,*pPSBlob; UINT width; UINT height; }; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); //-------------------------------------------------------------------------------------- // Register class and create window //-------------------------------------------------------------------------------------- HRESULT D3DBase::InitWindow( HINSTANCE hInstance, int nCmdShow,float w,float h ) { // Register class 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 = LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 ); wcex.hCursor = LoadCursor( NULL, IDC_ARROW ); wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 ); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"TutorialWindowClass"; wcex.hIconSm = LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_TUTORIAL1 ); if( !RegisterClassEx( &wcex ) ) return E_FAIL; // Create window g_hInst = hInstance; RECT rc = { 0, 0, w, h }; AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE ); g_hWnd = CreateWindow( L"TutorialWindowClass", L"Direct3D 11 Tutorial 3: Shaders", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL ); if( !g_hWnd ) return E_FAIL; ShowWindow( g_hWnd, nCmdShow ); return S_OK; } //-------------------------------------------------------------------------------------- // Create Direct3D device and swap chain //-------------------------------------------------------------------------------------- HRESULT D3DBase::InitDevice() { HRESULT hr = S_OK; RECT r
以上是关于DirectX11 学习笔记1 - 第一个程序的主要内容,如果未能解决你的问题,请参考以下文章