MeshUtilities.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 RenderBox& render_box, ColourbPremultiplied background_color,
  73. 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. const EdgeSizes& border_widths = render_box.GetBorderWidths();
  79. int num_borders = 0;
  80. for (int i = 0; i < 4; i++)
  81. if (border_colors[i].alpha > 0 && border_widths[i] > 0)
  82. num_borders += 1;
  83. const Vector2f fill_size = render_box.GetFillSize();
  84. const bool has_background = (background_color.alpha > 0 && fill_size.x > 0 && fill_size.y > 0);
  85. const bool has_border = (num_borders > 0);
  86. if (!has_background && !has_border)
  87. return;
  88. // Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
  89. const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
  90. const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
  91. vertices.reserve((int)vertices.size() + estimated_num_vertices);
  92. indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
  93. // Generate the geometry.
  94. GeometryBackgroundBorder geometry(vertices, indices);
  95. const BorderMetrics metrics =
  96. GeometryBackgroundBorder::ComputeBorderMetrics(render_box.GetBorderOffset(), border_widths, fill_size, render_box.GetBorderRadius());
  97. if (has_background)
  98. geometry.DrawBackground(metrics, background_color);
  99. if (has_border)
  100. geometry.DrawBorder(metrics, border_widths, border_colors);
  101. #if 0
  102. // Debug draw vertices
  103. if (render_box.border_radius != CornerSizes{})
  104. {
  105. const int num_vertices = (int)vertices.size();
  106. const int num_indices = (int)indices.size();
  107. vertices.reserve(num_vertices + 4 * num_vertices);
  108. indices.reserve(num_indices + 6 * num_indices);
  109. for (int i = 0; i < num_vertices; i++)
  110. MeshUtilities::GenerateQuad(out_mesh, vertices[i].position, Vector2f(3, 3), ColourbPremultiplied(255, 0, (i % 2) == 0 ? 0 : 255));
  111. }
  112. #endif
  113. #ifdef RMLUI_DEBUG
  114. const int num_vertices = (int)vertices.size();
  115. for (int index : indices)
  116. {
  117. RMLUI_ASSERT(index < num_vertices);
  118. }
  119. #endif
  120. }
  121. void MeshUtilities::GenerateBackground(Mesh& out_mesh, const RenderBox& render_box, ColourbPremultiplied color)
  122. {
  123. const Vector2f fill_size = render_box.GetFillSize();
  124. const bool has_background = (color.alpha > 0 && fill_size.x > 0 && fill_size.y > 0);
  125. if (!has_background)
  126. return;
  127. const BorderMetrics metrics = GeometryBackgroundBorder::ComputeBorderMetrics(render_box.GetBorderOffset(), render_box.GetBorderWidths(),
  128. fill_size, render_box.GetBorderRadius());
  129. Vector<Vertex>& vertices = out_mesh.vertices;
  130. Vector<int>& indices = out_mesh.indices;
  131. // Reserve geometry. A conservative estimate, does not take border-radii into account.
  132. vertices.reserve((int)vertices.size() + 4);
  133. indices.reserve((int)indices.size() + 6);
  134. // Generate the geometry
  135. GeometryBackgroundBorder geometry(vertices, indices);
  136. geometry.DrawBackground(metrics, color);
  137. }
  138. } // namespace Rml