line_builder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*************************************************************************/
  2. /* line_builder.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. tile_aspect = 1.f;
  93. _interpolate_color = false;
  94. _last_index[0] = 0;
  95. _last_index[1] = 0;
  96. }
  97. void LineBuilder::clear_output() {
  98. vertices.clear();
  99. colors.clear();
  100. indices.clear();
  101. uvs.clear();
  102. }
  103. void LineBuilder::build() {
  104. // Need at least 2 points to draw a line
  105. if (points.size() < 2) {
  106. clear_output();
  107. return;
  108. }
  109. ERR_FAIL_COND(tile_aspect <= 0.f);
  110. const float hw = width / 2.f;
  111. const float hw_sq = hw * hw;
  112. const float sharp_limit_sq = sharp_limit * sharp_limit;
  113. const int len = points.size();
  114. // Initial values
  115. Vector2 pos0 = points[0];
  116. Vector2 pos1 = points[1];
  117. Vector2 f0 = (pos1 - pos0).normalized();
  118. Vector2 u0 = rotate90(f0);
  119. Vector2 pos_up0 = pos0 + u0 * hw;
  120. Vector2 pos_down0 = pos0 - u0 * hw;
  121. Color color0;
  122. Color color1;
  123. float current_distance0 = 0.f;
  124. float current_distance1 = 0.f;
  125. float total_distance = 0.f;
  126. _interpolate_color = gradient != NULL;
  127. bool distance_required = _interpolate_color ||
  128. texture_mode == Line2D::LINE_TEXTURE_TILE ||
  129. texture_mode == Line2D::LINE_TEXTURE_STRETCH;
  130. if (distance_required)
  131. total_distance = calculate_total_distance(points);
  132. if (_interpolate_color)
  133. color0 = gradient->get_color(0);
  134. else
  135. colors.push_back(default_color);
  136. float uvx0 = 0.f;
  137. float uvx1 = 0.f;
  138. // Begin cap
  139. if (begin_cap_mode == Line2D::LINE_CAP_BOX) {
  140. // Push back first vertices a little bit
  141. pos_up0 -= f0 * hw;
  142. pos_down0 -= f0 * hw;
  143. // The line's outer length will be a little higher due to begin and end caps
  144. total_distance += width;
  145. current_distance0 += hw;
  146. current_distance1 = current_distance0;
  147. } else if (begin_cap_mode == Line2D::LINE_CAP_ROUND) {
  148. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  149. uvx0 = 0.5f / tile_aspect;
  150. }
  151. new_arc(pos0, pos_up0 - pos0, -Math_PI, color0, Rect2(0.f, 0.f, fmin(uvx0 * 2, 1.f), 1.f));
  152. total_distance += width;
  153. current_distance0 += hw;
  154. current_distance1 = current_distance0;
  155. }
  156. strip_begin(pos_up0, pos_down0, color0, uvx0);
  157. /*
  158. * pos_up0 ------------- pos_up1 --------------------
  159. * | |
  160. * pos0 - - - - - - - - - pos1 - - - - - - - - - pos2
  161. * | |
  162. * pos_down0 ------------ pos_down1 ------------------
  163. *
  164. * i-1 i i+1
  165. */
  166. // http://labs.hyperandroid.com/tag/opengl-lines
  167. // (not the same implementation but visuals help a lot)
  168. // For each additional segment
  169. for (int i = 1; i < len - 1; ++i) {
  170. pos1 = points[i];
  171. Vector2 pos2 = points[i + 1];
  172. Vector2 f1 = (pos2 - pos1).normalized();
  173. Vector2 u1 = rotate90(f1);
  174. // Determine joint orientation
  175. const float dp = u0.dot(f1);
  176. const Orientation orientation = (dp > 0.f ? UP : DOWN);
  177. Vector2 inner_normal0, inner_normal1;
  178. if (orientation == UP) {
  179. inner_normal0 = u0 * hw;
  180. inner_normal1 = u1 * hw;
  181. } else {
  182. inner_normal0 = -u0 * hw;
  183. inner_normal1 = -u1 * hw;
  184. }
  185. /*
  186. * ---------------------------
  187. * /
  188. * 0 / 1
  189. * / /
  190. * --------------------x------ /
  191. * / / (here shown with orientation == DOWN)
  192. * / /
  193. * / /
  194. * / /
  195. * 2 /
  196. * /
  197. */
  198. // Find inner intersection at the joint
  199. Vector2 corner_pos_in, corner_pos_out;
  200. SegmentIntersectionResult intersection_result = segment_intersection(
  201. pos0 + inner_normal0, pos1 + inner_normal0,
  202. pos1 + inner_normal1, pos2 + inner_normal1,
  203. &corner_pos_in);
  204. if (intersection_result == SEGMENT_INTERSECT)
  205. // Inner parts of the segments intersect
  206. corner_pos_out = 2.f * pos1 - corner_pos_in;
  207. else {
  208. // No intersection, segments are either parallel or too sharp
  209. corner_pos_in = pos1 + inner_normal0;
  210. corner_pos_out = pos1 - inner_normal0;
  211. }
  212. Vector2 corner_pos_up, corner_pos_down;
  213. if (orientation == UP) {
  214. corner_pos_up = corner_pos_in;
  215. corner_pos_down = corner_pos_out;
  216. } else {
  217. corner_pos_up = corner_pos_out;
  218. corner_pos_down = corner_pos_in;
  219. }
  220. Line2D::LineJointMode current_joint_mode = joint_mode;
  221. Vector2 pos_up1, pos_down1;
  222. if (intersection_result == SEGMENT_INTERSECT) {
  223. // Fallback on bevel if sharp angle is too high (because it would produce very long miters)
  224. if (current_joint_mode == Line2D::LINE_JOINT_SHARP && corner_pos_out.distance_squared_to(pos1) / hw_sq > sharp_limit_sq) {
  225. current_joint_mode = Line2D::LINE_JOINT_BEVEL;
  226. }
  227. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  228. // In this case, we won't create joint geometry,
  229. // The previous and next line quads will directly share an edge.
  230. pos_up1 = corner_pos_up;
  231. pos_down1 = corner_pos_down;
  232. } else {
  233. // Bevel or round
  234. if (orientation == UP) {
  235. pos_up1 = corner_pos_up;
  236. pos_down1 = pos1 - u0 * hw;
  237. } else {
  238. pos_up1 = pos1 + u0 * hw;
  239. pos_down1 = corner_pos_down;
  240. }
  241. }
  242. } else {
  243. // No intersection: fallback
  244. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  245. // There is no fallback implementation for LINE_JOINT_SHARP so switch to the LINE_JOINT_BEVEL
  246. current_joint_mode = Line2D::LINE_JOINT_BEVEL;
  247. }
  248. pos_up1 = corner_pos_up;
  249. pos_down1 = corner_pos_down;
  250. }
  251. // Add current line body quad
  252. // Triangles are clockwise
  253. if (distance_required) {
  254. current_distance1 += pos0.distance_to(pos1);
  255. }
  256. if (_interpolate_color) {
  257. color1 = gradient->get_color_at_offset(current_distance1 / total_distance);
  258. }
  259. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  260. uvx1 = current_distance1 / (width * tile_aspect);
  261. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  262. uvx1 = current_distance1 / total_distance;
  263. }
  264. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  265. // Swap vars for use in the next line
  266. color0 = color1;
  267. u0 = u1;
  268. f0 = f1;
  269. pos0 = pos1;
  270. if (intersection_result == SEGMENT_INTERSECT) {
  271. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  272. pos_up0 = pos_up1;
  273. pos_down0 = pos_down1;
  274. } else {
  275. if (orientation == UP) {
  276. pos_up0 = corner_pos_up;
  277. pos_down0 = pos1 - u1 * hw;
  278. } else {
  279. pos_up0 = pos1 + u1 * hw;
  280. pos_down0 = corner_pos_down;
  281. }
  282. }
  283. } else {
  284. pos_up0 = pos1 + u1 * hw;
  285. pos_down0 = pos1 - u1 * hw;
  286. }
  287. // From this point, bu0 and bd0 concern the next segment
  288. // Add joint geometry
  289. if (current_joint_mode != Line2D::LINE_JOINT_SHARP) {
  290. /* ________________ cbegin
  291. * / \
  292. * / \
  293. * ____________/_ _ _\ cend
  294. * | |
  295. * | |
  296. * | |
  297. */
  298. Vector2 cbegin, cend;
  299. if (orientation == UP) {
  300. cbegin = pos_down1;
  301. cend = pos_down0;
  302. } else {
  303. cbegin = pos_up1;
  304. cend = pos_up0;
  305. }
  306. if (current_joint_mode == Line2D::LINE_JOINT_BEVEL) {
  307. strip_add_tri(cend, orientation);
  308. } else if (current_joint_mode == Line2D::LINE_JOINT_ROUND) {
  309. Vector2 vbegin = cbegin - pos1;
  310. Vector2 vend = cend - pos1;
  311. strip_add_arc(pos1, vbegin.angle_to(vend), orientation);
  312. }
  313. if (intersection_result != SEGMENT_INTERSECT)
  314. // In this case the joint is too corrputed to be re-used,
  315. // start again the strip with fallback points
  316. strip_begin(pos_up0, pos_down0, color1, uvx1);
  317. }
  318. }
  319. // Last (or only) segment
  320. pos1 = points[points.size() - 1];
  321. Vector2 pos_up1 = pos1 + u0 * hw;
  322. Vector2 pos_down1 = pos1 - u0 * hw;
  323. // End cap (box)
  324. if (end_cap_mode == Line2D::LINE_CAP_BOX) {
  325. pos_up1 += f0 * hw;
  326. pos_down1 += f0 * hw;
  327. }
  328. if (distance_required) {
  329. current_distance1 += pos0.distance_to(pos1);
  330. }
  331. if (_interpolate_color) {
  332. color1 = gradient->get_color(gradient->get_points_count() - 1);
  333. }
  334. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  335. uvx1 = current_distance1 / (width * tile_aspect);
  336. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  337. uvx1 = current_distance1 / total_distance;
  338. }
  339. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  340. // End cap (round)
  341. if (end_cap_mode == Line2D::LINE_CAP_ROUND) {
  342. // Note: color is not used in case we don't interpolate...
  343. Color color = _interpolate_color ? gradient->get_color(gradient->get_points_count() - 1) : Color(0, 0, 0);
  344. new_arc(pos1, pos_up1 - pos1, Math_PI, color, Rect2(uvx1 - 0.5f / tile_aspect, 0.f, 1.0f / tile_aspect, 1.f));
  345. }
  346. }
  347. void LineBuilder::strip_begin(Vector2 up, Vector2 down, Color color, float uvx) {
  348. int vi = vertices.size();
  349. vertices.push_back(up);
  350. vertices.push_back(down);
  351. if (_interpolate_color) {
  352. colors.push_back(color);
  353. colors.push_back(color);
  354. }
  355. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  356. uvs.push_back(Vector2(uvx, 0.f));
  357. uvs.push_back(Vector2(uvx, 1.f));
  358. }
  359. _last_index[UP] = vi;
  360. _last_index[DOWN] = vi + 1;
  361. }
  362. void LineBuilder::strip_new_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  363. int vi = vertices.size();
  364. vertices.push_back(vertices[_last_index[UP]]);
  365. vertices.push_back(vertices[_last_index[DOWN]]);
  366. vertices.push_back(up);
  367. vertices.push_back(down);
  368. if (_interpolate_color) {
  369. colors.push_back(color);
  370. colors.push_back(color);
  371. colors.push_back(color);
  372. colors.push_back(color);
  373. }
  374. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  375. uvs.push_back(uvs[_last_index[UP]]);
  376. uvs.push_back(uvs[_last_index[DOWN]]);
  377. uvs.push_back(Vector2(uvx, UP));
  378. uvs.push_back(Vector2(uvx, DOWN));
  379. }
  380. indices.push_back(vi);
  381. indices.push_back(vi + 3);
  382. indices.push_back(vi + 1);
  383. indices.push_back(vi);
  384. indices.push_back(vi + 2);
  385. indices.push_back(vi + 3);
  386. _last_index[UP] = vi + 2;
  387. _last_index[DOWN] = vi + 3;
  388. }
  389. void LineBuilder::strip_add_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  390. int vi = vertices.size();
  391. vertices.push_back(up);
  392. vertices.push_back(down);
  393. if (_interpolate_color) {
  394. colors.push_back(color);
  395. colors.push_back(color);
  396. }
  397. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  398. uvs.push_back(Vector2(uvx, 0.f));
  399. uvs.push_back(Vector2(uvx, 1.f));
  400. }
  401. indices.push_back(_last_index[UP]);
  402. indices.push_back(vi + 1);
  403. indices.push_back(_last_index[DOWN]);
  404. indices.push_back(_last_index[UP]);
  405. indices.push_back(vi);
  406. indices.push_back(vi + 1);
  407. _last_index[UP] = vi;
  408. _last_index[DOWN] = vi + 1;
  409. }
  410. void LineBuilder::strip_add_tri(Vector2 up, Orientation orientation) {
  411. int vi = vertices.size();
  412. vertices.push_back(up);
  413. if (_interpolate_color) {
  414. colors.push_back(colors[colors.size() - 1]);
  415. }
  416. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  417. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  418. // UVs are just one slice of the texture all along
  419. // (otherwise we can't share the bottom vertice)
  420. uvs.push_back(uvs[_last_index[opposite_orientation]]);
  421. }
  422. indices.push_back(_last_index[opposite_orientation]);
  423. indices.push_back(vi);
  424. indices.push_back(_last_index[orientation]);
  425. _last_index[opposite_orientation] = vi;
  426. }
  427. void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation orientation) {
  428. // Take the two last vertices and extrude an arc made of triangles
  429. // that all share one of the initial vertices
  430. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  431. Vector2 vbegin = vertices[_last_index[opposite_orientation]] - center;
  432. float radius = vbegin.length();
  433. float angle_step = Math_PI / static_cast<float>(round_precision);
  434. float steps = Math::abs(angle_delta) / angle_step;
  435. if (angle_delta < 0.f)
  436. angle_step = -angle_step;
  437. float t = Vector2(1, 0).angle_to(vbegin);
  438. float end_angle = t + angle_delta;
  439. Vector2 rpos(0, 0);
  440. // Arc vertices
  441. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  442. rpos = center + Vector2(Math::cos(t), Math::sin(t)) * radius;
  443. strip_add_tri(rpos, orientation);
  444. }
  445. // Last arc vertice
  446. rpos = center + Vector2(Math::cos(end_angle), Math::sin(end_angle)) * radius;
  447. strip_add_tri(rpos, orientation);
  448. }
  449. void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Color color, Rect2 uv_rect) {
  450. // Make a standalone arc that doesn't use existing vertices,
  451. // with undistorted UVs from within a square section
  452. float radius = vbegin.length();
  453. float angle_step = Math_PI / static_cast<float>(round_precision);
  454. float steps = Math::abs(angle_delta) / angle_step;
  455. if (angle_delta < 0.f)
  456. angle_step = -angle_step;
  457. float t = Vector2(1, 0).angle_to(vbegin);
  458. float end_angle = t + angle_delta;
  459. Vector2 rpos(0, 0);
  460. float tt_begin = -Math_PI / 2.f;
  461. float tt = tt_begin;
  462. // Center vertice
  463. int vi = vertices.size();
  464. vertices.push_back(center);
  465. if (_interpolate_color)
  466. colors.push_back(color);
  467. if (texture_mode != Line2D::LINE_TEXTURE_NONE)
  468. uvs.push_back(interpolate(uv_rect, Vector2(0.5f, 0.5f)));
  469. // Arc vertices
  470. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  471. Vector2 sc = Vector2(Math::cos(t), Math::sin(t));
  472. rpos = center + sc * radius;
  473. vertices.push_back(rpos);
  474. if (_interpolate_color)
  475. colors.push_back(color);
  476. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  477. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  478. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  479. tt += angle_step;
  480. }
  481. }
  482. // Last arc vertice
  483. Vector2 sc = Vector2(Math::cos(end_angle), Math::sin(end_angle));
  484. rpos = center + sc * radius;
  485. vertices.push_back(rpos);
  486. if (_interpolate_color)
  487. colors.push_back(color);
  488. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  489. tt = tt_begin + angle_delta;
  490. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  491. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  492. }
  493. // Make up triangles
  494. int vi0 = vi;
  495. for (int ti = 0; ti < steps; ++ti) {
  496. indices.push_back(vi0);
  497. indices.push_back(++vi);
  498. indices.push_back(vi + 1);
  499. }
  500. }