D3D12渲染技术之形状几何

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jxw167/article/details/82701940

在本篇博客中,我们将展示如何为椭圆体,球体,圆柱体和圆锥体创建几何体。 这些形状对于绘制天空圆顶,调试,可视化碰撞检测和延迟渲染非常有用。
我们将几何生成代码放在GeometryGenerator类(GeometryGenerator.h / .cpp)中,GeometryGenerator是一个实用程序类,用于生成简单的几何形状,如网格,球体,圆柱体和盒子,我们在本博客中将它们用于演示程序, 这个类在系统内存中生成数据,然后我们必须将想要的数据复制到顶点和索引缓冲区。 GeometryGenerator会创建一些将在后面使用的顶点数据, 我们当前的演示中不需要这些数据,因此我们不会将此数据复制到顶点缓冲区中, MeshData结构是嵌套在GeometryGenerator中的简单结构,它存储顶点和索引列表:

class GeometryGenerator
{
public:
using uint16 = std::uint16_t;
  using uint32 = std::uint32_t;
 
  struct Vertex
  {
    Vertex(){}
    Vertex(
      const DirectX::XMFLOAT3& p, 
      const DirectX::XMFLOAT3& n, 
      const DirectX::XMFLOAT3& t, 
      const DirectX::XMFLOAT2& uv) :
      Position(p), 
      Normal(n), 
      TangentU(t), 
      TexC(uv){}
    Vertex(
      float px, float py, float pz, 
      float nx, float ny, float nz,
      float tx, float ty, float tz,
      float u, float v) : 
      Position(px,py,pz), 
      Normal(nx,ny,nz),
      TangentU(tx, ty, tz), 
      TexC(u,v){}
 
    DirectX::XMFLOAT3 Position;
    DirectX::XMFLOAT3 Normal;
    DirectX::XMFLOAT3 TangentU;
    DirectX::XMFLOAT2 TexC;
  };
  struct MeshData
  {
    std::vector<Vertex> Vertices;
    std::vector<uint32> Indices32;
 
    std::vector<uint16>& GetIndices16()
    {
      if(mIndices16.empty())
      {
        mIndices16.resize(Indices32.size());
        for(size_t i = 0; i < Indices32.size(); ++i)
          mIndices16[i] = static_cast<uint16>(Indices32[i]);
      }
 
      return mIndices16;
    }
 
    private:
      std::vector<uint16> mIndices16;
   };
 
…
};

下面把Demo中展示的内容给读者显示一下:
这里写图片描述

具体代码实现读者自行查看,在这里提醒,我们还是要下断点调试一下,掌握核心代码的作用。。。
代码下载地址:链接:https://pan.baidu.com/s/1X0Vikf6qGYGPKU-Nwf-wYA 密码:h79q

猜你喜欢

转载自blog.csdn.net/jxw167/article/details/82701940
今日推荐