vertex.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2006-2016 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "vertex.h"
  21. namespace love
  22. {
  23. namespace graphics
  24. {
  25. namespace vertex
  26. {
  27. static_assert(sizeof(Color) == 4, "sizeof(Color) incorrect!");
  28. static_assert(sizeof(Vertex) == sizeof(float)*2 + sizeof(float)*2 + sizeof(Color), "sizeof(Vertex) incorrect!");
  29. static_assert(sizeof(XYf_STf) == sizeof(float)*2 + sizeof(float)*2, "sizeof(XYf_STf) incorrect!");
  30. static_assert(sizeof(XYf_STf_RGBAub) == sizeof(float)*2 + sizeof(float)*2 + sizeof(Color), "sizeof(XYf_STf_RGBAub) incorrect!");
  31. static_assert(sizeof(XYf_STus_RGBAub) == sizeof(float)*2 + sizeof(uint16)*2 + sizeof(Color), "sizeof(XYf_STus_RGBAub) incorrect!");
  32. size_t getFormatStride(CommonFormat format)
  33. {
  34. switch (format)
  35. {
  36. case CommonFormat::NONE:
  37. return 0;
  38. case CommonFormat::XYf:
  39. return sizeof(float) * 2;
  40. case CommonFormat::RGBAub:
  41. return sizeof(uint8) * 4;
  42. case CommonFormat::XYf_STf:
  43. return sizeof(XYf_STf);
  44. case CommonFormat::XYf_STf_RGBAub:
  45. return sizeof(XYf_STf_RGBAub);
  46. case CommonFormat::XYf_STus_RGBAub:
  47. return sizeof(XYf_STus_RGBAub);
  48. }
  49. }
  50. int getIndexCount(TriangleIndexMode mode, int vertexCount)
  51. {
  52. switch (mode)
  53. {
  54. case TriangleIndexMode::NONE:
  55. return 0;
  56. case TriangleIndexMode::STRIP:
  57. case TriangleIndexMode::FAN:
  58. return 3 * (vertexCount - 2);
  59. case TriangleIndexMode::QUADS:
  60. return vertexCount * 6 / 4;
  61. }
  62. }
  63. template <typename T>
  64. static void fillIndicesT(TriangleIndexMode mode, T vertexStart, T vertexCount, T *indices)
  65. {
  66. switch (mode)
  67. {
  68. case TriangleIndexMode::NONE:
  69. break;
  70. case TriangleIndexMode::STRIP:
  71. {
  72. int i = 0;
  73. for (T index = 0; index < vertexCount - 2; index++)
  74. {
  75. indices[i++] = vertexStart + index;
  76. indices[i++] = vertexStart + index + 1 + (index & 1);
  77. indices[i++] = vertexStart + index + 2 - (index & 1);
  78. }
  79. }
  80. break;
  81. case TriangleIndexMode::FAN:
  82. {
  83. int i = 0;
  84. for (T index = 2; index < vertexCount; index++)
  85. {
  86. indices[i++] = vertexStart;
  87. indices[i++] = vertexStart + index - 1;
  88. indices[i++] = vertexStart + index;
  89. }
  90. }
  91. break;
  92. case TriangleIndexMode::QUADS:
  93. {
  94. // 0---2
  95. // | / |
  96. // 1---3
  97. int count = vertexCount / 4;
  98. for (int i = 0; i < count; i++)
  99. {
  100. int ii = i * 6;
  101. T vi = T(vertexStart + i * 4);
  102. indices[ii + 0] = vi + 0;
  103. indices[ii + 1] = vi + 1;
  104. indices[ii + 2] = vi + 2;
  105. indices[ii + 3] = vi + 2;
  106. indices[ii + 4] = vi + 1;
  107. indices[ii + 5] = vi + 3;
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. void fillIndices(TriangleIndexMode mode, uint16 vertexStart, uint16 vertexCount, uint16 *indices)
  114. {
  115. fillIndicesT(mode, vertexStart, vertexCount, indices);
  116. }
  117. void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, uint32 *indices)
  118. {
  119. fillIndicesT(mode, vertexStart, vertexCount, indices);
  120. }
  121. } // vertex
  122. } // graphics
  123. } // love