aboutsummaryrefslogtreecommitdiff
path: root/Graphics/Glyph/BufferBuilder.hs
blob: 570487588e0495180f42615466531f64610de1ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}

module Graphics.Glyph.BufferBuilder where

import Control.Monad
import Data.Array.Storable
import qualified Data.Foldable as Fold
import Data.Sequence as Seq
import Debug.Trace
import Foreign.Ptr
import Foreign.Storable
import Graphics.Glyph.Mat4
import Graphics.Glyph.Util
import Graphics.Rendering.OpenGL
import System.IO.Unsafe

data BufferBuilder3D = Plot BufferBuilder3D (GLfloat, GLfloat, GLfloat) Int Int | End

bufferSize :: BufferBuilder3D -> Int
bufferSize End = 0
bufferSize (Plot _ _ l _) = l

nelem :: BufferBuilder3D -> Int
nelem End = 0
nelem (Plot _ _ _ l) = l

sizeofGLfloat :: Int
sizeofGLfloat = 4

{- A state monad that keeps track of operations
 - and will compile them into a buffer -}
data Builder b a
  = Builder
      { bList :: Seq (BuildDatum b),
        bReturn :: a
      }
  | BuildError String

data BuildDatum b
  = VertexLink (b, b, b)
  | NormalLink (b, b, b)
  | ColorLink (b, b, b, b)
  | TextureLink (b, b)
  deriving (Show)

data CompiledBuild b = CompiledBuild
  { bStride :: Int,
    bEnabled :: (Bool, Bool, Bool),
    nElems :: Int,
    array :: StorableArray Int b
  }

bufferLength :: (Integral a) => CompiledBuild b -> a
bufferLength = fromIntegral . nElems

instance Show (CompiledBuild x) where
  show (CompiledBuild stride enabled n _) =
    "[CompiledBuild stride=" ++! stride ++ " enabled" ++! enabled ++ " n=" ++! n ++ "]"

instance Functor (Builder t) where
  fmap f b = b >>= (return . f)

instance Applicative (Builder t) where
  pure = return
  (<*>) afn aa = do
    fn <- afn
    a <- aa
    return (fn a)

instance Monad (Builder t) where
  (Builder lst1 _) >> (Builder lst2 ret) = Builder (lst2 >< lst1) ret
  BuildError str >> _ = BuildError str
  _ >> BuildError str = BuildError str

  b1@(Builder _ ret1) >>= func = b1 >> func ret1
  BuildError str >>= _ = BuildError str

  return = Builder empty

{- Add a vertex to the current builder -}
bVertex3 :: (a, a, a) -> Builder a ()
bVertex3 vert = Builder (Seq.singleton $ VertexLink vert) ()

bTexture2 :: (a, a) -> Builder a ()
bTexture2 tex = Builder (Seq.singleton $ TextureLink tex) ()

bNormal3 :: (a, a, a) -> Builder a ()
bNormal3 norm = Builder (Seq.singleton $ NormalLink norm) ()

bColor4 :: (a, a, a, a) -> Builder a ()
bColor4 col = Builder (Seq.singleton $ ColorLink col) ()

compilingBuilder :: (Storable b, Num b, Show b) => Builder b x -> IO (CompiledBuild b)
compilingBuilder (Builder lst _) = do
  -- Size of the elements
  let sizeof = sizeOf $ tmp (Seq.index lst 0)
        where
          tmp (VertexLink (a, _, _)) = a
          tmp _ = 0
  {- Simply figure out what types of elementse
   - exist in this buffer -}
  let en@(bn, bc, bt) =
        Fold.foldl'
          ( \(bn, bc, bt) ele ->
              case ele of
                NormalLink _ -> (True, bc, bt)
                ColorLink _ -> (bn, True, bt)
                TextureLink _ -> (bn, bc, True)
                VertexLink _ -> (bn, bc, bt)
          )
          (False, False, False)
          lst
  {- Calculate the stride; number of floats per element -}
  let stride = (3 + (?) bn * 3 + (?) bc * 4 + (?) bt * 2) * sizeof
        where
          (?) True = 1
          (?) False = 0
  --             Cur color normal texture buffer
  let (nverts, _, _, _, buffer) =
        Fold.foldl'
          ( \(num, cn, cc, ct, ll) ele ->
              -- trace ("foldl " ++! ele) $
              case ele of
                NormalLink nn -> (num, nn, cc, ct, ll)
                ColorLink nc -> (num, cn, nc, ct, ll)
                TextureLink nt -> (num, cn, cc, nt, ll)
                VertexLink vert ->
                  ( num + 1,
                    cn,
                    cc,
                    ct,
                    ll >< (tp3 True vert >< tp3 bn cn >< tp4 bc cc >< tp2 bt ct)
                  )
          )
          (0, (0, 0, 0), (0, 0, 0, 0), (0, 0), Seq.empty)
          (Seq.reverse lst)

  let blst = (Fold.toList buffer)
  arr <- blst `seq` newListArray (0, Seq.length buffer) blst
  let compiledRet = CompiledBuild stride en nverts arr
  compiledRet `seq` putStrLn ("Compiled: " ++! compiledRet) `seq` return compiledRet
  where
    tp2 True (a, b) = Seq.fromList [a, b]
    tp2 False _ = empty

    tp3 True (a, b, c) = Seq.fromList [a, b, c]
    tp3 False _ = empty

    tp4 True (a, b, c, d) = Seq.fromList [a, b, c, d]
    tp4 False _ = empty

storableArrayToBuffer :: (Storable el) => BufferTarget -> StorableArray Int el -> IO BufferObject
storableArrayToBuffer target arr = do
  let sizeof = sizeOf $ unsafePerformIO (readArray arr 0)
  [buffer] <- genObjectNames 1
  bindBuffer target $= Just buffer
  len <- getBounds arr >>= (\(a, b) -> return $ (b - a) * sizeof)
  withStorableArray arr $ \ptr ->
    bufferData target $= (fromIntegral len, ptr, StaticDraw)
  return buffer

vertexArrayDescriptor :: CompiledBuild GLfloat -> VertexArrayDescriptor GLfloat
vertexArrayDescriptor (CompiledBuild stride _ _ _) = VertexArrayDescriptor 3 Float (fromIntegral stride) (wordPtrToPtr 0)

normalArrayDescriptor :: CompiledBuild GLfloat -> Maybe (VertexArrayDescriptor GLfloat)
normalArrayDescriptor (CompiledBuild stride (True, _, _) _ _) =
  Just $
    VertexArrayDescriptor
      3
      Float
      (fromIntegral stride)
      (wordPtrToPtr (3 * 4))
normalArrayDescriptor _ = Nothing

colorArrayDescriptor :: CompiledBuild GLfloat -> Maybe (VertexArrayDescriptor GLfloat)
colorArrayDescriptor (CompiledBuild stride tup@(_, True, _) _ _) =
  Just $
    VertexArrayDescriptor
      4
      Float
      (fromIntegral stride)
      (wordPtrToPtr (offset tup))
  where
    offset (b1, _, _) = if b1 then (6 * 4) else (3 * 4)
colorArrayDescriptor _ = Nothing

textureArrayDescriptor :: CompiledBuild GLfloat -> Maybe (VertexArrayDescriptor GLfloat)
textureArrayDescriptor (CompiledBuild stride tup@(_, _, True) _ _) =
  Just $
    VertexArrayDescriptor
      2
      Float
      (fromIntegral stride)
      (wordPtrToPtr (offset tup))
  where
    offset (b1, b2, _) = (3 + (ifp b1 3) + (ifp b2 4)) * 4
    ifp b x = if b then x else 0
textureArrayDescriptor _ = Nothing

createBufferObject :: BufferTarget -> CompiledBuild GLfloat -> IO BufferObject
createBufferObject target (CompiledBuild _ _ _ arr) = storableArrayToBuffer target arr