{-
   Quake 1 planes, used for splitting planes in the BSP tree, for
   reference planes of faces, and for clip nodes.
   Sven Panne 2000.   mailto:Sven.Panne@informatik.uni-muenchen.de
-}
module Plane where

import IO(Handle)
import Monad(liftM)
import Geometry
import QuakeTypes

---------------------------------------------------------------------------

data QPlane = QPlane {
   planeOf   :: Plane Qfloat,
   planeType :: PlaneType     -- redundant type of plane, depending on normal (for lighting?)
 } deriving Show

qPlaneSize :: Int
qPlaneSize = qVectorSize + qfloatSize + planeTypeSize

qGetQPlane :: Handle -> IO QPlane
qGetQPlane h = do
   thePlaneNormal   <- qGetQVector h
   thePlaneDistance <- qGetQfloat h
   thePlaneType     <- qGetPlaneType h
   return $ QPlane {
      planeOf   = Plane {
         planeNormal   = thePlaneNormal,
         planeDistance = thePlaneDistance },
      planeType = thePlaneType }

---------------------------------------------------------------------------
-- NOTE 1: The order of the constructors is important!
-- NOTE 2: Quake's horizontal plane is x-y, OpenGL's x-z, so we have to
--         swap a little bit here.

data PlaneType =
   -- axial planes
     PlaneX
   | PlaneZ
   | PlaneY
   -- non-axial planes snapped to the nearest axial one
   | PlaneAnyX
   | PlaneAnyZ
   | PlaneAnyY
   deriving (Enum, Show)

planeTypeSize :: Int
planeTypeSize = qintSize

qGetPlaneType :: Handle -> IO PlaneType
qGetPlaneType = liftM (toEnum . fromIntegral) . qGetQint
