From e3dc0c4001c38048b74044fbc6177430c6775703 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 8 Oct 2020 18:38:59 +0200 Subject: [PATCH] Shadows: Fixes + two fixes for PVS Studio static analyzer. --- imgui_demo.cpp | 90 ++++++++++++++++++++------------------------------ imgui_draw.cpp | 12 +++---- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index b4351d5b..af1dde44 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -7701,92 +7701,72 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImGui::Dummy(ImVec2(200.0f, 200.0f)); const ImVec2 poly_centre(pos.x + 50.0f, pos.y + 100.0f); - ImVec2* poly_points; - int num_poly_points; + ImVec2 poly_points[8]; + int poly_points_count = 0; switch (poly_shape_index) { default: case 0: { - ImVec2 poly_point_data[] = - { - ImVec2(poly_centre.x - 32.0f, poly_centre.y), - ImVec2(poly_centre.x - 24.0f, poly_centre.y + 24.0f), - ImVec2(poly_centre.x, poly_centre.y + 32.0f), - ImVec2(poly_centre.x + 24.0f, poly_centre.y + 24.0f), - ImVec2(poly_centre.x + 32.0f, poly_centre.y), - ImVec2(poly_centre.x + 24.0f, poly_centre.y - 24.0f), - ImVec2(poly_centre.x, poly_centre.y - 32.0f), - ImVec2(poly_centre.x - 32.0f, poly_centre.y - 32.0f) - }; - poly_points = poly_point_data; - num_poly_points = 8; + poly_points[0] = ImVec2(poly_centre.x - 32.0f, poly_centre.y); + poly_points[1] = ImVec2(poly_centre.x - 24.0f, poly_centre.y + 24.0f); + poly_points[2] = ImVec2(poly_centre.x, poly_centre.y + 32.0f); + poly_points[3] = ImVec2(poly_centre.x + 24.0f, poly_centre.y + 24.0f); + poly_points[4] = ImVec2(poly_centre.x + 32.0f, poly_centre.y); + poly_points[5] = ImVec2(poly_centre.x + 24.0f, poly_centre.y - 24.0f); + poly_points[6] = ImVec2(poly_centre.x, poly_centre.y - 32.0f); + poly_points[7] = ImVec2(poly_centre.x - 32.0f, poly_centre.y - 32.0f); + poly_points_count = 8; break; } case 1: { - ImVec2 poly_point_data[] = - { - ImVec2(poly_centre.x + 40.0f, poly_centre.y - 20.0f), - ImVec2(poly_centre.x, poly_centre.y + 32.0f), - ImVec2(poly_centre.x - 24.0f, poly_centre.y - 32.0f) - }; - poly_points = poly_point_data; - num_poly_points = 3; + poly_points[0] = ImVec2(poly_centre.x + 40.0f, poly_centre.y - 20.0f); + poly_points[1] = ImVec2(poly_centre.x, poly_centre.y + 32.0f); + poly_points[2] = ImVec2(poly_centre.x - 24.0f, poly_centre.y - 32.0f); + poly_points_count = 3; break; } case 2: { - ImVec2 poly_point_data[] = - { - ImVec2(poly_centre.x - 32.0f, poly_centre.y), - ImVec2(poly_centre.x, poly_centre.y + 32.0f), - ImVec2(poly_centre.x + 32.0f, poly_centre.y), - ImVec2(poly_centre.x, poly_centre.y - 32.0f) - }; - poly_points = poly_point_data; - num_poly_points = 4; + poly_points[0] = ImVec2(poly_centre.x - 32.0f, poly_centre.y); + poly_points[1] = ImVec2(poly_centre.x, poly_centre.y + 32.0f); + poly_points[2] = ImVec2(poly_centre.x + 32.0f, poly_centre.y); + poly_points[3] = ImVec2(poly_centre.x, poly_centre.y - 32.0f); + poly_points_count = 4; break; } case 3: { - ImVec2 poly_point_data[] = - { - ImVec2(poly_centre.x - 4.0f, poly_centre.y - 20.0f), - ImVec2(poly_centre.x + 12.0f, poly_centre.y + 2.0f), - ImVec2(poly_centre.x + 8.0f, poly_centre.y + 16.0f), - ImVec2(poly_centre.x, poly_centre.y + 32.0f), - ImVec2(poly_centre.x - 16.0f, poly_centre.y - 32.0f) - }; - poly_points = poly_point_data; - num_poly_points = 5; + poly_points[0] = ImVec2(poly_centre.x - 4.0f, poly_centre.y - 20.0f); + poly_points[1] = ImVec2(poly_centre.x + 12.0f, poly_centre.y + 2.0f); + poly_points[2] = ImVec2(poly_centre.x + 8.0f, poly_centre.y + 16.0f); + poly_points[3] = ImVec2(poly_centre.x, poly_centre.y + 32.0f); + poly_points[4] = ImVec2(poly_centre.x - 16.0f, poly_centre.y - 32.0f); + poly_points_count = 5; break; } case 4: // Same as test case 3 but with reversed winding { - ImVec2 poly_point_data[] = - { - ImVec2(poly_centre.x - 16.0f, poly_centre.y - 32.0f), - ImVec2(poly_centre.x, poly_centre.y + 32.0f), - ImVec2(poly_centre.x + 8.0f, poly_centre.y + 16.0f), - ImVec2(poly_centre.x + 12.0f, poly_centre.y + 2.0f), - ImVec2(poly_centre.x - 4.0f, poly_centre.y - 20.0f) - }; - poly_points = poly_point_data; - num_poly_points = 5; + poly_points[0] = ImVec2(poly_centre.x - 16.0f, poly_centre.y - 32.0f); + poly_points[1] = ImVec2(poly_centre.x, poly_centre.y + 32.0f); + poly_points[2] = ImVec2(poly_centre.x + 8.0f, poly_centre.y + 16.0f); + poly_points[3] = ImVec2(poly_centre.x + 12.0f, poly_centre.y + 2.0f); + poly_points[4] = ImVec2(poly_centre.x - 4.0f, poly_centre.y - 20.0f); + poly_points_count = 5; break; } } // FIXME-SHADOWS: Offset forced to zero when shadow is not filled because it isn't supported ImDrawShadowFlags shadow_flags = shadow_filled ? ImDrawShadowFlags_None : ImDrawShadowFlags_CutOutShapeBackground; - draw_list->AddShadowConvexPoly(poly_points, num_poly_points, ImGui::GetColorU32(shadow_color), shadow_thickness, shadow_filled ? shadow_offset : ImVec2(0.0f, 0.0f), shadow_flags); + draw_list->AddShadowConvexPoly(poly_points, poly_points_count, ImGui::GetColorU32(shadow_color), shadow_thickness, shadow_filled ? shadow_offset : ImVec2(0.0f, 0.0f), shadow_flags); if (wireframe) - draw_list->AddPolyline(poly_points, num_poly_points, ImGui::GetColorU32(shape_color), true, 1.0f); + draw_list->AddPolyline(poly_points, poly_points_count, ImGui::GetColorU32(shape_color), true, 1.0f); else - draw_list->AddConvexPolyFilled(poly_points, num_poly_points, ImGui::GetColorU32(shape_color)); + draw_list->AddConvexPolyFilled(poly_points, poly_points_count, ImGui::GetColorU32(shape_color)); } draw_list->Flags = old_flags; diff --git a/imgui_draw.cpp b/imgui_draw.cpp index e1dfac2e..861e9994 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -394,8 +394,7 @@ int ImFontAtlasShadowTexConfig::CalcConvexTexWidth() const int ImFontAtlasShadowTexConfig::CalcConvexTexHeight() const { - // We have to pad the texture enough that we don't go off the edges when we expand the corner triangles - return (int)((TexCornerSize / ImCos(IM_PI * 0.25f)) + (GetConvexTexPadding() * 2)); + return CalcConvexTexWidth(); // Same value } //----------------------------------------------------------------------------- @@ -2217,9 +2216,10 @@ void ImDrawList::AddShadowConvexPoly(const ImVec2* points, int points_count, ImU if (cos_angle_coverage < 0.999999f) { - // If we are covering more than 90 degrees we need an intermediate vertex to stop the required expansion tending towards infinity, and thus the effective angle will be halved + // If we are covering more than 90 degrees we need an intermediate vertex to stop the required expansion tending towards infinity. + // And thus the effective angle will be halved (matches the similar code in loop below) float angle_coverage = ImAcos(cos_angle_coverage); - if (cos_angle_coverage <= 0.0f) + if (cos_angle_coverage <= 0.0f) // -V1051 angle_coverage *= 0.5f; edge_size_scales[edge_index] = 1.0f / ImCos(angle_coverage * 0.5f); // How much we need to expand our size by to avoid clipping the corner of the texture off } @@ -2258,10 +2258,10 @@ void ImDrawList::AddShadowConvexPoly(const ImVec2* points, int points_count, ImU // Add corner section float cos_angle_coverage = ImDot(edge_normal, prev_edge_normal); - if (cos_angle_coverage < 0.999999f) // Don't fill if the corner is actually straight { - // If we are covering more than 90 degrees we need an intermediate vertex to stop the required expansion tending towards infinity + // If we are covering more than 90 degrees we need an intermediate vertex to stop the required expansion tending towards infinity. + // And thus the effective angle has been halved (matches the similar code in loop above) int num_steps = (cos_angle_coverage <= 0.0f) ? 2 : 1; for (int step = 0; step < num_steps; step++)