Text3DBatch.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // Copyright (c) 2008-2017 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 "../Texture.h"
  25. #include "Text3DText.h"
  26. #include "Text3DBatch.h"
  27. #include "../../DebugNew.h"
  28. namespace Atomic
  29. {
  30. Vector3 Text3DBatch::posAdjust(0.0f, 0.0f, 0.0f);
  31. Text3DBatch::Text3DBatch() :
  32. element_(0),
  33. blendMode_(BLEND_REPLACE),
  34. texture_(0),
  35. invTextureSize_(Vector2::ONE),
  36. vertexData_(0),
  37. vertexStart_(0),
  38. vertexEnd_(0),
  39. useGradient_(false)
  40. {
  41. SetDefaultColor();
  42. }
  43. Text3DBatch::Text3DBatch(Text3DText* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  44. element_(element),
  45. blendMode_(blendMode),
  46. scissor_(scissor),
  47. texture_(texture),
  48. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  49. vertexData_(vertexData),
  50. vertexStart_(vertexData->Size()),
  51. vertexEnd_(vertexData->Size())
  52. {
  53. SetDefaultColor();
  54. }
  55. void Text3DBatch::SetColor(const Color& color)
  56. {
  57. color_ = color.ToUInt();
  58. }
  59. void Text3DBatch::SetDefaultColor()
  60. {
  61. if (element_)
  62. {
  63. color_ = element_->GetColor().ToUInt();
  64. useGradient_ = element_->HasColorGradient();
  65. }
  66. else
  67. {
  68. color_ = 0xffffffff;
  69. useGradient_ = false;
  70. }
  71. }
  72. void Text3DBatch::AddQuad(float x, float y, float width, float height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  73. {
  74. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  75. if (!useGradient_)
  76. {
  77. // If alpha is 0, nothing will be rendered, so do not add the quad
  78. if (!(color_ & 0xff000000))
  79. return;
  80. topLeftColor = color_;
  81. topRightColor = color_;
  82. bottomLeftColor = color_;
  83. bottomRightColor = color_;
  84. }
  85. else
  86. {
  87. topLeftColor = GetInterpolatedColor(x, y);
  88. topRightColor = GetInterpolatedColor(x + width, y);
  89. bottomLeftColor = GetInterpolatedColor(x, y + height);
  90. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  91. }
  92. const IntVector2& screenPos = IntVector2::ZERO; //element_->GetScreenPosition();
  93. float left = x + screenPos.x_ - posAdjust.x_;
  94. float right = left + width;
  95. float top = y + screenPos.y_ - posAdjust.x_;
  96. float bottom = top + height;
  97. float leftUV = texOffsetX * invTextureSize_.x_;
  98. float topUV = texOffsetY * invTextureSize_.y_;
  99. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  100. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  101. unsigned begin = vertexData_->Size();
  102. vertexData_->Resize(begin + 6 * TEXT3D_VERTEX_SIZE);
  103. float* dest = &(vertexData_->At(begin));
  104. vertexEnd_ = vertexData_->Size();
  105. dest[0] = left;
  106. dest[1] = top;
  107. dest[2] = 0.0f;
  108. ((unsigned&)dest[3]) = topLeftColor;
  109. dest[4] = leftUV;
  110. dest[5] = topUV;
  111. dest[6] = right;
  112. dest[7] = top;
  113. dest[8] = 0.0f;
  114. ((unsigned&)dest[9]) = topRightColor;
  115. dest[10] = rightUV;
  116. dest[11] = topUV;
  117. dest[12] = left;
  118. dest[13] = bottom;
  119. dest[14] = 0.0f;
  120. ((unsigned&)dest[15]) = bottomLeftColor;
  121. dest[16] = leftUV;
  122. dest[17] = bottomUV;
  123. dest[18] = right;
  124. dest[19] = top;
  125. dest[20] = 0.0f;
  126. ((unsigned&)dest[21]) = topRightColor;
  127. dest[22] = rightUV;
  128. dest[23] = topUV;
  129. dest[24] = right;
  130. dest[25] = bottom;
  131. dest[26] = 0.0f;
  132. ((unsigned&)dest[27]) = bottomRightColor;
  133. dest[28] = rightUV;
  134. dest[29] = bottomUV;
  135. dest[30] = left;
  136. dest[31] = bottom;
  137. dest[32] = 0.0f;
  138. ((unsigned&)dest[33]) = bottomLeftColor;
  139. dest[34] = leftUV;
  140. dest[35] = bottomUV;
  141. }
  142. void Text3DBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  143. int texWidth, int texHeight)
  144. {
  145. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  146. if (!useGradient_)
  147. {
  148. // If alpha is 0, nothing will be rendered, so do not add the quad
  149. if (!(color_ & 0xff000000))
  150. return;
  151. topLeftColor = color_;
  152. topRightColor = color_;
  153. bottomLeftColor = color_;
  154. bottomRightColor = color_;
  155. }
  156. else
  157. {
  158. topLeftColor = GetInterpolatedColor(x, y);
  159. topRightColor = GetInterpolatedColor(x + width, y);
  160. bottomLeftColor = GetInterpolatedColor(x, y + height);
  161. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  162. }
  163. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjust;
  164. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjust;
  165. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjust;
  166. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjust;
  167. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  168. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  169. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  170. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  171. unsigned begin = vertexData_->Size();
  172. vertexData_->Resize(begin + 6 * TEXT3D_VERTEX_SIZE);
  173. float* dest = &(vertexData_->At(begin));
  174. vertexEnd_ = vertexData_->Size();
  175. dest[0] = v1.x_;
  176. dest[1] = v1.y_;
  177. dest[2] = 0.0f;
  178. ((unsigned&)dest[3]) = topLeftColor;
  179. dest[4] = leftUV;
  180. dest[5] = topUV;
  181. dest[6] = v2.x_;
  182. dest[7] = v2.y_;
  183. dest[8] = 0.0f;
  184. ((unsigned&)dest[9]) = topRightColor;
  185. dest[10] = rightUV;
  186. dest[11] = topUV;
  187. dest[12] = v3.x_;
  188. dest[13] = v3.y_;
  189. dest[14] = 0.0f;
  190. ((unsigned&)dest[15]) = bottomLeftColor;
  191. dest[16] = leftUV;
  192. dest[17] = bottomUV;
  193. dest[18] = v2.x_;
  194. dest[19] = v2.y_;
  195. dest[20] = 0.0f;
  196. ((unsigned&)dest[21]) = topRightColor;
  197. dest[22] = rightUV;
  198. dest[23] = topUV;
  199. dest[24] = v4.x_;
  200. dest[25] = v4.y_;
  201. dest[26] = 0.0f;
  202. ((unsigned&)dest[27]) = bottomRightColor;
  203. dest[28] = rightUV;
  204. dest[29] = bottomUV;
  205. dest[30] = v3.x_;
  206. dest[31] = v3.y_;
  207. dest[32] = 0.0f;
  208. ((unsigned&)dest[33]) = bottomLeftColor;
  209. dest[34] = leftUV;
  210. dest[35] = bottomUV;
  211. }
  212. void Text3DBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  213. {
  214. if (!(element_->HasColorGradient() || element_->GetColor().ToUInt() & 0xff000000))
  215. return; // No gradient and alpha is 0, so do not add the quad
  216. if (!tiled)
  217. {
  218. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  219. return;
  220. }
  221. int tileX = 0;
  222. int tileY = 0;
  223. int tileW = 0;
  224. int tileH = 0;
  225. while (tileY < height)
  226. {
  227. tileX = 0;
  228. tileH = Min(height - tileY, texHeight);
  229. while (tileX < width)
  230. {
  231. tileW = Min(width - tileX, texWidth);
  232. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  233. tileX += tileW;
  234. }
  235. tileY += tileH;
  236. }
  237. }
  238. void Text3DBatch::AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  239. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD)
  240. {
  241. Vector3 v1 = (transform * Vector3((float)a.x_, (float)a.y_, 0.0f)) - posAdjust;
  242. Vector3 v2 = (transform * Vector3((float)b.x_, (float)b.y_, 0.0f)) - posAdjust;
  243. Vector3 v3 = (transform * Vector3((float)c.x_, (float)c.y_, 0.0f)) - posAdjust;
  244. Vector3 v4 = (transform * Vector3((float)d.x_, (float)d.y_, 0.0f)) - posAdjust;
  245. Vector2 uv1((float)texA.x_ * invTextureSize_.x_, (float)texA.y_ * invTextureSize_.y_);
  246. Vector2 uv2((float)texB.x_ * invTextureSize_.x_, (float)texB.y_ * invTextureSize_.y_);
  247. Vector2 uv3((float)texC.x_ * invTextureSize_.x_, (float)texC.y_ * invTextureSize_.y_);
  248. Vector2 uv4((float)texD.x_ * invTextureSize_.x_, (float)texD.y_ * invTextureSize_.y_);
  249. unsigned begin = vertexData_->Size();
  250. vertexData_->Resize(begin + 6 * TEXT3D_VERTEX_SIZE);
  251. float* dest = &(vertexData_->At(begin));
  252. vertexEnd_ = vertexData_->Size();
  253. dest[0] = v1.x_;
  254. dest[1] = v1.y_;
  255. dest[2] = 0.0f;
  256. ((unsigned&)dest[3]) = color_;
  257. dest[4] = uv1.x_;
  258. dest[5] = uv1.y_;
  259. dest[6] = v2.x_;
  260. dest[7] = v2.y_;
  261. dest[8] = 0.0f;
  262. ((unsigned&)dest[9]) = color_;
  263. dest[10] = uv2.x_;
  264. dest[11] = uv2.y_;
  265. dest[12] = v3.x_;
  266. dest[13] = v3.y_;
  267. dest[14] = 0.0f;
  268. ((unsigned&)dest[15]) = color_;
  269. dest[16] = uv3.x_;
  270. dest[17] = uv3.y_;
  271. dest[18] = v1.x_;
  272. dest[19] = v1.y_;
  273. dest[20] = 0.0f;
  274. ((unsigned&)dest[21]) = color_;
  275. dest[22] = uv1.x_;
  276. dest[23] = uv1.y_;
  277. dest[24] = v3.x_;
  278. dest[25] = v3.y_;
  279. dest[26] = 0.0f;
  280. ((unsigned&)dest[27]) = color_;
  281. dest[28] = uv3.x_;
  282. dest[29] = uv3.y_;
  283. dest[30] = v4.x_;
  284. dest[31] = v4.y_;
  285. dest[32] = 0.0f;
  286. ((unsigned&)dest[33]) = color_;
  287. dest[34] = uv4.x_;
  288. dest[35] = uv4.y_;
  289. }
  290. void Text3DBatch::AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  291. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD, const Color& colA,
  292. const Color& colB, const Color& colC, const Color& colD)
  293. {
  294. Vector3 v1 = (transform * Vector3((float)a.x_, (float)a.y_, 0.0f)) - posAdjust;
  295. Vector3 v2 = (transform * Vector3((float)b.x_, (float)b.y_, 0.0f)) - posAdjust;
  296. Vector3 v3 = (transform * Vector3((float)c.x_, (float)c.y_, 0.0f)) - posAdjust;
  297. Vector3 v4 = (transform * Vector3((float)d.x_, (float)d.y_, 0.0f)) - posAdjust;
  298. Vector2 uv1((float)texA.x_ * invTextureSize_.x_, (float)texA.y_ * invTextureSize_.y_);
  299. Vector2 uv2((float)texB.x_ * invTextureSize_.x_, (float)texB.y_ * invTextureSize_.y_);
  300. Vector2 uv3((float)texC.x_ * invTextureSize_.x_, (float)texC.y_ * invTextureSize_.y_);
  301. Vector2 uv4((float)texD.x_ * invTextureSize_.x_, (float)texD.y_ * invTextureSize_.y_);
  302. unsigned c1 = colA.ToUInt();
  303. unsigned c2 = colB.ToUInt();
  304. unsigned c3 = colC.ToUInt();
  305. unsigned c4 = colD.ToUInt();
  306. unsigned begin = vertexData_->Size();
  307. vertexData_->Resize(begin + 6 * TEXT3D_VERTEX_SIZE);
  308. float* dest = &(vertexData_->At(begin));
  309. vertexEnd_ = vertexData_->Size();
  310. dest[0] = v1.x_;
  311. dest[1] = v1.y_;
  312. dest[2] = 0.0f;
  313. ((unsigned&)dest[3]) = c1;
  314. dest[4] = uv1.x_;
  315. dest[5] = uv1.y_;
  316. dest[6] = v2.x_;
  317. dest[7] = v2.y_;
  318. dest[8] = 0.0f;
  319. ((unsigned&)dest[9]) = c2;
  320. dest[10] = uv2.x_;
  321. dest[11] = uv2.y_;
  322. dest[12] = v3.x_;
  323. dest[13] = v3.y_;
  324. dest[14] = 0.0f;
  325. ((unsigned&)dest[15]) = c3;
  326. dest[16] = uv3.x_;
  327. dest[17] = uv3.y_;
  328. dest[18] = v1.x_;
  329. dest[19] = v1.y_;
  330. dest[20] = 0.0f;
  331. ((unsigned&)dest[21]) = c1;
  332. dest[22] = uv1.x_;
  333. dest[23] = uv1.y_;
  334. dest[24] = v3.x_;
  335. dest[25] = v3.y_;
  336. dest[26] = 0.0f;
  337. ((unsigned&)dest[27]) = c3;
  338. dest[28] = uv3.x_;
  339. dest[29] = uv3.y_;
  340. dest[30] = v4.x_;
  341. dest[31] = v4.y_;
  342. dest[32] = 0.0f;
  343. ((unsigned&)dest[33]) = c4;
  344. dest[34] = uv4.x_;
  345. dest[35] = uv4.y_;
  346. }
  347. bool Text3DBatch::Merge(const Text3DBatch& batch)
  348. {
  349. if (batch.blendMode_ != blendMode_ ||
  350. batch.scissor_ != scissor_ ||
  351. batch.texture_ != texture_ ||
  352. batch.vertexData_ != vertexData_ ||
  353. batch.vertexStart_ != vertexEnd_)
  354. return false;
  355. vertexEnd_ = batch.vertexEnd_;
  356. return true;
  357. }
  358. unsigned Text3DBatch::GetInterpolatedColor(float x, float y)
  359. {
  360. const IntVector2& size = element_->GetSize();
  361. if (size.x_ && size.y_)
  362. {
  363. float cLerpX = Clamp(x / (float)size.x_, 0.0f, 1.0f);
  364. float cLerpY = Clamp(y / (float)size.y_, 0.0f, 1.0f);
  365. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  366. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  367. Color color = topColor.Lerp(bottomColor, cLerpY);
  368. color.a_ *= element_->GetOpacity();
  369. return color.ToUInt();
  370. }
  371. else
  372. {
  373. Color color = element_->GetColor(C_TOPLEFT);
  374. color.a_ *= element_->GetOpacity();
  375. return color.ToUInt();
  376. }
  377. }
  378. void Text3DBatch::AddOrMerge(const Text3DBatch& batch, PODVector<Text3DBatch>& batches)
  379. {
  380. if (batch.vertexEnd_ == batch.vertexStart_)
  381. return;
  382. if (!batches.Empty() && batches.Back().Merge(batch))
  383. return;
  384. batches.Push(batch);
  385. }
  386. }