Polyline.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * Copyright (c) 2006-2020 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Polyline.h"
  22. #include "graphics/Graphics.h"
  23. // C++
  24. #include <algorithm>
  25. // treat adjacent segments with angles between their directions <5 degree as straight
  26. static const float LINES_PARALLEL_EPS = 0.05f;
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. void Polyline::render(const Vector2 *coords, size_t count, size_t size_hint, float halfwidth, float pixel_size, bool draw_overdraw)
  32. {
  33. static std::vector<Vector2> anchors;
  34. anchors.clear();
  35. anchors.reserve(size_hint);
  36. static std::vector<Vector2> normals;
  37. normals.clear();
  38. normals.reserve(size_hint);
  39. // prepare vertex arrays
  40. if (draw_overdraw)
  41. halfwidth -= pixel_size * 0.3f;
  42. // compute sleeve
  43. bool is_looping = (coords[0] == coords[count - 1]);
  44. Vector2 s;
  45. if (!is_looping) // virtual starting point at second point mirrored on first point
  46. s = coords[1] - coords[0];
  47. else // virtual starting point at last vertex
  48. s = coords[0] - coords[count - 2];
  49. float len_s = s.getLength();
  50. Vector2 ns = s.getNormal(halfwidth / len_s);
  51. Vector2 q, r(coords[0]);
  52. for (size_t i = 0; i + 1 < count; i++)
  53. {
  54. q = r;
  55. r = coords[i + 1];
  56. renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth);
  57. }
  58. q = r;
  59. r = is_looping ? coords[1] : r + s;
  60. renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth);
  61. vertex_count = normals.size();
  62. size_t extra_vertices = 0;
  63. if (draw_overdraw)
  64. {
  65. calc_overdraw_vertex_count(is_looping);
  66. // When drawing overdraw lines using triangle strips, we want to add an
  67. // extra degenerate triangle in between the core line and the overdraw
  68. // line in order to break up the strip into two. This will let us draw
  69. // everything in one draw call.
  70. if (triangle_mode == vertex::TriangleIndexMode::STRIP)
  71. extra_vertices = 2;
  72. }
  73. // Use a single linear array for both the regular and overdraw vertices.
  74. vertices = new Vector2[vertex_count + extra_vertices + overdraw_vertex_count];
  75. for (size_t i = 0; i < vertex_count; ++i)
  76. vertices[i] = anchors[i] + normals[i];
  77. if (draw_overdraw)
  78. {
  79. overdraw = vertices + vertex_count + extra_vertices;
  80. overdraw_vertex_start = vertex_count + extra_vertices;
  81. render_overdraw(normals, pixel_size, is_looping);
  82. }
  83. // Add the degenerate triangle strip.
  84. if (extra_vertices)
  85. {
  86. vertices[vertex_count + 0] = vertices[vertex_count - 1];
  87. vertices[vertex_count + 1] = vertices[overdraw_vertex_start];
  88. }
  89. }
  90. void NoneJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
  91. Vector2 &s, float &len_s, Vector2 &ns,
  92. const Vector2 &q, const Vector2 &r, float hw)
  93. {
  94. // ns1------ns2
  95. // | |
  96. // q ------ r
  97. // | |
  98. // (-ns1)----(-ns2)
  99. anchors.push_back(q);
  100. anchors.push_back(q);
  101. normals.push_back(ns);
  102. normals.push_back(-ns);
  103. s = (r - q);
  104. len_s = s.getLength();
  105. ns = s.getNormal(hw / len_s);
  106. anchors.push_back(q);
  107. anchors.push_back(q);
  108. normals.push_back(ns);
  109. normals.push_back(-ns);
  110. }
  111. /** Calculate line boundary points.
  112. *
  113. * Sketch:
  114. *
  115. * u1
  116. * -------------+---...___
  117. * | ```'''-- ---
  118. * p- - - - - - q- - . _ _ | w/2
  119. * | ` ' ' r +
  120. * -------------+---...___ | w/2
  121. * u2 ```'''-- ---
  122. *
  123. * u1 and u2 depend on four things:
  124. * - the half line width w/2
  125. * - the previous line vertex p
  126. * - the current line vertex q
  127. * - the next line vertex r
  128. *
  129. * u1/u2 are the intersection points of the parallel lines to p-q and q-r,
  130. * i.e. the point where
  131. *
  132. * (q + w/2 * ns) + lambda * (q - p) = (q + w/2 * nt) + mu * (r - q) (u1)
  133. * (q - w/2 * ns) + lambda * (q - p) = (q - w/2 * nt) + mu * (r - q) (u2)
  134. *
  135. * with nt,nt being the normals on the segments s = p-q and t = q-r,
  136. *
  137. * ns = perp(s) / |s|
  138. * nt = perp(t) / |t|.
  139. *
  140. * Using the linear equation system (similar for u2)
  141. *
  142. * q + w/2 * ns + lambda * s - (q + w/2 * nt + mu * t) = 0 (u1)
  143. * <=> q-q + lambda * s - mu * t = (nt - ns) * w/2
  144. * <=> lambda * s - mu * t = (nt - ns) * w/2
  145. *
  146. * the intersection points can be efficiently calculated using Cramer's rule.
  147. */
  148. void MiterJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
  149. Vector2 &s, float &len_s, Vector2 &ns,
  150. const Vector2 &q, const Vector2 &r, float hw)
  151. {
  152. Vector2 t = (r - q);
  153. float len_t = t.getLength();
  154. Vector2 nt = t.getNormal(hw / len_t);
  155. anchors.push_back(q);
  156. anchors.push_back(q);
  157. float det = Vector2::cross(s, t);
  158. if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && Vector2::dot(s, t) > 0)
  159. {
  160. // lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
  161. normals.push_back(ns);
  162. normals.push_back(-ns);
  163. }
  164. else
  165. {
  166. // cramers rule
  167. float lambda = Vector2::cross((nt - ns), t) / det;
  168. Vector2 d = ns + s * lambda;
  169. normals.push_back(d);
  170. normals.push_back(-d);
  171. }
  172. s = t;
  173. ns = nt;
  174. len_s = len_t;
  175. }
  176. /** Calculate line boundary points.
  177. *
  178. * Sketch:
  179. *
  180. * uh1___uh2
  181. * .' '.
  182. * .' q '.
  183. * .' ' ' '.
  184. *.' ' .'. ' '.
  185. * ' .' ul'. '
  186. * p .' '. r
  187. *
  188. *
  189. * ul can be found as above, uh1 and uh2 are much simpler:
  190. *
  191. * uh1 = q + ns * w/2, uh2 = q + nt * w/2
  192. */
  193. void BevelJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
  194. Vector2 &s, float &len_s, Vector2 &ns,
  195. const Vector2 &q, const Vector2 &r, float hw)
  196. {
  197. Vector2 t = (r - q);
  198. float len_t = t.getLength();
  199. float det = Vector2::cross(s, t);
  200. if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && Vector2::dot(s, t) > 0)
  201. {
  202. // lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
  203. Vector2 n = t.getNormal(hw / len_t);
  204. anchors.push_back(q);
  205. anchors.push_back(q);
  206. normals.push_back(n);
  207. normals.push_back(-n);
  208. s = t;
  209. len_s = len_t;
  210. return; // early out
  211. }
  212. // cramers rule
  213. Vector2 nt = t.getNormal(hw / len_t);
  214. float lambda = Vector2::cross((nt - ns), t) / det;
  215. Vector2 d = ns + s * lambda;
  216. anchors.push_back(q);
  217. anchors.push_back(q);
  218. anchors.push_back(q);
  219. anchors.push_back(q);
  220. if (det > 0) // 'left' turn -> intersection on the top
  221. {
  222. normals.push_back(d);
  223. normals.push_back(-ns);
  224. normals.push_back(d);
  225. normals.push_back(-nt);
  226. }
  227. else
  228. {
  229. normals.push_back(ns);
  230. normals.push_back(-d);
  231. normals.push_back(nt);
  232. normals.push_back(-d);
  233. }
  234. s = t;
  235. len_s = len_t;
  236. ns = nt;
  237. }
  238. void Polyline::calc_overdraw_vertex_count(bool is_looping)
  239. {
  240. overdraw_vertex_count = 2 * vertex_count + (is_looping ? 0 : 2);
  241. }
  242. void Polyline::render_overdraw(const std::vector<Vector2> &normals, float pixel_size, bool is_looping)
  243. {
  244. // upper segment
  245. for (size_t i = 0; i + 1 < vertex_count; i += 2)
  246. {
  247. overdraw[i] = vertices[i];
  248. overdraw[i+1] = vertices[i] + normals[i] * (pixel_size / normals[i].getLength());
  249. }
  250. // lower segment
  251. for (size_t i = 0; i + 1 < vertex_count; i += 2)
  252. {
  253. size_t k = vertex_count - i - 1;
  254. overdraw[vertex_count + i] = vertices[k];
  255. overdraw[vertex_count + i+1] = vertices[k] + normals[k] * (pixel_size / normals[k].getLength());
  256. }
  257. // if not looping, the outer overdraw vertices need to be displaced
  258. // to cover the line endings, i.e.:
  259. // +- - - - //- - + +- - - - - //- - - +
  260. // +-------//-----+ : +-------//-----+ :
  261. // | core // line | --> : | core // line | :
  262. // +-----//-------+ : +-----//-------+ :
  263. // +- - //- - - - + +- - - //- - - - - +
  264. if (!is_looping)
  265. {
  266. // left edge
  267. Vector2 spacer = (overdraw[1] - overdraw[3]);
  268. spacer.normalize(pixel_size);
  269. overdraw[1] += spacer;
  270. overdraw[overdraw_vertex_count - 3] += spacer;
  271. // right edge
  272. spacer = (overdraw[vertex_count-1] - overdraw[vertex_count-3]);
  273. spacer.normalize(pixel_size);
  274. overdraw[vertex_count-1] += spacer;
  275. overdraw[vertex_count+1] += spacer;
  276. // we need to draw two more triangles to close the
  277. // overdraw at the line start.
  278. overdraw[overdraw_vertex_count-2] = overdraw[0];
  279. overdraw[overdraw_vertex_count-1] = overdraw[1];
  280. }
  281. }
  282. void NoneJoinPolyline::calc_overdraw_vertex_count(bool /*is_looping*/)
  283. {
  284. overdraw_vertex_count = 4 * (vertex_count-2); // less than ideal
  285. }
  286. void NoneJoinPolyline::render_overdraw(const std::vector<Vector2> &/*normals*/, float pixel_size, bool /*is_looping*/)
  287. {
  288. for (size_t i = 2; i + 3 < vertex_count; i += 4)
  289. {
  290. // v0-v2
  291. // | / | <- main quad line
  292. // v1-v3
  293. Vector2 s = vertices[i+0] - vertices[i+2];
  294. Vector2 t = vertices[i+0] - vertices[i+1];
  295. s.normalize(pixel_size);
  296. t.normalize(pixel_size);
  297. const size_t k = 4 * (i - 2);
  298. overdraw[k+0] = vertices[i+0];
  299. overdraw[k+1] = vertices[i+1];
  300. overdraw[k+2] = vertices[i+0] + s + t;
  301. overdraw[k+3] = vertices[i+1] + s - t;
  302. overdraw[k+4] = vertices[i+1];
  303. overdraw[k+5] = vertices[i+3];
  304. overdraw[k+6] = vertices[i+1] + s - t;
  305. overdraw[k+7] = vertices[i+3] - s - t;
  306. overdraw[k+ 8] = vertices[i+3];
  307. overdraw[k+ 9] = vertices[i+2];
  308. overdraw[k+10] = vertices[i+3] - s - t;
  309. overdraw[k+11] = vertices[i+2] - s + t;
  310. overdraw[k+12] = vertices[i+2];
  311. overdraw[k+13] = vertices[i+0];
  312. overdraw[k+14] = vertices[i+2] - s + t;
  313. overdraw[k+15] = vertices[i+0] + s + t;
  314. }
  315. }
  316. Polyline::~Polyline()
  317. {
  318. if (vertices)
  319. delete[] vertices;
  320. }
  321. void Polyline::draw(love::graphics::Graphics *gfx)
  322. {
  323. const Matrix4 &t = gfx->getTransform();
  324. bool is2D = t.isAffine2DTransform();
  325. Color32 curcolor = toColor32(gfx->getColor());
  326. int overdraw_start = (int) overdraw_vertex_start;
  327. int overdraw_count = (int) overdraw_vertex_count;
  328. int total_vertex_count = (int) vertex_count;
  329. if (overdraw)
  330. total_vertex_count = overdraw_start + overdraw_count;
  331. // love's automatic batching can only deal with < 65k vertices per draw.
  332. // uint16_max - 3 is evenly divisible by 6 (needed for quads mode).
  333. int maxvertices = LOVE_UINT16_MAX - 3;
  334. int advance = maxvertices;
  335. if (triangle_mode == vertex::TriangleIndexMode::STRIP)
  336. advance -= 2;
  337. for (int vertex_start = 0; vertex_start < total_vertex_count; vertex_start += advance)
  338. {
  339. const Vector2 *verts = vertices + vertex_start;
  340. Graphics::BatchedDrawCommand cmd;
  341. cmd.formats[0] = vertex::getSinglePositionFormat(is2D);
  342. cmd.formats[1] = vertex::CommonFormat::RGBAub;
  343. cmd.indexMode = triangle_mode;
  344. cmd.vertexCount = std::min(maxvertices, total_vertex_count - vertex_start);
  345. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  346. if (is2D)
  347. t.transformXY((Vector2 *) data.stream[0], verts, cmd.vertexCount);
  348. else
  349. t.transformXY0((Vector3 *) data.stream[0], verts, cmd.vertexCount);
  350. Color32 *colordata = (Color32 *) data.stream[1];
  351. int draw_rough_count = std::min(cmd.vertexCount, (int) vertex_count - vertex_start);
  352. // Constant vertex color up to the overdraw vertices.
  353. for (int i = 0; i < draw_rough_count; i++)
  354. colordata[i] = curcolor;
  355. if (overdraw)
  356. {
  357. int draw_remaining_count = cmd.vertexCount - draw_rough_count;
  358. int draw_overdraw_begin = overdraw_start - vertex_start;
  359. int draw_overdraw_end = draw_overdraw_begin + overdraw_count;
  360. draw_overdraw_begin = std::max(0, draw_overdraw_begin);
  361. int draw_overdraw_count = std::min(draw_remaining_count, draw_overdraw_end - draw_overdraw_begin);
  362. if (draw_overdraw_count > 0)
  363. {
  364. Color32 *colors = colordata + draw_overdraw_begin;
  365. fill_color_array(curcolor, colors, draw_overdraw_count);
  366. }
  367. }
  368. }
  369. }
  370. void Polyline::fill_color_array(Color32 constant_color, Color32 *colors, int count)
  371. {
  372. for (int i = 0; i < count; ++i)
  373. {
  374. Color32 c = constant_color;
  375. c.a *= (i+1) % 2; // avoids branching. equiv to if (i%2 == 1) c.a = 0;
  376. colors[i] = c;
  377. }
  378. }
  379. void NoneJoinPolyline::fill_color_array(Color32 constant_color, Color32 *colors, int count)
  380. {
  381. for (int i = 0; i < count; ++i)
  382. {
  383. Color32 c = constant_color;
  384. c.a *= (i & 3) < 2; // if (i % 4 == 2 || i % 4 == 3) c.a = 0
  385. colors[i] = c;
  386. }
  387. }
  388. } // graphics
  389. } // love