UIBatch.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "Matrix3x4.h"
  25. #include "ShaderVariation.h"
  26. #include "Texture.h"
  27. #include "UIElement.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. #ifdef USE_OPENGL
  32. static const float posAdjust = 0.0f;
  33. #else
  34. static const float posAdjust = 0.5f;
  35. #endif
  36. static const Vector3 posAdjustVec(posAdjust, posAdjust, 0.0f);
  37. UIBatch::UIBatch() :
  38. element_(0),
  39. blendMode_(BLEND_REPLACE),
  40. texture_(0),
  41. invTextureSize_(Vector2::ONE),
  42. fixedColor_(0),
  43. vertexData_(0),
  44. vertexStart_(0),
  45. vertexEnd_(0)
  46. {
  47. }
  48. UIBatch::UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  49. element_(element),
  50. blendMode_(blendMode),
  51. scissor_(scissor),
  52. texture_(texture),
  53. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  54. fixedColor_(element->GetDerivedColor().ToUInt()),
  55. vertexData_(vertexData),
  56. vertexStart_(vertexData->Size()),
  57. vertexEnd_(vertexData->Size())
  58. {
  59. }
  60. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight,
  61. Color* color)
  62. {
  63. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  64. if (color || !element_->HasColorGradient())
  65. {
  66. unsigned uintColor = color ? color->ToUInt() : fixedColor_;
  67. // If alpha is 0, nothing will be rendered, so do not add the quad
  68. if (!(uintColor & 0xff000000))
  69. return;
  70. topLeftColor = uintColor;
  71. topRightColor = uintColor;
  72. bottomLeftColor = uintColor;
  73. bottomRightColor = uintColor;
  74. }
  75. else
  76. {
  77. topLeftColor = GetInterpolatedColor(x, y);
  78. topRightColor = GetInterpolatedColor(x + width, y);
  79. bottomLeftColor = GetInterpolatedColor(x, y + height);
  80. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  81. }
  82. const IntVector2& screenPos = element_->GetScreenPosition();
  83. float left = (float)(x + screenPos.x_) - posAdjust;
  84. float right = left + (float)width;
  85. float top = (float)(y + screenPos.y_) - posAdjust;
  86. float bottom = top + (float)height;
  87. float leftUV = texOffsetX * invTextureSize_.x_;
  88. float topUV = texOffsetY * invTextureSize_.y_;
  89. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  90. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  91. unsigned begin = vertexData_->Size();
  92. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  93. float* dest = &(vertexData_->At(begin));
  94. vertexEnd_ = vertexData_->Size();
  95. *dest++ = left; *dest++ = top; *dest++ = 0.0f;
  96. *((unsigned*)dest) = topLeftColor; dest++;
  97. *dest++ = leftUV; *dest++ = topUV;
  98. *dest++ = right; *dest++ = top; *dest++ = 0.0f;
  99. *((unsigned*)dest) = topRightColor; dest++;
  100. *dest++ = rightUV; *dest++ = topUV;
  101. *dest++ = left; *dest++ = bottom; *dest++ = 0.0f;
  102. *((unsigned*)dest) = bottomLeftColor; dest++;
  103. *dest++ = leftUV; *dest++ = bottomUV;
  104. *dest++ = right; *dest++ = top; *dest++ = 0.0f;
  105. *((unsigned*)dest) = topRightColor; dest++;
  106. *dest++ = rightUV; *dest++ = topUV;
  107. *dest++ = right; *dest++ = bottom; *dest++ = 0.0f;
  108. *((unsigned*)dest) = bottomRightColor; dest++;
  109. *dest++ = rightUV; *dest++ = bottomUV;
  110. *dest++ = left; *dest++ = bottom; *dest++ = 0.0f;
  111. *((unsigned*)dest) = bottomLeftColor; dest++;
  112. *dest++ = leftUV; *dest++ = bottomUV;
  113. }
  114. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  115. int texWidth, int texHeight, Color* color)
  116. {
  117. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  118. if (color || !element_->HasColorGradient())
  119. {
  120. unsigned uintColor = color ? color->ToUInt() : fixedColor_;
  121. // If alpha is 0, nothing will be rendered, so do not add the quad
  122. if (!(uintColor & 0xff000000))
  123. return;
  124. topLeftColor = uintColor;
  125. topRightColor = uintColor;
  126. bottomLeftColor = uintColor;
  127. bottomRightColor = uintColor;
  128. }
  129. else
  130. {
  131. topLeftColor = GetInterpolatedColor(x, y);
  132. topRightColor = GetInterpolatedColor(x + width, y);
  133. bottomLeftColor = GetInterpolatedColor(x, y + height);
  134. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  135. }
  136. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjustVec;
  137. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjustVec;
  138. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjustVec;
  139. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjustVec;
  140. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  141. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  142. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  143. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  144. unsigned begin = vertexData_->Size();
  145. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  146. float* dest = &(vertexData_->At(begin));
  147. vertexEnd_ = vertexData_->Size();
  148. *dest++ = v1.x_; *dest++ = v1.y_; *dest++ = 0.0f;
  149. *((unsigned*)dest) = topLeftColor; dest++;
  150. *dest++ = leftUV; *dest++ = topUV;
  151. *dest++ = v2.x_; *dest++ = v2.y_; *dest++ = 0.0f;
  152. *((unsigned*)dest) = topRightColor; dest++;
  153. *dest++ = rightUV; *dest++ = topUV;
  154. *dest++ = v3.x_; *dest++ = v3.y_; *dest++ = 0.0f;
  155. *((unsigned*)dest) = bottomLeftColor; dest++;
  156. *dest++ = leftUV; *dest++ = bottomUV;
  157. *dest++ = v2.x_; *dest++ = v2.y_; *dest++ = 0.0f;
  158. *((unsigned*)dest) = topRightColor; dest++;
  159. *dest++ = rightUV; *dest++ = topUV;
  160. *dest++ = v4.x_; *dest++ = v4.y_; *dest++ = 0.0f;
  161. *((unsigned*)dest) = bottomRightColor; dest++;
  162. *dest++ = rightUV; *dest++ = bottomUV;
  163. *dest++ = v3.x_; *dest++ = v3.y_; *dest++ = 0.0f;
  164. *((unsigned*)dest) = bottomLeftColor; dest++;
  165. *dest++ = leftUV; *dest++ = bottomUV;
  166. }
  167. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  168. {
  169. if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
  170. return; // No gradient and alpha is 0, so do not add the quad
  171. if (!tiled)
  172. {
  173. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  174. return;
  175. }
  176. int tileX = 0;
  177. int tileY = 0;
  178. int tileW = 0;
  179. int tileH = 0;
  180. while (tileY < height)
  181. {
  182. tileX = 0;
  183. tileH = Min(height - tileY, texHeight);
  184. while (tileX < width)
  185. {
  186. tileW = Min(width - tileX, texWidth);
  187. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  188. tileX += tileW;
  189. }
  190. tileY += tileH;
  191. }
  192. }
  193. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight,
  194. const Color& color)
  195. {
  196. Color derivedColor(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity());
  197. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight, &derivedColor);
  198. }
  199. bool UIBatch::Merge(const UIBatch& batch)
  200. {
  201. if (batch.blendMode_ != blendMode_ ||
  202. batch.scissor_ != scissor_ ||
  203. batch.texture_ != texture_ ||
  204. batch.vertexData_ != vertexData_ ||
  205. batch.vertexStart_ != vertexEnd_)
  206. return false;
  207. vertexEnd_ = batch.vertexEnd_;
  208. return true;
  209. }
  210. unsigned UIBatch::GetInterpolatedColor(int x, int y)
  211. {
  212. const IntVector2& size = element_->GetSize();
  213. if (size.x_ && size.y_)
  214. {
  215. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  216. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  217. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  218. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  219. Color color = topColor.Lerp(bottomColor, cLerpY);
  220. color.a_ *= element_->GetDerivedOpacity();
  221. return color.ToUInt();
  222. }
  223. else
  224. {
  225. Color color = element_->GetColor(C_TOPLEFT);
  226. color.a_ *= element_->GetDerivedOpacity();
  227. return color.ToUInt();
  228. }
  229. }
  230. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  231. {
  232. if (batch.vertexEnd_ == batch.vertexStart_)
  233. return;
  234. if (!batches.Empty() && batches.Back().Merge(batch))
  235. return;
  236. batches.Push(batch);
  237. }
  238. }