UIBatch.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "PixelShader.h"
  25. #include "Renderer.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. mQuads = quads;
  35. mQuadStart = mQuads->size();
  36. mQuadCount = 0;
  37. }
  38. }
  39. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY)
  40. {
  41. if (!mQuads)
  42. return;
  43. static UIQuad quad;
  44. const IntVector2& screenPos = element.getScreenPosition();
  45. quad.mLeft = x + screenPos.mX;
  46. quad.mTop = y + screenPos.mY;
  47. quad.mRight = quad.mLeft + width;
  48. quad.mBottom = quad.mTop + height;
  49. quad.mLeftUV = texOffsetX;
  50. quad.mTopUV = texOffsetY;
  51. quad.mRightUV = texOffsetX + width;
  52. quad.mBottomUV = texOffsetY + height;
  53. if (element.hasColorGradient())
  54. {
  55. quad.mTopLeftColor = getInterpolatedColor(element, x, y);
  56. quad.mTopRightColor = getInterpolatedColor(element, x + width, y);
  57. quad.mBottomLeftColor = getInterpolatedColor(element, x, y + height);
  58. quad.mBottomRightColor = getInterpolatedColor(element, x + width, y + height);
  59. }
  60. else
  61. {
  62. Color color = element.getColor(C_TOPLEFT);
  63. color.mA *= element.getDerivedOpacity();
  64. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  65. if (color.mA <= 0.0f)
  66. return;
  67. unsigned uintColor = getD3DColor(color);
  68. quad.mTopLeftColor = uintColor;
  69. quad.mTopRightColor = uintColor;
  70. quad.mBottomLeftColor = uintColor;
  71. quad.mBottomRightColor = uintColor;
  72. }
  73. mQuads->push_back(quad);
  74. mQuadCount++;
  75. }
  76. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  77. int texHeight)
  78. {
  79. if (!mQuads)
  80. return;
  81. static UIQuad quad;
  82. const IntVector2& screenPos = element.getScreenPosition();
  83. quad.mLeft = x + screenPos.mX;
  84. quad.mTop = y + screenPos.mY;
  85. quad.mRight = quad.mLeft + width;
  86. quad.mBottom = quad.mTop + height;
  87. quad.mLeftUV = texOffsetX;
  88. quad.mTopUV = texOffsetY;
  89. quad.mRightUV = texOffsetX + texWidth;
  90. quad.mBottomUV = texOffsetY + texHeight;
  91. if (element.hasColorGradient())
  92. {
  93. quad.mTopLeftColor = getInterpolatedColor(element, x, y);
  94. quad.mTopRightColor = getInterpolatedColor(element, x + width, y);
  95. quad.mBottomLeftColor = getInterpolatedColor(element, x, y + height);
  96. quad.mBottomRightColor = getInterpolatedColor(element, x + width, y + height);
  97. }
  98. else
  99. {
  100. Color color = element.getColor(C_TOPLEFT);
  101. color.mA *= element.getDerivedOpacity();
  102. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  103. if (color.mA <= 0.0f)
  104. return;
  105. unsigned uintColor = getD3DColor(color);
  106. quad.mTopLeftColor = uintColor;
  107. quad.mTopRightColor = uintColor;
  108. quad.mBottomLeftColor = uintColor;
  109. quad.mBottomRightColor = uintColor;
  110. }
  111. mQuads->push_back(quad);
  112. mQuadCount++;
  113. }
  114. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  115. int texHeight, const Color& color)
  116. {
  117. if (!mQuads)
  118. return;
  119. static UIQuad quad;
  120. const IntVector2& screenPos = element.getScreenPosition();
  121. quad.mLeft = x + screenPos.mX;
  122. quad.mTop = y + screenPos.mY;
  123. quad.mRight = quad.mLeft + width;
  124. quad.mBottom = quad.mTop + height;
  125. quad.mLeftUV = texOffsetX;
  126. quad.mTopUV = texOffsetY;
  127. quad.mRightUV = texOffsetX + texWidth;
  128. quad.mBottomUV = texOffsetY + texHeight;
  129. Color derivedColor(color.mR, color.mG, color.mB, color.mA * element.getDerivedOpacity());
  130. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  131. if (derivedColor.mA <= 0.0f)
  132. return;
  133. unsigned uintColor = getD3DColor(derivedColor);
  134. quad.mTopLeftColor = uintColor;
  135. quad.mTopRightColor = uintColor;
  136. quad.mBottomLeftColor = uintColor;
  137. quad.mBottomRightColor = uintColor;
  138. mQuads->push_back(quad);
  139. mQuadCount++;
  140. }
  141. bool UIBatch::merge(const UIBatch& batch)
  142. {
  143. if ((batch.mBlendMode != mBlendMode) ||
  144. (batch.mScissor != mScissor) ||
  145. (batch.mTexture != mTexture) ||
  146. (batch.mQuads != mQuads) ||
  147. (batch.mQuadStart != mQuadStart + mQuadCount))
  148. return false;
  149. mQuadCount += batch.mQuadCount;
  150. return true;
  151. }
  152. void UIBatch::draw(Renderer* renderer, VertexShader* vs, PixelShader* ps) const
  153. {
  154. if ((!mQuads) || (!mQuadCount))
  155. return;
  156. // Use alpha test if not alpha blending
  157. if ((mBlendMode != BLEND_ALPHA) && (mBlendMode != BLEND_ADDALPHA) && (mBlendMode != BLEND_PREMULALPHA))
  158. renderer->setAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  159. else
  160. renderer->setAlphaTest(false);
  161. renderer->setBlendMode(mBlendMode);
  162. renderer->setScissorTest(true, mScissor);
  163. renderer->setTexture(0, mTexture);
  164. renderer->setVertexShader(vs);
  165. renderer->setPixelShader(ps);
  166. static const Vector2 posAdjust(0.5f, 0.5f);
  167. Vector2 invScreenSize(1.0f / (float)renderer->getWidth(), 1.0f / (float)renderer->getHeight());
  168. const std::vector<UIQuad>& quads = *mQuads;
  169. if (mTexture)
  170. {
  171. Vector2 invTextureSize(1.0f / (float)mTexture->getWidth(), 1.0f / (float)mTexture->getHeight());
  172. renderer->beginImmediate(TRIANGLE_LIST, mQuadCount * 6, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  173. float* dest = (float*)renderer->getImmediateDataPtr();
  174. for (unsigned i = mQuadStart; i < mQuadStart + mQuadCount; ++i)
  175. {
  176. const UIQuad& quad = quads[i];
  177. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  178. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  179. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  180. topLeftUV = Vector2((float)quad.mLeftUV, (float)quad.mTopUV) * invTextureSize;
  181. bottomRightUV = Vector2((float)quad.mRightUV, (float)quad.mBottomUV) * invTextureSize;
  182. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  183. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  184. *dest++ = topLeftUV.mX; *dest++ = topLeftUV.mY;
  185. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  186. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  187. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  188. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  189. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  190. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  191. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  192. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  193. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  194. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  195. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  196. *dest++ = bottomRightUV.mX; *dest++ = bottomRightUV.mY;
  197. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  198. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  199. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  200. }
  201. renderer->endImmediate();
  202. }
  203. else
  204. {
  205. renderer->beginImmediate(TRIANGLE_LIST, mQuadCount * 6, MASK_POSITION | MASK_COLOR);
  206. float* dest = (float*)renderer->getImmediateDataPtr();
  207. for (unsigned i = mQuadStart; i < mQuadStart + mQuadCount; ++i)
  208. {
  209. const UIQuad& quad = quads[i];
  210. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  211. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  212. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  213. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  214. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  215. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  216. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  217. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  218. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  219. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  220. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  221. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  222. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  223. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  224. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  225. }
  226. renderer->endImmediate();
  227. }
  228. }
  229. void UIBatch::addOrMerge(const UIBatch& batch, std::vector<UIBatch>& batches)
  230. {
  231. if (!batch.mQuadCount)
  232. return;
  233. if (!batches.size())
  234. batches.push_back(batch);
  235. else
  236. {
  237. if (batches.back().merge(batch))
  238. return;
  239. else
  240. batches.push_back(batch);
  241. }
  242. }
  243. unsigned UIBatch::getInterpolatedColor(UIElement& element, int x, int y)
  244. {
  245. const IntVector2& size = element.getSize();
  246. if ((size.mX) && (size.mY))
  247. {
  248. float cLerpX = clamp((float)x / (float)size.mX, 0.0f, 1.0f);
  249. float cLerpY = clamp((float)y / (float)size.mY, 0.0f, 1.0f);
  250. Color topColor = element.getColor(C_TOPLEFT).lerp(element.getColor(C_TOPRIGHT), cLerpX);
  251. Color bottomColor = element.getColor(C_BOTTOMLEFT).lerp(element.getColor(C_BOTTOMRIGHT), cLerpX);
  252. Color color = topColor.lerp(bottomColor, cLerpY);
  253. color.mA *= element.getDerivedOpacity();
  254. return getD3DColor(color);
  255. }
  256. else
  257. {
  258. Color color = element.getColor(C_TOPLEFT);
  259. color.mA *= element.getDerivedOpacity();
  260. return getD3DColor(color);
  261. }
  262. }