UIBatch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "ShaderProgram.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, ShaderProgram* vs, ShaderProgram* ps) 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. graphics->SetShaders(vs, ps);
  160. Vector2 posAdjust(0.5f, 0.5f);
  161. Vector2 invScreenSize(1.0f / (float)graphics->GetWidth(), 1.0f / (float)graphics->GetHeight());
  162. const PODVector<UIQuad>& quads = *quads_;
  163. if (texture_)
  164. {
  165. Vector2 invTextureSize(1.0f / (float)texture_->GetWidth(), 1.0f / (float)texture_->GetHeight());
  166. graphics->BeginImmediate(TRIANGLE_LIST, quadCount_ * 6, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  167. float* dest = (float*)graphics->GetImmediateDataPtr();
  168. for (unsigned i = quadStart_; i < quadStart_ + quadCount_; ++i)
  169. {
  170. const UIQuad& quad = quads[i];
  171. Vector2 topLeft, bottoright_, topLeftUV, bottorightUV_;
  172. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  173. bottoright_ = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  174. topLeftUV = Vector2((float)quad.leftUV_, (float)quad.topUV_) * invTextureSize;
  175. bottorightUV_ = Vector2((float)quad.rightUV_, (float)quad.bottomUV_) * invTextureSize;
  176. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  177. *((unsigned*)dest) = quads[i].topLeftColor_; dest++;
  178. *dest++ = topLeftUV.x_; *dest++ = topLeftUV.y_;
  179. *dest++ = bottoright_.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  180. *((unsigned*)dest) = quads[i].topRightColor_; dest++;
  181. *dest++ = bottorightUV_.x_; *dest++ = topLeftUV.y_;
  182. *dest++ = topLeft.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  183. *((unsigned*)dest) = quads[i].bottomLeftColor_; dest++;
  184. *dest++ = topLeftUV.x_; *dest++ = bottorightUV_.y_;
  185. *dest++ = bottoright_.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  186. *((unsigned*)dest) = quads[i].topRightColor_; dest++;
  187. *dest++ = bottorightUV_.x_; *dest++ = topLeftUV.y_;
  188. *dest++ = bottoright_.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  189. *((unsigned*)dest) = quads[i].bottomRightColor_; dest++;
  190. *dest++ = bottorightUV_.x_; *dest++ = bottorightUV_.y_;
  191. *dest++ = topLeft.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  192. *((unsigned*)dest) = quads[i].bottomLeftColor_; dest++;
  193. *dest++ = topLeftUV.x_; *dest++ = bottorightUV_.y_;
  194. }
  195. graphics->EndImmediate();
  196. }
  197. else
  198. {
  199. graphics->BeginImmediate(TRIANGLE_LIST, quadCount_ * 6, MASK_POSITION | MASK_COLOR);
  200. float* dest = (float*)graphics->GetImmediateDataPtr();
  201. for (unsigned i = quadStart_; i < quadStart_ + quadCount_; ++i)
  202. {
  203. const UIQuad& quad = quads[i];
  204. Vector2 topLeft, bottoright_, topLeftUV, bottorightUV_;
  205. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  206. bottoright_ = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  207. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  208. *((unsigned*)dest) = quads[i].topLeftColor_; dest++;
  209. *dest++ = bottoright_.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  210. *((unsigned*)dest) = quads[i].topRightColor_; dest++;
  211. *dest++ = topLeft.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  212. *((unsigned*)dest) = quads[i].bottomLeftColor_; dest++;
  213. *dest++ = bottoright_.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  214. *((unsigned*)dest) = quads[i].topRightColor_; dest++;
  215. *dest++ = bottoright_.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  216. *((unsigned*)dest) = quads[i].bottomRightColor_; dest++;
  217. *dest++ = topLeft.x_; *dest++ = bottoright_.y_; *dest++ = 0.0f;
  218. *((unsigned*)dest) = quads[i].bottomLeftColor_; dest++;
  219. }
  220. graphics->EndImmediate();
  221. }
  222. }
  223. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  224. {
  225. if (!batch.quadCount_)
  226. return;
  227. if (!batches.Size())
  228. batches.Push(batch);
  229. else
  230. {
  231. if (batches.Back().Merge(batch))
  232. return;
  233. else
  234. batches.Push(batch);
  235. }
  236. }
  237. unsigned UIBatch::GetInterpolatedColor(UIElement& element, int x, int y)
  238. {
  239. const IntVector2& size = element.GetSize();
  240. if ((size.x_) && (size.y_))
  241. {
  242. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  243. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  244. Color topColor = element.GetColor(C_TOPLEFT).Lerp(element.GetColor(C_TOPRIGHT), cLerpX);
  245. Color bottocolor_ = element.GetColor(C_BOTTOMLEFT).Lerp(element.GetColor(C_BOTTOMRIGHT), cLerpX);
  246. Color color = topColor.Lerp(bottocolor_, cLerpY);
  247. color.a_ *= element.GetDerivedOpacity();
  248. return color.ToUInt();
  249. }
  250. else
  251. {
  252. Color color = element.GetColor(C_TOPLEFT);
  253. color.a_ *= element.GetDerivedOpacity();
  254. return color.ToUInt();
  255. }
  256. }