Fix circle path components.

This commit is contained in:
Chinmay Garde
2021-08-18 19:26:59 -07:00
committed by Dan Field
parent 000ab23052
commit 117aaa3a01
6 changed files with 54 additions and 33 deletions

View File

@@ -34,6 +34,7 @@ struct Command {
size_t index_count = 0u;
std::string label;
PrimitiveType primitive_type = PrimitiveType::kTriangle;
WindingOrder winding = WindingOrder::kClockwise;
bool BindVertices(const VertexBuffer& buffer);

View File

@@ -357,7 +357,9 @@ bool RenderPass::EncodeCommands(Allocator& allocator,
command.pipeline->GetMTLRenderPipelineState());
pass_bindings.SetDepthStencilState(
command.pipeline->GetMTLDepthStencilState());
[pass setFrontFacingWinding:MTLWindingClockwise];
[pass setFrontFacingWinding:command.winding == WindingOrder::kClockwise
? MTLWindingClockwise
: MTLWindingCounterClockwise];
[pass setCullMode:MTLCullModeBack];
if (!bind_stage_resources(command.vertex_bindings, ShaderStage::kVertex)) {
return false;

View File

@@ -88,6 +88,7 @@ bool Tessellator::Tessellate(const std::vector<Point>& contours,
return false;
}
// TODO(csg): This copy can be elided entirely for the current use case.
std::vector<Point> points;
std::vector<uint32_t> indices;
@@ -111,4 +112,8 @@ bool Tessellator::Tessellate(const std::vector<Point>& contours,
return true;
}
WindingOrder Tessellator::GetFrontFaceWinding() const {
return WindingOrder::kClockwise;
}
} // namespace impeller

View File

@@ -8,6 +8,7 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/compositor/formats.h"
#include "impeller/geometry/point.h"
namespace impeller {
@@ -30,6 +31,8 @@ class Tessellator {
FillType GetFillType() const;
WindingOrder GetFrontFaceWinding() const;
using VertexCallback = std::function<void(Point)>;
[[nodiscard]] bool Tessellate(const std::vector<Point>& vertices,
VertexCallback callback) const;

View File

@@ -145,50 +145,59 @@ PathBuilder& PathBuilder::SmoothCubicCurveTo(Point point,
PathBuilder& PathBuilder::AddRect(Rect rect) {
current_ = rect.origin;
auto topLeft = rect.origin;
auto bottomLeft = rect.origin + Point{0.0, rect.size.height};
auto bottomRight = rect.origin + Point{rect.size.width, rect.size.height};
auto topRight = rect.origin + Point{rect.size.width, 0.0};
auto tl = rect.origin;
auto bl = rect.origin + Point{0.0, rect.size.height};
auto br = rect.origin + Point{rect.size.width, rect.size.height};
auto tr = rect.origin + Point{rect.size.width, 0.0};
prototype_.AddLinearComponent(topLeft, bottomLeft)
.AddLinearComponent(bottomLeft, bottomRight)
.AddLinearComponent(bottomRight, topRight)
.AddLinearComponent(topRight, topLeft);
prototype_.AddLinearComponent(tl, tr)
.AddLinearComponent(tr, br)
.AddLinearComponent(br, bl)
.AddLinearComponent(bl, tl);
return *this;
}
PathBuilder& PathBuilder::AddCircle(const Point& center, Scalar radius) {
current_ = center + Point{0.0, radius};
PathBuilder& PathBuilder::AddCircle(const Point& c, Scalar r) {
current_ = c + Point{0.0, r};
const Scalar diameter = radius * 2.0;
const Scalar magic = kArcApproximationMagic * radius;
// m for magic
const auto m = kArcApproximationMagic * r;
prototype_.AddCubicComponent(
{center.x + radius, center.y}, //
{center.x + radius + magic, center.y}, //
{center.x + diameter, center.y + radius - magic}, //
{center.x + diameter, center.y + radius} //
//----------------------------------------------------------------------------
/// Top right arc.
///
prototype_.AddCubicComponent({c.x, c.y - r}, // p1
{c.x + m, c.y - r}, // cp1
{c.x + r, c.y - m}, // cp2
{c.x + r, c.y} // p2
);
prototype_.AddCubicComponent(
{center.x + diameter, center.y + radius}, //
{center.x + diameter, center.y + radius + magic}, //
{center.x + radius + magic, center.y + diameter}, //
{center.x + radius, center.y + diameter} //
//----------------------------------------------------------------------------
/// Bottom right arc.
///
prototype_.AddCubicComponent({c.x + r, c.y}, // p1
{c.x + r, c.y + m}, // cp1
{c.x + m, c.y + r}, // cp2
{c.x, c.y + r} // p2
);
prototype_.AddCubicComponent(
{center.x + radius, center.y + diameter}, //
{center.x + radius - magic, center.y + diameter}, //
{center.x, center.y + radius + magic}, //
{center.x, center.y + radius} //
//----------------------------------------------------------------------------
/// Bottom left arc.
///
prototype_.AddCubicComponent({c.x, c.y + r}, // p1
{c.x - m, c.y + r}, // cp1
{c.x - r, c.y + m}, // cp2
{c.x - r, c.y} // p2
);
prototype_.AddCubicComponent({center.x, center.y + radius}, //
{center.x, center.y + radius - magic}, //
{center.x + radius - magic, center.y}, //
{center.x + radius, center.y} //
//----------------------------------------------------------------------------
/// Top left arc.
///
prototype_.AddCubicComponent({c.x - r, c.y}, // p1
{c.x - r, c.y - m}, // cp1
{c.x - m, c.y - r}, // cp2
{c.x, c.y - r} // p2
);
return *this;

View File

@@ -252,7 +252,7 @@ TEST_F(PrimitivesTest, CanRenderToTexture) {
}
TEST_F(PrimitivesTest, CanRenderPath) {
auto path = PathBuilder{}.AddRect({10, 10, 100, 100}).CreatePath();
auto path = PathBuilder{}.AddCircle({550, 550}, 500).CreatePath();
ASSERT_FALSE(path.GetBoundingBox().IsZero());
using BoxPipeline = PipelineT<BoxFadeVertexShader, BoxFadeFragmentShader>;
@@ -306,6 +306,7 @@ TEST_F(PrimitivesTest, CanRenderPath) {
FS::BindContents2(cmd, bridge, sampler);
cmd.primitive_type = PrimitiveType::kTriangle;
cmd.winding = tessellator.GetFrontFaceWinding();
VS::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize());