UIBatch.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. const Color& color = element.getColor(C_TOPLEFT);
  63. unsigned finalColor = getD3DColor(Color(color.mR, color.mG, color.mB, element.getDerivedOpacity()));
  64. quad.mTopLeftColor = finalColor;
  65. quad.mTopRightColor = finalColor;
  66. quad.mBottomLeftColor = finalColor;
  67. quad.mBottomRightColor = finalColor;
  68. }
  69. mQuads->push_back(quad);
  70. mQuadCount++;
  71. }
  72. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  73. {
  74. if (!mQuads)
  75. return;
  76. static UIQuad quad;
  77. const IntVector2& screenPos = element.getScreenPosition();
  78. quad.mLeft = x + screenPos.mX;
  79. quad.mTop = y + screenPos.mY;
  80. quad.mRight = quad.mLeft + width;
  81. quad.mBottom = quad.mTop + height;
  82. quad.mLeftUV = texOffsetX;
  83. quad.mTopUV = texOffsetY;
  84. quad.mRightUV = texOffsetX + texWidth;
  85. quad.mBottomUV = texOffsetY + texHeight;
  86. if (element.hasColorGradient())
  87. {
  88. quad.mTopLeftColor = getInterpolatedColor(element, x, y);
  89. quad.mTopRightColor = getInterpolatedColor(element, x + width, y);
  90. quad.mBottomLeftColor = getInterpolatedColor(element, x, y + height);
  91. quad.mBottomRightColor = getInterpolatedColor(element, x + width, y + height);
  92. }
  93. else
  94. {
  95. const Color& color = element.getColor(C_TOPLEFT);
  96. unsigned finalColor = getD3DColor(Color(color.mR, color.mG, color.mB, element.getDerivedOpacity()));
  97. quad.mTopLeftColor = finalColor;
  98. quad.mTopRightColor = finalColor;
  99. quad.mBottomLeftColor = finalColor;
  100. quad.mBottomRightColor = finalColor;
  101. }
  102. mQuads->push_back(quad);
  103. mQuadCount++;
  104. }
  105. bool UIBatch::merge(const UIBatch& batch)
  106. {
  107. if ((batch.mBlendMode != mBlendMode) ||
  108. (batch.mScissor != mScissor) ||
  109. (batch.mTexture != mTexture) ||
  110. (batch.mQuads != mQuads) ||
  111. (batch.mQuadStart != mQuadStart + mQuadCount))
  112. return false;
  113. mQuadCount += batch.mQuadCount;
  114. return true;
  115. }
  116. void UIBatch::draw(Renderer* renderer, VertexShader* vs, PixelShader* ps) const
  117. {
  118. if ((!mQuads) || (!mQuadCount))
  119. return;
  120. // Use alpha test if not alpha blending
  121. if ((mBlendMode != BLEND_ALPHA) && (mBlendMode != BLEND_ADDALPHA) && (mBlendMode != BLEND_PREMULALPHA))
  122. renderer->setAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  123. else
  124. renderer->setAlphaTest(false);
  125. renderer->setBlendMode(mBlendMode);
  126. renderer->setScissorTest(true, mScissor);
  127. renderer->setTexture(0, mTexture);
  128. renderer->setVertexShader(vs);
  129. renderer->setPixelShader(ps);
  130. static const Vector2 posAdjust(0.5f, 0.5f);
  131. Vector2 invScreenSize(1.0f / (float)renderer->getWidth(), 1.0f / (float)renderer->getHeight());
  132. const std::vector<UIQuad>& quads = *mQuads;
  133. if (mTexture)
  134. {
  135. Vector2 invTextureSize(1.0f / (float)mTexture->getWidth(), 1.0f / (float)mTexture->getHeight());
  136. renderer->beginImmediate(TRIANGLE_LIST, mQuadCount * 6, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  137. float* dest = (float*)renderer->getImmediateDataPtr();
  138. for (unsigned i = mQuadStart; i < mQuadStart + mQuadCount; ++i)
  139. {
  140. const UIQuad& quad = quads[i];
  141. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  142. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  143. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  144. topLeftUV = Vector2((float)quad.mLeftUV, (float)quad.mTopUV) * invTextureSize;
  145. bottomRightUV = Vector2((float)quad.mRightUV, (float)quad.mBottomUV) * invTextureSize;
  146. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  147. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  148. *dest++ = topLeftUV.mX; *dest++ = topLeftUV.mY;
  149. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  150. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  151. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  152. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  153. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  154. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  155. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  156. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  157. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  158. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  159. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  160. *dest++ = bottomRightUV.mX; *dest++ = bottomRightUV.mY;
  161. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  162. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  163. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  164. }
  165. renderer->endImmediate();
  166. }
  167. else
  168. {
  169. renderer->beginImmediate(TRIANGLE_LIST, quads.size() * 6, MASK_POSITION | MASK_COLOR);
  170. float* dest = (float*)renderer->getImmediateDataPtr();
  171. for (unsigned i = 0; i < quads.size(); ++i)
  172. {
  173. const UIQuad& quad = quads[i];
  174. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  175. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  176. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  177. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  178. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  179. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  180. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  181. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  182. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  183. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  184. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  185. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  186. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  187. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  188. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  189. }
  190. renderer->endImmediate();
  191. }
  192. }
  193. void UIBatch::addOrMerge(const UIBatch& batch, std::vector<UIBatch>& batches)
  194. {
  195. if (!batches.size())
  196. batches.push_back(batch);
  197. else
  198. {
  199. if (batches.back().merge(batch))
  200. return;
  201. else
  202. batches.push_back(batch);
  203. }
  204. }
  205. unsigned UIBatch::getInterpolatedColor(UIElement& element, int x, int y)
  206. {
  207. const IntVector2& size = element.getSize();
  208. if ((size.mX) && (size.mY))
  209. {
  210. float cLerpX = clamp((float)x / (float)size.mX, 0.0f, 1.0f);
  211. float cLerpY = clamp((float)y / (float)size.mY, 0.0f, 1.0f);
  212. Color topColor = element.getColor(C_TOPLEFT).lerp(element.getColor(C_TOPRIGHT), cLerpX);
  213. Color bottomColor = element.getColor(C_BOTTOMLEFT).lerp(element.getColor(C_BOTTOMRIGHT), cLerpX);
  214. Color color = topColor.lerp(bottomColor, cLerpY);
  215. return getD3DColor(Color(color.mR, color.mG, color.mB, element.getDerivedOpacity()));
  216. }
  217. else
  218. {
  219. const Color& color = element.getColor(C_TOPLEFT);
  220. return getD3DColor(Color(color.mR, color.mG, color.mB, element.getDerivedOpacity()));
  221. }
  222. }