UIBatch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. {
  109. quads_ = quads;
  110. quadStart_ = quads_->Size();
  111. quadCount_ = 0;
  112. }
  113. }
  114. void UIBatch::AddQuad(const UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY)
  115. {
  116. if (!quads_)
  117. return;
  118. UIQuad quad(element, x, y, width, height, texOffsetX, texOffsetY);
  119. if (quad.defined_)
  120. {
  121. quads_->Push(quad);
  122. ++quadCount_;
  123. }
  124. }
  125. void UIBatch::AddQuad(const UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  126. int texHeight)
  127. {
  128. if (!quads_)
  129. return;
  130. UIQuad quad(element, x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  131. if (quad.defined_)
  132. {
  133. quads_->Push(quad);
  134. ++quadCount_;
  135. }
  136. }
  137. void UIBatch::AddQuad(const UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  138. int texHeight, bool tiled)
  139. {
  140. if (!quads_)
  141. return;
  142. if (!(element.HasColorGradient() || element.GetDerivedColor().ToUInt() & 0xff000000))
  143. return; // No gradient and alpha is 0, so do not add the quads
  144. if (!tiled)
  145. {
  146. AddQuad(element, x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  147. return;
  148. }
  149. int tileX = 0;
  150. int tileY = 0;
  151. int tileW = 0;
  152. int tileH = 0;
  153. while (tileY < height)
  154. {
  155. tileX = 0;
  156. tileH = Min(height - tileY, texHeight);
  157. while (tileX < width)
  158. {
  159. tileW = Min(width - tileX, texWidth);
  160. AddQuad(element, x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  161. tileX += tileW;
  162. }
  163. tileY += tileH;
  164. }
  165. }
  166. void UIBatch::AddQuad(const UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  167. int texHeight, const Color& color)
  168. {
  169. if (!quads_)
  170. return;
  171. Color derivedColor(color.r_, color.g_, color.b_, color.a_ * element.GetDerivedOpacity());
  172. UIQuad quad(element, x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight, &derivedColor);
  173. if (quad.defined_)
  174. {
  175. quads_->Push(quad);
  176. ++quadCount_;
  177. }
  178. }
  179. bool UIBatch::Merge(const UIBatch& batch)
  180. {
  181. if ((batch.blendMode_ != blendMode_) ||
  182. (batch.scissor_ != scissor_) ||
  183. (batch.texture_ != texture_) ||
  184. (batch.quads_ != quads_) ||
  185. (batch.quadStart_ != quadStart_ + quadCount_))
  186. return false;
  187. quadCount_ += batch.quadCount_;
  188. return true;
  189. }
  190. void UIBatch::UpdateGeometry(Graphics* graphics, void* lockedData)
  191. {
  192. if (!quadCount_)
  193. return;
  194. #ifdef USE_OPENGL
  195. Vector2 posAdjust(Vector2::ZERO);
  196. #else
  197. Vector2 posAdjust(0.5f, 0.5f);
  198. #endif
  199. Vector2 invScreenSize(1.0f / (float)graphics->GetWidth(), 1.0f / (float)graphics->GetHeight());
  200. float* dest = (float*)lockedData;
  201. if (texture_)
  202. {
  203. Vector2 invTextureSize(1.0f / (float)texture_->GetWidth(), 1.0f / (float)texture_->GetHeight());
  204. for (unsigned i = 0; i < quadCount_; ++i)
  205. {
  206. const UIQuad& quad = quads_->At(quadStart_ + 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. topLeftUV = Vector2((float)quad.leftUV_, (float)quad.topUV_) * invTextureSize;
  211. bottomRightUV = Vector2((float)quad.rightUV_, (float)quad.bottomUV_) * invTextureSize;
  212. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  213. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  214. *dest++ = topLeftUV.x_; *dest++ = topLeftUV.y_;
  215. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  216. *((unsigned*)dest) = quad.topRightColor_; dest++;
  217. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  218. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  219. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  220. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  221. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  222. *((unsigned*)dest) = quad.topRightColor_; dest++;
  223. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  224. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  225. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  226. *dest++ = bottomRightUV.x_; *dest++ = bottomRightUV.y_;
  227. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  228. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  229. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  230. }
  231. }
  232. else
  233. {
  234. for (unsigned i = 0; i < quadCount_; ++i)
  235. {
  236. const UIQuad& quad = quads_->At(quadStart_ + i);
  237. Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  238. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  239. bottomRight = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  240. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  241. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  242. dest += 2; // Jump over unused UV coordinates
  243. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  244. *((unsigned*)dest) = quad.topRightColor_; dest++;
  245. dest += 2;
  246. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  247. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  248. dest += 2;
  249. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  250. *((unsigned*)dest) = quad.topRightColor_; dest++;
  251. dest += 2;
  252. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  253. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  254. dest += 2;
  255. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  256. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  257. dest += 2;
  258. }
  259. }
  260. }
  261. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  262. {
  263. if (!batch.quadCount_)
  264. return;
  265. if (!batches.Empty() && batches.Back().Merge(batch))
  266. return;
  267. batches.Push(batch);
  268. }
  269. }