diff options
Diffstat (limited to 'scene/2d/line_builder.cpp')
-rw-r--r-- | scene/2d/line_builder.cpp | 120 |
1 files changed, 36 insertions, 84 deletions
diff --git a/scene/2d/line_builder.cpp b/scene/2d/line_builder.cpp index 6b06f2227a..a8a2639ccf 100644 --- a/scene/2d/line_builder.cpp +++ b/scene/2d/line_builder.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -53,22 +53,15 @@ static SegmentIntersectionResult segment_intersection( float ub = (ab.x * (a.y - c.y) - ab.y * (a.x - c.x)) / div; *out_intersection = a + ua * ab; if (ua >= 0.f && ua <= 1.f && - ub >= 0.f && ub <= 1.f) + ub >= 0.f && ub <= 1.f) { return SEGMENT_INTERSECT; + } return SEGMENT_NO_INTERSECT; } return SEGMENT_PARALLEL; } -// TODO I'm pretty sure there is an even faster way to swap things -template <typename T> -static inline void swap(T &a, T &b) { - T tmp = a; - a = b; - b = tmp; -} - static float calculate_total_distance(const Vector<Vector2> &points) { float d = 0.f; for (int i = 1; i < points.size(); ++i) { @@ -93,20 +86,6 @@ static inline Vector2 interpolate(const Rect2 &r, const Vector2 &v) { //---------------------------------------------------------------------------- LineBuilder::LineBuilder() { - joint_mode = Line2D::LINE_JOINT_SHARP; - width = 10; - curve = nullptr; - default_color = Color(0.4, 0.5, 1); - gradient = nullptr; - sharp_limit = 2.f; - round_precision = 8; - begin_cap_mode = Line2D::LINE_CAP_NONE; - end_cap_mode = Line2D::LINE_CAP_NONE; - tile_aspect = 1.f; - - _interpolate_color = false; - _last_index[0] = 0; - _last_index[1] = 0; } void LineBuilder::clear_output() { @@ -117,7 +96,6 @@ void LineBuilder::clear_output() { } void LineBuilder::build() { - // Need at least 2 points to draw a line if (points.size() < 2) { clear_output(); @@ -158,28 +136,32 @@ void LineBuilder::build() { //Adjust totalDistance. // The line's outer length will be a little higher due to begin and end caps if (begin_cap_mode == Line2D::LINE_CAP_BOX || begin_cap_mode == Line2D::LINE_CAP_ROUND) { - if (retrieve_curve) + if (retrieve_curve) { total_distance += width * curve->interpolate_baked(0.f) * 0.5f; - else + } else { total_distance += width * 0.5f; + } } if (end_cap_mode == Line2D::LINE_CAP_BOX || end_cap_mode == Line2D::LINE_CAP_ROUND) { - if (retrieve_curve) + if (retrieve_curve) { total_distance += width * curve->interpolate_baked(1.f) * 0.5f; - else + } else { total_distance += width * 0.5f; + } } } - if (_interpolate_color) + if (_interpolate_color) { color0 = gradient->get_color(0); - else + } else { colors.push_back(default_color); + } float uvx0 = 0.f; float uvx1 = 0.f; - if (retrieve_curve) + if (retrieve_curve) { width_factor = curve->interpolate_baked(0.f); + } pos_up0 += u0 * hw * width_factor; pos_down0 -= u0 * hw * width_factor; @@ -220,7 +202,6 @@ void LineBuilder::build() { // For each additional segment for (int i = 1; i < len - 1; ++i) { - pos1 = points[i]; Vector2 pos2 = points[i + 1]; @@ -271,10 +252,10 @@ void LineBuilder::build() { pos1 + inner_normal1, pos2 + inner_normal1, &corner_pos_in); - if (intersection_result == SEGMENT_INTERSECT) + if (intersection_result == SEGMENT_INTERSECT) { // Inner parts of the segments intersect corner_pos_out = 2.f * pos1 - corner_pos_in; - else { + } else { // No intersection, segments are either parallel or too sharp corner_pos_in = pos1 + inner_normal0; corner_pos_out = pos1 - inner_normal0; @@ -359,7 +340,6 @@ void LineBuilder::build() { // Add joint geometry if (current_joint_mode != Line2D::LINE_JOINT_SHARP) { - /* ________________ cbegin * / \ * / \ @@ -386,10 +366,11 @@ void LineBuilder::build() { strip_add_arc(pos1, vbegin.angle_to(vend), orientation); } - if (intersection_result != SEGMENT_INTERSECT) - // In this case the joint is too corrputed to be re-used, + if (intersection_result != SEGMENT_INTERSECT) { + // In this case the joint is too corrupted to be re-used, // start again the strip with fallback points strip_begin(pos_up0, pos_down0, color1, uvx1); + } } } // Last (or only) segment @@ -456,39 +437,6 @@ void LineBuilder::strip_begin(Vector2 up, Vector2 down, Color color, float uvx) _last_index[DOWN] = vi + 1; } -void LineBuilder::strip_new_quad(Vector2 up, Vector2 down, Color color, float uvx) { - int vi = vertices.size(); - - vertices.push_back(vertices[_last_index[UP]]); - vertices.push_back(vertices[_last_index[DOWN]]); - vertices.push_back(up); - vertices.push_back(down); - - if (_interpolate_color) { - colors.push_back(color); - colors.push_back(color); - colors.push_back(color); - colors.push_back(color); - } - - if (texture_mode != Line2D::LINE_TEXTURE_NONE) { - uvs.push_back(uvs[_last_index[UP]]); - uvs.push_back(uvs[_last_index[DOWN]]); - uvs.push_back(Vector2(uvx, UP)); - uvs.push_back(Vector2(uvx, DOWN)); - } - - indices.push_back(vi); - indices.push_back(vi + 3); - indices.push_back(vi + 1); - indices.push_back(vi); - indices.push_back(vi + 2); - indices.push_back(vi + 3); - - _last_index[UP] = vi + 2; - _last_index[DOWN] = vi + 3; -} - void LineBuilder::strip_add_quad(Vector2 up, Vector2 down, Color color, float uvx) { int vi = vertices.size(); @@ -529,7 +477,7 @@ void LineBuilder::strip_add_tri(Vector2 up, Orientation orientation) { if (texture_mode != Line2D::LINE_TEXTURE_NONE) { // UVs are just one slice of the texture all along - // (otherwise we can't share the bottom vertice) + // (otherwise we can't share the bottom vertex) uvs.push_back(uvs[_last_index[opposite_orientation]]); } @@ -541,7 +489,6 @@ void LineBuilder::strip_add_tri(Vector2 up, Orientation orientation) { } void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation orientation) { - // Take the two last vertices and extrude an arc made of triangles // that all share one of the initial vertices @@ -551,8 +498,9 @@ void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation o float angle_step = Math_PI / static_cast<float>(round_precision); float steps = Math::abs(angle_delta) / angle_step; - if (angle_delta < 0.f) + if (angle_delta < 0.f) { angle_step = -angle_step; + } float t = Vector2(1, 0).angle_to(vbegin); float end_angle = t + angle_delta; @@ -564,13 +512,12 @@ void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation o strip_add_tri(rpos, orientation); } - // Last arc vertice + // Last arc vertex rpos = center + Vector2(Math::cos(end_angle), Math::sin(end_angle)) * radius; strip_add_tri(rpos, orientation); } void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Color color, Rect2 uv_rect) { - // Make a standalone arc that doesn't use existing vertices, // with undistorted UVs from within a square section @@ -578,22 +525,25 @@ void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Col float angle_step = Math_PI / static_cast<float>(round_precision); float steps = Math::abs(angle_delta) / angle_step; - if (angle_delta < 0.f) + if (angle_delta < 0.f) { angle_step = -angle_step; + } float t = Vector2(1, 0).angle_to(vbegin); float end_angle = t + angle_delta; Vector2 rpos(0, 0); - float tt_begin = -Math_PI / 2.f; + float tt_begin = -Math_PI / 2.0f; float tt = tt_begin; // Center vertice int vi = vertices.size(); vertices.push_back(center); - if (_interpolate_color) + if (_interpolate_color) { colors.push_back(color); - if (texture_mode != Line2D::LINE_TEXTURE_NONE) + } + if (texture_mode != Line2D::LINE_TEXTURE_NONE) { uvs.push_back(interpolate(uv_rect, Vector2(0.5f, 0.5f))); + } // Arc vertices for (int ti = 0; ti < steps; ++ti, t += angle_step) { @@ -601,8 +551,9 @@ void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Col rpos = center + sc * radius; vertices.push_back(rpos); - if (_interpolate_color) + if (_interpolate_color) { colors.push_back(color); + } if (texture_mode != Line2D::LINE_TEXTURE_NONE) { Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt)); uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f)))); @@ -610,12 +561,13 @@ void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Col } } - // Last arc vertice + // Last arc vertex Vector2 sc = Vector2(Math::cos(end_angle), Math::sin(end_angle)); rpos = center + sc * radius; vertices.push_back(rpos); - if (_interpolate_color) + if (_interpolate_color) { colors.push_back(color); + } if (texture_mode != Line2D::LINE_TEXTURE_NONE) { tt = tt_begin + angle_delta; Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt)); |