home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / GeometryGenerator.h < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  4.0 KB  |  120 lines

  1. //***************************************************************************************
  2. // GeometryGenerator.h by Frank Luna (C) 2011 All Rights Reserved.
  3. //   
  4. // Defines a static class for procedurally generating the geometry of 
  5. // common mathematical objects.
  6. //
  7. // All triangles are generated "outward" facing.  If you want "inward" 
  8. // facing triangles (for example, if you want to place the camera inside
  9. // a sphere to simulate a sky), you will need to:
  10. //   1. Change the Direct3D cull mode or manually reverse the winding order.
  11. //   2. Invert the normal.
  12. //   3. Update the texture coordinates and tangent vectors.
  13. //***************************************************************************************
  14.  
  15. #pragma once
  16.  
  17. #include <cstdint>
  18. #include <DirectXMath.h>
  19. #include <vector>
  20.  
  21. class GeometryGenerator
  22. {
  23. public:
  24.  
  25.     using uint16 = std::uint16_t;
  26.     using uint32 = std::uint32_t;
  27.  
  28.     struct Vertex
  29.     {
  30.         Vertex(){}
  31.         Vertex(
  32.             const DirectX::XMFLOAT3& p, 
  33.             const DirectX::XMFLOAT3& n, 
  34.             const DirectX::XMFLOAT3& t, 
  35.             const DirectX::XMFLOAT2& uv) :
  36.             Position(p), 
  37.             Normal(n), 
  38.             TangentU(t), 
  39.             TexC(uv){}
  40.         Vertex(
  41.             float px, float py, float pz, 
  42.             float nx, float ny, float nz,
  43.             float tx, float ty, float tz,
  44.             float u, float v) : 
  45.             Position(px,py,pz), 
  46.             Normal(nx,ny,nz),
  47.             TangentU(tx, ty, tz), 
  48.             TexC(u,v){}
  49.  
  50.         DirectX::XMFLOAT3 Position;
  51.         DirectX::XMFLOAT3 Normal;
  52.         DirectX::XMFLOAT3 TangentU;
  53.         DirectX::XMFLOAT2 TexC;
  54.     };
  55.  
  56.     struct MeshData
  57.     {
  58.         std::vector<Vertex> Vertices;
  59.         std::vector<uint32> Indices32;
  60.  
  61.         std::vector<uint16>& GetIndices16()
  62.         {
  63.             if(mIndices16.empty())
  64.             {
  65.                 mIndices16.resize(Indices32.size());
  66.                 for(size_t i = 0; i < Indices32.size(); ++i)
  67.                     mIndices16[i] = static_cast<uint16>(Indices32[i]);
  68.             }
  69.  
  70.             return mIndices16;
  71.         }
  72.  
  73.     private:
  74.         std::vector<uint16> mIndices16;
  75.     };
  76.  
  77.     ///<summary>
  78.     /// Creates a box centered at the origin with the given dimensions, where each
  79.     /// face has m rows and n columns of vertices.
  80.     ///</summary>
  81.     MeshData CreateBox(float width, float height, float depth, uint32 numSubdivisions);
  82.  
  83.     ///<summary>
  84.     /// Creates a sphere centered at the origin with the given radius.  The
  85.     /// slices and stacks parameters control the degree of tessellation.
  86.     ///</summary>
  87.     MeshData CreateSphere(float radius, uint32 sliceCount, uint32 stackCount);
  88.  
  89.     ///<summary>
  90.     /// Creates a geosphere centered at the origin with the given radius.  The
  91.     /// depth controls the level of tessellation.
  92.     ///</summary>
  93.     MeshData CreateGeosphere(float radius, uint32 numSubdivisions);
  94.  
  95.     ///<summary>
  96.     /// Creates a cylinder parallel to the y-axis, and centered about the origin.  
  97.     /// The bottom and top radius can vary to form various cone shapes rather than true
  98.     // cylinders.  The slices and stacks parameters control the degree of tessellation.
  99.     ///</summary>
  100.     MeshData CreateCylinder(float bottomRadius, float topRadius, float height, uint32 sliceCount, uint32 stackCount);
  101.  
  102.     ///<summary>
  103.     /// Creates an mxn grid in the xz-plane with m rows and n columns, centered
  104.     /// at the origin with the specified width and depth.
  105.     ///</summary>
  106.     MeshData CreateGrid(float width, float depth, uint32 m, uint32 n);
  107.  
  108.     ///<summary>
  109.     /// Creates a quad aligned with the screen.  This is useful for postprocessing and screen effects.
  110.     ///</summary>
  111.     MeshData CreateQuad(float x, float y, float w, float h, float depth);
  112.  
  113. private:
  114.     void Subdivide(MeshData& meshData);
  115.     Vertex MidPoint(const Vertex& v0, const Vertex& v1);
  116.     void BuildCylinderTopCap(float bottomRadius, float topRadius, float height, uint32 sliceCount, uint32 stackCount, MeshData& meshData);
  117.     void BuildCylinderBottomCap(float bottomRadius, float topRadius, float height, uint32 sliceCount, uint32 stackCount, MeshData& meshData);
  118. };
  119.  
  120.