summaryrefslogtreecommitdiff
path: root/examples/pickInt.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pickInt.hs')
-rw-r--r--examples/pickInt.hs192
1 files changed, 192 insertions, 0 deletions
diff --git a/examples/pickInt.hs b/examples/pickInt.hs
new file mode 100644
index 0000000..2703e91
--- /dev/null
+++ b/examples/pickInt.hs
@@ -0,0 +1,192 @@
1{-# LANGUAGE PackageImports, LambdaCase, OverloadedStrings, StandaloneDeriving, ViewPatterns #-}
2import Control.Monad
3import Data.Aeson
4import Data.Vect (Mat4(..), Vec3(..), Vec4(..))
5import Graphics.GL.Core33 as GL
6import LambdaCube.GL as LambdaCubeGL
7import LambdaCube.GL.Mesh as LambdaCubeGL
8import Text.Printf
9import "GLFW-b" Graphics.UI.GLFW as GLFW
10import qualified Data.ByteString as SB
11import qualified Data.Map as Map
12import qualified Data.Vect as Vc
13import qualified Data.Vector as V
14import qualified Foreign as F
15import qualified Foreign.C.Types as F
16import qualified LambdaCube.GL.Type as LC
17import qualified LambdaCube.Linear as LCLin
18
19----------------------------------------------------
20-- See: http://lambdacube3d.com/getting-started
21----------------------------------------------------
22
23screenDim :: (Int, Int)
24screenDim = (800, 600)
25(screenW, screenH) = screenDim
26
27main :: IO ()
28main = do
29 Just pipePickDesc <- decodeStrict <$> SB.readFile "pickInt.json"
30 Just pipeDrawDesc <- decodeStrict <$> SB.readFile "pickIntDraw.json"
31
32 win <- initWindow "LambdaCube 3D integer picking" 800 600
33
34 -- setup render data
35 let inputSchema = makeSchema $ do
36 defObjectArray "objects" Triangles $ do
37 "position" @: Attribute_V2F
38 "id" @: Attribute_Int
39 "color" @: Attribute_V4F
40 defUniforms $ do
41 "viewProj" @: M44F
42
43 storage <- LambdaCubeGL.allocStorage inputSchema
44
45 -- upload geometry to GPU and add to pipeline input
46 LambdaCubeGL.uploadMeshToGPU triangleA >>= LambdaCubeGL.addMeshToObjectArray storage "objects" []
47 LambdaCubeGL.uploadMeshToGPU triangleB >>= LambdaCubeGL.addMeshToObjectArray storage "objects" []
48
49 -- allocate GL pipeline
50 pipePick <- LambdaCubeGL.allocRenderer pipePickDesc
51 pipeDraw <- LambdaCubeGL.allocRenderer pipeDrawDesc
52 errPick <- LambdaCubeGL.setStorage pipePick storage
53 errDraw <- LambdaCubeGL.setStorage pipeDraw storage
54 case (errPick, errDraw) of -- check schema compatibility
55 (Just err, _) -> putStrLn err
56 (_, Just err) -> putStrLn err
57 (Nothing, Nothing) -> loop
58 where loop = do
59 -- update graphics input
60 GLFW.getWindowSize win >>= \(w,h) -> LambdaCubeGL.setScreenSize storage (fromIntegral w) (fromIntegral h)
61 LambdaCubeGL.updateUniforms storage $ do
62 let (x, y) = (,) 0 0
63 cvpos = Vec3 x (-y) 0
64 toScreen = screenM screenW screenH
65 "viewProj" @= pure (mat4ToM44F $! (Vc.fromProjective $! Vc.translation cvpos) Vc..*. toScreen)
66
67 (curX, curY) <- GLFW.getCursorPos win
68 let pickPoints = -- should be fb 0 fb 1 (pick)
69 [ (clamp curX 800, clamp curY 600)
70 , (0, 0) -- black 0
71 , (200, 200) -- ..blue, ffff0000 2
72 , (600, 400) -- ..red, ff0000ff 1
73 ] :: [(Int, Int)]
74 clamp v m = min (pred m) $ max 0 (floor v)
75
76 -- render to render texture
77 LambdaCubeGL.renderFrame pipePick
78 case LC.glOutputs pipePick of
79 [LC.GLOutputRenderTexture (fromIntegral -> fbo) _rendTex] -> do
80 rtexPicks <- collectPicks fbo pickPoints
81 printPicks pickPoints rtexPicks
82 x -> error $ "Unexpected outputs: " <> show x
83
84 -- render to framebuffer & pick
85 LambdaCubeGL.renderFrame pipeDraw
86 colorPicks <- collectPicks 0 pickPoints
87 printPicks pickPoints colorPicks
88
89 GLFW.swapBuffers win
90 GLFW.pollEvents
91
92 let keyIsPressed k = fmap (==KeyState'Pressed) $ GLFW.getKey win k
93 escape <- keyIsPressed Key'Escape
94 if escape then return () else loop
95 collectPicks :: Int -> [(Int, Int)] -> IO [Int]
96 collectPicks fb picks =
97 forM picks $ (fromIntegral <$>) . pickFrameBuffer fb screenDim
98 printPicks pickPoints colorPicks = do
99 forM_ (zip pickPoints colorPicks) $ \((x,y), col)-> do
100 printf "%d:%d: %x " x y col
101 putStrLn ""
102
103 LambdaCubeGL.disposeRenderer pipePick
104 LambdaCubeGL.disposeRenderer pipeDraw
105 LambdaCubeGL.disposeStorage storage
106 GLFW.destroyWindow win
107 GLFW.terminate
108
109deriving instance Show (LC.GLOutput)
110deriving instance Show (LC.GLTexture)
111
112-- geometry data: triangles
113scale = 250.0
114s = scale
115
116triangleA :: LambdaCubeGL.Mesh
117triangleA = Mesh
118 { mAttributes = Map.fromList
119 [ ("position", A_V2F $ V.fromList [V2 s s, V2 s (-s), V2 (-s) (-s)])
120 , ("color", A_V4F $ V.fromList $ take 4 $ repeat $ V4 1 0 0 1)
121 , ("id", A_Int $ V.fromList [1, 1, 1])
122 ]
123 , mPrimitive = P_Triangles
124 }
125
126triangleB :: LambdaCubeGL.Mesh
127triangleB = Mesh
128 { mAttributes = Map.fromList
129 [ ("position", A_V2F $ V.fromList [V2 s s, V2 (-s) (-s), V2 (-s) s])
130 , ("color", A_V4F $ V.fromList $ take 4 $ repeat $ V4 0 0 1 1)
131 , ("id", A_Int $ V.fromList [2, 2, 2])
132 ]
133 , mPrimitive = P_Triangles
134 }
135
136vec4ToV4F :: Vec4 -> LCLin.V4F
137vec4ToV4F (Vc.Vec4 x y z w) = LCLin.V4 x y z w
138
139mat4ToM44F :: Mat4 -> LCLin.M44F
140mat4ToM44F (Mat4 a b c d) = LCLin.V4 (vec4ToV4F a) (vec4ToV4F b) (vec4ToV4F c) (vec4ToV4F d)
141
142screenM :: Int -> Int -> Mat4
143screenM w h = scaleM
144 where (fw, fh) = (fromIntegral w, fromIntegral h)
145 scaleM = Vc.Mat4 (Vc.Vec4 (1/fw) 0 0 0)
146 (Vc.Vec4 0 (1/fh) 0 0)
147 (Vc.Vec4 0 0 1 0)
148 (Vc.Vec4 0 0 0 0.5)
149
150pickFrameBuffer
151 :: Int -- ^ framebuffer
152 -> (Int, Int) -- ^ FB dimensions
153 -> (Int, Int) -- ^ pick coordinates
154 -> IO F.Word32 -- ^ resultant pixel value
155pickFrameBuffer fb (w, h) (x, y) = do
156 glFinish
157 glBindFramebuffer GL_READ_FRAMEBUFFER $ fromIntegral fb
158 let (fbmode, format) =
159 if fb == 0
160 then (GL_BACK_LEFT, GL_RGBA)
161 else (GL_COLOR_ATTACHMENT0, GL_RGBA_INTEGER)
162 glReadBuffer fbmode
163 withFrameBuffer w format x (h - y - 1) 1 1 $ \p -> fromIntegral <$> F.peek (F.castPtr p :: F.Ptr F.Word32)
164
165withFrameBuffer :: Int -> GLenum -> Int -> Int -> Int -> Int -> (F.Ptr F.Word8 -> IO a) -> IO a
166withFrameBuffer rowLen format x y w h fn = F.allocaBytes (w*h*4) $ \p -> do
167 glPixelStorei GL_UNPACK_LSB_FIRST 0
168 glPixelStorei GL_UNPACK_SWAP_BYTES 0
169 glPixelStorei GL_UNPACK_ROW_LENGTH $ fromIntegral rowLen
170 glPixelStorei GL_UNPACK_IMAGE_HEIGHT 0
171 glPixelStorei GL_UNPACK_SKIP_ROWS 0
172 glPixelStorei GL_UNPACK_SKIP_PIXELS 0
173 glPixelStorei GL_UNPACK_SKIP_IMAGES 0
174 glPixelStorei GL_UNPACK_ALIGNMENT 1
175 glReadPixels (fromIntegral x) (fromIntegral y) (fromIntegral w) (fromIntegral h) format GL_UNSIGNED_BYTE $ F.castPtr p
176 glPixelStorei GL_UNPACK_ROW_LENGTH 0
177 fn p
178
179initWindow :: String -> Int -> Int -> IO Window
180initWindow title width height = do
181 GLFW.init
182 GLFW.defaultWindowHints
183 mapM_ GLFW.windowHint
184 [ WindowHint'ContextVersionMajor 3
185 , WindowHint'ContextVersionMinor 3
186 , WindowHint'OpenGLProfile OpenGLProfile'Core
187 , WindowHint'OpenGLForwardCompat True
188 ]
189 Just win <- GLFW.createWindow width height title Nothing Nothing
190 GLFW.makeContextCurrent $ Just win
191 return win
192