Refactor picture rendering code

This commit is contained in:
jgk
2021-03-10 22:45:43 +01:00
parent a2fa713bde
commit e2c35b21bc
4 changed files with 153 additions and 289 deletions
+3 -35
View File
@@ -2,7 +2,7 @@
--{-# LANGUAGE Strict #-}
module Picture.Data
where
import Shader.Data
import Data.Monoid
import qualified Data.Foldable as F
import qualified Data.Sequence as Se
@@ -12,11 +12,9 @@ import Geometry.Data
import Control.Lens
import Foreign
--import Foreign
import Graphics.Rendering.OpenGL hiding (Point (..),translate,scale,imageHeight,imageWidth)
import qualified Control.Foldl as F
--import Graphics.Rendering.OpenGL hiding (Point (..),translate,scale,imageHeight,imageWidth)
import Control.Monad
@@ -29,34 +27,6 @@ data RenderType
| RenderLine [(Point3,Point4)]
| RenderEllipse [(Point3,Point4)]
pokeShaders :: [FullShader RenderType] -> F.FoldM IO RenderType [Int]
pokeShaders fss = traverse pokeShader fss
pokeShader :: FullShader RenderType -> F.FoldM IO RenderType Int
pokeShader fs = F.FoldM (pokeRender fls (zip ptrs nAtss)) (return 0) return
where vao = _shaderVAO fs
(_,ptrs,nAtss) = unzip3 $ _vaoBufferTargets $ vao
fls = _shaderPokeStrategy fs
pokeRender :: (RenderType -> [[[Float]]])
-> [(Ptr Float,Int)] -> Int -> RenderType -> IO Int
pokeRender toFs ptrs n rt = pokeList ptrs n (toFs rt)
pokeList :: [(Ptr Float,Int)] -> Int -> [[[Float]]] -> IO Int
pokeList ptrs n fsss = foldM (pokePtrs ptrs) n fsss
pokePtrs :: [(Ptr Float,Int)] -> Int -> [[Float]] -> IO Int
pokePtrs ptrIs n fss = do
zipWithM_ f ptrIs fss
return $ n + 1
where f (ptr,i) fs = pokeArrayOff ptr (i*n) fs
pokeArrayOff :: Storable a => Ptr a -> Int -> [a] -> IO ()
pokeArrayOff ptr i xs =
zipWithM_ (pokeElemOff ptr) [i..] xs
type RGBA = (Float,Float,Float,Float)
type Color = (Float,Float,Float,Float)
@@ -93,7 +63,6 @@ instance Functor (RTree a) where
fmap f (RBranches i ts) = RBranches i $ (fmap (fmap f)) ts
fmap f (RLeaf x) = RLeaf (f x)
flat2 (x,y) = [x,y]
flat3 (x,y,z) = [x,y,z]
flat4 (x,y,z,w) = [x,y,z,w]
@@ -114,7 +83,6 @@ data Picture
| OverPic (Point3 -> Point3) (Point2 -> Point2) Float (Point4 -> Point4) Picture
| OnLayer Int Picture
blank :: Picture
{-# INLINE blank #-}
blank = Blank