Geometry.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/Geometry.h"
  30. #include "../../Include/RmlUi/Core.h"
  31. #include "GeometryDatabase.h"
  32. namespace Rml {
  33. namespace Core {
  34. static bool read_texel_offset = false;
  35. static Vector2f texel_offset;
  36. Geometry::Geometry(Element* _host_element)
  37. {
  38. host_element = _host_element;
  39. host_context = nullptr;
  40. GeometryDatabase::AddGeometry(this);
  41. texture = nullptr;
  42. fixed_texcoords = false;
  43. compile_attempted = false;
  44. compiled_geometry = 0;
  45. }
  46. Geometry::Geometry(Context* _host_context)
  47. {
  48. host_element = nullptr;
  49. host_context = _host_context;
  50. GeometryDatabase::AddGeometry(this);
  51. texture = nullptr;
  52. fixed_texcoords = false;
  53. compile_attempted = false;
  54. compiled_geometry = 0;
  55. }
  56. Geometry::~Geometry()
  57. {
  58. GeometryDatabase::RemoveGeometry(this);
  59. Release();
  60. }
  61. // Set the host element for this geometry; this should be passed in the constructor if possible.
  62. void Geometry::SetHostElement(Element* _host_element)
  63. {
  64. if (host_element == _host_element)
  65. return;
  66. if (host_element != nullptr)
  67. {
  68. Release();
  69. host_context = nullptr;
  70. }
  71. host_element = _host_element;
  72. }
  73. void Geometry::Render(const Vector2f& translation)
  74. {
  75. RenderInterface* render_interface = GetRenderInterface();
  76. if (render_interface == nullptr)
  77. return;
  78. // Render our compiled geometry if possible.
  79. if (compiled_geometry)
  80. {
  81. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  82. }
  83. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  84. // immediate mode.
  85. else
  86. {
  87. if (vertices.empty() ||
  88. indices.empty())
  89. return;
  90. if (!compile_attempted)
  91. {
  92. if (!fixed_texcoords)
  93. {
  94. fixed_texcoords = true;
  95. if (!read_texel_offset)
  96. {
  97. read_texel_offset = true;
  98. texel_offset.x = render_interface->GetHorizontalTexelOffset();
  99. texel_offset.y = render_interface->GetVerticalTexelOffset();
  100. }
  101. // Add a half-texel offset if required.
  102. if (texel_offset.x != 0 ||
  103. texel_offset.y != 0)
  104. {
  105. for (size_t i = 0; i < vertices.size(); ++i)
  106. vertices[i].position += texel_offset;
  107. }
  108. }
  109. compile_attempted = true;
  110. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0);
  111. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  112. // immediately render the compiled version.
  113. if (compiled_geometry)
  114. {
  115. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  116. return;
  117. }
  118. }
  119. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  120. // render the uncompiled version.
  121. render_interface->RenderGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  122. }
  123. }
  124. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  125. std::vector< Vertex >& Geometry::GetVertices()
  126. {
  127. return vertices;
  128. }
  129. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  130. std::vector< int >& Geometry::GetIndices()
  131. {
  132. return indices;
  133. }
  134. // Gets the geometry's texture.
  135. const Texture* Geometry::GetTexture() const
  136. {
  137. return texture;
  138. }
  139. // Sets the geometry's texture.
  140. void Geometry::SetTexture(const Texture* _texture)
  141. {
  142. texture = _texture;
  143. Release();
  144. }
  145. void Geometry::Release(bool clear_buffers)
  146. {
  147. if (compiled_geometry)
  148. {
  149. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  150. compiled_geometry = 0;
  151. }
  152. compile_attempted = false;
  153. if (clear_buffers)
  154. {
  155. vertices.clear();
  156. indices.clear();
  157. fixed_texcoords = false;
  158. }
  159. }
  160. // Returns the host context's render interface.
  161. RenderInterface* Geometry::GetRenderInterface()
  162. {
  163. if (host_context == nullptr)
  164. {
  165. if (host_element != nullptr)
  166. host_context = host_element->GetContext();
  167. }
  168. if (host_context == nullptr)
  169. return Rml::Core::GetRenderInterface();
  170. else
  171. return host_context->GetRenderInterface();
  172. }
  173. }
  174. }