{- Tri3d.hs - Triangles. For the purpose of efficiency and time, this renderer works with triangles ONLY. No quad's, no arbitrary convex polygons, etc. etc. etc. -} module Tri3d where import Vec3d import Mat3d import qualified GraphicsUtils as G type Tri3 a = (Vector3, Vector3, Vector3, a) type Tri2 a = (Vector2, Vector2, Vector2, a) rotTriX :: Tri3 a -> Float -> Tri3 a rotTriX (v1, v2, v3, xtra) theta = (v1', v2', v3', xtra) where tm = rotX theta v1' = v1 `vecMultMat` tm v2' = v2 `vecMultMat` tm v3' = v3 `vecMultMat` tm rotTriY :: Tri3 a -> Float -> Tri3 a rotTriY (v1, v2, v3, xtra) theta = (v1', v2', v3', xtra) where tm = rotY theta v1' = v1 `vecMultMat` tm v2' = v2 `vecMultMat` tm v3' = v3 `vecMultMat` tm rotTriZ :: Tri3 a -> Float -> Tri3 a rotTriZ (v1, v2, v3, xtra) theta = (v1', v2', v3', xtra) where tm = rotZ theta v1' = v1 `vecMultMat` tm v2' = v2 `vecMultMat` tm v3' = v3 `vecMultMat` tm {- This is used for the back-to-front sort of the triangle list. Very important for rendering properly -} averageZ :: Tri3 a -> Float averageZ (v1, v2, v3, _) = (z1 + z2 + z3) / 3.0 where (_,_,z1) = v1 (_,_,z2) = v2 (_,_,z3) = v3 flatten :: Tri3 a -> (Vector3 -> Vector2) -> Tri2 a flatten (v1, v2, v3, xtra) f = (f v1, f v2, f v3, xtra) zSign :: Tri2 a -> Bool zSign (v1, v2, v3, _) = (z > 0) where (x1,y1) = v1 (x2,y2) = v2 (x3,y3) = v3 z = (x3-x1)*(y3-y2) - (x3-x2)*(y3-y1)