<div dir="ltr"><div><div><div>Tim and Christopher, here is my code. There is a lot of it, but I've tried to cut out the irrelevent parts. <br><br>If I change the type of Transform3D.append from<br><br><font face="monospace,monospace">append :: (Floating a) => Transformer a -> Transform a -> Transformer a<br><br></font></div><font face="monospace,monospace">to<br></font><br><font face="monospace,monospace">append :: Transformer Double -> Transform Double -> Transformer Double<br><br></font></div><font face="monospace,monospace"><font face="arial,helvetica,sans-serif">The framerate goes from ~1400 fps to ~2200 fps.</font><br><br></font></div><font face="monospace,monospace"></font><div><div><div><div><div><br><font face="monospace,monospace"><br>module Render where<br><br>...<br><br>renderTree :: GLInfo -> Tree (Attribute Double) -> IO ()<br>renderTree (GLInfo names attribs uniforms) t = fst $ cFold2 f (return (),identity) t<br>  where f acc Hidden = Skip acc<br>        f (a,tr) (Transformation t) = Continue (a, append tr t)<br>        f (a,tr) (Clip b) =<br>          let (x1:.y1:._, x2:.y2:._) = extremes $ applyAABB tr b<br>              a' = do<br>                glUniform2f (uniforms M.! "maskMin") (realToFrac x1) (realToFrac y1)<br>                glUniform2f (uniforms M.! "maskMax") (realToFrac x2) (realToFrac y2)<br>          in  Continue (a >> a', tr)<br>        f (a,tr) (Texture _ (x1:.y1:.x2:.y2:._)) =<br>          let a' = do<br>                glUniform1i (uniforms M.! "texSampler") 0<br>                glUniform4f (uniforms M.! "uvCoords") (realToFrac x1) (realToFrac y1) (realToFrac x2) (realToFrac y2)<br>                glUniformMatrix4fv' (uniforms M.! "mvp") 1 (fromBool False) (map realToFrac $ transformerToList tr)<br>                glDrawElements gl_TRIANGLES (fromIntegral $ attribs M.! "iboLenSquare") gl_UNSIGNED_INT nullPtr<br>          in  Continue (a >> a', tr)<br>        f acc _ = Continue acc<br><br>...<br><br><br><br>module Transform3D where<br><br>...<br><br>data Transform a =<br>    RotationZ a<br>  | Scale (Vec3 a)<br>  | Translation (Vec3 a) deriving (Eq, Read, Show)<br><br>newtype Transformer a = Transformer (Vec4 (Vec4 a))<br><br>append :: (Floating a) => Transformer a -> Transform a -> Transformer a<br>append (Transformer m) t = Transformer $ m #*# toMatrix t<br><br>identity :: (Num a) => Transformer a<br>identity = Transformer identityMatrix<br><br>toMatrix :: (Floating a) => Transform a -> Vec4 (Vec4 a)<br>toMatrix (RotationZ z) = rotationZMatrix z<br>toMatrix (Scale (x:.y:.z:._)) = scaleMatrix x y z<br>toMatrix (Translation (x:.y:.z:._)) = translationMatrix x y z<br><br>identityMatrix :: (Num a) => Vec4 (Vec4 a)<br>identityMatrix =<br>  (1:.0:.0:.0:.Nil):.<br>  (0:.1:.0:.0:.Nil):.<br>  (0:.0:.1:.0:.Nil):.<br>  (0:.0:.0:.1:.Nil):.Nil<br><br>rotationZMatrix :: (Floating a) => a -> Vec4 (Vec4 a)<br>rotationZMatrix a =<br>  let c = cos a<br>      s = sin a<br>  in  (c:.(-s):.0:.0:.Nil):.<br>      (s:.c:.0:.0:.Nil):.<br>      (0:.0:.1:.0:.Nil):.<br>      (0:.0:.0:.1:.Nil):.Nil<br><br>scaleMatrix :: (Num a) => a -> a -> a -> Vec4 (Vec4 a)<br>scaleMatrix x y z =<br>  (x:.0:.0:.0:.Nil):.<br>  (0:.y:.0:.0:.Nil):.<br>  (0:.0:.z:.0:.Nil):.<br>  (0:.0:.0:.1:.Nil):.Nil<br><br>translationMatrix :: (Num a) => a -> a -> a -> Vec4 (Vec4 a)<br>translationMatrix x y z =<br>  (1:.0:.0:.x:.Nil):.<br>  (0:.1:.0:.y:.Nil):.<br>  (0:.0:.1:.z:.Nil):.<br>  (0:.0:.0:.1:.Nil):.Nil<br><br>...<br><br><br><br>module Vector where<br><br>...<br><br>infixr 5 :.<br>--data Cons u t = (:.) t (u t) deriving (Eq, Read, Show)<br>data Cons u t = (:.) ! t ! (u t) deriving (Eq, Read, Show)<br><br>data Nil t = Nil deriving (Eq, Read, Show)<br><br>...<br><br>(|*#) :: (Num t, Vector w, Vector u) => w t -> w (u t) -> u t<br>(|*#) v m = (transpose m) #*| v<br><br>(#*#) :: (Num t, Vector u, Vector v, Vector w) => u (v t) -> v (w t) -> u (w t)<br>(#*#) x y = transpose $ fmap (x #*|) (transpose y)<br><br>dot :: (Num t, Vector v) => v t -> v t -> t<br>dot xs ys = sum ((*) <$> xs <*> ys)<br><br>...<br></font></div></div></div></div></div></div>