Tutorial 5: 3D Transformation
D3D中,变换就是通过和一个变换矩阵相乘来执行的。
D3D中有3中基本的图元变换方式:
平移translation (where it lies in space relative to the origin):
XMMatrixTranslation
XMMatrixTranslationFromVector
旋转rotation (its direction in relation to the x, y, z frame):
XMMatrixRotationX
XMMatrixRotationY
XMMatrixRotationZ
XMMatrixRotationAxis
缩放scaling (its distance from origin):
XMMatrixRotationAxis
复杂的运动(变换)都可以用以上基本变换复合而成。
深度缓存的使用:(深度缓存就是一个2D texture,所以我们使用CreateTexture2D来创建深度缓存)
ID3D11Texture2D* g_pDepthStencil = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
// Create depth stencil texture创建深度模板纹理
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; //A 32-bit z-buffer format that uses 24 bits for the depth channel and 8 bits for the stencil //channel.
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;
// Create the depth stencil view创建深度模板视图
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof(descDSV) );
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;//The resource will be accessed as a 2D texture.
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView );
if( FAILED( hr ) )
return hr;
//将render targets和depth-stencil buffer绑定到设备的Output-merger state
g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);
//每一帧render之前,需要清空深度模板视图 Clear the depth buffer to 1.0 (max depth)
g_pImmediateContext->ClearDepthStencilView( g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );