ShapeGeometryUtil.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Math/Vector2.h>
  10. #include <AzCore/Math/Vector3.h>
  11. #include <AzCore/std/containers/vector.h>
  12. namespace AzFramework
  13. {
  14. class DebugDisplayRequests;
  15. }
  16. namespace LmbrCentral
  17. {
  18. struct ShapeDrawParams;
  19. /// Buffers used for rendering Shapes.
  20. /// Generated from shape properties.
  21. struct ShapeMesh
  22. {
  23. AZStd::vector<AZ::Vector3> m_vertexBuffer; ///< Vertices of the shape.
  24. AZStd::vector<AZ::u32> m_indexBuffer; ///< Indices of the shape.
  25. AZStd::vector<AZ::Vector3> m_lineBuffer; ///< Lines of the shape.
  26. };
  27. /// Writes 3 indices (1 tri) to the buffer and returns a pointer to the next index.
  28. AZ::u32* WriteTriangle(AZ::u32 a, AZ::u32 b, AZ::u32 c, AZ::u32* indices);
  29. /// Writes a vertex to the buffer and returns a pointer to the next vertex.
  30. AZ::Vector3* WriteVertex(const AZ::Vector3& vertex, AZ::Vector3* vertices);
  31. /// Draw a ShapeMesh (previously generated vertices, indices and lines).
  32. void DrawShape(
  33. AzFramework::DebugDisplayRequests& debugDisplay,
  34. const ShapeDrawParams& shapeDrawParams,
  35. const ShapeMesh& shapeMesh,
  36. const AZ::Vector3& shapeOffset = AZ::Vector3::CreateZero());
  37. /// Return a vector of vertices representing a list of triangles to render (CCW).
  38. /// This is implemented using the Ear Clipping method:
  39. /// (https://www.gamedev.net/articles/programming/graphics/polygon-triangulation-r3334/)
  40. /// @param vertices List of vertices to process (pass by value as vertices is
  41. /// modified inside the function so must be copied).
  42. AZStd::vector<AZ::Vector3> GenerateTriangles(AZStd::vector<AZ::Vector2> vertices);
  43. /// Determine if a list of ordered vertices have clockwise winding order.
  44. /// http://blog.element84.com/polygon-winding.html
  45. bool ClockwiseOrder(const AZStd::vector<AZ::Vector2>& vertices);
  46. namespace CapsuleTubeUtil
  47. {
  48. /// Generates all indices, assumes index pointer is valid.
  49. void GenerateSolidMeshIndices(
  50. AZ::u32 sides, AZ::u32 segments, AZ::u32 capSegments, AZ::u32* indices);
  51. /// Generate verts to be used when drawing triangles for the start cap.
  52. AZ::Vector3* GenerateSolidStartCap(
  53. const AZ::Vector3& localPosition, const AZ::Vector3& direction,
  54. const AZ::Vector3& side, float radius, AZ::u32 sides, AZ::u32 capSegments, AZ::Vector3* vertices);
  55. /// Generate verts to be used when drawing triangles for the end cap.
  56. AZ::Vector3* GenerateSolidEndCap(
  57. const AZ::Vector3& localPosition, const AZ::Vector3& direction,
  58. const AZ::Vector3& side, float radius, AZ::u32 sides, AZ::u32 capSegments, AZ::Vector3* vertices);
  59. /// Generate vertices to be used for a loop of a segment along a tube or capsule (for use with index buffer).
  60. AZ::Vector3* GenerateSegmentVertices(
  61. const AZ::Vector3& point,
  62. const AZ::Vector3& axis,
  63. const AZ::Vector3& normal,
  64. float radius,
  65. AZ::u32 sides,
  66. AZ::Vector3* vertices);
  67. /// Generate a circle/loop for a given segment along the capsule/tube - Produces a series of begin/end
  68. /// line segments to draw in DrawLines.
  69. AZ::Vector3* GenerateWireLoop(
  70. const AZ::Vector3& localPosition, const AZ::Vector3& direction, const AZ::Vector3& side,
  71. AZ::u32 sides, float radius, AZ::Vector3* vertices);
  72. /// Generate a series of lines to be drawn, arcing around the end of a capsule/tube
  73. /// Two arcs, one horizontal and one vertical, arcing 180 degrees of a sphere.
  74. AZ::Vector3* GenerateWireCap(
  75. const AZ::Vector3& localPosition, const AZ::Vector3& direction, const AZ::Vector3& side,
  76. float radius, AZ::u32 capSegments, AZ::Vector3* vertices);
  77. /// Given a position, forward axis, side axis and angle (radians), calculate
  78. /// the position of a final point on a sphere by summing the rotation of those
  79. /// two axis from their starting orientation.
  80. AZ::Vector3 CalculatePositionOnSphere(
  81. const AZ::Vector3& localPosition, const AZ::Vector3& forwardAxis, const AZ::Vector3& sideAxis,
  82. float radius, float angle);
  83. }
  84. }