Text3DBatch.cpp 14 KB

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