Geometry.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include <Rocket/Core/Geometry.h>
  29. #include <Rocket/Core.h>
  30. #include "GeometryDatabase.h"
  31. namespace Rocket {
  32. namespace Core {
  33. static bool read_texel_offset = false;
  34. static Vector2f texel_offset;
  35. Geometry::Geometry(Element* _host_element)
  36. {
  37. host_element = _host_element;
  38. host_context = NULL;
  39. GeometryDatabase::AddGeometry(this);
  40. texture = NULL;
  41. fixed_texcoords = false;
  42. compile_attempted = false;
  43. compiled_geometry = 0;
  44. }
  45. Geometry::Geometry(Context* _host_context)
  46. {
  47. host_element = NULL;
  48. host_context = _host_context;
  49. GeometryDatabase::AddGeometry(this);
  50. texture = NULL;
  51. fixed_texcoords = false;
  52. compile_attempted = false;
  53. compiled_geometry = 0;
  54. }
  55. Geometry::~Geometry()
  56. {
  57. GeometryDatabase::RemoveGeometry(this);
  58. Release();
  59. }
  60. // Set the host element for this geometry; this should be passed in the constructor if possible.
  61. void Geometry::SetHostElement(Element* _host_element)
  62. {
  63. if (host_element == _host_element)
  64. return;
  65. if (host_element != NULL)
  66. {
  67. Release();
  68. host_context = NULL;
  69. }
  70. host_element = _host_element;
  71. }
  72. void Geometry::Render(const Vector2f& translation)
  73. {
  74. RenderInterface* render_interface = GetRenderInterface();
  75. if (render_interface == NULL)
  76. return;
  77. // Render our compiled geometry if possible.
  78. if (compiled_geometry)
  79. {
  80. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  81. }
  82. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  83. // immediate mode.
  84. else
  85. {
  86. if (vertices.empty() ||
  87. indices.empty())
  88. return;
  89. if (!compile_attempted)
  90. {
  91. if (!fixed_texcoords)
  92. {
  93. fixed_texcoords = true;
  94. if (!read_texel_offset)
  95. {
  96. read_texel_offset = true;
  97. texel_offset.x = render_interface->GetHorizontalTexelOffset();
  98. texel_offset.y = render_interface->GetVerticalTexelOffset();
  99. }
  100. // Add a half-texel offset if required.
  101. if (texel_offset.x != 0 ||
  102. texel_offset.y != 0)
  103. {
  104. for (size_t i = 0; i < vertices.size(); ++i)
  105. vertices[i].position += texel_offset;
  106. }
  107. }
  108. compile_attempted = true;
  109. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != NULL ? texture->GetHandle(GetRenderInterface()) : 0);
  110. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  111. // immediately render the compiled version.
  112. if (compiled_geometry)
  113. {
  114. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  115. return;
  116. }
  117. }
  118. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  119. // render the uncompiled version.
  120. render_interface->RenderGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != NULL ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  121. }
  122. }
  123. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  124. std::vector< Vertex >& Geometry::GetVertices()
  125. {
  126. return vertices;
  127. }
  128. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  129. std::vector< int >& Geometry::GetIndices()
  130. {
  131. return indices;
  132. }
  133. // Gets the geometry's texture.
  134. const Texture* Geometry::GetTexture() const
  135. {
  136. return texture;
  137. }
  138. // Sets the geometry's texture.
  139. void Geometry::SetTexture(const Texture* _texture)
  140. {
  141. texture = _texture;
  142. Release();
  143. }
  144. void Geometry::Release(bool clear_buffers)
  145. {
  146. if (compiled_geometry)
  147. {
  148. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  149. compiled_geometry = 0;
  150. }
  151. compile_attempted = false;
  152. if (clear_buffers)
  153. {
  154. vertices.clear();
  155. indices.clear();
  156. fixed_texcoords = false;
  157. }
  158. }
  159. // Returns the host context's render interface.
  160. RenderInterface* Geometry::GetRenderInterface()
  161. {
  162. if (host_context == NULL)
  163. {
  164. if (host_element != NULL)
  165. host_context = host_element->GetContext();
  166. }
  167. if (host_context == NULL)
  168. return Rocket::Core::GetRenderInterface();
  169. else
  170. return host_context->GetRenderInterface();
  171. }
  172. }
  173. }