UIBatch.cpp 11 KB

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