Render bezier curves using distance fields

This commit is contained in:
jgk
2021-03-14 00:13:29 +01:00
parent 3aefb8319d
commit b0c8c10658
6 changed files with 51 additions and 37 deletions
+33 -7
View File
@@ -71,23 +71,49 @@ polygonCol = PolygonCol 0
-- note that much of work computing the width of the bezier curve is done here
bezierQuad :: Color -> Color -> Float -> Float -> Point2 -> Point2 -> Point2 -> Picture
bezierQuad cola colc ra rc a b c = BezierQuad 0 [(aX, cola, aX +.+ aRadVec , fracRadA )
,(bX, colb, (0,0) , 0 )
,(cX, colc, cX +.+ cRadVec , fracRadC )
bezierQuad cola colc ra rc a b c = BezierQuad 0 [-- ( (0,0) , cola, (0,0), (0,0) )
(aIn, cola, (fa aIn,fc aIn) , (1,0) )
,(aIn, cola, (fa aIn,fc aIn) , (1,0) )
,(cIn, colc, (fa cIn,fc cIn) , (0,1) )
,( aX, cola, (1,0) , (fa' aX,fc' aX) )
,( cX, colc, (0,1) , (fa' cX,fc' cX) )
,( bX, colb, (0,0) , (fa' bX,fc' bX) )
,( bX, colb, (0,0) , (fa' bX,fc' bX) )
]
where colb = mixColors 0.5 0.5 cola colc
b2a | isLHS a b c = a -.- b
| otherwise = b -.- a
aRadVec = ra *.* (normalizeV $ vNormal b2a)
aX = a -.- 0.5 *.* aRadVec
aX = a -.- 0.5 *.* aRadVec
aIn = a +.+ 0.5 *.* aRadVec
b2c | isLHS a b c = b -.- c
| otherwise = c -.- b
cRadVec = rc *.* (normalizeV $ vNormal b2c)
cX = c -.- 0.5 *.* cRadVec
cX = c -.- 0.5 *.* cRadVec
cIn = c +.+ 0.5 *.* cRadVec
bX = (b -.- (0.5 *.* aRadVec) )
-.- (0.5 *.* cRadVec)
fracRadA = ra / magV (aX -.- cX)
fracRadC = rc / magV (aX -.- cX)
bIn = b +.+ (0.5 *.* aRadVec)
+.+ (0.5 *.* cRadVec)
fa = extrapolate aX cX bX
fc = extrapolate cX aX bX
fa' = extrapolate aIn cIn bIn
fc' = extrapolate cIn aIn bIn
-- given a one and two zeros of a linear function over x and y,
-- determine the function
-- so if f(ox,oy) = 1 and f(ax,ay) = f(bx,by) = 0, determines f
extrapolate :: Point2 -> Point2 -> Point2 -> Point2 -> Float
extrapolate (ox,oy) (ax,ay) (bx,by) (x,y) =
( x * ( ay - by )
+ y * ( bx - ax )
+ (ax * by - bx * ay)
)
/
( ox * ( ay - by )
+ ax * ( by - oy )
+ bx * ( oy - ay )
)
color :: RGBA -> Picture -> Picture
{-# INLINE color #-}
+2 -2
View File
@@ -22,7 +22,7 @@ import Data.Traversable
data RenderType
= RenderPoly [(Point3,Point4)]
| RenderBezQ [(Point3,Point4,Point3)]
| RenderBezQ [(Point3,Point4,Point4)]
| RenderText [(Point3,Point4,Point3)]
| RenderArc (Point3,Point4,Point4)
| RenderLine [(Point3,Point4)]
@@ -75,7 +75,7 @@ data Picture
= Blank
| Text Int String
| Polygon Int [Point2]
| BezierQuad Int [(Point2,RGBA,Point2,Float)]
| BezierQuad Int [(Point2,RGBA,Point2,Point2)]
| PolygonCol Int [(Point2,RGBA)]
| Circle Int RGBA RGBA Float
| ThickArc Int Float Float Float Float
+4 -2
View File
@@ -50,7 +50,7 @@ preloadRender = do
"data/texture/charMap.png"
bezierQuadShader
<- makeShader "bezierQuad" [vert,geom,frag] [(0,3),(1,4),(2,3)] Triangles pokeBezQStrat
<- makeShader "bezierQuad" [vert,frag] [(0,3),(1,4),(2,4)] TriangleStrip pokeBezQStrat
--the following vbo is set up to contain one fixed vertex
dummyvbo <- genObjectName
@@ -84,7 +84,9 @@ cleanUpRenderPreload pd = do
{-# INLINE pokeBezQStrat #-}
pokeBezQStrat :: RenderType -> [[[Float]]]
pokeBezQStrat (RenderBezQ vs) = fmap (\((x,y,z),(r,g,b,a),(s,t,v)) -> [[x,y,z],[r,g,b,a],[s,t,v]]) vs
pokeBezQStrat (RenderBezQ vs) = fmap (\((x,y,z),(r,g,b,a),(s,t,u,v)) -> [[x,y,z],[r,g,b,a],[s,t,u,v]]
)
vs
pokeBezQStrat _ = []
{-# INLINE pokeTriStrat #-}
+1 -1
View File
@@ -20,7 +20,7 @@ picToLTree mx (PolygonCol i vs)
picToLTree mx (BezierQuad i vs)
= filtB mx i $ LLeaf $ RenderBezQ $ zip3 (map zeroZ ps) cols rs
where (ps,cols,offps,rads) = unzip4 vs
rs = zipWith (\(x,y) z -> (x,y,z)) offps rads
rs = zipWith (\(x,y) (z,w) -> (x,y,z,w)) offps rads
picToLTree mx (Circle i colC colE r)
= filtB mx i $ LLeaf $ RenderEllipse [( (-r, r,0), colC)