Twinside / Rasterific

A drawing engine in Haskell
BSD 3-Clause "New" or "Revised" License
140 stars 11 forks source link

`cacheDrawing` ignores `withClipping` #54

Closed vasiliyl closed 4 years ago

vasiliyl commented 4 years ago

Hello!

After caching the drawing which was clipped, the resulting drawing is the same as the original drawing before clipping.

Here is sample code:

import Codec.Picture( PixelRGBA8( .. ), writePng )
import Graphics.Rasterific
import Graphics.Rasterific.Texture

main :: IO ()
main = do
  let
    red = PixelRGBA8 255 0 0 255
    blue = PixelRGBA8 0 0 255 255
    rect =
      withTexture (uniformTexture red) $
        fill $
        rectangle (V2 0 0) 100 100
    clippedRect =
      withClipping
        (fill $ rectangle (V2 0 0) 50 50)
        rect
    cachedRect =
      cacheDrawing 50 50 96 clippedRect

  putStrLn "-------------------------"
  putStrLn $ dumpDrawing rect
  putStrLn "-------------------------"
  putStrLn $ dumpDrawing clippedRect
  putStrLn "-------------------------"
  putStrLn $ dumpDrawing cachedRect
  putStrLn "-------------------------"

  writePng "rect.png" $
    renderDrawing 100 100 blue rect
  writePng "clippedRect.png" $
    renderDrawing 100 100 blue clippedRect
  writePng "cachedRect.png" $
    renderDrawing 100 100 blue cachedRect

Here is the output:

-------------------------
withTexture (uniformTexture (PixelRGBA8 255 0 0 255)) (fill [LinePrim Line (V2 0.0 0.0) (V2 100.0 0.0),LinePrim Line (V2 100.0 0.0) (V2 100.0 100.0),LinePrim Line (V2 100.0 100.0) (V2 0.0 100.0),LinePrim Line (V2 0.0 100.0) (V2 0.0 0.0)] >>=
return ()) >>=
return ()
-------------------------
withClipping (withTexture (uniformTexture (255)) (fill [LinePrim Line (V2 0.0 0.0) (V2 50.0 0.0),LinePrim Line (V2 50.0 0.0) (V2 50.0 50.0),LinePrim Line (V2 50.0 50.0) (V2 0.0 50.0),LinePrim Line (V2 0.0 50.0) (V2 0.0 0.0)] >>=
return ()) >>=
return ())
         (withTexture (uniformTexture (PixelRGBA8 255 0 0 255)) (fill [LinePrim Line (V2 0.0 0.0) (V2 100.0 0.0),LinePrim Line (V2 100.0 0.0) (V2 100.0 100.0),LinePrim Line (V2 100.0 100.0) (V2 0.0 100.0),LinePrim Line (V2 0.0 100.0) (V2 0.0 0.0)] >>=
return ()) >>=
return ())
 >>= return ()
-------------------------
withTransform (Transformation {_transformA = 1.0, _transformC = 0.0, _transformE = -0.5, _transformB = 0.0, _transformD = 1.0, _transformF = -0.5}) (withTexture (sampledImageTexture <IMG>) (fill [LinePrim Line (V2 0.0 0.0) (V2 101.0 0.0),LinePrim Line (V2 101.0 0.0) (V2 101.0 101.0),LinePrim Line (V2 101.0 101.0) (V2 0.0 101.0),LinePrim Line (V2 0.0 101.0) (V2 0.0 0.0)] >>=
return ()) >>=
return ()) >>=
 return ()
-------------------------

Here is the result:

rect.png: rect clippedRect.png: clippedRect cachedRect.png: cachedRect

I think that clippedRect.png and cachedRect.png should be identical.

Twinside commented 4 years ago

This case should now be covered, but if you have more complex clipping & caching problem I'd like to hear it.

vasiliyl commented 4 years ago

Thanks, this fixes my current situation. Closing this now, I will file another issue if I encounter some other problems.