packages feed

HGamer3D-Data-0.1.5: HGamer3D/Data/Matrix4.hs

-- This source file is part of HGamer3D
-- (A project to enable 3D game development in Haskell)
-- For the latest info, see http://www.althainz.de/HGamer3D.html
--
-- (c) 2011 Peter Althainz
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- Matrix4.hs, the 4x4 matrix type 

module HGamer3D.Data.Matrix4 

(
	Matrix4,
	HGamer3D.Data.Matrix4.matToLists,
	HGamer3D.Data.Matrix4.matFromLists,
	extendV3,
	oneV3,
	zeroV3,
	toV3,
	HGamer3D.Data.Matrix4.multvm,
	HGamer3D.Data.Matrix4.multmv,
	HGamer3D.Data.Matrix4.multmm,
	HGamer3D.Data.Matrix4.transpose,
	
	HGamer3D.Data.Matrix4.translation,
	HGamer3D.Data.Matrix4.rotationX,
	HGamer3D.Data.Matrix4.rotationY,
	HGamer3D.Data.Matrix4.rotationZ,
	HGamer3D.Data.Matrix4.rotationVec,
	HGamer3D.Data.Matrix4.rotationEuler,
	HGamer3D.Data.Matrix4.rotationQuat
)

where

import Data.Vec.Base
import Data.Vec.Nat
import Data.Vec.Packed
import Data.Vec.LinAlg
import Data.Vec.LinAlg.Transform3D
import HGamer3D.Data.Vector3
import HGamer3D.Data.Vector4
import HGamer3D.Data.Quaternion

newtype Matrix4 = Matrix4 (Mat44 Float)

matToLists :: Matrix4 -> [[Float]]
matToLists (Matrix4 mat) = Data.Vec.Base.matToLists mat

matFromLists :: [[Float]] -> Matrix4
matFromLists lists = Matrix4 $ Data.Vec.Base.matFromLists lists

extendV3 :: Vector3 -> Float -> Vector4
extendV3 v3 f = vector4 (v3X v3) (v3X v3) (v3Z v3) f

oneV3 :: Vector3 -> Vector4
oneV3 v3 = extendV3 v3 1.0

zeroV3 :: Vector3 -> Vector4
zeroV3 v3 = extendV3 v3 0.0

toV3 :: Vector4 -> Vector3
toV3 v4 = vector3 (v4X v4) (v4Y v4) (v4Z v4)

multvm :: Vector4 -> Matrix4 -> Vector4
multvm (Vector4 v) (Matrix4 m) = Vector4 $ pack (Data.Vec.LinAlg.multvm (unpack v) m)

multmv :: Matrix4 -> Vector4 -> Vector4
multmv (Matrix4 m) (Vector4 v) = Vector4 $ pack (Data.Vec.LinAlg.multmv m (unpack v))

multmm :: Matrix4 -> Matrix4 -> Matrix4
multmm (Matrix4 m1) (Matrix4 m2) = Matrix4 (Data.Vec.LinAlg.multmm m1 m2)

transpose :: Matrix4 -> Matrix4
transpose (Matrix4 m) = Matrix4 (Data.Vec.LinAlg.transpose m)

translation :: Vector3 -> Matrix4
translation (Vector3 v)  = Matrix4 (Data.Vec.LinAlg.Transform3D.translation (unpack v) )

rotationX :: Float -> Matrix4
rotationX f  = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationX f )

rotationY :: Float -> Matrix4
rotationY f  = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationY f )

rotationZ :: Float -> Matrix4
rotationZ f  = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationZ f )

rotationVec :: Vector3 -> Float -> Matrix4
rotationVec (Vector3 v) f  = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationVec (unpack v) f )

rotationEuler :: Vector3 -> Matrix4
rotationEuler (Vector3 v)  = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationEuler (unpack v) )

rotationQuat :: Quaternion -> Matrix4
rotationQuat q = Matrix4 (Data.Vec.LinAlg.Transform3D.rotationQuat v4 ) where
	w = qW q
	v = qV q
	x = v3X v
	y = v3Y v
	z = v3Z v
	v4 = x :. y :. z :. w :. ()