本文主要是介绍龙书D3D11章节习题答案(第六章),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
以下答案仅供参考,有错欢迎留言。
Chapter 6:Drawing in Direct3D
提醒:记得多备份几份原书提供的BoxDemo.cpp和ShapeDemo.cpp,下面的题目基本上都由此基础上改动。
同类文章:
DirectX 11游戏编程学习笔记之8: 第6章Drawing in Direct3D(在Direct3D中绘制)(习题解答)
找到同类的文章实属不易,大家可以借鉴一番这篇文章(作者提供了完整的代码下载,而我比较懒。。下面只会说我大致的步骤)
勘误:题中D3D10_INPUT_ELEMENT_DESC更正为D3D11_...
1. Write down the D3D11_INPUT_ELEMENT_DESC array for the following vertex structure:
struct Vertex{
XMFLOAT3 Pos;
XMFLOAT3 Tangent;
XMFLOAT3 Normal;
XMFLOAT2 Tex0;
XMFLOAT2 Tex1;
XMCOLOR Color;
};
D3D11_INPUT_ELEMENT_DESC vertexDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 52, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
2. Redo the Colored Cube demo, but this time usetwo vertex buffers (and two input slots)to feed the pipeline with
vertices, one that stores the position element and the other that stores the color element.
Your D3D11_INPUT_ELEMENT_DESC array will look like this:
D3D11_INPUT_ELEMENT_DESC vertexDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
The position element is hooked up to input slot 0, and the color element is hooked up to input slot 1. Moreover
note that the D3D11_INPUT_ELEMENT_DESC::AlignedByteOffset is 0 for both elements; this is because the
position and color elements are no longer interleaved in a single input slot.
题目做起来其实比较麻烦:先说一下大概~首先题目要求使用2个Vertex Buffer以及2个input slot(最大可用个数为16,索引对应0~15)。好,记住了条件,我们来分析下装配顶点到渲染管线所用到的ID3D11DeviceContext::IASetVertexBuffers方法:
第一个参数StartSlot(UINT)表示从几号槽(槽即上述所说input slot的简称,范围是0到15号)开始计数,因为题目中POSITION占0号槽,COLOR占1号槽,所以从0号槽开始计数,StartSlot = 0。
第二个参数NumBuffers(UINT)表示用到的buffer的个数,要求是2。建两个不同的VertexBuffer需要我们拆分原先的struct Vertex成两个,就像这样:
struct Vertex
{XMFLOAT3 Pos;XMFLOAT4 Color;
};struct vertexPos
{XMFLOAT4 Color;
};struct vertexColor
{XMFLOAT4 Color;
};
继续拆
:
vertexPos vertexPos[] ={{ XMFLOAT3(-1.0f, -1.0f, -1.0f) },{ XMFLOAT3(-1.0f, +1.0f, -1.0f) },{ XMFLOAT3(+1.0f, +1.0f, -1.0f) },{ XMFLOAT3(+1.0f, -1.0f, -1.0f) },{ XMFLOAT3(-1.0f, -1.0f, +1.0f) },{ XMFLOAT3(-1.0f, +1.0f, +1.0f) },{ XMFLOAT3(+1.0f, +1.0f, +1.0f) },{ XMFLOAT3(+1.0f, -1.0f, +1.0f) }};vertexColor vertexColor[] ={{ (const float*)&Colors::White },{ (const float*)&Colors::Black },{ (const float*)&Colors::Red },{ (const float*)&Colors::Green },{ (const float*)&Colors::Blue },{ (const float*)&Colors::Yellow },{ (const float*)&Colors::Cyan },{ (const float*)&Colors::Magenta }};
然后就简单了,对于Buffer的BUFFER_DESC,修改vbd.ByteWidth = sizeof(vertexPos) * 8;对于Buffer的SUBRESOURCE_DATA, 修改 vinitData.pSysMem = vertexPos;接着创建完这个存放位置信息的Buffer,我们再修改vbd.ByteWidth = sizeof(vertexColor) * 8,vinitData.pSysMem = vertexColor;,创建存放颜色信息的Buffer,就像这样:
D3D11_BUFFER_DESC vbd;vbd.Usage = D3D11_USAGE_IMMUTABLE;vbd.ByteWidth = sizeof(vertexPos) * 8;vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;vbd.CPUAccessFlags = 0;vbd.MiscFlags = 0;vbd.StructureByteStride = 0;D3D11_SUBRESOURCE_DATA vinitData;vinitData.pSysMem = vertexPos;HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB[0]));vbd.ByteWidth = sizeof(vertexColor) * 8;vinitData.pSysMem = vertexColor;HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB[1]));
要学会节约,所以这里重复利用了一下vbd和vinitData。
你可能会发现我用的是mBoxVB[0]和mBoxVB[1],而mBoxVB却是ID3D11Buffer*类型的。。别急,下面就会说到相应修改~
第三个参数ppVertexBuffers(ID3D11Buffer*const*)表示存放所有buffer地址的一个数组,那么我们肯定需要把建立好的两个Buffer放到数组里再传参,我的做法是把原有的成员变量ID3D11Buffer* mBoxVB修改为ID3D11Buffer* mBoxVB[2],当然你也可以用其他方式创建数组。那么相应地,要记得把构造函数初始化改为:mBoxVB[0] = nullptr, mBoxVB[1] = nullptr; 析构函数里改为:Re
这篇关于龙书D3D11章节习题答案(第六章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!