TriangleInputHelper.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <TriangleInputHelper.h>
  9. namespace UnitTest
  10. {
  11. TriangleInput CreatePlane(float width, float height, AZ::u32 segmentsX, AZ::u32 segmentsY)
  12. {
  13. TriangleInput plane;
  14. plane.m_vertices.resize((segmentsX + 1) * (segmentsY + 1));
  15. plane.m_uvs.resize((segmentsX + 1) * (segmentsY + 1));
  16. plane.m_indices.resize((segmentsX * segmentsY * 2) * 3);
  17. const NvCloth::SimParticleFormat topLeft(
  18. -width * 0.5f,
  19. -height * 0.5f,
  20. 0.0f,
  21. 0.0f);
  22. // Vertices and UVs
  23. for (AZ::u32 y = 0; y < segmentsY + 1; ++y)
  24. {
  25. for (AZ::u32 x = 0; x < segmentsX + 1; ++x)
  26. {
  27. const AZ::u32 segmentIndex = x + y * (segmentsX + 1);
  28. const float fractionX = ((float)x / (float)segmentsX);
  29. const float fractionY = ((float)y / (float)segmentsY);
  30. NvCloth::SimParticleFormat position(
  31. fractionX * width,
  32. fractionY * height,
  33. 0.0f,
  34. (y > 0) ? 1.0f : 0.0f);
  35. plane.m_vertices[segmentIndex] = topLeft + position;
  36. plane.m_uvs[segmentIndex] = NvCloth::SimUVType(fractionX, fractionY);
  37. }
  38. }
  39. // Triangles indices
  40. for (AZ::u32 y = 0; y < segmentsY; ++y)
  41. {
  42. for (AZ::u32 x = 0; x < segmentsX; ++x)
  43. {
  44. const AZ::u32 segmentIndex = (x + y * segmentsX) * 2 * 3;
  45. const AZ::u32 firstTriangleStartIndex = segmentIndex;
  46. const AZ::u32 secondTriangleStartIndex = segmentIndex + 3;
  47. //Top left to bottom right
  48. plane.m_indices[firstTriangleStartIndex + 0] = static_cast<NvCloth::SimIndexType>((x + 0) + (y + 0) * (segmentsX + 1));
  49. plane.m_indices[firstTriangleStartIndex + 1] = static_cast<NvCloth::SimIndexType>((x + 1) + (y + 0) * (segmentsX + 1));
  50. plane.m_indices[firstTriangleStartIndex + 2] = static_cast<NvCloth::SimIndexType>((x + 1) + (y + 1) * (segmentsX + 1));
  51. plane.m_indices[secondTriangleStartIndex + 0] = static_cast<NvCloth::SimIndexType>((x + 0) + (y + 0) * (segmentsX + 1));
  52. plane.m_indices[secondTriangleStartIndex + 1] = static_cast<NvCloth::SimIndexType>((x + 1) + (y + 1) * (segmentsX + 1));
  53. plane.m_indices[secondTriangleStartIndex + 2] = static_cast<NvCloth::SimIndexType>((x + 0) + (y + 1) * (segmentsX + 1));
  54. }
  55. }
  56. return plane;
  57. }
  58. } // namespace UnitTest