RectangleMesh.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Mesh.h>
  9. #include <Mathematics/Rectangle.h>
  10. #include <Mathematics/Vector3.h>
  11. #include <memory>
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class RectangleMesh : public Mesh<Real>
  16. {
  17. public:
  18. // Create a mesh that tessellates a rectangle.
  19. RectangleMesh(MeshDescription const& description, Rectangle<3, Real> const& rectangle)
  20. :
  21. Mesh<Real>(description, { MeshTopology::RECTANGLE }),
  22. mRectangle(rectangle)
  23. {
  24. if (!this->mDescription.constructed)
  25. {
  26. // The logger system will report these errors in the Mesh
  27. // constructor.
  28. return;
  29. }
  30. if (!this->mTCoords)
  31. {
  32. mDefaultTCoords.resize(this->mDescription.numVertices);
  33. this->mTCoords = mDefaultTCoords.data();
  34. this->mTCoordStride = sizeof(Vector2<Real>);
  35. this->mDescription.allowUpdateFrame = this->mDescription.wantDynamicTangentSpaceUpdate;
  36. if (this->mDescription.allowUpdateFrame)
  37. {
  38. if (!this->mDescription.hasTangentSpaceVectors)
  39. {
  40. this->mDescription.allowUpdateFrame = false;
  41. }
  42. if (!this->mNormals)
  43. {
  44. this->mDescription.allowUpdateFrame = false;
  45. }
  46. }
  47. }
  48. this->ComputeIndices();
  49. InitializeTCoords();
  50. InitializePositions();
  51. if (this->mDescription.allowUpdateFrame)
  52. {
  53. InitializeFrame();
  54. }
  55. else if (this->mNormals)
  56. {
  57. InitializeNormals();
  58. }
  59. }
  60. // Member access.
  61. inline Rectangle<3, Real> const& GetRectangle() const
  62. {
  63. return mRectangle;
  64. }
  65. protected:
  66. void InitializeTCoords()
  67. {
  68. Vector2<Real> tcoord;
  69. for (uint32_t r = 0, i = 0; r < this->mDescription.numRows; ++r)
  70. {
  71. tcoord[1] = (Real)r / (Real)(this->mDescription.numRows - 1);
  72. for (uint32_t c = 0; c < this->mDescription.numCols; ++c, ++i)
  73. {
  74. tcoord[0] = (Real)c / (Real)(this->mDescription.numCols - 1);
  75. this->TCoord(i) = tcoord;
  76. }
  77. }
  78. }
  79. void InitializePositions()
  80. {
  81. for (uint32_t r = 0, i = 0; r < this->mDescription.numRows; ++r)
  82. {
  83. for (uint32_t c = 0; c < this->mDescription.numCols; ++c, ++i)
  84. {
  85. Vector2<Real> tcoord = this->TCoord(i);
  86. Real w0 = ((Real)2 * tcoord[0] - (Real)1) * mRectangle.extent[0];
  87. Real w1 = ((Real)2 * tcoord[1] - (Real)1) * mRectangle.extent[1];
  88. this->Position(i) = mRectangle.center + w0 * mRectangle.axis[0] + w1 * mRectangle.axis[1];
  89. }
  90. }
  91. }
  92. void InitializeNormals()
  93. {
  94. Vector3<Real> normal = UnitCross(mRectangle.axis[0], mRectangle.axis[1]);
  95. for (uint32_t i = 0; i < this->mDescription.numVertices; ++i)
  96. {
  97. this->Normal(i) = normal;
  98. }
  99. }
  100. void InitializeFrame()
  101. {
  102. Vector3<Real> normal = UnitCross(mRectangle.axis[0], mRectangle.axis[1]);
  103. Vector3<Real> tangent{ (Real)1, (Real)0, (Real)0 };
  104. Vector3<Real> bitangent{ (Real)0, (Real)1, (Real)0 };
  105. // bitangent = Cross(normal,tangent)
  106. // TODO: Are tangent and bitangent correct?
  107. for (uint32_t i = 0; i < this->mDescription.numVertices; ++i)
  108. {
  109. if (this->mNormals)
  110. {
  111. this->Normal(i) = normal;
  112. }
  113. if (this->mTangents)
  114. {
  115. this->Tangent(i) = tangent;
  116. }
  117. if (this->mBitangents)
  118. {
  119. this->Bitangent(i) = bitangent;
  120. }
  121. if (this->mDPDUs)
  122. {
  123. this->DPDU(i) = tangent;
  124. }
  125. if (this->mDPDVs)
  126. {
  127. this->DPDV(i) = bitangent;
  128. }
  129. }
  130. }
  131. Rectangle<3, Real> mRectangle;
  132. // If the client does not request texture coordinates, they will be
  133. // computed internally for use in evaluation of the surface geometry.
  134. std::vector<Vector2<Real>> mDefaultTCoords;
  135. };
  136. }