summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/Hello.hs8
-rw-r--r--examples/HelloJson.hs99
-rw-r--r--examples/Panels_Diffuse.pngbin490310 -> 0 bytes
-rw-r--r--examples/hello.json1
-rw-r--r--examples/hello.lc8
-rw-r--r--examples/logo.pngbin0 -> 126891 bytes
6 files changed, 108 insertions, 8 deletions
diff --git a/examples/Hello.hs b/examples/Hello.hs
index 3cc8c63..1cba1b5 100644
--- a/examples/Hello.hs
+++ b/examples/Hello.hs
@@ -17,7 +17,7 @@ main = do
17 Left err -> fail $ "compile error:\n" ++ err 17 Left err -> fail $ "compile error:\n" ++ err
18 Right pd -> return pd 18 Right pd -> return pd
19 19
20 win <- initWindow "LambdaCube 3D DSL Hello World" 640 480 20 win <- initWindow "LambdaCube 3D DSL Hello World" 640 640
21 21
22 -- setup render data 22 -- setup render data
23 let inputSchema = makeSchema $ do 23 let inputSchema = makeSchema $ do
@@ -35,7 +35,7 @@ main = do
35 LambdaCubeGL.uploadMeshToGPU triangleB >>= LambdaCubeGL.addMeshToObjectArray storage "objects" [] 35 LambdaCubeGL.uploadMeshToGPU triangleB >>= LambdaCubeGL.addMeshToObjectArray storage "objects" []
36 36
37 -- load image and upload texture 37 -- load image and upload texture
38 Right img <- Juicy.readImage "Panels_Diffuse.png" 38 Right img <- Juicy.readImage "logo.png"
39 textureData <- LambdaCubeGL.uploadTexture2DToGPU img 39 textureData <- LambdaCubeGL.uploadTexture2DToGPU img
40 40
41 -- allocate GL pipeline 41 -- allocate GL pipeline
@@ -70,7 +70,7 @@ triangleA :: LambdaCubeGL.Mesh
70triangleA = Mesh 70triangleA = Mesh
71 { mAttributes = Map.fromList 71 { mAttributes = Map.fromList
72 [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 1 (-1), V2 (-1) (-1)]) 72 [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 1 (-1), V2 (-1) (-1)])
73 , ("uv", A_V2F $ SV.fromList [V2 0 0, V2 0 1, V2 1 1]) 73 , ("uv", A_V2F $ SV.fromList [V2 1 1, V2 0 1, V2 0 0])
74 ] 74 ]
75 , mPrimitive = P_Triangles 75 , mPrimitive = P_Triangles
76 , mGPUData = Nothing 76 , mGPUData = Nothing
@@ -80,7 +80,7 @@ triangleB :: LambdaCubeGL.Mesh
80triangleB = Mesh 80triangleB = Mesh
81 { mAttributes = Map.fromList 81 { mAttributes = Map.fromList
82 [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 (-1) (-1), V2 (-1) 1]) 82 [ ("position", A_V2F $ SV.fromList [V2 1 1, V2 (-1) (-1), V2 (-1) 1])
83 , ("uv", A_V2F $ SV.fromList [V2 0 0, V2 1 1, V2 1 0]) 83 , ("uv", A_V2F $ SV.fromList [V2 1 1, V2 0 0, V2 1 0])
84 ] 84 ]
85 , mPrimitive = P_Triangles 85 , mPrimitive = P_Triangles
86 , mGPUData = Nothing 86 , mGPUData = Nothing
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 #-}
2import "GLFW-b" Graphics.UI.GLFW as GLFW
3import qualified Data.Map as Map
4import qualified Data.Vector.Storable as SV
5
6import "lambdacube-gl-ir" LambdaCube.GL as LambdaCubeGL -- renderer
7import "lambdacube-gl-ir" LambdaCube.GL.Mesh as LambdaCubeGL
8
9import Codec.Picture as Juicy
10
11import Data.Aeson
12import qualified Data.ByteString as SB
13
14main :: IO ()
15main = 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
67triangleA :: LambdaCubeGL.Mesh
68triangleA = 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
77triangleB :: LambdaCubeGL.Mesh
78triangleB = 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
87initWindow :: String -> Int -> Int -> IO Window
88initWindow 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
diff --git a/examples/Panels_Diffuse.png b/examples/Panels_Diffuse.png
deleted file mode 100644
index 15b242c..0000000
--- a/examples/Panels_Diffuse.png
+++ /dev/null
Binary files differ
diff --git a/examples/hello.json b/examples/hello.json
new file mode 100644
index 0000000..797bfd0
--- /dev/null
+++ b/examples/hello.json
@@ -0,0 +1 @@
{"textures":[],"commands":[{"tag":"SetRenderTarget","arg0":0},{"tag":"ClearRenderTarget","arg0":[{"tag":"ClearImage","clearValue":{"tag":"VV4F","arg0":{"w":1,"z":0.4,"x":0.0,"y":0.0}},"imageSemantic":{"tag":"Color"}}]},{"tag":"SetProgram","arg0":0},{"tag":"SetSamplerUniform","arg0":"diffuseTexture","arg1":0},{"tag":"SetRasterContext","arg0":{"arg3":{"tag":"LastVertex"},"tag":"TriangleCtx","arg0":{"tag":"CullNone"},"arg1":{"tag":"PolygonFill"},"arg2":{"tag":"NoOffset"}}},{"tag":"SetAccumulationContext","arg0":{"accViewportName":null,"tag":"AccumulationContext","accOperations":[{"tag":"ColorOp","arg0":{"tag":"NoBlending"},"arg1":{"tag":"VV4B","arg0":{"w":true,"z":true,"x":true,"y":true}}}]}},{"tag":"RenderSlot","arg0":0}],"slots":[{"tag":"Slot","slotPrimitive":{"tag":"Triangles"},"slotStreams":{"uv":{"tag":"V2F"},"position":{"tag":"V2F"}},"slotName":"objects","slotUniforms":{"time":{"tag":"Float"},"diffuseTexture":{"tag":"FTexture2D"}},"slotPrograms":[0]}],"programs":[{"programInTextures":{"diffuseTexture":{"tag":"FTexture2D"}},"tag":"Program","programOutput":[{"tag":"Parameter","ty":{"tag":"V4F"},"name":"f0"}],"programStreams":{"q1":{"tag":"Parameter","ty":{"tag":"V2F"},"name":"position"},"r1":{"tag":"Parameter","ty":{"tag":"V2F"},"name":"uv"}},"fragmentShader":"#version 330 core\nvec4 texture2D(sampler2D s, vec2 uv){return texture(s,uv);}\nuniform sampler2D diffuseTexture ;\nsmooth in vec2 v0 ;\nout vec4 f0 ;\nvoid main() {\nf0 = texture2D ( diffuseTexture,v0 );\n}\n","vertexShader":"#version 330 core\nvec4 texture2D(sampler2D s, vec2 uv){return texture(s,uv);}\nuniform float time ;\nin vec2 q1 ;\nin vec2 r1 ;\nsmooth out vec2 v0 ;\nvoid main() {\nv0 = r1;\ngl_Position = ( mat4 ( vec4 ( cos ( time ),sin ( time ),0.0,0.0 ),vec4 ( ( 0.0 ) - ( sin ( time ) ),cos ( time ),0.0,0.0 ),vec4 ( 0.0,0.0,1.0,0.0 ),vec4 ( 0.0,0.0,0.0,1.0 ) ) ) * ( vec4 ( ( q1 ).x,( q1 ).y,-1.0,1.0 ) );\ngl_PointSize = 1.0;\n}\n","geometryShader":null,"programUniforms":{"time":{"tag":"Float"},"diffuseTexture":{"tag":"FTexture2D"}}}],"samplers":[],"tag":"Pipeline","backend":{"tag":"OpenGL33"},"streams":[],"targets":[{"tag":"RenderTarget","renderTargets":[{"tag":"TargetItem","targetSemantic":{"tag":"Color"},"targetRef":{"tag":"Framebuffer","arg0":{"tag":"Color"}}}]}]} \ No newline at end of file
diff --git a/examples/hello.lc b/examples/hello.lc
index 4254a69..20f8292 100644
--- a/examples/hello.lc
+++ b/examples/hello.lc
@@ -3,11 +3,11 @@ sampler = Sampler PointFilter MirroredRepeat (Texture2DSlot "diffuseTexture")
3main = let 3main = let
4 emptyFB = FrameBuffer (colorImage1 (V4 0.0 0.0 0.4 1.0)) 4 emptyFB = FrameBuffer (colorImage1 (V4 0.0 0.0 0.4 1.0))
5 rasterCtx = TriangleCtx CullNone PolygonFill NoOffset LastVertex 5 rasterCtx = TriangleCtx CullNone PolygonFill NoOffset LastVertex
6 fragmentCtx = AccumulationContext (ColorOp NoBlending (V4 True True True True)) 6 fragmentCtx = (ColorOp NoBlending (V4 True True True True))
7 vertexShader (p,uv) = VertexOut (rotMatrixZ time *. (V4 p%x p%y (-1) 1)) 1.0 () (Smooth uv) 7 vertexShader (p,uv) = VertexOut (rotMatrixZ time *. (V4 p%x p%y (-1) 1)) 1.0 () (Smooth uv)
8 vertexStream = Fetch "objects" Triangles (Attribute "position" :: Vec 2 Float, Attribute "uv" :: Vec 2 Float) 8 vertexStream = Fetch @Triangle "objects" (Attribute "position" :: Vec 2 Float, Attribute "uv" :: Vec 2 Float)
9 primitiveStream = Transform vertexShader vertexStream 9 primitiveStream = Transform vertexShader vertexStream
10 fragmentStream = Rasterize rasterCtx primitiveStream 10 fragmentStream = Rasterize rasterCtx primitiveStream
11 fragmentShader uv = FragmentOut $ texture2D sampler uv 11 fragmentShader = FragmentShader $ \uv -> texture2D sampler uv
12 frame = Accumulate fragmentCtx PassAll fragmentShader fragmentStream emptyFB 12 frame = accumulate fragmentCtx PassAll fragmentShader fragmentStream emptyFB
13 in ScreenOut frame 13 in ScreenOut frame
diff --git a/examples/logo.png b/examples/logo.png
new file mode 100644
index 0000000..4471676
--- /dev/null
+++ b/examples/logo.png
Binary files differ