UIBatch.cpp 7.5 KB

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