【问题标题】:directx11 can't add multiple objectsdirectx11 不能添加多个对象
【发布时间】:2013-01-12 03:04:00
【问题描述】:

大家好,我正处于一个真正的绑定中,我目前正在使用 Frank d Luna http://www.d3dcoder.net/d3d11.htm 编写的 DirectX11 教程 Shapes example in 3d game programming with DirectX11,我目前正在尝试添加第二个可以独立放置的立方体第一个立方体,我创建了它,当我构建项目时,两个立方体现在都消失了,我认为问题出在几何生成器上。

请帮忙:(

    void ShapesApp::BuildGeometryBuffers()

{ GeometryGenerator::MeshData 框;

//added
GeometryGenerator::MeshData boxtwo;


GeometryGenerator::MeshData grid;
GeometryGenerator::MeshData sphere;
GeometryGenerator::MeshData cylinder;

GeometryGenerator geoGen;

geoGen.CreateBox(1.0f, 1.0f, 1.0f, box);

geoGen.CreateBox(1.0f, 1.0f, 1.0f, boxtwo);
geoGen.CreateGrid(20.0f, 30.0f, 60, 40, grid);
geoGen.CreateSphere(0.5f, 20, 20, sphere);
//geoGen.CreateGeosphere(0.5f, 2, sphere);
geoGen.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20, cylinder);

// Cache the vertex offsets to each object in the concatenated vertex buffer.
//added


mBoxVertexOffset      = 0;
mBoxtwoVertexOffset = box.Vertices.size();
mGridVertexOffset     = mBoxtwoVertexOffset + boxtwo.Vertices.size();
mSphereVertexOffset   = mGridVertexOffset + grid.Vertices.size();
mCylinderVertexOffset = mSphereVertexOffset + sphere.Vertices.size();



// Cache the index count of each object.

mBoxIndexCount      = box.Indices.size();
//added
mBoxtwoIndexCount   = boxtwo.Indices.size();
mGridIndexCount     = grid.Indices.size();
mSphereIndexCount   = sphere.Indices.size();
mCylinderIndexCount = cylinder.Indices.size();


// Cache the starting index for each object in the concatenated index buffer.
mBoxIndexOffset      = 0;
//added
mBoxtwoIndexOffset      = mBoxIndexCount;
mGridIndexOffset     = mBoxtwoIndexOffset + mBoxtwoIndexCount;
mSphereIndexOffset   = mGridIndexOffset + mGridIndexCount;
mCylinderIndexOffset = mSphereIndexOffset + mSphereIndexCount;



UINT totalVertexCount = 

    box.Vertices.size() + 
    //added
    boxtwo.Vertices.size() + 
    grid.Vertices.size() + 
    sphere.Vertices.size() +
    cylinder.Vertices.size();

UINT totalIndexCount = 
    mBoxIndexCount + 
    //added
mBoxtwoIndexCount + 
    mGridIndexCount + 
    mSphereIndexCount +
    mCylinderIndexCount;

【问题讨论】:

  • 你帖子的标题是“directx11 can't multiple objects”,我觉得你应该更正一下。
  • 我认为你是对的,对不起:O

标签: 3d directx-11


【解决方案1】:

您似乎做对了第一部分,但还有更多内容。

  • 然后您需要为第二个框添加顶点位置
  • 在向量索引缓冲区中插入第二个框的索引
  • 添加代码以绘制第二个框
  • 创建第二个盒子世界矩阵来定位它

我会建议你再读一遍本章,在继续阅读之前,你必须完全理解每一章并能够正确修改其示例,否则你会在后面的章节中遇到很多问题.

这是一个带有代码更改的补丁,添加了第二个框:

    --- D:/MyStuff/Book Files/Frank Luna/DVD/Code/Chapter 6 Drawing in Direct3D/Shapes/ShapesDemoOriginal.cpp   Mon May 09 22:10:02 2011
    +++ D:/MyStuff/Book Files/Frank Luna/DVD/Code/Chapter 6 Drawing in Direct3D/Shapes/ShapesDemo.cpp   Fri Jan 11 12:37:54 2013
    @@ -58,0 +59 @@
    +   XMFLOAT4X4 mBoxWorld2;
    @@ -65,0 +67 @@
    +   int mBoxVertexOffset2;
    @@ -70,0 +73 @@
    +   UINT mBoxIndexOffset2;
    @@ -75,0 +79 @@
    +   UINT mBoxIndexCount2;
    @@ -122,0 +127,4 @@
    +   XMMATRIX boxScale2 = XMMatrixScaling(1.0f, 1.0f, 1.0f);
    +   XMMATRIX boxOffset2 = XMMatrixTranslation(2.0f, 0.5f, 0.0f);
    +   XMStoreFloat4x4(&mBoxWorld2, XMMatrixMultiply(boxScale2, boxOffset2));
    +
    @@ -226,0 +235,6 @@
    +       
    +       // Draw the box2.
    +       world = XMLoadFloat4x4(&mBoxWorld2);
    +       mfxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&(world*viewProj)));
    +       mTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
    +       md3dImmediateContext->DrawIndexed(mBoxIndexCount2, mBoxIndexOffset2,         mBoxVertexOffset2);
    @@ -303,0 +318 @@
    +   GeometryGenerator::MeshData box2;
    @@ -309,0 +325 @@
    +   geoGen.CreateBox(1.0f, 1.0f, 1.0f, box2);
    @@ -317 +333,2 @@
    -   mGridVertexOffset     = box.Vertices.size();
    +   mBoxVertexOffset2     = box.Vertices.size();
    +   mGridVertexOffset     = box.Vertices.size() + box2.Vertices.size();
    @@ -322,0 +340 @@
    +   mBoxIndexCount2     = box2.Indices.size();
    @@ -329 +347,2 @@
    -   mGridIndexOffset     = mBoxIndexCount;
    +   mBoxIndexOffset2     = mBoxIndexCount;
    +   mGridIndexOffset     = mBoxIndexOffset2 + mBoxIndexCount2;
    @@ -334,0 +354 @@
    +       box2.Vertices.size() + 
    @@ -340,0 +361 @@
    +       mBoxIndexCount2 + 
    @@ -360,0 +382,6 @@
    +   for(size_t i = 0; i < box2.Vertices.size(); ++i, ++k)
    +   {
    +       vertices[k].Pos   = box2.Vertices[i].Position;
    +       vertices[k].Color = black;
    +   }
    +
    @@ -394,0 +422 @@
    +   indices.insert(indices.end(), box2.Indices.begin(), box2.Indices.end());

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 2021-10-16
    • 1970-01-01
    • 2021-07-14
    • 2011-07-29
    • 2011-03-09
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多