[Haskell-beginners] Double vs. Num

Ben Rogalski bwrogalski at gmail.com
Thu May 5 14:10:45 UTC 2016


Tim and Christopher, here is my code. There is a lot of it, but I've tried
to cut out the irrelevent parts.

If I change the type of Transform3D.append from

append :: (Floating a) => Transformer a -> Transform a -> Transformer a

to

append :: Transformer Double -> Transform Double -> Transformer Double

The framerate goes from ~1400 fps to ~2200 fps.



module Render where

...

renderTree :: GLInfo -> Tree (Attribute Double) -> IO ()
renderTree (GLInfo names attribs uniforms) t = fst $ cFold2 f (return
(),identity) t
  where f acc Hidden = Skip acc
        f (a,tr) (Transformation t) = Continue (a, append tr t)
        f (a,tr) (Clip b) =
          let (x1:.y1:._, x2:.y2:._) = extremes $ applyAABB tr b
              a' = do
                glUniform2f (uniforms M.! "maskMin") (realToFrac x1)
(realToFrac y1)
                glUniform2f (uniforms M.! "maskMax") (realToFrac x2)
(realToFrac y2)
          in  Continue (a >> a', tr)
        f (a,tr) (Texture _ (x1:.y1:.x2:.y2:._)) =
          let a' = do
                glUniform1i (uniforms M.! "texSampler") 0
                glUniform4f (uniforms M.! "uvCoords") (realToFrac x1)
(realToFrac y1) (realToFrac x2) (realToFrac y2)
                glUniformMatrix4fv' (uniforms M.! "mvp") 1 (fromBool False)
(map realToFrac $ transformerToList tr)
                glDrawElements gl_TRIANGLES (fromIntegral $ attribs M.!
"iboLenSquare") gl_UNSIGNED_INT nullPtr
          in  Continue (a >> a', tr)
        f acc _ = Continue acc

...



module Transform3D where

...

data Transform a =
    RotationZ a
  | Scale (Vec3 a)
  | Translation (Vec3 a) deriving (Eq, Read, Show)

newtype Transformer a = Transformer (Vec4 (Vec4 a))

append :: (Floating a) => Transformer a -> Transform a -> Transformer a
append (Transformer m) t = Transformer $ m #*# toMatrix t

identity :: (Num a) => Transformer a
identity = Transformer identityMatrix

toMatrix :: (Floating a) => Transform a -> Vec4 (Vec4 a)
toMatrix (RotationZ z) = rotationZMatrix z
toMatrix (Scale (x:.y:.z:._)) = scaleMatrix x y z
toMatrix (Translation (x:.y:.z:._)) = translationMatrix x y z

identityMatrix :: (Num a) => Vec4 (Vec4 a)
identityMatrix =
  (1:.0:.0:.0:.Nil):.
  (0:.1:.0:.0:.Nil):.
  (0:.0:.1:.0:.Nil):.
  (0:.0:.0:.1:.Nil):.Nil

rotationZMatrix :: (Floating a) => a -> Vec4 (Vec4 a)
rotationZMatrix a =
  let c = cos a
      s = sin a
  in  (c:.(-s):.0:.0:.Nil):.
      (s:.c:.0:.0:.Nil):.
      (0:.0:.1:.0:.Nil):.
      (0:.0:.0:.1:.Nil):.Nil

scaleMatrix :: (Num a) => a -> a -> a -> Vec4 (Vec4 a)
scaleMatrix x y z =
  (x:.0:.0:.0:.Nil):.
  (0:.y:.0:.0:.Nil):.
  (0:.0:.z:.0:.Nil):.
  (0:.0:.0:.1:.Nil):.Nil

translationMatrix :: (Num a) => a -> a -> a -> Vec4 (Vec4 a)
translationMatrix x y z =
  (1:.0:.0:.x:.Nil):.
  (0:.1:.0:.y:.Nil):.
  (0:.0:.1:.z:.Nil):.
  (0:.0:.0:.1:.Nil):.Nil

...



module Vector where

...

infixr 5 :.
--data Cons u t = (:.) t (u t) deriving (Eq, Read, Show)
data Cons u t = (:.) ! t ! (u t) deriving (Eq, Read, Show)

data Nil t = Nil deriving (Eq, Read, Show)

...

(|*#) :: (Num t, Vector w, Vector u) => w t -> w (u t) -> u t
(|*#) v m = (transpose m) #*| v

(#*#) :: (Num t, Vector u, Vector v, Vector w) => u (v t) -> v (w t) -> u
(w t)
(#*#) x y = transpose $ fmap (x #*|) (transpose y)

dot :: (Num t, Vector v) => v t -> v t -> t
dot xs ys = sum ((*) <$> xs <*> ys)

...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160505/2f68534c/attachment-0001.html>


More information about the Beginners mailing list