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
229
230
231
232
233
234
235
236
237
238
239
240
241
|
{-# LANGUAGE OverloadedStrings, PackageImports, MonadComprehensions #-}
module SampleIR where
import "GLFW-b" Graphics.UI.GLFW as GLFW
import Data.Monoid
import Control.Monad
import Control.Applicative
import Data.Vect
import qualified Data.Trie as T
import qualified Data.Vector.Storable as SV
import qualified Data.Vector as V
import Text.Show.Pretty
import Backend.GL as GL
import Backend.GL.Mesh
import IR as IR
import System.Environment
import Driver (compileMain)
-- Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
-- A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
g_vertex_buffer_data =
[ ( 1.0, 1.0,-1.0)
, ( 1.0,-1.0,-1.0)
, (-1.0,-1.0,-1.0)
, ( 1.0, 1.0,-1.0)
, (-1.0,-1.0,-1.0)
, (-1.0, 1.0,-1.0)
, ( 1.0, 1.0,-1.0)
, ( 1.0, 1.0, 1.0)
, ( 1.0,-1.0, 1.0)
, ( 1.0, 1.0,-1.0)
, ( 1.0,-1.0, 1.0)
, ( 1.0,-1.0,-1.0)
, ( 1.0, 1.0, 1.0)
, (-1.0,-1.0, 1.0)
, ( 1.0,-1.0, 1.0)
, ( 1.0, 1.0, 1.0)
, (-1.0, 1.0, 1.0)
, (-1.0,-1.0, 1.0)
, (-1.0, 1.0, 1.0)
, (-1.0,-1.0,-1.0)
, (-1.0,-1.0, 1.0)
, (-1.0, 1.0, 1.0)
, (-1.0, 1.0,-1.0)
, (-1.0,-1.0,-1.0)
, ( 1.0, 1.0,-1.0)
, (-1.0, 1.0,-1.0)
, (-1.0, 1.0, 1.0)
, ( 1.0, 1.0,-1.0)
, (-1.0, 1.0, 1.0)
, ( 1.0, 1.0, 1.0)
, ( 1.0, 1.0,-1.0)
, ( 1.0, 1.0, 1.0)
, (-1.0, 1.0, 1.0)
, ( 1.0, 1.0,-1.0)
, (-1.0, 1.0, 1.0)
, (-1.0, 1.0,-1.0)
]
-- Two UV coordinatesfor each vertex. They were created with Blender.
g_uv_buffer_data =
[ (0.0, 0.0)
, (0.0, 1.0)
, (1.0, 1.0)
, (0.0, 0.0)
, (1.0, 1.0)
, (1.0, 0.0)
, (0.0, 0.0)
, (1.0, 0.0)
, (1.0, 1.0)
, (0.0, 0.0)
, (1.0, 1.0)
, (0.0, 1.0)
, (1.0, 0.0)
, (0.0, 1.0)
, (1.0, 1.0)
, (1.0, 0.0)
, (0.0, 0.0)
, (0.0, 1.0)
, (0.0, 0.0)
, (1.0, 1.0)
, (0.0, 1.0)
, (0.0, 0.0)
, (1.0, 0.0)
, (1.0, 1.0)
, (0.0, 0.0)
, (1.0, 0.0)
, (1.0, 1.0)
, (0.0, 0.0)
, (1.0, 1.0)
, (0.0, 1.0)
, (0.0, 0.0)
, (0.0, 1.0)
, (1.0, 1.0)
, (0.0, 0.0)
, (1.0, 1.0)
, (1.0, 0.0)
]
myCube :: Mesh
myCube = Mesh
{ mAttributes = T.fromList
[ ("position4", A_V4F $ SV.fromList [V4 x y z 1 | (x,y,z) <- g_vertex_buffer_data])
, ("vertexUV", A_V2F $ SV.fromList [V2 u v | (u,v) <- g_uv_buffer_data])
]
, mPrimitive = P_Triangles
, mGPUData = Nothing
}
main :: IO ()
main = do
win <- initWindow "LambdaCube 3D DSL Sample" 1024 768
let keyIsPressed k = fmap (==KeyState'Pressed) $ getKey win k
n <- getArgs
let srcName = case n of
[fn] -> fn
_ -> "gfx03"
let inputSchema =
PipelineSchema
{ GL.slots = T.fromList [("stream",SlotSchema Triangles $ T.fromList [("position",TV3F),("normal",TV3F),("UVTex",TV2F)])
,("stream4",SlotSchema Triangles $ T.fromList [("position4",TV4F),("vertexUV",TV2F)])
]
, uniforms = T.fromList [("MVP",M44F),("MVP2",M44F)]
}
pplInput <- mkGLPipelineInput inputSchema
gpuCube <- compileMesh myCube
gpuMonkey <- loadMesh "Monkey.lcmesh"
addMesh pplInput "stream4" gpuCube []
addMesh pplInput "stream" gpuMonkey []
let setup = do
pplRes <- compileMain "../lambdacube-dsl/tests/accept" srcName
case pplRes of
Left err -> putStrLn ("error: " ++ err) >> return Nothing
Right ppl -> do
putStrLn $ ppShow ppl
renderer <- allocPipeline ppl
setPipelineInput renderer (Just pplInput)
sortSlotObjects pplInput
putStrLn "reloaded"
return $ Just renderer
let cm' = fromProjective (lookat (Vec3 4 0.5 (-0.6)) (Vec3 0 0 0) (Vec3 0 1 0))
cm = fromProjective (lookat (Vec3 3 1.3 0.3) (Vec3 0 0 0) (Vec3 0 1 0))
loop renderer = do
(w,h) <- getWindowSize win
let uniformMap = uniformSetter pplInput
texture = uniformFTexture2D "myTextureSampler" uniformMap
mvp = uniformM44F "MVP" uniformMap
mvp' = uniformM44F "MVP2" uniformMap
pm = perspective 0.1 100 (pi/4) (fromIntegral w / fromIntegral h)
setScreenSize pplInput (fromIntegral w) (fromIntegral h)
Just t <- getTime
let angle = pi / 24 * realToFrac t
mm = fromProjective $ rotationEuler $ Vec3 angle 0 0
mvp $! mat4ToM44F $! mm .*. cm .*. pm
mvp' $! mat4ToM44F $! mm .*. cm' .*. pm
renderPipeline renderer
swapBuffers win >> pollEvents
k <- keyIsPressed Key'Escape
reload <- keyIsPressed Key'R
rend' <- if not reload then return renderer else do
r <- setup
case r of
Nothing -> return renderer
Just a -> do
disposePipeline renderer
return a
when k $ disposePipeline rend'
unless k $ loop rend'
r <- setup
case r of
Just a -> loop a
Nothing -> return ()
destroyWindow win
terminate
vec4ToV4F :: Vec4 -> V4F
vec4ToV4F (Vec4 x y z w) = V4 x y z w
mat4ToM44F :: Mat4 -> M44F
mat4ToM44F (Mat4 a b c d) = V4 (vec4ToV4F a) (vec4ToV4F b) (vec4ToV4F c) (vec4ToV4F d)
initWindow :: String -> Int -> Int -> IO Window
initWindow title width height = do
GLFW.init
defaultWindowHints
mapM_ windowHint
[ WindowHint'ContextVersionMajor 3
, WindowHint'ContextVersionMinor 3
, WindowHint'OpenGLProfile OpenGLProfile'Core
, WindowHint'OpenGLForwardCompat True
]
Just win <- createWindow width height title Nothing Nothing
makeContextCurrent $ Just win
return win
-- | Perspective transformation matrix in row major order.
perspective :: Float -- ^ Near plane clipping distance (always positive).
-> Float -- ^ Far plane clipping distance (always positive).
-> Float -- ^ Field of view of the y axis, in radians.
-> Float -- ^ Aspect ratio, i.e. screen's width\/height.
-> Mat4
perspective n f fovy aspect = transpose $
Mat4 (Vec4 (2*n/(r-l)) 0 (-(r+l)/(r-l)) 0)
(Vec4 0 (2*n/(t-b)) ((t+b)/(t-b)) 0)
(Vec4 0 0 (-(f+n)/(f-n)) (-2*f*n/(f-n)))
(Vec4 0 0 (-1) 0)
where
t = n*tan(fovy/2)
b = -t
r = aspect*t
l = -r
-- | Pure orientation matrix defined by Euler angles.
rotationEuler :: Vec3 -> Proj4
rotationEuler (Vec3 a b c) = orthogonal $ toOrthoUnsafe $ rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c
-- | Camera transformation matrix.
lookat :: Vec3 -- ^ Camera position.
-> Vec3 -- ^ Target position.
-> Vec3 -- ^ Upward direction.
-> Proj4
lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r)
where
w = normalize $ pos &- target
u = normalize $ up &^ w
v = w &^ u
r = transpose $ Mat3 u v w
|