3
0

WhiteBoxRenderData.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "WhiteBoxRenderData.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. namespace WhiteBox
  11. {
  12. void WhiteBoxRenderData::Reflect(AZ::ReflectContext* context)
  13. {
  14. WhiteBoxFace::Reflect(context);
  15. WhiteBoxMaterial::Reflect(context);
  16. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  17. {
  18. serializeContext->Class<WhiteBoxRenderData>()
  19. ->Version(2)
  20. ->Field("Faces", &WhiteBoxRenderData::m_faces)
  21. ->Field("Material", &WhiteBoxRenderData::m_material);
  22. }
  23. }
  24. void WhiteBoxVertex::Reflect(AZ::ReflectContext* context)
  25. {
  26. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  27. {
  28. serializeContext->Class<WhiteBoxVertex>()
  29. ->Version(1)
  30. ->Field("Position", &WhiteBoxVertex::m_position)
  31. ->Field("UV", &WhiteBoxVertex::m_uv);
  32. }
  33. }
  34. void WhiteBoxFace::Reflect(AZ::ReflectContext* context)
  35. {
  36. WhiteBoxVertex::Reflect(context);
  37. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serializeContext->Class<WhiteBoxFace>()
  40. ->Version(1)
  41. ->Field("Vertex1", &WhiteBoxFace::m_v1)
  42. ->Field("Vertex2", &WhiteBoxFace::m_v2)
  43. ->Field("Vertex3", &WhiteBoxFace::m_v3)
  44. ->Field("Normal", &WhiteBoxFace::m_normal);
  45. }
  46. }
  47. WhiteBoxFaces BuildCulledWhiteBoxFaces(const WhiteBoxFaces& inFaces)
  48. {
  49. // actual face count can be less than the original face count if any degenerate
  50. // faces are detected and removed
  51. const size_t inFaceCount = inFaces.size();
  52. size_t outFaceCount = 0;
  53. WhiteBoxFaces outFaces;
  54. // resize to the worst case number of faces
  55. outFaces.resize(inFaceCount);
  56. for (size_t inFaceIndex = 0; inFaceIndex < inFaceCount; ++inFaceIndex)
  57. {
  58. const WhiteBoxFace face = inFaces[inFaceIndex];
  59. const AZ::Vector3& vertex1 = face.m_v1.m_position;
  60. const AZ::Vector3& vertex2 = face.m_v2.m_position;
  61. const AZ::Vector3& vertex3 = face.m_v3.m_position;
  62. const auto areaSquared = 0.5f * ((vertex2 - vertex1).Cross(vertex3 - vertex1)).GetLengthSq();
  63. // only copy non-degenerate triangles (area is less than zero)
  64. if (areaSquared > DegenerateTriangleAreaSquareEpsilon)
  65. {
  66. outFaces[outFaceCount] = face;
  67. outFaceCount++;
  68. }
  69. }
  70. // now that we know the number of faces we need we can safely resize the vector without any
  71. // additional allocations as the number of faces will be equal to or less than the original size
  72. outFaces.resize(outFaceCount);
  73. return outFaces;
  74. }
  75. } // namespace WhiteBox