MeshUtilities.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. #include "GeometryBackgroundBorder.h"
  34. namespace Rml {
  35. void MeshUtilities::GenerateQuad(Mesh& mesh, Vector2f origin, Vector2f dimensions, ColourbPremultiplied colour)
  36. {
  37. GenerateQuad(mesh, origin, dimensions, colour, Vector2f(0, 0), Vector2f(1, 1));
  38. }
  39. void MeshUtilities::GenerateQuad(Mesh& mesh, Vector2f origin, Vector2f dimensions, ColourbPremultiplied colour, Vector2f top_left_texcoord,
  40. Vector2f bottom_right_texcoord)
  41. {
  42. const int v0 = (int)mesh.vertices.size();
  43. const int i0 = (int)mesh.indices.size();
  44. mesh.vertices.resize(mesh.vertices.size() + 4);
  45. mesh.indices.resize(mesh.indices.size() + 6);
  46. Vertex* vertices = mesh.vertices.data();
  47. int* indices = mesh.indices.data();
  48. vertices[v0 + 0].position = origin;
  49. vertices[v0 + 0].colour = colour;
  50. vertices[v0 + 0].tex_coord = top_left_texcoord;
  51. vertices[v0 + 1].position = Vector2f(origin.x + dimensions.x, origin.y);
  52. vertices[v0 + 1].colour = colour;
  53. vertices[v0 + 1].tex_coord = Vector2f(bottom_right_texcoord.x, top_left_texcoord.y);
  54. vertices[v0 + 2].position = origin + dimensions;
  55. vertices[v0 + 2].colour = colour;
  56. vertices[v0 + 2].tex_coord = bottom_right_texcoord;
  57. vertices[v0 + 3].position = Vector2f(origin.x, origin.y + dimensions.y);
  58. vertices[v0 + 3].colour = colour;
  59. vertices[v0 + 3].tex_coord = Vector2f(top_left_texcoord.x, bottom_right_texcoord.y);
  60. indices[i0 + 0] = v0 + 0;
  61. indices[i0 + 1] = v0 + 3;
  62. indices[i0 + 2] = v0 + 1;
  63. indices[i0 + 3] = v0 + 1;
  64. indices[i0 + 4] = v0 + 3;
  65. indices[i0 + 5] = v0 + 2;
  66. }
  67. void MeshUtilities::GenerateLine(Mesh& mesh, Vector2f position, Vector2f size, ColourbPremultiplied color)
  68. {
  69. Math::SnapToPixelGrid(position, size);
  70. MeshUtilities::GenerateQuad(mesh, position, size, color);
  71. }
  72. void MeshUtilities::GenerateBackgroundBorder(Mesh& out_mesh, const Box& box, Vector2f offset, Vector4f border_radius,
  73. ColourbPremultiplied background_color, const ColourbPremultiplied border_colors[4])
  74. {
  75. RMLUI_ASSERT(border_colors);
  76. Vector<Vertex>& vertices = out_mesh.vertices;
  77. Vector<int>& indices = out_mesh.indices;
  78. EdgeSizes border_widths = {
  79. // TODO: Move rounding to computed values (round border only).
  80. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Top)),
  81. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Right)),
  82. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Bottom)),
  83. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Left)),
  84. };
  85. int num_borders = 0;
  86. for (int i = 0; i < 4; i++)
  87. if (border_colors[i].alpha > 0 && border_widths[i] > 0)
  88. num_borders += 1;
  89. const Vector2f padding_size = box.GetSize(BoxArea::Padding).Round();
  90. const bool has_background = (background_color.alpha > 0 && padding_size.x > 0 && padding_size.y > 0);
  91. const bool has_border = (num_borders > 0);
  92. if (!has_background && !has_border)
  93. return;
  94. // Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
  95. const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
  96. const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
  97. vertices.reserve((int)vertices.size() + estimated_num_vertices);
  98. indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
  99. // Generate the geometry.
  100. GeometryBackgroundBorder geometry(vertices, indices);
  101. const BorderMetrics metrics = GeometryBackgroundBorder::ComputeBorderMetrics(offset.Round(), border_widths, padding_size, border_radius);
  102. if (has_background)
  103. geometry.DrawBackground(metrics, background_color);
  104. if (has_border)
  105. geometry.DrawBorder(metrics, border_widths, border_colors);
  106. #if 0
  107. // Debug draw vertices
  108. if (border_radius != Vector4f(0))
  109. {
  110. const int num_vertices = (int)vertices.size();
  111. const int num_indices = (int)indices.size();
  112. vertices.resize(num_vertices + 4 * num_vertices);
  113. indices.resize(num_indices + 6 * num_indices);
  114. for (int i = 0; i < num_vertices; i++)
  115. {
  116. MeshUtilities::GenerateQuad(vertices.data() + num_vertices + 4 * i, indices.data() + num_indices + 6 * i, vertices[i].position,
  117. Vector2f(3, 3), ColourbPremultiplied(255, 0, (i % 2) == 0 ? 0 : 255), num_vertices + 4 * i);
  118. }
  119. }
  120. #endif
  121. #ifdef RMLUI_DEBUG
  122. const int num_vertices = (int)vertices.size();
  123. for (int index : indices)
  124. {
  125. RMLUI_ASSERT(index < num_vertices);
  126. }
  127. #endif
  128. }
  129. void MeshUtilities::GenerateBackground(Mesh& out_mesh, const Box& box, Vector2f offset, Vector4f border_radius, ColourbPremultiplied color,
  130. BoxArea fill_area)
  131. {
  132. RMLUI_ASSERTMSG(fill_area >= BoxArea::Border && fill_area <= BoxArea::Content,
  133. "Rectangle geometry only supports border, padding and content boxes.");
  134. EdgeSizes edge_sizes = {};
  135. for (int area = (int)BoxArea::Border; area < (int)fill_area; area++)
  136. {
  137. // TODO: Move rounding to computed values (round border only).
  138. edge_sizes[0] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Top));
  139. edge_sizes[1] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Right));
  140. edge_sizes[2] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Bottom));
  141. edge_sizes[3] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Left));
  142. }
  143. const Vector2f inner_size = box.GetSize(fill_area).Round();
  144. const bool has_background = (color.alpha > 0 && inner_size.x > 0 && inner_size.y > 0);
  145. if (!has_background)
  146. return;
  147. const BorderMetrics metrics = GeometryBackgroundBorder::ComputeBorderMetrics(offset.Round(), edge_sizes, inner_size, border_radius);
  148. Vector<Vertex>& vertices = out_mesh.vertices;
  149. Vector<int>& indices = out_mesh.indices;
  150. // Reserve geometry. A conservative estimate, does not take border-radii into account.
  151. vertices.reserve((int)vertices.size() + 4);
  152. indices.reserve((int)indices.size() + 6);
  153. // Generate the geometry
  154. GeometryBackgroundBorder geometry(vertices, indices);
  155. geometry.DrawBackground(metrics, color);
  156. }
  157. } // namespace Rml