line_builder.cpp 18 KB

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