summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-04-26 20:24:50 -0400
committerJoe Crayne <joe@jerkface.net>2019-04-26 20:24:50 -0400
commitcffec1ebd78808154e98b6f3ec46578372a1331f (patch)
tree2a1f4a6b703dc7f1a3c70b9b91862971da41f4e3
parent3ab58407dbdb5d6b595313315a92864a9b76706d (diff)
Lambdacube code for MeshSketch.
-rw-r--r--MeshSketch.hs47
-rw-r--r--meshsketch.lc22
2 files changed, 67 insertions, 2 deletions
diff --git a/MeshSketch.hs b/MeshSketch.hs
index 331d435..30d14b7 100644
--- a/MeshSketch.hs
+++ b/MeshSketch.hs
@@ -4,6 +4,8 @@
4module MeshSketch where 4module MeshSketch where
5 5
6import Control.Monad 6import Control.Monad
7import qualified Data.Aeson as JSON
8import qualified Data.ByteString as SB
7import Data.Coerce 9import Data.Coerce
8import Data.Functor 10import Data.Functor
9import Data.IORef 11import Data.IORef
@@ -12,7 +14,11 @@ import GI.Gdk
12import GI.GObject.Functions 14import GI.GObject.Functions
13import GI.Gtk hiding (IsWindow,windowFullscreen,windowUnfullscreen) 15import GI.Gtk hiding (IsWindow,windowFullscreen,windowUnfullscreen)
14import Numeric.LinearAlgebra 16import Numeric.LinearAlgebra
17import LambdaCube.GL as LC
18import LambdaCube.IR
19import System.IO.Error
15 20
21import LambdaCube.Gtk
16import CubeMap 22import CubeMap
17 23
18data MeshMaker = MeshMaker 24data MeshMaker = MeshMaker
@@ -36,6 +42,10 @@ data State = State
36 , stSkyboxes :: Skyboxes 42 , stSkyboxes :: Skyboxes
37 , stSkybox :: IORef Int 43 , stSkybox :: IORef Int
38 , stFullscreen :: IO () 44 , stFullscreen :: IO ()
45 , stPipeline :: Pipeline
46 , stSchema :: PipelineSchema
47 , stStorage :: GLStorage
48 , stRenderer :: GLRenderer
39 } 49 }
40 50
41initCamera :: Camera 51initCamera :: Camera
@@ -61,6 +71,18 @@ new = do
61 -- _ <- on w #destroy $ onDestroy mm 71 -- _ <- on w #destroy $ onDestroy mm
62 return w 72 return w
63 73
74loadPipeline :: IO (Either String (PipelineSchema,Pipeline))
75loadPipeline = do
76 pipelineDesc <- do
77 maybe (Left "Unable to parse meshsketch.json") Right . JSON.decodeStrict <$> SB.readFile "meshsketch.json"
78 `catchIOError` \e -> return $ Left (show e)
79 -- setup render data
80 let inputSchema = makeSchema $ do
81 defObjectArray "skypoints" Points $ do
82 "position" @: Attribute_V3F
83 return $ (,) inputSchema <$> pipelineDesc
84
85
64onRealize :: MeshMaker -> IO () 86onRealize :: MeshMaker -> IO ()
65onRealize mm@(MeshMaker w ref) = do 87onRealize mm@(MeshMaker w ref) = do
66 putStrLn "realize!" 88 putStrLn "realize!"
@@ -76,6 +98,16 @@ onRealize mm@(MeshMaker w ref) = do
76 , EventMaskScrollMask 98 , EventMaskScrollMask
77 , EventMaskKeyPressMask -- , EventMaskKeyReleaseMask 99 , EventMaskKeyPressMask -- , EventMaskKeyReleaseMask
78 ] 100 ]
101
102 Right (schema,pipeline) <- loadPipeline
103
104 gLAreaMakeCurrent w
105
106 storage <- allocStorage schema
107 -- upload state
108 renderer <- allocRenderer pipeline
109 compat <- setStorage renderer storage -- check schema compatibility
110
79 cam <- newIORef initCamera 111 cam <- newIORef initCamera
80 skyboxes <- loadSkyboxes 112 skyboxes <- loadSkyboxes
81 skybox <- newIORef 0 113 skybox <- newIORef 0
@@ -87,9 +119,14 @@ onRealize mm@(MeshMaker w ref) = do
87 , stSkyboxes = skyboxes 119 , stSkyboxes = skyboxes
88 , stSkybox = skybox 120 , stSkybox = skybox
89 , stFullscreen = toggle 121 , stFullscreen = toggle
122 , stPipeline = pipeline
123 , stSchema = schema
124 , stStorage = storage
125 , stRenderer = renderer
90 } 126 }
91 127
92 _ <- on w #event $ onEvent w st 128 _ <- on w #event $ onEvent w st
129 _ <- on w #render $ onRender w st
93 writeIORef ref $ Just st 130 writeIORef ref $ Just st
94 131
95onUnrealize :: MeshMaker -> IO () 132onUnrealize :: MeshMaker -> IO ()
@@ -97,13 +134,19 @@ onUnrealize (MeshMaker w ref) = do
97 putStrLn "unrealize!" 134 putStrLn "unrealize!"
98 readIORef ref >>= \case 135 readIORef ref >>= \case
99 Just st -> do 136 Just st -> do
137 -- signalHandlerDisconnect w (sigRender st)
138 -- signalHandlerDisconnect w (sigEvent st)
100 return () 139 return ()
101 Nothing -> return () -- Shouldn't happen. 140 Nothing -> return () -- Shouldn't happen.
102 writeIORef ref Nothing 141 writeIORef ref Nothing
103 142
104 143
105onRender :: MeshMaker -> GLContext -> IO Bool 144onRender :: w -> State -> GLContext -> IO Bool
106onRender (MeshMaker w ref) gl = do 145onRender w st gl = do
146 putStrLn "render"
147 r <- fixupRenderTarget (stRenderer st)
148 -- lcSetUniforms lc gl s x
149 LC.renderFrame r
107 return True 150 return True
108 151
109onEvent :: w -> State -> Event -> IO Bool 152onEvent :: w -> State -> Event -> IO Bool
diff --git a/meshsketch.lc b/meshsketch.lc
new file mode 100644
index 0000000..9ca422d
--- /dev/null
+++ b/meshsketch.lc
@@ -0,0 +1,22 @@
1makeFrame (cam :: Mat 4 4 Float)
2 (skybox :: Texture)
3 (skypoints :: PrimitiveStream Point ((Vec 3 Float)))
4
5 = imageFrame (emptyDepthImage 1, emptyColorImage (V4 0 0 0.4 1))
6 `overlay`
7 skypoints
8 & mapPrimitives (\((p)) -> ( cam *. V4 p%x p%y p%z 1, V3 p%x (-p%y) (-p%z) ) )
9 & rasterizePrimitives (PointCtx (PointSize 1.0)
10 1.0 -- GL_POINT_FADE_THRESHOLD
11 UpperLeft) -- texture y increases downward
12 ((Smooth))
13 & mapFragments (\((d)) -> ((texture2D (Sampler PointFilter MirroredRepeat skybox) d%xy )))
14 & accumulateWith (DepthOp Always True, ColorOp NoBlending (V4 True True True True))
15
16
17textureCubeSlot s = Texture2DSlot s
18
19main = renderFrame $
20 makeFrame (Uniform "Cam")
21 (textureCubeSlot "Skybox")
22 (fetch "skypoints" ((Attribute "position")))