入职作业总结(3.1)D3D11初始化

文中提到的书,如无特殊标注,都指经典的directx编程指南:《Introduction to 3D Game Programming with Directx 11》

D3D初始化主要根据以下步骤:

  1. Create the ID3D11Device and ID3D11DeviceContext interfaces using the D3D11CreateDevice function.
  2. Check 4X MSAA quality level support using the ID3D11Device::CheckMultisampleQualityLevels method.
  3. Describe the characteristics of the swap chain we are going to create by filling out an instance of the DXGI_SWAP_CHAIN_DESC structure.
  4. Query the IDXGIFactory instance that was used to create the device, and create an IDXGISwapChain instance.
  5. Create a render target view to the swap chain’s back buffer.
  6. Create the depth/stencil buffer and its associated depth/stencil view.
  7. Bind the render target view and depth/stencil view to the output merger stage of the rendering pipeline so that they can be used by Direct3D.
  8. Set the viewport.

1.创建 ID3D11DeviceID3D11DeviceContext

两者的作用如下:

  1. The ID3D11Device interface is used to check feature support, and allocate resources.
  2. The ID3D11DeviceContext interface is used to set render states, bind resources to the graphics pipeline, and issue rendering commands.
    补充一下:DeviceContext分immediate contextdeferred context两类。一般主线程内都设置为immediate context

ID3D11Device的创建可用如下函数:

HRESULT D3D11CreateDevice(
IDXGIAdapter *pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
CONST D3D_FEATURE_LEVEL *pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
ID3D11Device **ppDevice,
D3D_FEATURE_LEVEL *pFeatureLevel,
ID3D11DeviceContext **ppImmediateContext
);
  1. pAdapter指明了要使用的设备(个人理解为显卡),设为nullptr表示当前采用的主设备。
  2. DriverType一般使用D3D_DRIVER_TYPE_HARDWARE。只有当硬件设备无法支持时考虑其他选项(效果一般较差)。其他可选的有:D3D_DRIVER_TYPE_REFERENCE(非常慢的软件模拟设备)、D3D_DRIVER_TYPE_WARP(D3D10版本提供的软件模拟设备,不被D3D11支持)、D3D_DRIVER_TYPE_SOFTWARE(软件设备总称,用于选择其他第三方软件模拟设备)。
  3. Software:在第二项使用D3D_DRIVER_TYPE_SOFTWARE,在这个参数里提供相应的软件设备。在使用硬件设备时一般置空
  4. Flags:标志位,有两个常见的标志位(可以通过|,即“位或”相结合):D3D11_CREATE_DEVICE_DEBUG(这个标志位开启时,D3D会把调试信息发送到VC++ output window)、D3D11_CREATE_DEVICE_SINGLETHREADED(如果能保证D3D仅在单线程下被调用,则可以提供效率——此处我理解是仅针对该进程内部,如果没有多线程即可)。
  5. pFeatureLevels:可选的D3D_FEATURE_LEVEL数组列表。函数会依次测试数组中各项,直到遇到一个可用的D3D_FEATURE_LEVEL置为nullptr表示选用可用的最高级D3D_FEATURE_LEVEL
  6. FeatureLevels:第5个参数中数组元素的个数。如果第5个参数为nullptr,这里用0填充。
  7. SDKVersion:用D3D11_SDK_VERSION
### DirectX11 中实现跳跃游戏功能 #### 3.1 游戏初始化与资源加载 为了在 DirectX11 中创建一个简单的跳跃游戏,首先需要完成 Direct3D 设备和交换链的初始化。这一步骤确保了图形渲染环境已经准备好。 ```cpp // 初始化Direct3D设备和交换链 ID3D11Device* device; IDXGISwapChain* swapChain; ID3D11DeviceContext* context; DXGI_SWAP_CHAIN_DESC scd; ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); scd.BufferCount = 1; scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.OutputWindow = hWnd; scd.SampleDesc.Count = 1; scd.Windowed = TRUE; D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, D3D11_SDK_VERSION, &scd, &swapChain, &device, NULL, &context); ``` #### 3.2 创建并管理游戏角色 定义游戏角色及其属性,包括位置、速度以及加速度等参数。这些数据结构将帮助模拟角色的动作行为。 ```cpp struct Character { XMFLOAT3 position; // 当前坐标 (x,y,z) XMFLOAT3 velocity; // 移动速度矢量 float gravity = -9.8f;// 重力常数 }; Character player; player.position = XMFLOAT3(0.0f, 0.0f, 0.0f); // 初始位置设为原点 player.velocity = XMFLOAT3(0.0f, 0.0f, 0.0f);// 初始静止状态 ``` #### 3.3 物理引擎集成 引基础物理计算来处理跳跃动作。当玩家按下跳跃键时,给定初始向上冲力,并应用重力影响使其逐渐下降回到地面[^2]。 ```cpp void Jump(Character& character) { if (!IsGrounded(character)) return; XMStoreFloat3( &character.velocity, XMLoadFloat3(&character.velocity) + XMVectorSet(0.0f, JUMP_FORCE, 0.0f, 0.0f) ); } bool IsGrounded(const Character& character) { // 假设地面高度固定为零 return character.position.y <= 0.0f && fabs(character.velocity.y) < FLT_EPSILON; } ``` #### 3.4 更新循环中的运动更新 每帧调用 Update 函数以根据当前时间间隔 dt 来调整角色的位置。这里会涉及到简单积分法的应用,即基于速度改变位移。 ```cpp void Update(float deltaTimeSeconds) { // 应用力学定律更新速度 XMVECTOR v = XMLoadFloat3(&player.velocity); v += XMVectorSet(0.0f, player.gravity * deltaTimeSeconds, 0.0f, 0.0f); // 如果低于地面,则反弹到地上停止下落 if (XMVectorGetX(XMLoadFloat3(&player.position)) < 0.0f && XMVectorGetY(v) < 0.0f) { v = XMVectorSetY(v, 0.0f); XMStoreFloat3(&player.position, XMVectorSetY(XMLoadFloat3(&player.position), 0.0f)); } XMStoreFloat3(&player.velocity, v); // 使用新的速度更新位置 XMStoreFloat3(&player.position, XMLoadFloat3(&player.position) + XMLoadFloat3(&player.velocity) * deltaTimeSeconds); } ``` #### 3.5 用户输响应 监听键盘或触屏事件触发跳跃逻辑。对于桌面应用程序而言,通常是通过检测特定按键的状态来进行判断;而在移动端则可能涉及手势识别或其他形式的人机交互接口[^1]。 ```cpp if (GetAsyncKeyState(VK_SPACE) & 0x8000) { // 检测空格键是否被按压 Jump(player); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值