完成模型定義後就可以進行顯示處理。CreateWindowSizeDependentResources方法首先根據窗口狀態初始化顯示比例,並設置可視角度爲70度(DirectX只支持弧度,度轉爲弧度可通過常量XM_PI實現);0.01f和100.0f則用來設置可見距離。這些參數之間的詳細介紹見directx10遊戲編程入門 5.6。
void CubeRenderer::CreateWindowSizeDependentResources()
{
Direct3DBase::CreateWindowSizeDependentResources();
float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
float fovAngleY = 70.0f * XM_PI / 180.0f;
XMStoreFloat4x4(
&m_constantBufferData.projection,
XMMatrixTranspose(
XMMatrixMultiply(
XMMatrixPerspectiveFovRH(
fovAngleY,
aspectRatio,
0.01f,
100.0f
),
XMLoadFloat4x4(&m_orientationTransform3D)
)
)
);
}
至此,各項準備工作就緒,場景中的正方體和攝像機的位置已經定好,可以開拍了!
應用展示的是一個旋轉正方體,那麼必然有一個方法負責更新不同時刻正方體的旋轉角度。下面的Update方法用timeTotal * XM_PIDIV4來實現這個功能。XMMatrixRotationY方法說明正方體沿Y軸旋轉。其他部分則是設置觀察的位置。eye說明眼睛所在的位置;at則是眼睛觀察的方向;up用來表示座標系中的“上”,一般不做更改。而眼睛的可視角和視力已經在CreateWindowSizeDependentResources方法中定義過。這一步更新模型狀態,確定哪些內容會顯示在屏幕上,但並沒有真正顯示結果。
void CubeRenderer::Update(float timeTotal, float timeDelta)
{
(void) timeDelta; // 未使用的參數。
XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
}
Render方法實現最終圖像的繪製,其主要就是設置各個功能的緩衝區地址。
void CubeRenderer::Render()
{
const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
m_d3dContext->ClearRenderTargetView(
m_renderTargetView.Get(),
midnightBlue
);
m_d3dContext->ClearDepthStencilView(
m_depthStencilView.Get(),
D3D11_CLEAR_DEPTH,
1.0f,
0
);
// 僅在加載時繪製多維數據集(加載爲異步過程)。
if (!m_loadingComplete)
{
return;
}
m_d3dContext->OMSetRenderTargets(
1,
m_renderTargetView.GetAddressOf(),
m_depthStencilView.Get()
);
m_d3dContext->UpdateSubresource(
m_constantBuffer.Get(),
0,
NULL,
&m_constantBufferData,
0,
0
);
UINT stride = sizeof(VertexPositionColor);
UINT offset = 0;
m_d3dContext->IASetVertexBuffers(
0,
1,
m_vertexBuffer.GetAddressOf(),
&stride,
&offset
);
m_d3dContext->IASetIndexBuffer(
m_indexBuffer.Get(),
DXGI_FORMAT_R16_UINT,
0
);
m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_d3dContext->IASetInputLayout(m_inputLayout.Get());
m_d3dContext->VSSetShader(
m_vertexShader.Get(),
nullptr,
0
);
m_d3dContext->VSSetConstantBuffers(
0,
1,
m_constantBuffer.GetAddressOf()
);
m_d3dContext->PSSetShader(
m_pixelShader.Get(),
nullptr,
0
);
m_d3dContext->DrawIndexed(
m_indexCount,
0,
0
);
}
至此,整個CubeRenderer.cpp文件結束,完成旋轉正方體的實現。通過這個示例程序瞭解DirectX的工作原理,結合DirectX 10 3D遊戲編程入門,對其中涉及的數學知識和渲染過程有更深的體會。3D編程真是有點複雜,還要好好研究下。