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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE PatternSynonyms #-}
module MeshSketch where
import Control.Monad
import qualified Data.Aeson as JSON
import qualified Data.ByteString as SB
import Data.Coerce
import Data.Functor
import qualified Data.Map as Map
import Data.IORef
import Foreign.C.Types
import GI.Gdk
import GI.GObject.Functions
import GI.Gtk hiding (IsWindow,windowFullscreen,windowUnfullscreen)
import Numeric.LinearAlgebra hiding ((<>))
import LambdaCube.GL as LC
-- import LambdaCube.GL.Type as LC
import LambdaCube.IR
import System.IO.Error
import CubeMap
import LambdaCube.GL.HMatrix ()
import LambdaCube.Gtk
import Matrix
data MeshMaker = MeshMaker
{ mmWidget :: GLArea
, mmRealized :: IORef (Maybe State)
}
data Camera = Camera
{ camHeightAngle :: Float
, camTarget :: Vector Float
, camDirection :: Vector Float
, camDistance :: Float
, camWidth :: Float
, camHeight :: Float
, camUp :: Vector Float
, camWorldToScreen :: Maybe (Matrix Float)
, camScreenToWorld :: Maybe (Matrix Float)
}
data State = State
{ stCamera :: IORef Camera
, stSkyboxes :: Skyboxes
, stSkybox :: IORef Int
, stFullscreen :: IO ()
, stPipeline :: Pipeline
, stSchema :: PipelineSchema
, stStorage :: GLStorage
, stRenderer :: GLRenderer
}
initCamera :: Camera
initCamera = Camera
{ camHeightAngle = pi/6
, camTarget = fromList [0,0,0]
, camDirection = fromList [0,0,-1]
, camDistance = 10
, camWidth = 0
, camHeight = 0
, camUp = fromList [0,1,0]
, camWorldToScreen = Nothing
, camScreenToWorld = Nothing
}
viewProjection :: Camera -> (Camera,Matrix Float)
viewProjection c
| Just m <- camWorldToScreen c = (c,m)
| otherwise = (c { camWorldToScreen = Just m' }, m')
where
m' = proj <> cam
cam = lookat pos (camTarget c) (camUp c)
pos = camTarget c - scale (camDistance c) (camDirection c)
proj = perspective 0.1 100 (camHeightAngle c) (camWidth c / camHeight c)
new :: IO GLArea
new = do
w <- gLAreaNew
ref <- newIORef Nothing
let mm = MeshMaker w ref
-- _ <- on w #createContext $ onCreateContext mm
_ <- on w #realize $ onRealize mm
_ <- on w #unrealize $ onUnrealize mm
-- _ <- on w #destroy $ onDestroy mm
return w
loadPipeline :: IO (Either String (PipelineSchema,Pipeline))
loadPipeline = do
pipelineDesc <- do
maybe (Left "Unable to parse meshsketch.json") Right . JSON.decodeStrict <$> SB.readFile "meshsketch.json"
`catchIOError` \e -> return $ Left (show e)
-- setup render data
let inputSchema = makeSchema $ do
defObjectArray "skypoints" Points $ do
"position" @: Attribute_V3F
defUniforms $ do
"Cam" @: M44F
"Skybox" @: FTextureCube
return $ (,) inputSchema <$> pipelineDesc
onRealize :: MeshMaker -> IO ()
onRealize mm@(MeshMaker w ref) = do
putStrLn "realize!"
readIORef ref >>= \case
Just st -> onUnrealize mm -- Shouldn't happen.
Nothing -> return ()
set w [ #canFocus := True ] -- For keyboard events.
widgetAddEvents w
[ EventMaskPointerMotionMask
, EventMaskButtonPressMask
, EventMaskButtonReleaseMask
, EventMaskTouchMask
, EventMaskScrollMask
, EventMaskKeyPressMask -- , EventMaskKeyReleaseMask
]
Right (schema,pipeline) <- loadPipeline
gLAreaMakeCurrent w
storage <- allocStorage schema
-- upload state
renderer <- allocRenderer pipeline
compat <- setStorage renderer storage -- check schema compatibility
cam <- newIORef initCamera
skyboxes <- loadSkyboxes
skybox <- newIORef 0
Just pwidget <- get w #parent
Just parent <- get pwidget #window
toggle <- mkFullscreenToggle parent
let st = State
{ stCamera = cam
, stSkyboxes = skyboxes
, stSkybox = skybox
, stFullscreen = toggle
, stPipeline = pipeline
, stSchema = schema
, stStorage = storage
, stRenderer = renderer
}
_ <- on w #event $ onEvent w st
_ <- on w #render $ onRender w st
writeIORef ref $ Just st
onUnrealize :: MeshMaker -> IO ()
onUnrealize (MeshMaker w ref) = do
putStrLn "unrealize!"
readIORef ref >>= \case
Just st -> do
-- signalHandlerDisconnect w (sigRender st)
-- signalHandlerDisconnect w (sigEvent st)
return ()
Nothing -> return () -- Shouldn't happen.
writeIORef ref Nothing
onRender :: w -> State -> GLContext -> IO Bool
onRender w st gl = do
putStrLn "render"
mat_vp <- atomicModifyIORef' (stCamera st) viewProjection
r <- fixupRenderTarget (stRenderer st)
{-
let ks = Map.keys $ uniformSetup (stStorage st)
us = uniforms (stSchema st)
print (us,ks)
-}
LC.updateUniforms (stStorage st) $ do
"Cam" @= return mat_vp
-- todo Skybox texture
LC.renderFrame r
return True
onEvent :: w -> State -> Event -> IO Bool
onEvent w st ev = do
msrc <- eventGetSourceDevice ev
inputSource <- forM msrc $ \src -> do
src <- get src #inputSource
return src
etype <- get ev #type
-- putStrLn $ "onEvent! " ++ show (etype,inputSource)
let put x = putStrLn (show inputSource ++ " " ++ show x)
case etype of
EventTypeMotionNotify -> do
mev <- get ev #motion
x <- get mev #x
y <- get mev #y
put (x,y)
return ()
EventTypeScroll -> do
sev <- get ev #scroll
d <- get sev #direction
put d
return ()
EventTypeKeyPress -> do
kev <- get ev #key
val <- get kev #keyval <&> \k -> if k > 0x5A then k - 0x20 else k
case val of
KEY_N -> do
modifyIORef' (stSkybox st) $ \n -> (n + 1) `mod` (skyboxCount $ stSkyboxes st)
idx <- readIORef (stSkybox st)
put (skyboxNames (stSkyboxes st) !! idx)
return ()
KEY_F -> do
put 'F'
stFullscreen st
_ -> return ()
e -> return ()
return False
mkFullscreenToggle :: IsWindow a => a -> IO (IO ())
mkFullscreenToggle w = do
full <- newIORef False
return $ do
b <- atomicModifyIORef' full $ \b -> (not b, not b)
if b then windowFullscreen w
else windowUnfullscreen w
|