UIBatch.cpp 11 KB

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