From ca8179a93e2c5ac0caa168c1da57e36f654182d9 Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Wed, 8 Aug 2012 15:16:52 +0200 Subject: Model transformVerts and transformNormals are now two separate functions --- Spear.lkshs | 10 ++++---- Spear.lkshw | 2 +- Spear/Assets/Model.hsc | 62 +++++++++++++++++++++++++++------------------- Spear/Assets/Model/Model.c | 19 ++++++++++---- Spear/Assets/Model/Model.h | 9 ++++--- Spear/Scene/Loader.hs | 21 ++++++++++------ 6 files changed, 76 insertions(+), 47 deletions(-) diff --git a/Spear.lkshs b/Spear.lkshs index 26a1ff6..a3f492f 100644 --- a/Spear.lkshs +++ b/Spear.lkshs @@ -1,18 +1,18 @@ Version of session file format: 1 Time of storage: - "Tue Aug 7 23:33:12 CEST 2012" -Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 2, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 263) 197)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 691) 954 -Population: [(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs" 75)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Collision/Collisioner.hs" 551)),[SplitP LeftP]),(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/GameObject.hs" 3124)),[SplitP LeftP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 286 (SystemScope,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics.hs" 207)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Rigid.hs" 2175)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/Spatial.hs" 0)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Types.hs" 194)),[SplitP LeftP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/World.hs" 3782)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 1603)),[SplitP LeftP])] + "Wed Aug 8 13:25:10 CEST 2012" +Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 1, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 273) 205)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 716) 954 +Population: [(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/GameObject.hs" 3129)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/IDStore.hs" 186)),[SplitP LeftP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 286 (PackageScope False,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([[5]],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics.hs" 207)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Rigid.hs" 2175)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/Spatial.hs" 0)),[SplitP LeftP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/World.hs" 3781)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 1603)),[SplitP LeftP])] Window size: (1841,964) Completion size: (750,400) Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" -Active pane: Just "GameObject.hs" +Active pane: Just "IDStore.hs" Toolbar visible: True FindbarState: (False,FindState {entryStr = "asd", entryHist = ["gravity","asdad","rotSpeed","azimuth","mandatory","mandao","col","forward","MouseButton"], replaceStr = "mandatory'", replaceHist = [], caseSensitive = False, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) Recently opened files: - ["/home/jeanne/programming/haskell/Spear/Spear/App/Input.hs"] + ["/home/jeanne/programming/haskell/Spear/demos/simple-scene/Spear/IDStore.hs","/home/jeanne/programming/haskell/Spear/Spear/Physics/Types.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs","/home/jeanne/programming/haskell/Spear/Spear/Collision/Collisioner.hs","/home/jeanne/programming/haskell/Spear/Spear/App/Input.hs"] Recently opened workspaces: ["/home/jeanne/leksah.lkshw"] \ No newline at end of file diff --git a/Spear.lkshw b/Spear.lkshw index ef77439..cc572e4 100644 --- a/Spear.lkshw +++ b/Spear.lkshw @@ -1,7 +1,7 @@ Version of workspace file format: 1 Time of storage: - "Wed Aug 8 12:06:13 CEST 2012" + "Wed Aug 8 15:15:08 CEST 2012" Name of the workspace: "Spear" File paths of contained packages: diff --git a/Spear/Assets/Model.hsc b/Spear/Assets/Model.hsc index 5fd2db3..9e718c7 100644 --- a/Spear/Assets/Model.hsc +++ b/Spear/Assets/Model.hsc @@ -29,7 +29,8 @@ module Spear.Assets.Model , animationByName , numAnimations -- * Manipulation -, transform +, transformVerts +, transformNormals , toGround ) where @@ -232,72 +233,72 @@ fromCAnimation (CAnimation cname start end) = Animation (B.unpack cname) (fromIntegral start) (fromIntegral end) --- | Release the given 'Model'. +-- | Release the model. releaseModel :: Model -> Setup () releaseModel = release . rkey --- | Free the given 'CModel'. +-- | Free the C model. freeModel :: CModel -> IO () freeModel model = Foreign.with model model_free --- | Return 'True' if the given 'Model' is animated, 'False' otherwise. +-- | Return 'True' if the model is animated, 'False' otherwise. animated :: Model -> Bool animated = (>1) . numFrames --- | Return the given 'Model's vertices. +-- | Return the model's vertices. vertices :: Model -> Ptr Vec3 vertices = cVerts . modelData --- | Return the given 'Model's normals. +-- | Return the model's normals. normals :: Model -> Ptr Vec3 normals = cNormals . modelData --- | Return the given 'Model's texCoords. +-- | Return the model's texCoords. texCoords :: Model -> Ptr TexCoord texCoords = cTexCoords . modelData --- | Return the given 'Model's triangles. +-- | Return the model's triangles. triangles :: Model -> Ptr Triangle triangles = cTris . modelData --- | Return the given 'Model's skins. +-- | Return the model's skins. skins :: Model -> Ptr Skin skins = cSkins . modelData --- | Return the given 'Model's number of frames. +-- | Return the model's number of frames. numFrames :: Model -> Int numFrames = fromIntegral . cnFrames . modelData --- | Return the given 'Model's number of vertices. +-- | Return the model's number of vertices. numVertices :: Model -> Int numVertices = fromIntegral . cnVerts . modelData --- | Return the given 'Model's number of triangles. +-- | Return the model's number of triangles. numTriangles :: Model -> Int numTriangles = fromIntegral . cnTris . modelData --- | Return the given 'Model's number of texture coordinates. +-- | Return the model's number of texture coordinates. numTexCoords :: Model -> Int numTexCoords = fromIntegral . cnTexCoords . modelData --- | Return the given 'Model's number of skins. +-- | Return the model's number of skins. numSkins :: Model -> Int numSkins = fromIntegral . cnSkins . modelData --- | Return the underlying 'CModel'. +-- | Return the underlying C model. cmodel :: Model -> CModel cmodel = modelData @@ -312,27 +313,38 @@ animationByName :: Model -> String -> Maybe Animation animationByName model anim = V.find ((==) anim . name) $ mAnimations model --- | Return the number of animations in the given 'Model'. +-- | Return the number of animations of the given model. numAnimations :: Model -> Int numAnimations = V.length . mAnimations --- | Transform the given 'Model's vertices with the given matrix. -transform :: M4.Matrix4 -> Model -> IO () -transform mat (Model model _ _) = +-- | Transform the model's vertices with the given matrix. +transformVerts :: M4.Matrix4 -> Model -> IO () +transformVerts mat (Model model _ _) = allocaBytes (16*sizeFloat) $ \matPtr -> - allocaBytes (9*sizeFloat) $ \normalPtr -> with model $ \modelPtr -> do poke matPtr mat - poke normalPtr $ fastNormalMatrix mat - model_transform modelPtr matPtr normalPtr + model_transform_vertices modelPtr matPtr -foreign import ccall "Model.h model_transform" - model_transform :: Ptr CModel -> Ptr M4.Matrix4 -> Ptr M3.Matrix3 -> IO () +-- | Transform the model's normals with the given matrix. +transformNormals :: M3.Matrix3 -> Model -> IO () +transformNormals mat (Model model _ _) = + allocaBytes (9*sizeFloat) $ \normalPtr -> + with model $ \modelPtr -> do + poke normalPtr mat + model_transform_normals modelPtr normalPtr + + +foreign import ccall "Model.h model_transform_vertices" + model_transform_vertices :: Ptr CModel -> Ptr M4.Matrix4 -> IO () + + +foreign import ccall "Model.h model_transform_normals" + model_transform_normals :: Ptr CModel -> Ptr M3.Matrix3 -> IO () --- | Transform the given 'Model' such that its lowest point has y = 0. +-- | Transform the model such that its lowest point has y = 0. toGround :: Model -> IO () toGround (Model model _ _) = with model model_to_ground diff --git a/Spear/Assets/Model/Model.c b/Spear/Assets/Model/Model.c index 837dd93..f6b2f1f 100644 --- a/Spear/Assets/Model/Model.c +++ b/Spear/Assets/Model/Model.c @@ -56,19 +56,28 @@ static void mul_normal (float m[9], vec3* n) } -void model_transform (Model* model, float mat[16], float normal[9]) +void model_transform_vertices (Model* model, float mat[16]) { unsigned i = 0; unsigned j = model->numVertices * model->numFrames; vec3* v = model->vertices; - vec3* n = model->normals; - for (; i < j; ++i) + for (; i < j; ++i, ++v) { mul (mat, v); + } +} + + +void model_transform_normals (Model* model, float normal[9]) +{ + unsigned i = 0; + unsigned j = model->numVertices * model->numFrames; + vec3* n = model->normals; + + for (; i < j; ++i, ++n) + { mul_normal (normal, n); - v++; - n++; } } diff --git a/Spear/Assets/Model/Model.h b/Spear/Assets/Model/Model.h index 34c6444..84be6aa 100644 --- a/Spear/Assets/Model/Model.h +++ b/Spear/Assets/Model/Model.h @@ -69,10 +69,13 @@ extern "C" { /// The 'model' pointer itself is not freed. void model_free (Model* model); -/// Transform the given Model's vertices by the given matrix. -void model_transform (Model* model, float mat[16], float normal[9]); +/// Transform the Model's vertices by the given matrix. +void model_transform_verts (Model* model, float mat[16]); -/// Translate the given Model such that its lowest point has y = 0. +/// Transform the Model's normals by the given matrix. +void model_transform_normals (Model* model, float normal[9]); + +/// Translate the Model such that its lowest point has y = 0. void model_to_ground (Model* model); #ifdef __cplusplus diff --git a/Spear/Scene/Loader.hs b/Spear/Scene/Loader.hs index 6001bdb..06e0aa9 100644 --- a/Spear/Scene/Loader.hs +++ b/Spear/Scene/Loader.hs @@ -20,6 +20,7 @@ where import Spear.Assets.Model as Model import qualified Spear.GLSL as GLSL import Spear.Math.Matrix4 as M4 +import Spear.Math.MatrixUtils (fastNormalMatrix) import Spear.Math.Vector3 as V3 import Spear.Math.Vector4 import Spear.Render.AnimatedModel @@ -192,20 +193,24 @@ loadModel' file rotation scale = do Just rot -> setupIO $ rotateModel model rot Nothing -> return () case scale of - Just s -> setupIO $ Model.transform (scalev s) model + Just s -> setupIO $ Model.transformVerts (scalev s) model Nothing -> return () setupIO $ toGround model return model rotateModel :: Model -> Rotation -> IO () -rotateModel model (Rotation x y z order) = case order of - XYZ -> Model.transform (rotZ z * rotY y * rotX x) model - XZY -> Model.transform (rotY y * rotZ z * rotX x) model - YXZ -> Model.transform (rotZ z * rotX x * rotY y) model - YZX -> Model.transform (rotX x * rotZ z * rotY y) model - ZXY -> Model.transform (rotY y * rotX x * rotZ z) model - ZYX -> Model.transform (rotX x * rotY y * rotZ z) model +rotateModel model (Rotation x y z order) = + let mat = case order of + XYZ -> rotZ z * rotY y * rotX x + XZY -> rotY y * rotZ z * rotX x + YXZ -> rotZ z * rotX x * rotY y + YZX -> rotX x * rotZ z * rotY y + ZXY -> rotY y * rotX x * rotZ z + ZYX -> rotX x * rotY y * rotZ z + normalMat = fastNormalMatrix mat + in + Model.transformVerts mat model >> Model.transformNormals normalMat model loadTexture :: FilePath -> Loader GLSL.Texture -- cgit v1.2.3