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