UIBatch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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.h"
  25. #include "ShaderVariation.h"
  26. #include "Texture.h"
  27. #include "UIElement.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. void UIBatch::Begin(PODVector<UIQuad>* quads)
  32. {
  33. if (quads)
  34. {
  35. quads_ = quads;
  36. quadStart_ = quads_->Size();
  37. quadCount_ = 0;
  38. }
  39. }
  40. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY)
  41. {
  42. if (!quads_)
  43. return;
  44. UIQuad quad;
  45. const IntVector2& screenPos = element.GetScreenPosition();
  46. quad.left_ = x + screenPos.x_;
  47. quad.top_ = y + screenPos.y_;
  48. quad.right_ = quad.left_ + width;
  49. quad.bottom_ = quad.top_ + height;
  50. quad.leftUV_ = texOffsetX;
  51. quad.topUV_ = texOffsetY;
  52. quad.rightUV_ = texOffsetX + width;
  53. quad.bottomUV_ = texOffsetY + height;
  54. if (element.HasColorGradient())
  55. {
  56. quad.topLeftColor_ = GetInterpolatedColor(element, x, y);
  57. quad.topRightColor_ = GetInterpolatedColor(element, x + width, y);
  58. quad.bottomLeftColor_ = GetInterpolatedColor(element, x, y + height);
  59. quad.bottomRightColor_ = GetInterpolatedColor(element, x + width, y + height);
  60. }
  61. else
  62. {
  63. unsigned uintColor = element.GetUIntColor();
  64. // If alpha is 0, nothing will be rendered, so do not add the quad
  65. if (!(uintColor & 0xff000000))
  66. return;
  67. quad.topLeftColor_ = uintColor;
  68. quad.topRightColor_ = uintColor;
  69. quad.bottomLeftColor_ = uintColor;
  70. quad.bottomRightColor_ = uintColor;
  71. }
  72. quads_->Push(quad);
  73. quadCount_++;
  74. }
  75. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  76. int texHeight)
  77. {
  78. if (!quads_)
  79. return;
  80. UIQuad quad;
  81. const IntVector2& screenPos = element.GetScreenPosition();
  82. quad.left_ = x + screenPos.x_;
  83. quad.top_ = y + screenPos.y_;
  84. quad.right_ = quad.left_ + width;
  85. quad.bottom_ = quad.top_ + height;
  86. quad.leftUV_ = texOffsetX;
  87. quad.topUV_ = texOffsetY;
  88. quad.rightUV_ = texOffsetX + texWidth;
  89. quad.bottomUV_ = texOffsetY + texHeight;
  90. if (element.HasColorGradient())
  91. {
  92. quad.topLeftColor_ = GetInterpolatedColor(element, x, y);
  93. quad.topRightColor_ = GetInterpolatedColor(element, x + width, y);
  94. quad.bottomLeftColor_ = GetInterpolatedColor(element, x, y + height);
  95. quad.bottomRightColor_ = GetInterpolatedColor(element, x + width, y + height);
  96. }
  97. else
  98. {
  99. unsigned uintColor = element.GetUIntColor();
  100. // If alpha is 0, nothing will be rendered, so do not add the quad
  101. if (!(uintColor & 0xff000000))
  102. return;
  103. quad.topLeftColor_ = uintColor;
  104. quad.topRightColor_ = uintColor;
  105. quad.bottomLeftColor_ = uintColor;
  106. quad.bottomRightColor_ = uintColor;
  107. }
  108. quads_->Push(quad);
  109. quadCount_++;
  110. }
  111. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  112. int texHeight, const Color& color)
  113. {
  114. if (!quads_)
  115. return;
  116. UIQuad quad;
  117. const IntVector2& screenPos = element.GetScreenPosition();
  118. quad.left_ = x + screenPos.x_;
  119. quad.top_ = y + screenPos.y_;
  120. quad.right_ = quad.left_ + width;
  121. quad.bottom_ = quad.top_ + height;
  122. quad.leftUV_ = texOffsetX;
  123. quad.topUV_ = texOffsetY;
  124. quad.rightUV_ = texOffsetX + texWidth;
  125. quad.bottomUV_ = texOffsetY + texHeight;
  126. Color derivedColor(color.r_, color.g_, color.b_, color.a_ * element.GetDerivedOpacity());
  127. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  128. if (derivedColor.a_ <= 0.0f)
  129. return;
  130. unsigned uintColor = derivedColor.ToUInt();
  131. quad.topLeftColor_ = uintColor;
  132. quad.topRightColor_ = uintColor;
  133. quad.bottomLeftColor_ = uintColor;
  134. quad.bottomRightColor_ = uintColor;
  135. quads_->Push(quad);
  136. quadCount_++;
  137. }
  138. bool UIBatch::Merge(const UIBatch& batch)
  139. {
  140. if ((batch.blendMode_ != blendMode_) ||
  141. (batch.scissor_ != scissor_) ||
  142. (batch.texture_ != texture_) ||
  143. (batch.quads_ != quads_) ||
  144. (batch.quadStart_ != quadStart_ + quadCount_))
  145. return false;
  146. quadCount_ += batch.quadCount_;
  147. return true;
  148. }
  149. void UIBatch::UpdateGeometry(Graphics* graphics, void* lockedData)
  150. {
  151. if (!quadCount_)
  152. return;
  153. #ifdef USE_OPENGL
  154. Vector2 posAdjust(Vector2::ZERO);
  155. #else
  156. Vector2 posAdjust(0.5f, 0.5f);
  157. #endif
  158. Vector2 invScreenSize(1.0f / (float)graphics->GetWidth(), 1.0f / (float)graphics->GetHeight());
  159. float* dest = (float*)lockedData;
  160. if (texture_)
  161. {
  162. Vector2 invTextureSize(1.0f / (float)texture_->GetWidth(), 1.0f / (float)texture_->GetHeight());
  163. for (unsigned i = 0; i < quadCount_; ++i)
  164. {
  165. const UIQuad& quad = quads_->At(quadStart_ + i);
  166. Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  167. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  168. bottomRight = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  169. topLeftUV = Vector2((float)quad.leftUV_, (float)quad.topUV_) * invTextureSize;
  170. bottomRightUV = Vector2((float)quad.rightUV_, (float)quad.bottomUV_) * invTextureSize;
  171. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  172. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  173. *dest++ = topLeftUV.x_; *dest++ = topLeftUV.y_;
  174. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  175. *((unsigned*)dest) = quad.topRightColor_; dest++;
  176. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  177. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  178. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  179. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  180. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  181. *((unsigned*)dest) = quad.topRightColor_; dest++;
  182. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  183. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  184. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  185. *dest++ = bottomRightUV.x_; *dest++ = bottomRightUV.y_;
  186. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  187. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  188. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  189. }
  190. }
  191. else
  192. {
  193. for (unsigned i = 0; i < quadCount_; ++i)
  194. {
  195. const UIQuad& quad = quads_->At(quadStart_ + i);
  196. Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  197. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  198. bottomRight = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  199. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  200. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  201. dest += 2; // Jump over unused UV coordinates
  202. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  203. *((unsigned*)dest) = quad.topRightColor_; dest++;
  204. dest += 2;
  205. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  206. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  207. dest += 2;
  208. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  209. *((unsigned*)dest) = quad.topRightColor_; dest++;
  210. dest += 2;
  211. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  212. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  213. dest += 2;
  214. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  215. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  216. dest += 2;
  217. }
  218. }
  219. }
  220. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  221. {
  222. if (!batch.quadCount_)
  223. return;
  224. if (!batches.Size())
  225. batches.Push(batch);
  226. else
  227. {
  228. if (batches.Back().Merge(batch))
  229. return;
  230. else
  231. batches.Push(batch);
  232. }
  233. }
  234. unsigned UIBatch::GetInterpolatedColor(UIElement& element, int x, int y)
  235. {
  236. const IntVector2& size = element.GetSize();
  237. if (size.x_ && size.y_)
  238. {
  239. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  240. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  241. Color topColor = element.GetColor(C_TOPLEFT).Lerp(element.GetColor(C_TOPRIGHT), cLerpX);
  242. Color bottocolor_ = element.GetColor(C_BOTTOMLEFT).Lerp(element.GetColor(C_BOTTOMRIGHT), cLerpX);
  243. Color color = topColor.Lerp(bottocolor_, cLerpY);
  244. color.a_ *= element.GetDerivedOpacity();
  245. return color.ToUInt();
  246. }
  247. else
  248. {
  249. Color color = element.GetColor(C_TOPLEFT);
  250. color.a_ *= element.GetDerivedOpacity();
  251. return color.ToUInt();
  252. }
  253. }
  254. }