line_builder.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*************************************************************************/
  2. /* line_builder.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "line_builder.h"
  31. //----------------------------------------------------------------------------
  32. // Util
  33. //----------------------------------------------------------------------------
  34. enum SegmentIntersectionResult {
  35. SEGMENT_PARALLEL = 0,
  36. SEGMENT_NO_INTERSECT = 1,
  37. SEGMENT_INTERSECT = 2
  38. };
  39. static SegmentIntersectionResult segment_intersection(
  40. Vector2 a, Vector2 b, Vector2 c, Vector2 d,
  41. Vector2 *out_intersection) {
  42. // http://paulbourke.net/geometry/pointlineplane/ <-- Good stuff
  43. Vector2 cd = d - c;
  44. Vector2 ab = b - a;
  45. float div = cd.y * ab.x - cd.x * ab.y;
  46. if (Math::abs(div) > 0.001f) {
  47. float ua = (cd.x * (a.y - c.y) - cd.y * (a.x - c.x)) / div;
  48. float ub = (ab.x * (a.y - c.y) - ab.y * (a.x - c.x)) / div;
  49. *out_intersection = a + ua * ab;
  50. if (ua >= 0.f && ua <= 1.f &&
  51. ub >= 0.f && ub <= 1.f)
  52. return SEGMENT_INTERSECT;
  53. return SEGMENT_NO_INTERSECT;
  54. }
  55. return SEGMENT_PARALLEL;
  56. }
  57. // TODO I'm pretty sure there is an even faster way to swap things
  58. template <typename T>
  59. static inline void swap(T &a, T &b) {
  60. T tmp = a;
  61. a = b;
  62. b = tmp;
  63. }
  64. static float calculate_total_distance(const Vector<Vector2> &points) {
  65. float d = 0.f;
  66. for (int i = 1; i < points.size(); ++i) {
  67. d += points[i].distance_to(points[i - 1]);
  68. }
  69. return d;
  70. }
  71. static inline Vector2 rotate90(const Vector2 &v) {
  72. // Note: the 2D referential is X-right, Y-down
  73. return Vector2(v.y, -v.x);
  74. }
  75. static inline Vector2 interpolate(const Rect2 &r, const Vector2 &v) {
  76. return Vector2(
  77. Math::lerp(r.position.x, r.position.x + r.get_size().x, v.x),
  78. Math::lerp(r.position.y, r.position.y + r.get_size().y, v.y));
  79. }
  80. //----------------------------------------------------------------------------
  81. // LineBuilder
  82. //----------------------------------------------------------------------------
  83. LineBuilder::LineBuilder() {
  84. joint_mode = Line2D::LINE_JOINT_SHARP;
  85. width = 10;
  86. default_color = Color(0.4, 0.5, 1);
  87. gradient = NULL;
  88. sharp_limit = 2.f;
  89. round_precision = 8;
  90. begin_cap_mode = Line2D::LINE_CAP_NONE;
  91. end_cap_mode = Line2D::LINE_CAP_NONE;
  92. _interpolate_color = false;
  93. _last_index[0] = 0;
  94. _last_index[1] = 0;
  95. }
  96. void LineBuilder::clear_output() {
  97. vertices.clear();
  98. colors.clear();
  99. indices.clear();
  100. }
  101. void LineBuilder::build() {
  102. // Need at least 2 points to draw a line
  103. if (points.size() < 2) {
  104. clear_output();
  105. return;
  106. }
  107. const float hw = width / 2.f;
  108. const float hw_sq = hw * hw;
  109. const float sharp_limit_sq = sharp_limit * sharp_limit;
  110. const int len = points.size();
  111. // Initial values
  112. Vector2 pos0 = points[0];
  113. Vector2 pos1 = points[1];
  114. Vector2 f0 = (pos1 - pos0).normalized();
  115. Vector2 u0 = rotate90(f0);
  116. Vector2 pos_up0 = pos0 + u0 * hw;
  117. Vector2 pos_down0 = pos0 - u0 * hw;
  118. Color color0;
  119. Color color1;
  120. float current_distance0 = 0.f;
  121. float current_distance1 = 0.f;
  122. float total_distance = 0.f;
  123. _interpolate_color = gradient != NULL;
  124. bool distance_required = _interpolate_color || texture_mode == Line2D::LINE_TEXTURE_TILE;
  125. if (distance_required)
  126. total_distance = calculate_total_distance(points);
  127. if (_interpolate_color)
  128. color0 = gradient->get_color(0);
  129. else
  130. colors.push_back(default_color);
  131. float uvx0 = 0.f;
  132. float uvx1 = 0.f;
  133. // Begin cap
  134. if (begin_cap_mode == Line2D::LINE_CAP_BOX) {
  135. // Push back first vertices a little bit
  136. pos_up0 -= f0 * hw;
  137. pos_down0 -= f0 * hw;
  138. // The line's outer length will be a little higher due to begin and end caps
  139. total_distance += width;
  140. current_distance0 += hw;
  141. current_distance1 = current_distance0;
  142. } else if (begin_cap_mode == Line2D::LINE_CAP_ROUND) {
  143. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  144. uvx0 = 0.5f;
  145. }
  146. new_arc(pos0, pos_up0 - pos0, -Math_PI, color0, Rect2(0.f, 0.f, 1.f, 1.f));
  147. total_distance += width;
  148. current_distance0 += hw;
  149. current_distance1 = current_distance0;
  150. }
  151. strip_begin(pos_up0, pos_down0, color0, uvx0);
  152. /*
  153. * pos_up0 ------------- pos_up1 --------------------
  154. * | |
  155. * pos0 - - - - - - - - - pos1 - - - - - - - - - pos2
  156. * | |
  157. * pos_down0 ------------ pos_down1 ------------------
  158. *
  159. * i-1 i i+1
  160. */
  161. // http://labs.hyperandroid.com/tag/opengl-lines
  162. // (not the same implementation but visuals help a lot)
  163. // For each additional segment
  164. for (int i = 1; i < len - 1; ++i) {
  165. pos1 = points[i];
  166. Vector2 pos2 = points[i + 1];
  167. Vector2 f1 = (pos2 - pos1).normalized();
  168. Vector2 u1 = rotate90(f1);
  169. // Determine joint orientation
  170. const float dp = u0.dot(f1);
  171. const Orientation orientation = (dp > 0.f ? UP : DOWN);
  172. Vector2 inner_normal0, inner_normal1;
  173. if (orientation == UP) {
  174. inner_normal0 = u0 * hw;
  175. inner_normal1 = u1 * hw;
  176. } else {
  177. inner_normal0 = -u0 * hw;
  178. inner_normal1 = -u1 * hw;
  179. }
  180. /*
  181. * ---------------------------
  182. * /
  183. * 0 / 1
  184. * / /
  185. * --------------------x------ /
  186. * / / (here shown with orientation == DOWN)
  187. * / /
  188. * / /
  189. * / /
  190. * 2 /
  191. * /
  192. */
  193. // Find inner intersection at the joint
  194. Vector2 corner_pos_in, corner_pos_out;
  195. SegmentIntersectionResult intersection_result = segment_intersection(
  196. pos0 + inner_normal0, pos1 + inner_normal0,
  197. pos1 + inner_normal1, pos2 + inner_normal1,
  198. &corner_pos_in);
  199. if (intersection_result == SEGMENT_INTERSECT)
  200. // Inner parts of the segments intersect
  201. corner_pos_out = 2.f * pos1 - corner_pos_in;
  202. else {
  203. // No intersection, segments are either parallel or too sharp
  204. corner_pos_in = pos1 + inner_normal0;
  205. corner_pos_out = pos1 - inner_normal0;
  206. }
  207. Vector2 corner_pos_up, corner_pos_down;
  208. if (orientation == UP) {
  209. corner_pos_up = corner_pos_in;
  210. corner_pos_down = corner_pos_out;
  211. } else {
  212. corner_pos_up = corner_pos_out;
  213. corner_pos_down = corner_pos_in;
  214. }
  215. Line2D::LineJointMode current_joint_mode = joint_mode;
  216. Vector2 pos_up1, pos_down1;
  217. if (intersection_result == SEGMENT_INTERSECT) {
  218. // Fallback on bevel if sharp angle is too high (because it would produce very long miters)
  219. if (current_joint_mode == Line2D::LINE_JOINT_SHARP && corner_pos_out.distance_squared_to(pos1) / hw_sq > sharp_limit_sq) {
  220. current_joint_mode = Line2D::LINE_JOINT_BEVEL;
  221. }
  222. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  223. // In this case, we won't create joint geometry,
  224. // The previous and next line quads will directly share an edge.
  225. pos_up1 = corner_pos_up;
  226. pos_down1 = corner_pos_down;
  227. } else {
  228. // Bevel or round
  229. if (orientation == UP) {
  230. pos_up1 = corner_pos_up;
  231. pos_down1 = pos1 - u0 * hw;
  232. } else {
  233. pos_up1 = pos1 + u0 * hw;
  234. pos_down1 = corner_pos_down;
  235. }
  236. }
  237. } else {
  238. // No intersection: fallback
  239. pos_up1 = corner_pos_up;
  240. pos_down1 = corner_pos_down;
  241. }
  242. // Add current line body quad
  243. // Triangles are clockwise
  244. if (distance_required) {
  245. current_distance1 += pos0.distance_to(pos1);
  246. }
  247. if (_interpolate_color) {
  248. color1 = gradient->get_color_at_offset(current_distance1 / total_distance);
  249. }
  250. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  251. uvx0 = current_distance0 / width;
  252. uvx1 = current_distance1 / width;
  253. }
  254. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  255. // Swap vars for use in the next line
  256. color0 = color1;
  257. u0 = u1;
  258. f0 = f1;
  259. pos0 = pos1;
  260. current_distance0 = current_distance1;
  261. if (intersection_result == SEGMENT_INTERSECT) {
  262. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  263. pos_up0 = pos_up1;
  264. pos_down0 = pos_down1;
  265. } else {
  266. if (orientation == UP) {
  267. pos_up0 = corner_pos_up;
  268. pos_down0 = pos1 - u1 * hw;
  269. } else {
  270. pos_up0 = pos1 + u1 * hw;
  271. pos_down0 = corner_pos_down;
  272. }
  273. }
  274. } else {
  275. pos_up0 = pos1 + u1 * hw;
  276. pos_down0 = pos1 - u1 * hw;
  277. }
  278. // From this point, bu0 and bd0 concern the next segment
  279. // Add joint geometry
  280. if (current_joint_mode != Line2D::LINE_JOINT_SHARP) {
  281. /* ________________ cbegin
  282. * / \
  283. * / \
  284. * ____________/_ _ _\ cend
  285. * | |
  286. * | |
  287. * | |
  288. */
  289. Vector2 cbegin, cend;
  290. if (orientation == UP) {
  291. cbegin = pos_down1;
  292. cend = pos_down0;
  293. } else {
  294. cbegin = pos_up1;
  295. cend = pos_up0;
  296. }
  297. if (current_joint_mode == Line2D::LINE_JOINT_BEVEL) {
  298. strip_add_tri(cend, orientation);
  299. } else if (current_joint_mode == Line2D::LINE_JOINT_ROUND) {
  300. Vector2 vbegin = cbegin - pos1;
  301. Vector2 vend = cend - pos1;
  302. strip_add_arc(pos1, vbegin.angle_to(vend), orientation);
  303. }
  304. if (intersection_result != SEGMENT_INTERSECT)
  305. // In this case the joint is too fucked up to be re-used,
  306. // start again the strip with fallback points
  307. strip_begin(pos_up0, pos_down0, color1, uvx1);
  308. }
  309. }
  310. // Last (or only) segment
  311. pos1 = points[points.size() - 1];
  312. Vector2 pos_up1 = pos1 + u0 * hw;
  313. Vector2 pos_down1 = pos1 - u0 * hw;
  314. // End cap (box)
  315. if (end_cap_mode == Line2D::LINE_CAP_BOX) {
  316. pos_up1 += f0 * hw;
  317. pos_down1 += f0 * hw;
  318. }
  319. if (distance_required) {
  320. current_distance1 += pos0.distance_to(pos1);
  321. }
  322. if (_interpolate_color) {
  323. color1 = gradient->get_color(gradient->get_points_count() - 1);
  324. }
  325. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  326. uvx1 = current_distance1 / width;
  327. }
  328. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  329. // End cap (round)
  330. if (end_cap_mode == Line2D::LINE_CAP_ROUND) {
  331. // Note: color is not used in case we don't interpolate...
  332. Color color = _interpolate_color ? gradient->get_color(gradient->get_points_count() - 1) : Color(0, 0, 0);
  333. new_arc(pos1, pos_up1 - pos1, Math_PI, color, Rect2(uvx1 - 0.5f, 0.f, 1.f, 1.f));
  334. }
  335. }
  336. void LineBuilder::strip_begin(Vector2 up, Vector2 down, Color color, float uvx) {
  337. int vi = vertices.size();
  338. vertices.push_back(up);
  339. vertices.push_back(down);
  340. if (_interpolate_color) {
  341. colors.push_back(color);
  342. colors.push_back(color);
  343. }
  344. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  345. uvs.push_back(Vector2(uvx, 0.f));
  346. uvs.push_back(Vector2(uvx, 1.f));
  347. }
  348. _last_index[UP] = vi;
  349. _last_index[DOWN] = vi + 1;
  350. }
  351. void LineBuilder::strip_new_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  352. int vi = vertices.size();
  353. vertices.push_back(vertices[_last_index[UP]]);
  354. vertices.push_back(vertices[_last_index[DOWN]]);
  355. vertices.push_back(up);
  356. vertices.push_back(down);
  357. if (_interpolate_color) {
  358. colors.push_back(color);
  359. colors.push_back(color);
  360. colors.push_back(color);
  361. colors.push_back(color);
  362. }
  363. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  364. uvs.push_back(uvs[_last_index[UP]]);
  365. uvs.push_back(uvs[_last_index[DOWN]]);
  366. uvs.push_back(Vector2(uvx, UP));
  367. uvs.push_back(Vector2(uvx, DOWN));
  368. }
  369. indices.push_back(vi);
  370. indices.push_back(vi + 3);
  371. indices.push_back(vi + 1);
  372. indices.push_back(vi);
  373. indices.push_back(vi + 2);
  374. indices.push_back(vi + 3);
  375. _last_index[UP] = vi + 2;
  376. _last_index[DOWN] = vi + 3;
  377. }
  378. void LineBuilder::strip_add_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  379. int vi = vertices.size();
  380. vertices.push_back(up);
  381. vertices.push_back(down);
  382. if (_interpolate_color) {
  383. colors.push_back(color);
  384. colors.push_back(color);
  385. }
  386. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  387. uvs.push_back(Vector2(uvx, 0.f));
  388. uvs.push_back(Vector2(uvx, 1.f));
  389. }
  390. indices.push_back(_last_index[UP]);
  391. indices.push_back(vi + 1);
  392. indices.push_back(_last_index[DOWN]);
  393. indices.push_back(_last_index[UP]);
  394. indices.push_back(vi);
  395. indices.push_back(vi + 1);
  396. _last_index[UP] = vi;
  397. _last_index[DOWN] = vi + 1;
  398. }
  399. void LineBuilder::strip_add_tri(Vector2 up, Orientation orientation) {
  400. int vi = vertices.size();
  401. vertices.push_back(up);
  402. if (_interpolate_color) {
  403. colors.push_back(colors[colors.size() - 1]);
  404. }
  405. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  406. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  407. // UVs are just one slice of the texture all along
  408. // (otherwise we can't share the bottom vertice)
  409. uvs.push_back(uvs[_last_index[opposite_orientation]]);
  410. }
  411. indices.push_back(_last_index[opposite_orientation]);
  412. indices.push_back(vi);
  413. indices.push_back(_last_index[orientation]);
  414. _last_index[opposite_orientation] = vi;
  415. }
  416. void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation orientation) {
  417. // Take the two last vertices and extrude an arc made of triangles
  418. // that all share one of the initial vertices
  419. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  420. Vector2 vbegin = vertices[_last_index[opposite_orientation]] - center;
  421. float radius = vbegin.length();
  422. float angle_step = Math_PI / static_cast<float>(round_precision);
  423. float steps = Math::abs(angle_delta) / angle_step;
  424. if (angle_delta < 0.f)
  425. angle_step = -angle_step;
  426. float t = Vector2(1, 0).angle_to(vbegin);
  427. float end_angle = t + angle_delta;
  428. Vector2 rpos(0, 0);
  429. // Arc vertices
  430. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  431. rpos = center + Vector2(Math::cos(t), Math::sin(t)) * radius;
  432. strip_add_tri(rpos, orientation);
  433. }
  434. // Last arc vertice
  435. rpos = center + Vector2(Math::cos(end_angle), Math::sin(end_angle)) * radius;
  436. strip_add_tri(rpos, orientation);
  437. }
  438. void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Color color, Rect2 uv_rect) {
  439. // Make a standalone arc that doesn't use existing vertices,
  440. // with undistorted UVs from within a square section
  441. float radius = vbegin.length();
  442. float angle_step = Math_PI / static_cast<float>(round_precision);
  443. float steps = Math::abs(angle_delta) / angle_step;
  444. if (angle_delta < 0.f)
  445. angle_step = -angle_step;
  446. float t = Vector2(1, 0).angle_to(vbegin);
  447. float end_angle = t + angle_delta;
  448. Vector2 rpos(0, 0);
  449. float tt_begin = -Math_PI / 2.f;
  450. float tt = tt_begin;
  451. // Center vertice
  452. int vi = vertices.size();
  453. vertices.push_back(center);
  454. if (_interpolate_color)
  455. colors.push_back(color);
  456. if (texture_mode != Line2D::LINE_TEXTURE_NONE)
  457. uvs.push_back(interpolate(uv_rect, Vector2(0.5f, 0.5f)));
  458. // Arc vertices
  459. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  460. Vector2 sc = Vector2(Math::cos(t), Math::sin(t));
  461. rpos = center + sc * radius;
  462. vertices.push_back(rpos);
  463. if (_interpolate_color)
  464. colors.push_back(color);
  465. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  466. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  467. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  468. tt += angle_step;
  469. }
  470. }
  471. // Last arc vertice
  472. Vector2 sc = Vector2(Math::cos(end_angle), Math::sin(end_angle));
  473. rpos = center + sc * radius;
  474. vertices.push_back(rpos);
  475. if (_interpolate_color)
  476. colors.push_back(color);
  477. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  478. tt = tt_begin + angle_delta;
  479. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  480. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  481. }
  482. // Make up triangles
  483. int vi0 = vi;
  484. for (int ti = 0; ti < steps; ++ti) {
  485. indices.push_back(vi0);
  486. indices.push_back(++vi);
  487. indices.push_back(vi + 1);
  488. }
  489. }