module Physics.Hpysics.Visualization where import Graphics.Rendering.OpenGL hiding (Sphere,Plane) import qualified Graphics.UI.GLUT as GLUT import Physics.Hpysics.Types import Physics.Hpysics.Utils import Physics.Hpysics.Body import Physics.Hpysics.Poly withTranslatedCoords :: Vec -> IO () -> IO () withTranslatedCoords shift action = do matrixMode $= Modelview 0 let vecToGL (Vec x y z) = Vector3 x y z translate $ vecToGL $ shift action translate $ vecToGL $ neg shift withTransformedCoords :: Vec -> Quaternion -> IO () -> IO () withTransformedCoords shift rot action = preservingMatrix $ do rotationMatrix <- quaternionToGLMatrix rot withTranslatedCoords shift $ do multMatrix rotationMatrix action renderShape :: Shape -> IO () renderShape p = renderPrimitive Lines $ mapM_ ((\(Vec x1 y1 z1,Vec x2 y2 z2) -> (vertex$Vertex3 x1 y1 z1)>>(vertex$Vertex3 x2 y2 z2)).computeEdge) (edges p) renderBody :: Body -> IO () renderBody = renderShape . getShape renderWorld w = sequence_ $ map renderBody $ bodies w -- utils mat33ToGLMatrix :: Mat33 -> IO (GLmatrix FloatType) mat33ToGLMatrix (Mat33 a11 a12 a13 a21 a22 a23 a31 a32 a33) = newMatrix RowMajor [ a11, a12, a13, 0, a21, a22, a23, 0, a31, a32, a33, 0, 0, 0, 0, 1] quaternionToGLMatrix :: Quaternion -> IO (GLmatrix FloatType) quaternionToGLMatrix = mat33ToGLMatrix . quaternionToMatrix