diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-14 13:08:28 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-14 13:08:52 +0100 |
commit | e3780f65e84234ce8b8aa3569a0963a1637681af (patch) | |
tree | 3a0b62b9ee3eb236f9b2c760ba23686cdd1f02c4 /examples/HelloJson.hs | |
parent | 5d869d1da1add07d926c06f6729b2ac49ed215ff (diff) |
update sample texture and add example to load a pipeline from json
Diffstat (limited to 'examples/HelloJson.hs')
-rw-r--r-- | examples/HelloJson.hs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/examples/HelloJson.hs b/examples/HelloJson.hs new file mode 100644 index 0000000..a0c8a2d --- /dev/null +++ b/examples/HelloJson.hs | |||
@@ -0,0 +1,99 @@ | |||
1 | {-# LANGUAGE PackageImports, LambdaCase, OverloadedStrings #-} | ||
2 | import "GLFW-b" Graphics.UI.GLFW as GLFW | ||
3 | import qualified Data.Map as Map | ||
4 | import qualified Data.Vector.Storable as SV | ||
5 | |||
6 | import "lambdacube-gl-ir" LambdaCube.GL as LambdaCubeGL -- renderer | ||
7 | import "lambdacube-gl-ir" LambdaCube.GL.Mesh as LambdaCubeGL | ||
8 | |||
9 | import Codec.Picture as Juicy | ||
10 | |||
11 | import Data.Aeson | ||
12 | import qualified Data.ByteString as SB | ||
13 | |||
14 | main :: IO () | ||
15 | main = do | ||
16 | Just pipelineDesc <- decodeStrict <$> SB.readFile "hello.json" | ||
17 | |||
18 | win <- initWindow "LambdaCube 3D DSL Hello World" 640 640 | ||
19 | |||
20 | -- setup render data | ||
21 | let inputSchema = makeSchema $ do | ||
22 | defObjectArray "objects" Triangles $ do | ||
23 | "position" @: Attribute_V2F | ||
24 | "uv" @: Attribute_V2F | ||
25 | defUniforms $ do | ||
26 | "time" @: Float | ||
27 | "diffuseTexture" @: FTexture2D | ||
28 | |||
29 | storage <- LambdaCubeGL.allocStorage inputSchema | ||
30 | |||
31 | -- upload geometry to GPU and add to pipeline input | ||
32 | LambdaCubeGL.uploadMeshToGPU triangleA >>= LambdaCubeGL.addMeshToObjectArray storage "objects" [] | ||
33 | LambdaCubeGL.uploadMeshToGPU triangleB >>= LambdaCubeGL.addMeshToObjectArray storage "objects" [] | ||
34 | |||
35 | -- load image and upload texture | ||
36 | Right img <- Juicy.readImage "logo.png" | ||
37 | textureData <- LambdaCubeGL.uploadTexture2DToGPU img | ||
38 | |||
39 | -- allocate GL pipeline | ||
40 | renderer <- LambdaCubeGL.allocRenderer pipelineDesc | ||
41 | LambdaCubeGL.setStorage renderer storage >>= \case -- check schema compatibility | ||
42 | Just err -> putStrLn err | ||
43 | Nothing -> loop | ||
44 | where loop = do | ||
45 | -- update graphics input | ||
46 | GLFW.getWindowSize win >>= \(w,h) -> LambdaCubeGL.setScreenSize storage (fromIntegral w) (fromIntegral h) | ||
47 | LambdaCubeGL.updateUniforms storage $ do | ||
48 | "diffuseTexture" @= return textureData | ||
49 | "time" @= do | ||
50 | Just t <- GLFW.getTime | ||
51 | return (realToFrac t :: Float) | ||
52 | -- render | ||
53 | LambdaCubeGL.renderFrame renderer | ||
54 | GLFW.swapBuffers win | ||
55 | GLFW.pollEvents | ||
56 | |||
57 | let keyIsPressed k = fmap (==KeyState'Pressed) $ GLFW.getKey win k | ||
58 | escape <- keyIsPressed Key'Escape | ||
59 | if escape then return () else loop | ||
60 | |||
61 | LambdaCubeGL.disposeRenderer renderer | ||
62 | LambdaCubeGL.disposeStorage storage | ||
63 | GLFW.destroyWindow win | ||
64 | GLFW.terminate | ||
65 | |||
66 | -- geometry data: triangles | ||
67 | triangleA :: LambdaCubeGL.Mesh | ||
68 | triangleA = Mesh | ||
69 | { mAttributes = Map.fromList | ||
70 | [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 1 (-1), V2 (-1) (-1)]) | ||
71 | , ("uv", A_V2F $ SV.fromList [V2 1 1, V2 0 1, V2 0 0]) | ||
72 | ] | ||
73 | , mPrimitive = P_Triangles | ||
74 | , mGPUData = Nothing | ||
75 | } | ||
76 | |||
77 | triangleB :: LambdaCubeGL.Mesh | ||
78 | triangleB = Mesh | ||
79 | { mAttributes = Map.fromList | ||
80 | [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 (-1) (-1), V2 (-1) 1]) | ||
81 | , ("uv", A_V2F $ SV.fromList [V2 1 1, V2 0 0, V2 1 0]) | ||
82 | ] | ||
83 | , mPrimitive = P_Triangles | ||
84 | , mGPUData = Nothing | ||
85 | } | ||
86 | |||
87 | initWindow :: String -> Int -> Int -> IO Window | ||
88 | initWindow title width height = do | ||
89 | GLFW.init | ||
90 | GLFW.defaultWindowHints | ||
91 | mapM_ GLFW.windowHint | ||
92 | [ WindowHint'ContextVersionMajor 3 | ||
93 | , WindowHint'ContextVersionMinor 3 | ||
94 | , WindowHint'OpenGLProfile OpenGLProfile'Core | ||
95 | , WindowHint'OpenGLForwardCompat True | ||
96 | ] | ||
97 | Just win <- GLFW.createWindow width height title Nothing Nothing | ||
98 | GLFW.makeContextCurrent $ Just win | ||
99 | return win | ||