module HOff.Display
( drawOFF
) where
import HOff.Parser as HOff
import Graphics.Rendering.OpenGL as GL
import Data.List
import Data.Foldable
drawOFF :: (VertexComponent a, ColorComponent a,Floating a,Show a)
=> OFF a Int
-> IO ()
drawOFF (OFF ps fs) = mapM_ (drawFace ps) fs
drawFace :: (VertexComponent a, ColorComponent a,Floating a,Show a)
=> [Vertice a]
-> HOff.Face Int
-> IO ()
drawFace ps (Face is) = GL.renderPrimitive GL.Polygon $ do
GL.color faceColor
mapM_ (ver . (ps!!)) is
where
vA = diffTriTuple (ps !! (is !! 0)) (ps !! (is !! 1))
vB = diffTriTuple (ps !! (is !! 1)) (ps !! (is !! 2))
k = cosZAngVec $ vA `xProd` vB
faceColor = toColor $ (k*0.5+0.5) `cProd` (1,0,0)
ver (Vertice (a,b,c)) = GL.vertex $ GL.Vertex3 a b c
diffTriTuple :: Num a => Vertice a -> Vertice a -> (a,a,a)
diffTriTuple (Vertice (a,b,c)) (Vertice (x,y,z)) = (ax,by,cz)
xProd :: Num a => (a,a,a) -> (a,a,a) -> (a,a,a)
xProd (a,b,c) (x,y,z) = (b * z y * c, a * z x * c, a * y x * b)
cosZAngVec :: Floating a => (a,a,a) -> a
cosZAngVec (a,b,c) = b / sqrt (a^2 + b^2 + c^2)
cProd :: Num a => a -> (a,a,a) -> (a,a,a)
cProd k (a,b,c) = (k*a, k*b, k*c)
toColor :: (a,a,a) -> Color3 a
toColor (a,b,c) = Color3 a b c
moveColor :: Num a => a -> a -> (a,a,a) -> (a,a,a)
moveColor k b (x,y,z) = ( k * x + b
, k * y + b
, k * z + b
)