UIBatch.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.h"
  24. #include "Matrix3x4.h"
  25. #include "ShaderVariation.h"
  26. #include "Texture.h"
  27. #include "UIElement.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. #ifdef URHO3D_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. element_(0),
  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(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  49. element_(element),
  50. blendMode_(blendMode),
  51. scissor_(scissor),
  52. texture_(texture),
  53. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  54. vertexData_(vertexData),
  55. vertexStart_(vertexData->Size()),
  56. vertexEnd_(vertexData->Size())
  57. {
  58. SetDefaultColor();
  59. }
  60. void UIBatch::SetColor(const Color& color, bool overrideAlpha)
  61. {
  62. if (!element_)
  63. overrideAlpha = true;
  64. useGradient_ = false;
  65. color_ = overrideAlpha ? color.ToUInt() : Color(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity()).ToUInt();
  66. }
  67. void UIBatch::SetDefaultColor()
  68. {
  69. if (element_)
  70. {
  71. color_ = element_->GetDerivedColor().ToUInt();
  72. useGradient_ = element_->HasColorGradient();
  73. }
  74. else
  75. {
  76. color_ = 0xffffffff;
  77. useGradient_ = false;
  78. }
  79. }
  80. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  81. {
  82. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  83. if (!useGradient_)
  84. {
  85. // If alpha is 0, nothing will be rendered, so do not add the quad
  86. if (!(color_ & 0xff000000))
  87. return;
  88. topLeftColor = color_;
  89. topRightColor = color_;
  90. bottomLeftColor = color_;
  91. bottomRightColor = color_;
  92. }
  93. else
  94. {
  95. topLeftColor = GetInterpolatedColor(x, y);
  96. topRightColor = GetInterpolatedColor(x + width, y);
  97. bottomLeftColor = GetInterpolatedColor(x, y + height);
  98. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  99. }
  100. const IntVector2& screenPos = element_->GetScreenPosition();
  101. float left = (float)(x + screenPos.x_) - posAdjust;
  102. float right = left + (float)width;
  103. float top = (float)(y + screenPos.y_) - posAdjust;
  104. float bottom = top + (float)height;
  105. float leftUV = texOffsetX * invTextureSize_.x_;
  106. float topUV = texOffsetY * invTextureSize_.y_;
  107. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  108. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  109. unsigned begin = vertexData_->Size();
  110. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  111. float* dest = &(vertexData_->At(begin));
  112. vertexEnd_ = vertexData_->Size();
  113. dest[0] = left; dest[1] = top; dest[2] = 0.0f;
  114. ((unsigned&)dest[3]) = topLeftColor;
  115. dest[4] = leftUV; dest[5] = topUV;
  116. dest[6] = right; dest[7] = top; dest[8] = 0.0f;
  117. ((unsigned&)dest[9]) = topRightColor;
  118. dest[10] = rightUV; dest[11] = topUV;
  119. dest[12] = left; dest[13] = bottom; dest[14] = 0.0f;
  120. ((unsigned&)dest[15]) = bottomLeftColor;
  121. dest[16] = leftUV; dest[17] = bottomUV;
  122. dest[18] = right; dest[19] = top; dest[20] = 0.0f;
  123. ((unsigned&)dest[21]) = topRightColor;
  124. dest[22] = rightUV; dest[23] = topUV;
  125. dest[24] = right; dest[25] = bottom; dest[26] = 0.0f;
  126. ((unsigned&)dest[27]) = bottomRightColor;
  127. dest[28] = rightUV; dest[29] = bottomUV;
  128. dest[30] = left; dest[31] = bottom; dest[32] = 0.0f;
  129. ((unsigned&)dest[33]) = bottomLeftColor;
  130. dest[34] = leftUV; dest[35] = bottomUV;
  131. dest += 36;
  132. }
  133. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  134. int texWidth, int texHeight)
  135. {
  136. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  137. if (!useGradient_)
  138. {
  139. // If alpha is 0, nothing will be rendered, so do not add the quad
  140. if (!(color_ & 0xff000000))
  141. return;
  142. topLeftColor = color_;
  143. topRightColor = color_;
  144. bottomLeftColor = color_;
  145. bottomRightColor = color_;
  146. }
  147. else
  148. {
  149. topLeftColor = GetInterpolatedColor(x, y);
  150. topRightColor = GetInterpolatedColor(x + width, y);
  151. bottomLeftColor = GetInterpolatedColor(x, y + height);
  152. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  153. }
  154. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjustVec;
  155. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjustVec;
  156. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjustVec;
  157. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjustVec;
  158. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  159. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  160. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  161. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  162. unsigned begin = vertexData_->Size();
  163. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  164. float* dest = &(vertexData_->At(begin));
  165. vertexEnd_ = vertexData_->Size();
  166. dest[0] = v1.x_; dest[1] = v1.y_; dest[2] = 0.0f;
  167. ((unsigned&)dest[3]) = topLeftColor;
  168. dest[4] = leftUV; dest[5] = topUV;
  169. dest[6] = v2.x_; dest[7] = v2.y_; dest[8] = 0.0f;
  170. ((unsigned&)dest[9]) = topRightColor;
  171. dest[10] = rightUV; dest[11] = topUV;
  172. dest[12] = v3.x_; dest[13] = v3.y_; dest[14] = 0.0f;
  173. ((unsigned&)dest[15]) = bottomLeftColor;
  174. dest[16] = leftUV; dest[17] = bottomUV;
  175. dest[18] = v2.x_; dest[19] = v2.y_; dest[20] = 0.0f;
  176. ((unsigned&)dest[21]) = topRightColor;
  177. dest[22] = rightUV; dest[23] = topUV;
  178. dest[24] = v4.x_; dest[25] = v4.y_; dest[26] = 0.0f;
  179. ((unsigned&)dest[27]) = bottomRightColor;
  180. dest[28] = rightUV; dest[29] = bottomUV;
  181. dest[30] = v3.x_; dest[31] = v3.y_; dest[32] = 0.0f;
  182. ((unsigned&)dest[33]) = bottomLeftColor;
  183. dest[34] = leftUV; dest[35] = bottomUV;
  184. }
  185. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  186. {
  187. if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
  188. return; // No gradient and alpha is 0, so do not add the quad
  189. if (!tiled)
  190. {
  191. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  192. return;
  193. }
  194. int tileX = 0;
  195. int tileY = 0;
  196. int tileW = 0;
  197. int tileH = 0;
  198. while (tileY < height)
  199. {
  200. tileX = 0;
  201. tileH = Min(height - tileY, texHeight);
  202. while (tileX < width)
  203. {
  204. tileW = Min(width - tileX, texWidth);
  205. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  206. tileX += tileW;
  207. }
  208. tileY += tileH;
  209. }
  210. }
  211. bool UIBatch::Merge(const UIBatch& batch)
  212. {
  213. if (batch.blendMode_ != blendMode_ ||
  214. batch.scissor_ != scissor_ ||
  215. batch.texture_ != texture_ ||
  216. batch.vertexData_ != vertexData_ ||
  217. batch.vertexStart_ != vertexEnd_)
  218. return false;
  219. vertexEnd_ = batch.vertexEnd_;
  220. return true;
  221. }
  222. unsigned UIBatch::GetInterpolatedColor(int x, int y)
  223. {
  224. const IntVector2& size = element_->GetSize();
  225. if (size.x_ && size.y_)
  226. {
  227. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  228. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  229. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  230. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  231. Color color = topColor.Lerp(bottomColor, cLerpY);
  232. color.a_ *= element_->GetDerivedOpacity();
  233. return color.ToUInt();
  234. }
  235. else
  236. {
  237. Color color = element_->GetColor(C_TOPLEFT);
  238. color.a_ *= element_->GetDerivedOpacity();
  239. return color.ToUInt();
  240. }
  241. }
  242. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  243. {
  244. if (batch.vertexEnd_ == batch.vertexStart_)
  245. return;
  246. if (!batches.Empty() && batches.Back().Merge(batch))
  247. return;
  248. batches.Push(batch);
  249. }
  250. }