diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-10 22:58:12 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-10 22:58:12 +0100 |
commit | 2344d2af7eb3e5515408d134a074b989e7b5efde (patch) | |
tree | 01b905a55d0b8e0560097241dd2cd277f6bfab46 /src | |
parent | e936d3c32c17c0d00939893fa85996c3807ed3e7 (diff) |
update example
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/GL.hs | 13 | ||||
-rw-r--r-- | src/LambdaCube/GL/Backend.hs | 10 | ||||
-rw-r--r-- | src/LambdaCube/GL/Input.hs | 33 |
3 files changed, 48 insertions, 8 deletions
diff --git a/src/LambdaCube/GL.hs b/src/LambdaCube/GL.hs index 3e11be1..258d2ba 100644 --- a/src/LambdaCube/GL.hs +++ b/src/LambdaCube/GL.hs | |||
@@ -28,6 +28,7 @@ module LambdaCube.GL ( | |||
28 | uploadTexture2DToGPU', | 28 | uploadTexture2DToGPU', |
29 | 29 | ||
30 | -- GL: Renderer, Storage, Object | 30 | -- GL: Renderer, Storage, Object |
31 | GLUniformName, | ||
31 | GLRenderer, | 32 | GLRenderer, |
32 | GLStorage, | 33 | GLStorage, |
33 | Object, | 34 | Object, |
@@ -48,7 +49,6 @@ module LambdaCube.GL ( | |||
48 | setObjectOrder, | 49 | setObjectOrder, |
49 | objectUniformSetter, | 50 | objectUniformSetter, |
50 | setScreenSize, | 51 | setScreenSize, |
51 | sortSlotObjects, | ||
52 | 52 | ||
53 | uniformBool, | 53 | uniformBool, |
54 | uniformV2B, | 54 | uniformV2B, |
@@ -80,7 +80,16 @@ module LambdaCube.GL ( | |||
80 | uniformM43F, | 80 | uniformM43F, |
81 | uniformM44F, | 81 | uniformM44F, |
82 | 82 | ||
83 | uniformFTexture2D | 83 | uniformFTexture2D, |
84 | |||
85 | -- schema builder utility functions | ||
86 | (@:), | ||
87 | defObjectArray, | ||
88 | defUniforms, | ||
89 | makeSchema, | ||
90 | |||
91 | (@=), | ||
92 | updateUniforms | ||
84 | ) where | 93 | ) where |
85 | 94 | ||
86 | import LambdaCube.GL.Type | 95 | import LambdaCube.GL.Type |
diff --git a/src/LambdaCube/GL/Backend.hs b/src/LambdaCube/GL/Backend.hs index 2753ac3..677e925 100644 --- a/src/LambdaCube/GL/Backend.hs +++ b/src/LambdaCube/GL/Backend.hs | |||
@@ -696,12 +696,12 @@ renderSlot cmds = forM_ cmds $ \cmd -> do | |||
696 | texUnit <- readIORef tuRef | 696 | texUnit <- readIORef tuRef |
697 | glActiveTexture $ GL_TEXTURE0 + fromIntegral texUnit | 697 | glActiveTexture $ GL_TEXTURE0 + fromIntegral texUnit |
698 | glBindTexture txTarget txObj | 698 | glBindTexture txTarget txObj |
699 | putStrLn $ "to texture unit " ++ show texUnit ++ " texture object " ++ show txObj | 699 | --putStrLn $ "to texture unit " ++ show texUnit ++ " texture object " ++ show txObj |
700 | GLSetVertexAttrib idx val -> do | 700 | GLSetVertexAttrib idx val -> do |
701 | glDisableVertexAttribArray idx | 701 | glDisableVertexAttribArray idx |
702 | setVertexAttrib idx val | 702 | setVertexAttrib idx val |
703 | isOk <- checkGL | 703 | --isOk <- checkGL |
704 | putStrLn $ isOk ++ " - " ++ show cmd | 704 | --putStrLn $ isOk ++ " - " ++ show cmd |
705 | 705 | ||
706 | renderFrame :: GLRenderer -> IO () | 706 | renderFrame :: GLRenderer -> IO () |
707 | renderFrame glp = do | 707 | renderFrame glp = do |
@@ -750,8 +750,8 @@ renderFrame glp = do | |||
750 | GLSaveImage | 750 | GLSaveImage |
751 | GLLoadImage | 751 | GLLoadImage |
752 | -} | 752 | -} |
753 | isOk <- checkGL | 753 | --isOk <- checkGL |
754 | putStrLn $ isOk ++ " - " ++ show cmd | 754 | --putStrLn $ isOk ++ " - " ++ show cmd |
755 | 755 | ||
756 | data CGState | 756 | data CGState |
757 | = CGState | 757 | = CGState |
diff --git a/src/LambdaCube/GL/Input.hs b/src/LambdaCube/GL/Input.hs index 742fc35..7e4ba74 100644 --- a/src/LambdaCube/GL/Input.hs +++ b/src/LambdaCube/GL/Input.hs | |||
@@ -1,8 +1,10 @@ | |||
1 | {-# LANGUAGE FlexibleContexts #-} | ||
1 | module LambdaCube.GL.Input where | 2 | module LambdaCube.GL.Input where |
2 | 3 | ||
3 | import Control.Applicative | 4 | import Control.Applicative |
4 | import Control.Exception | 5 | import Control.Exception |
5 | import Control.Monad | 6 | import Control.Monad |
7 | import Control.Monad.Writer | ||
6 | import Data.IORef | 8 | import Data.IORef |
7 | import Data.Map (Map) | 9 | import Data.Map (Map) |
8 | import Data.IntMap (IntMap) | 10 | import Data.IntMap (IntMap) |
@@ -68,7 +70,7 @@ allocStorage sch = do | |||
68 | } | 70 | } |
69 | 71 | ||
70 | disposeStorage :: GLStorage -> IO () | 72 | disposeStorage :: GLStorage -> IO () |
71 | disposeStorage = error "not implemented: disposeStorage" | 73 | disposeStorage _ = putStrLn "not implemented: disposeStorage" |
72 | 74 | ||
73 | -- object | 75 | -- object |
74 | addObject :: GLStorage -> String -> Primitive -> Maybe (IndexStream Buffer) -> Map String (Stream Buffer) -> [String] -> IO Object | 76 | addObject :: GLStorage -> String -> Primitive -> Maybe (IndexStream Buffer) -> Map String (Stream Buffer) -> [String] -> IO Object |
@@ -137,6 +139,7 @@ addObject input slotName prim indices attribs uniformNames = do | |||
137 | let emptyV = V.replicate (V.length $ glPrograms p) [] | 139 | let emptyV = V.replicate (V.length $ glPrograms p) [] |
138 | return $ emptyV // [(prgIdx,createObjectCommands (glTexUnitMapping p) topUnis obj (glPrograms p ! prgIdx))| prgIdx <- glSlotPrograms p ! pSlotIdx] | 140 | return $ emptyV // [(prgIdx,createObjectCommands (glTexUnitMapping p) topUnis obj (glPrograms p ! prgIdx))| prgIdx <- glSlotPrograms p ! pSlotIdx] |
139 | writeIORef cmdsRef cmds | 141 | writeIORef cmdsRef cmds |
142 | sortSlotObjects input | ||
140 | return obj | 143 | return obj |
141 | 144 | ||
142 | removeObject :: GLStorage -> Object -> IO () | 145 | removeObject :: GLStorage -> Object -> IO () |
@@ -149,6 +152,7 @@ setObjectOrder :: GLStorage -> Object -> Int -> IO () | |||
149 | setObjectOrder p obj i = do | 152 | setObjectOrder p obj i = do |
150 | writeIORef (objOrder obj) i | 153 | writeIORef (objOrder obj) i |
151 | modifyIORef (slotVector p ! objSlot obj) $ \(GLSlot objs sorted _) -> GLSlot objs sorted Reorder | 154 | modifyIORef (slotVector p ! objSlot obj) $ \(GLSlot objs sorted _) -> GLSlot objs sorted Reorder |
155 | sortSlotObjects p | ||
152 | 156 | ||
153 | objectUniformSetter :: Object -> Map GLUniformName InputSetter | 157 | objectUniformSetter :: Object -> Map GLUniformName InputSetter |
154 | objectUniformSetter = objUniSetter | 158 | objectUniformSetter = objUniSetter |
@@ -387,3 +391,30 @@ uniformM44F n is = case Map.lookup n is of | |||
387 | uniformFTexture2D n is = case Map.lookup n is of | 391 | uniformFTexture2D n is = case Map.lookup n is of |
388 | Just (SFTexture2D fun) -> fun | 392 | Just (SFTexture2D fun) -> fun |
389 | _ -> nullSetter n "FTexture2D" | 393 | _ -> nullSetter n "FTexture2D" |
394 | |||
395 | a @: b = tell [(a,b)] | ||
396 | defObjectArray n p m = mapM_ tell [PipelineSchema (Map.singleton n $ ObjectArraySchema p $ Map.singleton a t) mempty | (a,t) <- execWriter m] | ||
397 | defUniforms m = tell $ PipelineSchema mempty $ Map.fromList $ execWriter m | ||
398 | makeSchema a = execWriter a :: PipelineSchema | ||
399 | |||
400 | unionObjectArraySchema (ObjectArraySchema a1 b1) (ObjectArraySchema a2 b2) = | ||
401 | ObjectArraySchema (if a1 == a2 then a1 else error $ "object array schema primitive mismatch " ++ show (a1,a2)) | ||
402 | (Map.unionWith (\a b -> if a == b then a else error $ "object array schema attribute type mismatch " ++ show (a,b)) b1 b2) | ||
403 | |||
404 | instance Monoid PipelineSchema where | ||
405 | mempty = PipelineSchema mempty mempty | ||
406 | mappend (PipelineSchema a1 b1) (PipelineSchema a2 b2) = | ||
407 | PipelineSchema (Map.unionWith unionObjectArraySchema a1 a2) (Map.unionWith (\a b -> if a == b then a else error $ "schema type mismatch " ++ show (a,b)) b1 b2) | ||
408 | |||
409 | type UniM = Writer [GLStorage -> IO ()] | ||
410 | |||
411 | class UniformSetter a where | ||
412 | (@=) :: GLUniformName -> IO a -> UniM () | ||
413 | |||
414 | instance UniformSetter Float where | ||
415 | n @= act = tell [\s -> let f = uniformFloat n (uniformSetter s) in f =<< act] | ||
416 | |||
417 | instance UniformSetter TextureData where | ||
418 | n @= act = tell [\s -> let f = uniformFTexture2D n (uniformSetter s) in f =<< act] | ||
419 | |||
420 | updateUniforms storage m = sequence_ $ let l = map ($ storage) $ execWriter m in l | ||