UIBatch.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Math/Matrix3x4.h"
  26. #include "../Graphics/ShaderVariation.h"
  27. #include "../Graphics/Texture.h"
  28. #include "../UI/UIBatch.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  31. {
  32. #ifdef ATOMIC_OPENGL
  33. static const float posAdjust = 0.0f;
  34. #else
  35. static const float posAdjust = 0.5f;
  36. #endif
  37. static const Vector3 posAdjustVec(posAdjust, posAdjust, 0.0f);
  38. UIBatch::UIBatch() :
  39. blendMode_(BLEND_REPLACE),
  40. texture_(0),
  41. invTextureSize_(Vector2::ONE),
  42. vertexData_(0),
  43. vertexStart_(0),
  44. vertexEnd_(0)
  45. {
  46. SetDefaultColor();
  47. }
  48. UIBatch::UIBatch(BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  49. blendMode_(blendMode),
  50. scissor_(scissor),
  51. texture_(texture),
  52. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  53. vertexData_(vertexData),
  54. vertexStart_(vertexData->Size()),
  55. vertexEnd_(vertexData->Size())
  56. {
  57. SetDefaultColor();
  58. }
  59. void UIBatch::SetColor(const Color& color, bool overrideAlpha)
  60. {
  61. useGradient_ = false;
  62. color_ = color.ToUInt();
  63. }
  64. void UIBatch::SetDefaultColor()
  65. {
  66. color_ = 0xffffffff;
  67. useGradient_ = false;
  68. }
  69. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  70. {
  71. // If alpha is 0, nothing will be rendered, so do not add the quad
  72. if (!(color_ & 0xff000000))
  73. return;
  74. //const IntVector2& screenPos = element_->GetScreenPosition();
  75. IntVector2 screenPos(0, 0);
  76. float left = (float)(x + screenPos.x_) - posAdjust;
  77. float right = left + (float)width;
  78. float top = (float)(y + screenPos.y_) - posAdjust;
  79. float bottom = top + (float)height;
  80. float leftUV = texOffsetX * invTextureSize_.x_;
  81. float topUV = texOffsetY * invTextureSize_.y_;
  82. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  83. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  84. unsigned begin = vertexData_->Size();
  85. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  86. float* dest = &(vertexData_->At(begin));
  87. vertexEnd_ = vertexData_->Size();
  88. dest[0] = left; dest[1] = top; dest[2] = 0.0f;
  89. ((unsigned&)dest[3]) = color_;
  90. dest[4] = leftUV; dest[5] = topUV;
  91. dest[6] = right; dest[7] = top; dest[8] = 0.0f;
  92. ((unsigned&)dest[9]) = color_;
  93. dest[10] = rightUV; dest[11] = topUV;
  94. dest[12] = left; dest[13] = bottom; dest[14] = 0.0f;
  95. ((unsigned&)dest[15]) = color_;
  96. dest[16] = leftUV; dest[17] = bottomUV;
  97. dest[18] = right; dest[19] = top; dest[20] = 0.0f;
  98. ((unsigned&)dest[21]) = color_;
  99. dest[22] = rightUV; dest[23] = topUV;
  100. dest[24] = right; dest[25] = bottom; dest[26] = 0.0f;
  101. ((unsigned&)dest[27]) = color_;
  102. dest[28] = rightUV; dest[29] = bottomUV;
  103. dest[30] = left; dest[31] = bottom; dest[32] = 0.0f;
  104. ((unsigned&)dest[33]) = color_;
  105. dest[34] = leftUV; dest[35] = bottomUV;
  106. dest += 36;
  107. }
  108. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  109. int texWidth, int texHeight)
  110. {
  111. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjustVec;
  112. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjustVec;
  113. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjustVec;
  114. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjustVec;
  115. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  116. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  117. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  118. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  119. unsigned begin = vertexData_->Size();
  120. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  121. float* dest = &(vertexData_->At(begin));
  122. vertexEnd_ = vertexData_->Size();
  123. dest[0] = v1.x_; dest[1] = v1.y_; dest[2] = 0.0f;
  124. ((unsigned&)dest[3]) = color_;
  125. dest[4] = leftUV; dest[5] = topUV;
  126. dest[6] = v2.x_; dest[7] = v2.y_; dest[8] = 0.0f;
  127. ((unsigned&)dest[9]) = color_;
  128. dest[10] = rightUV; dest[11] = topUV;
  129. dest[12] = v3.x_; dest[13] = v3.y_; dest[14] = 0.0f;
  130. ((unsigned&)dest[15]) = color_;
  131. dest[16] = leftUV; dest[17] = bottomUV;
  132. dest[18] = v2.x_; dest[19] = v2.y_; dest[20] = 0.0f;
  133. ((unsigned&)dest[21]) = color_;
  134. dest[22] = rightUV; dest[23] = topUV;
  135. dest[24] = v4.x_; dest[25] = v4.y_; dest[26] = 0.0f;
  136. ((unsigned&)dest[27]) = color_;
  137. dest[28] = rightUV; dest[29] = bottomUV;
  138. dest[30] = v3.x_; dest[31] = v3.y_; dest[32] = 0.0f;
  139. ((unsigned&)dest[33]) = color_;
  140. dest[34] = leftUV; dest[35] = bottomUV;
  141. }
  142. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  143. {
  144. if (!tiled)
  145. {
  146. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  147. return;
  148. }
  149. int tileX = 0;
  150. int tileY = 0;
  151. int tileW = 0;
  152. int tileH = 0;
  153. while (tileY < height)
  154. {
  155. tileX = 0;
  156. tileH = Min(height - tileY, texHeight);
  157. while (tileX < width)
  158. {
  159. tileW = Min(width - tileX, texWidth);
  160. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  161. tileX += tileW;
  162. }
  163. tileY += tileH;
  164. }
  165. }
  166. bool UIBatch::Merge(const UIBatch& batch)
  167. {
  168. if (batch.blendMode_ != blendMode_ ||
  169. batch.scissor_ != scissor_ ||
  170. batch.texture_ != texture_ ||
  171. batch.vertexData_ != vertexData_ ||
  172. batch.vertexStart_ != vertexEnd_)
  173. return false;
  174. vertexEnd_ = batch.vertexEnd_;
  175. return true;
  176. }
  177. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  178. {
  179. if (batch.vertexEnd_ == batch.vertexStart_)
  180. return;
  181. if (!batches.Empty() && batches.Back().Merge(batch))
  182. return;
  183. batches.Push(batch);
  184. }
  185. }