~alcinnz/Mondrian

ref: 5b6139fdea49c56fce282042b58c955fa9dca6e8 Mondrian/lib/Graphics/Rendering/Rect/Backgrounds.hs -rw-r--r-- 1.3 KiB
5b6139fd — Adrian Cochrane Fix coordinatespace for texture-pixel lookup. 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Graphics.Rendering.Rect.Backgrounds(Backgrounds(..), renderBackgrounds) where

import Graphics.Rendering.Rect.CSS.Backgrounds
import Graphics.Rendering.Rect.Types
import Graphics.Rendering.Rect.Image (Texture)
import qualified Data.ByteString.Char8 as B8
import Linear (M44)

import Control.Monad.IO.Class (MonadIO(..))
import Data.Maybe (fromMaybe, listToMaybe)
import Control.Monad (forM)

baseFragmentShader = B8.pack $ unlines [
    "#version 330 core",
    "out vec4 fcolour;",
    "uniform vec4 colour;",
    "void main() { fcolour = colour; }"
  ]

imageFragmentShader = B8.pack $ unlines [
    "#version 330 core",
    "in vec2 coord;",
    "out vec4 fcolour;",
    "uniform sampler2D image;",
    "void main() { fcolour = texture(image, coord/textureSize(image, 0)); }"
  ]

renderBackgrounds :: (MonadIO m, MonadIO n) =>
    n (Backgrounds Texture -> Rects -> M44 Float -> m ())
renderBackgrounds = do
    base <- renderRectWith baseFragmentShader ["colour"]
    layer <- renderRectWith imageFragmentShader []
    return $ \self a b -> do
        base [] [c $ background self] (headDef borderBox $ clip self) a b
        let layers = image self `zip` (clip self ++ repeat borderBox)
        forM layers $ \(img0, clip0) ->
            layer [img0] [] clip0 a b
        return ()

headDef def = fromMaybe def . listToMaybe