DirectX11笔记(三)--Direct3D初始化代码

2024-05-11 22:58

本文主要是介绍DirectX11笔记(三)--Direct3D初始化代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里先提出InitDevice方法, 讲解请参考前一篇文章: DirectX11笔记(三)–Direct3D初始化2

HRESULT InitDevice( )
{// 窗口信息RECT rc;GetClientRect( g_hWnd, &rc );UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 创建交换链描述DXGI_SWAP_CHAIN_DESC sd;sd.BufferDesc.Width = width;sd.BufferDesc.Height = height;sd.BufferDesc.RefreshRate.Numerator = 60;sd.BufferDesc.RefreshRate.Denominator = 1;sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.BufferCount = 1;sd.OutputWindow = g_hWnd;sd.Windowed = true;sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;sd.Flags = 0;sd.SampleDesc.Count = 1; // 不使用 MSAAsd.SampleDesc.Quality = 0;// 调试模式UINT createDeviceFlags = 0;#if defined( DEBUG ) || defined( _DEBUG )createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;#endif// 特征等级D3D_FEATURE_LEVEL featureLevels[] ={D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,};UINT numFeatureLevels = ARRAYSIZE( featureLevels );// 创建 ID3D11Device, ID3D11DeviceContext, IDXGISwapChainHRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,g_driverType,NULL,createDeviceFlags,featureLevels, numFeatureLevels,D3D11_SDK_VERSION,&sd,&g_pSwapChain,&g_pd3dDevice,&g_featureLevel,&g_pImmediateContext );if( FAILED(hr) ){MessageBox( 0, L"D3D11CreateDeviceAndSwapChain Failed.", 0, 0 );return false;}if( g_featureLevel != D3D_FEATURE_LEVEL_11_0 ){MessageBox( 0, L"Direct3D Feature Level 11 unsupported.", 0, 0 );return false;}// 创建 render target viewID3D11Texture2D* pBackBuffer = NULL;hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );if( FAILED( hr ) )return hr;hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );pBackBuffer->Release();if( FAILED( hr ) )return hr;// 创建深度模板缓冲D3D11_TEXTURE2D_DESC descDepth;ZeroMemory(&descDepth, sizeof(descDepth));descDepth.Width = width;descDepth.Height = height;descDepth.MipLevels = 1;descDepth.ArraySize = 1;descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;descDepth.SampleDesc.Count = 1;descDepth.SampleDesc.Quality = 0;descDepth.Usage = D3D11_USAGE_DEFAULT;descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;descDepth.CPUAccessFlags = 0;descDepth.MiscFlags = 0;hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencil);if (FAILED(hr))return hr;// 创建深度模板视图D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;ZeroMemory(&descDSV, sizeof(descDSV));descDSV.Format = descDepth.Format;descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;descDSV.Texture2D.MipSlice = 0;hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);if (FAILED(hr))return hr;g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );// 设置 viewportD3D11_VIEWPORT vp;vp.Width = (FLOAT)width;vp.Height = (FLOAT)height;vp.MinDepth = 0.0f;vp.MaxDepth = 1.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_pImmediateContext->RSSetViewports( 1, &vp );return true;
}

接下来是完整代码, 包括Win32程序的框架, 以后有时间的话补一个对Win32程序的简单说明

#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>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;
ID3D11Texture2D*        g_pDepthStencil = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
ID3D11VertexShader*     g_pVertexShader = NULL;
ID3D11PixelShader*      g_pPixelShader = NULL;
ID3D11InputLayout*      g_pVertexLayout = NULL;
ID3D11Buffer*           g_pVertexBuffer = NULL;
ID3D11Buffer*           g_pIndexBuffer = NULL;
ID3D11Buffer*           g_pVSConstBuffer = NULL;
ID3D11Buffer*           g_pPSConstBuffer = NULL;
UINT                    g_IndexNum = 0;HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HRESULT InitDevice( );int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{UNREFERENCED_PARAMETER( hPrevInstance );UNREFERENCED_PARAMETER( lpCmdLine );if( FAILED( InitWindow( hInstance, nCmdShow ) ) )return 0;if( FAILED( InitDevice() ) ){//CleanupDevice();return 0;}// Main message loopMSG msg = {0};while( WM_QUIT != msg.message ){if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){TranslateMessage( &msg );DispatchMessage( &msg );}else{//Render();}}//CleanupDevice();return ( int )msg.wParam;
}HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{// Register classWNDCLASSEX 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( NULL,IDC_ARROW );wcex.hCursor = LoadCursor( NULL, IDC_ARROW );wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );wcex.lpszMenuName = NULL;wcex.lpszClassName = L"Triangle3D";wcex.hIconSm = LoadIcon( NULL,IDC_ARROW );if( !RegisterClassEx( &wcex ) )return E_FAIL;// Create windowg_hInst = hInstance;RECT rc = { 0, 0, 640, 480 };AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );g_hWnd = CreateWindow( L"Triangle3D", L"Triangle3D", 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;
}LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{PAINTSTRUCT ps;HDC hdc;switch( message ){case WM_PAINT:hdc = BeginPaint( hWnd, &ps );EndPaint( hWnd, &ps );break;case WM_DESTROY:PostQuitMessage( 0 );break;default:return DefWindowProc( hWnd, message, wParam, lParam );}return 0;
}HRESULT InitDevice( )
{// 窗口信息RECT rc;GetClientRect( g_hWnd, &rc );UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 创建交换链描述DXGI_SWAP_CHAIN_DESC sd;sd.BufferDesc.Width = width;sd.BufferDesc.Height = height;sd.BufferDesc.RefreshRate.Numerator = 60;sd.BufferDesc.RefreshRate.Denominator = 1;sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.BufferCount = 1;sd.OutputWindow = g_hWnd;sd.Windowed = true;sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;sd.Flags = 0;sd.SampleDesc.Count = 1; // 不使用 MSAAsd.SampleDesc.Quality = 0;// 调试模式UINT createDeviceFlags = 0;#if defined( DEBUG ) || defined( _DEBUG )createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;#endif// 特征等级D3D_FEATURE_LEVEL featureLevels[] ={D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,};UINT numFeatureLevels = ARRAYSIZE( featureLevels );// 创建 ID3D11Device, ID3D11DeviceContext, IDXGISwapChainHRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,g_driverType,NULL,createDeviceFlags,featureLevels, numFeatureLevels,D3D11_SDK_VERSION,&sd,&g_pSwapChain,&g_pd3dDevice,&g_featureLevel,&g_pImmediateContext );if( FAILED(hr) ){MessageBox( 0, L"D3D11CreateDeviceAndSwapChain Failed.", 0, 0 );return false;}if( g_featureLevel != D3D_FEATURE_LEVEL_11_0 ){MessageBox( 0, L"Direct3D Feature Level 11 unsupported.", 0, 0 );return false;}// 创建 render target viewID3D11Texture2D* pBackBuffer = NULL;hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );if( FAILED( hr ) )return hr;hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );pBackBuffer->Release();if( FAILED( hr ) )return hr;// 创建深度模板缓冲D3D11_TEXTURE2D_DESC descDepth;ZeroMemory(&descDepth, sizeof(descDepth));descDepth.Width = width;descDepth.Height = height;descDepth.MipLevels = 1;descDepth.ArraySize = 1;descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;descDepth.SampleDesc.Count = 1;descDepth.SampleDesc.Quality = 0;descDepth.Usage = D3D11_USAGE_DEFAULT;descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;descDepth.CPUAccessFlags = 0;descDepth.MiscFlags = 0;hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencil);if (FAILED(hr))return hr;// 创建深度模板视图D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;ZeroMemory(&descDSV, sizeof(descDSV));descDSV.Format = descDepth.Format;descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;descDSV.Texture2D.MipSlice = 0;hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);if (FAILED(hr))return hr;g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );// 设置 viewportD3D11_VIEWPORT vp;vp.Width = (FLOAT)width;vp.Height = (FLOAT)height;vp.MinDepth = 0.0f;vp.MaxDepth = 1.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_pImmediateContext->RSSetViewports( 1, &vp );return true;
}

这篇关于DirectX11笔记(三)--Direct3D初始化代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980896

相关文章

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

轻松掌握python的dataclass让你的代码更简洁优雅

《轻松掌握python的dataclass让你的代码更简洁优雅》本文总结了几个我在使用Python的dataclass时常用的技巧,dataclass装饰器可以帮助我们简化数据类的定义过程,包括设置默... 目录1. 传统的类定义方式2. dataclass装饰器定义类2.1. 默认值2.2. 隐藏敏感信息

opencv实现像素统计的示例代码

《opencv实现像素统计的示例代码》本文介绍了OpenCV中统计图像像素信息的常用方法和函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 统计像素值的基本信息2. 统计像素值的直方图3. 统计像素值的总和4. 统计非零像素的数量

IDEA常用插件之代码扫描SonarLint详解

《IDEA常用插件之代码扫描SonarLint详解》SonarLint是一款用于代码扫描的插件,可以帮助查找隐藏的bug,下载并安装插件后,右键点击项目并选择“Analyze”、“Analyzewit... 目录SonajavascriptrLint 查找隐藏的bug下载安装插件扫描代码查看结果总结Sona

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

Java实现批量化操作Excel文件的示例代码

《Java实现批量化操作Excel文件的示例代码》在操作Excel的场景中,通常会有一些针对Excel的批量操作,这篇文章主要为大家详细介绍了如何使用GcExcel实现批量化操作Excel,感兴趣的可... 目录前言 | 问题背景什么是GcExcel场景1 批量导入Excel文件,并读取特定区域的数据场景2

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n