UIBatch.cpp 11 KB

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