aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear.cabal29
-rw-r--r--Spear.lkshs14
-rw-r--r--Spear.lkshw2
-rw-r--r--Spear/Assets/Model.hsc139
-rw-r--r--Spear/Assets/Model/Model.c27
-rw-r--r--Spear/Assets/Model/Model.h3
-rw-r--r--Spear/Render/AnimatedModel.hs29
-rw-r--r--Spear/Render/StaticModel.hs7
8 files changed, 179 insertions, 71 deletions
diff --git a/Spear.cabal b/Spear.cabal
index ffe11dc..254f181 100644
--- a/Spear.cabal
+++ b/Spear.cabal
@@ -16,24 +16,25 @@ library
16 StateVar -any, base -any, bytestring -any, directory -any, 16 StateVar -any, base -any, bytestring -any, directory -any,
17 mtl -any, transformers -any, resourcet -any, parsec >=3.1.3, 17 mtl -any, transformers -any, resourcet -any, parsec >=3.1.3,
18 containers -any, vector -any, array -any 18 containers -any, vector -any, array -any
19 exposed-modules: Spear.Math.QuadTree Spear.Physics.Types Spear.App 19 exposed-modules: Spear.Scene.GameObject Spear.Math.QuadTree
20 Spear.App.Application Spear.App.Input Spear.Assets.Image 20 Spear.Physics.Types Spear.App Spear.App.Application Spear.App.Input
21 Spear.Assets.Model Spear.Collision Spear.Math.AABB 21 Spear.Assets.Image Spear.Assets.Model Spear.Collision
22 Spear.Collision.Collision Spear.Collision.Collisioner 22 Spear.Math.AABB Spear.Collision.Collision
23 Spear.Math.Circle Spear.Math.Triangle Spear.Collision.Types 23 Spear.Collision.Collisioner Spear.Math.Circle Spear.Math.Triangle
24 Spear.Game Spear.GLSL Spear.GLSL.Buffer Spear.GLSL.Error 24 Spear.Collision.Types Spear.Game Spear.GLSL Spear.GLSL.Buffer
25 Spear.GLSL.Management Spear.GLSL.Texture Spear.GLSL.Uniform 25 Spear.GLSL.Error Spear.GLSL.Management Spear.GLSL.Texture
26 Spear.GLSL.VAO Spear.Math.Camera Spear.Math.Entity 26 Spear.GLSL.Uniform Spear.GLSL.VAO Spear.Math.Camera
27 Spear.Math.Matrix3 Spear.Math.Matrix4 Spear.Math.MatrixUtils 27 Spear.Math.Entity Spear.Math.Matrix3 Spear.Math.Matrix4
28 Spear.Math.Plane Spear.Math.Quaternion Spear.Math.Spatial 28 Spear.Math.MatrixUtils Spear.Math.Plane Spear.Math.Quaternion
29 Spear.Math.Vector3 Spear.Math.Vector4 Spear.Physics 29 Spear.Math.Spatial Spear.Math.Vector3 Spear.Math.Vector4
30 Spear.Physics.Rigid Spear.Render.AnimatedModel 30 Spear.Physics Spear.Physics.Rigid Spear.Render.AnimatedModel
31 Spear.Render.Material Spear.Render.Model Spear.Render.Program 31 Spear.Render.Material Spear.Render.Model Spear.Render.Program
32 Spear.Render.Renderable Spear.Render.StaticModel 32 Spear.Render.Renderable Spear.Render.StaticModel
33 Spear.Render.Texture Spear.Scene.Graph Spear.Scene.Light 33 Spear.Render.Texture Spear.Scene.Graph Spear.Scene.Light
34 Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources 34 Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources
35 Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID 35 Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID
36 Spear.Updatable Spear.Math.Vector2 36 Spear.Updatable Spear.Math.Vector2 Spear.Math.Quad Spear.Math.Ray
37 Spear.Math.Segment Spear.Math.Utils
37 exposed: True 38 exposed: True
38 buildable: True 39 buildable: True
39 build-tools: hsc2hs -any 40 build-tools: hsc2hs -any
@@ -56,4 +57,4 @@ library
56 Spear/Sys 57 Spear/Sys
57 hs-source-dirs: . 58 hs-source-dirs: .
58 ghc-options: -O2 -rtsopts 59 ghc-options: -O2 -rtsopts
59 60 \ No newline at end of file
diff --git a/Spear.lkshs b/Spear.lkshs
index 2663b79..8ff60d0 100644
--- a/Spear.lkshs
+++ b/Spear.lkshs
@@ -1,18 +1,18 @@
1Version of session file format: 1Version of session file format:
2 1 2 1
3Time of storage: 3Time of storage:
4 "Tue Aug 28 18:24:30 CEST 2012" 4 "Tue Aug 28 22:08:10 CEST 2012"
5Layout: 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}) 240) 199)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 696) 954 5Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 3, 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}) 308) 194)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 683) 954
6Population: [(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 328 (PackageScope False,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([[0]],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP])] 6Population: [(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Render/AnimatedModel.hs" 247)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs" 2235)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Collision/Collisioner.hs" 1019)),[SplitP LeftP]),(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Scene/GameObject.hs" 1898)),[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 328 (PackageScope False,False) (Just (ModuleName ["Spear","Collision","Collision"]),Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([[0,7],[0,4],[0,1],[0]],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP])]
7Window size: (1820,939) 7Window size: (1820,939)
8Completion size: 8Completion size:
9 (750,400) 9 (750,399)
10Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" 10Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw"
11Active pane: Just "Modules" 11Active pane: Just "GameObject.hs"
12Toolbar visible: 12Toolbar visible:
13 True 13 True
14FindbarState: (False,FindState {entryStr = "asad", entryHist = ["asad","Octree","idxs","asd","elemIndexa","elemtIn","splitAt","allocaBytes","copyArray","allocaArray","allocaa","putStrLn"], replaceStr = "QuadTree", replaceHist = [], caseSensitive = True, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) 14FindbarState: (False,FindState {entryStr = "asd", entryHist = ["copy_tr","asad","Octree","idxs","asd","elemIndexa","elemtIn","splitAt","allocaBytes","copyArray","allocaArray","allocaa"], replaceStr = "QuadTree", replaceHist = [], caseSensitive = True, entireWord = False, wrapAround = False, regex = False, lineNr = 1})
15Recently opened files: 15Recently opened files:
16 ["/home/jeanne/programming/haskell/Spear/Spear/Math/AABB.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Circle.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/Scene/Scene.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/QuadTree.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Sphere.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Triangle.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Spatial.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Quaternion.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Plane.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Octree.hs"] 16 ["/home/jeanne/programming/haskell/Spear/Spear/Collision/Collision.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/Matrix3.hs","/home/jeanne/programming/haskell/Spear/Spear/Math/AABB.hs","/home/jeanne/programming/haskell/Spear/Spear/Render/Texture.hs","/home/jeanne/programming/haskell/Spear/Spear/Render/Program.hs","/home/jeanne/programming/haskell/Spear/demos/simple-scene/Game/GameObject/Factory.hs","/home/jeanne/programming/haskell/Spear/Spear/Assets/Model.hsc","/home/jeanne/programming/haskell/Spear/Spear/Assets/Model/Model.h","/home/jeanne/programming/haskell/Spear/Spear/Assets/Model/Model.c","/home/jeanne/programming/haskell/Spear/Spear/App/Input.hs","/home/jeanne/programming/haskell/Spear/Spear/Scene/SceneResources.hs","/home/jeanne/programming/haskell/Spear/Spear/Render/StaticModel.hs"]
17Recently opened workspaces: 17Recently opened workspaces:
18 ["/home/jeanne/programming/haskell/hagen/hagen.lkshw","/home/jeanne/programming/haskell/foo/foo.lkshw","/home/jeanne/programming/haskell/Spear/Spear.lkshw","/home/jeanne/programming/haskell/nexus/nexus.lkshw","/home/jeanne/leksah.lkshw"] \ No newline at end of file 18 ["/home/jeanne/programming/haskell/hagen/hagen.lkshw","/home/jeanne/programming/haskell/foo/foo.lkshw","/home/jeanne/programming/haskell/Spear/Spear.lkshw","/home/jeanne/programming/haskell/nexus/nexus.lkshw","/home/jeanne/leksah.lkshw"] \ No newline at end of file
diff --git a/Spear.lkshw b/Spear.lkshw
index 5345907..b41cb88 100644
--- a/Spear.lkshw
+++ b/Spear.lkshw
@@ -1,7 +1,7 @@
1Version of workspace file format: 1Version of workspace file format:
2 1 2 1
3Time of storage: 3Time of storage:
4 "Tue Aug 28 17:23:50 CEST 2012" 4 "Tue Aug 28 21:08:58 CEST 2012"
5Name of the workspace: 5Name of the workspace:
6 "Spear" 6 "Spear"
7File paths of contained packages: 7File paths of contained packages:
diff --git a/Spear/Assets/Model.hsc b/Spear/Assets/Model.hsc
index b7cb90d..1b01062 100644
--- a/Spear/Assets/Model.hsc
+++ b/Spear/Assets/Model.hsc
@@ -3,9 +3,11 @@
3module Spear.Assets.Model 3module Spear.Assets.Model
4( 4(
5 -- * Data types 5 -- * Data types
6 Vec3(..) 6 Vec2(..)
7, Vec3(..)
7, TexCoord(..) 8, TexCoord(..)
8, CTriangle(..) 9, CTriangle(..)
10, Box(..)
9, Skin(..) 11, Skin(..)
10, Animation(..) 12, Animation(..)
11, Triangle(..) 13, Triangle(..)
@@ -21,6 +23,7 @@ module Spear.Assets.Model
21, transformVerts 23, transformVerts
22, transformNormals 24, transformNormals
23, toGround 25, toGround
26, modelBoxes
24) 27)
25where 28where
26 29
@@ -31,7 +34,8 @@ import Spear.Setup
31import qualified Data.ByteString.Char8 as B 34import qualified Data.ByteString.Char8 as B
32import Data.Char (toLower) 35import Data.Char (toLower)
33import Data.List (splitAt, elemIndex) 36import Data.List (splitAt, elemIndex)
34import qualified Data.Vector.Storable as V 37import qualified Data.Vector as V
38import qualified Data.Vector.Storable as S
35import Foreign.Ptr 39import Foreign.Ptr
36import Foreign.Storable 40import Foreign.Storable
37import Foreign.C.Types 41import Foreign.C.Types
@@ -61,8 +65,26 @@ sizeFloat = #{size float}
61sizePtr = #{size int*} 65sizePtr = #{size int*}
62 66
63 67
68-- | A 2D vector.
69data Vec2 = Vec2 {-# UNPACK #-} !Float {-# UNPACK #-} !Float
70
71
72instance Storable Vec2 where
73 sizeOf _ = 2*sizeFloat
74 alignment _ = alignment (undefined :: CFloat)
75
76 peek ptr = do
77 f0 <- peekByteOff ptr 0
78 f1 <- peekByteOff ptr sizeFloat
79 return $ Vec2 f0 f1
80
81 poke ptr (Vec2 f0 f1) = do
82 pokeByteOff ptr 0 f0
83 pokeByteOff ptr sizeFloat f1
84
85
64-- | A 3D vector. 86-- | A 3D vector.
65data Vec3 = Vec3 !Float !Float !Float 87data Vec3 = Vec3 {-# UNPACK #-} !Float {-# UNPACK #-} !Float {-# UNPACK #-} !Float
66 88
67 89
68instance Storable Vec3 where 90instance Storable Vec3 where
@@ -82,7 +104,7 @@ instance Storable Vec3 where
82 104
83 105
84-- | A 2D texture coordinate. 106-- | A 2D texture coordinate.
85data TexCoord = TexCoord !Float !Float 107data TexCoord = TexCoord {-# UNPACK #-} !Float {-# UNPACK #-} !Float
86 108
87 109
88instance Storable TexCoord where 110instance Storable TexCoord where
@@ -101,12 +123,12 @@ instance Storable TexCoord where
101 123
102-- | A raw triangle holding vertex/normal and texture indices. 124-- | A raw triangle holding vertex/normal and texture indices.
103data CTriangle = CTriangle 125data CTriangle = CTriangle
104 { vertexIndex0 :: !CUShort 126 { vertexIndex0 :: {-# UNPACK #-} !CUShort
105 , vertexIndex1 :: !CUShort 127 , vertexIndex1 :: {-# UNPACK #-} !CUShort
106 , vertexIndex2 :: !CUShort 128 , vertexIndex2 :: {-# UNPACK #-} !CUShort
107 , textureIndex1 :: !CUShort 129 , textureIndex1 :: {-# UNPACK #-} !CUShort
108 , textureIndex2 :: !CUShort 130 , textureIndex2 :: {-# UNPACK #-} !CUShort
109 , textureIndex3 :: !CUShort 131 , textureIndex3 :: {-# UNPACK #-} !CUShort
110 } 132 }
111 133
112 134
@@ -135,6 +157,28 @@ instance Storable CTriangle where
135 #{poke triangle, textureIndices[2]} ptr t2 157 #{poke triangle, textureIndices[2]} ptr t2
136 158
137 159
160-- | A 2D axis-aligned bounding box.
161data Box = Box {-# UNPACK #-} !Vec2 {-# UNPACK #-} !Vec2
162
163
164instance Storable Box where
165 sizeOf _ = 4 * sizeFloat
166 alignment _ = alignment (undefined :: CFloat)
167
168 peek ptr = do
169 f0 <- peekByteOff ptr 0
170 f1 <- peekByteOff ptr sizeFloat
171 f2 <- peekByteOff ptr $ 2*sizeFloat
172 f3 <- peekByteOff ptr $ 3*sizeFloat
173 return $ Box (Vec2 f0 f1) (Vec2 f2 f3)
174
175 poke ptr (Box (Vec2 f0 f1) (Vec2 f2 f3)) = do
176 pokeByteOff ptr 0 f0
177 pokeByteOff ptr sizeFloat f1
178 pokeByteOff ptr (2*sizeFloat) f2
179 pokeByteOff ptr (3*sizeFloat) f3
180
181
138-- | A model skin. 182-- | A model skin.
139newtype Skin = Skin { skinName :: B.ByteString } 183newtype Skin = Skin { skinName :: B.ByteString }
140 184
@@ -179,12 +223,12 @@ instance Storable Animation where
179 223
180-- | A 3D model. 224-- | A 3D model.
181data Model = Model 225data Model = Model
182 { vertices :: V.Vector Vec3 -- ^ Array of 'numFrames' * 'numVerts' vertices. 226 { vertices :: S.Vector Vec3 -- ^ Array of 'numFrames' * 'numVerts' vertices.
183 , normals :: V.Vector Vec3 -- ^ Array of 'numFrames' * 'numVerts' normals. 227 , normals :: S.Vector Vec3 -- ^ Array of 'numFrames' * 'numVerts' normals.
184 , texCoords :: V.Vector TexCoord -- ^ Array of 'numTexCoords' texture coordinates. 228 , texCoords :: S.Vector TexCoord -- ^ Array of 'numTexCoords' texture coordinates.
185 , triangles :: V.Vector CTriangle -- ^ Array of 'numTriangles' triangles. 229 , triangles :: S.Vector CTriangle -- ^ Array of 'numTriangles' triangles.
186 , skins :: V.Vector Skin -- ^ Array of 'numSkins' skins. 230 , skins :: S.Vector Skin -- ^ Array of 'numSkins' skins.
187 , animations :: V.Vector Animation -- ^ Array of 'numAnimations' animations. 231 , animations :: S.Vector Animation -- ^ Array of 'numAnimations' animations.
188 , numFrames :: Int -- ^ Number of frames. 232 , numFrames :: Int -- ^ Number of frames.
189 , numVerts :: Int -- ^ Number of vertices (and normals) per frame. 233 , numVerts :: Int -- ^ Number of vertices (and normals) per frame.
190 , numTriangles :: Int -- ^ Number of triangles in one frame. 234 , numTriangles :: Int -- ^ Number of triangles in one frame.
@@ -211,12 +255,12 @@ instance Storable Model where
211 pTriangles <- peekByteOff ptr (3*sizePtr) 255 pTriangles <- peekByteOff ptr (3*sizePtr)
212 pSkins <- peekByteOff ptr (4*sizePtr) 256 pSkins <- peekByteOff ptr (4*sizePtr)
213 pAnimations <- peekByteOff ptr (5*sizePtr) 257 pAnimations <- peekByteOff ptr (5*sizePtr)
214 vertices <- fmap V.fromList $ peekArray (numVertices*numFrames) pVerts 258 vertices <- fmap S.fromList $ peekArray (numVertices*numFrames) pVerts
215 normals <- fmap V.fromList $ peekArray (numVertices*numFrames) pNormals 259 normals <- fmap S.fromList $ peekArray (numVertices*numFrames) pNormals
216 texCoords <- fmap V.fromList $ peekArray numTexCoords pTexCoords 260 texCoords <- fmap S.fromList $ peekArray numTexCoords pTexCoords
217 triangles <- fmap V.fromList $ peekArray numTriangles pTriangles 261 triangles <- fmap S.fromList $ peekArray numTriangles pTriangles
218 skins <- fmap V.fromList $ peekArray numSkins pSkins 262 skins <- fmap S.fromList $ peekArray numSkins pSkins
219 animations <- fmap V.fromList $ peekArray numAnimations pAnimations 263 animations <- fmap S.fromList $ peekArray numAnimations pAnimations
220 return $ 264 return $
221 Model vertices normals texCoords triangles skins animations 265 Model vertices normals texCoords triangles skins animations
222 numFrames numVertices numTriangles numTexCoords numSkins numAnimations 266 numFrames numVertices numTriangles numTexCoords numSkins numAnimations
@@ -224,12 +268,12 @@ instance Storable Model where
224 poke ptr 268 poke ptr
225 (Model verts normals texCoords tris skins animations 269 (Model verts normals texCoords tris skins animations
226 numFrames numVerts numTris numTex numSkins numAnimations) = 270 numFrames numVerts numTris numTex numSkins numAnimations) =
227 V.unsafeWith verts $ \pVerts -> 271 S.unsafeWith verts $ \pVerts ->
228 V.unsafeWith normals $ \pNormals -> 272 S.unsafeWith normals $ \pNormals ->
229 V.unsafeWith texCoords $ \pTexCoords -> 273 S.unsafeWith texCoords $ \pTexCoords ->
230 V.unsafeWith tris $ \pTris -> 274 S.unsafeWith tris $ \pTris ->
231 V.unsafeWith skins $ \pSkins -> 275 S.unsafeWith skins $ \pSkins ->
232 V.unsafeWith animations $ \pAnimations -> do 276 S.unsafeWith animations $ \pAnimations -> do
233 #{poke Model, vertices} ptr pVerts 277 #{poke Model, vertices} ptr pVerts
234 #{poke Model, normals} ptr pNormals 278 #{poke Model, normals} ptr pNormals
235 #{poke Model, texCoords} ptr pTexCoords 279 #{poke Model, texCoords} ptr pTexCoords
@@ -349,13 +393,13 @@ animated = (>1) . numFrames
349 393
350-- | Return the model's ith animation. 394-- | Return the model's ith animation.
351animation :: Model -> Int -> Animation 395animation :: Model -> Int -> Animation
352animation model i = animations model V.! i 396animation model i = animations model S.! i
353 397
354 398
355-- | Return the animation specified by the given string. 399-- | Return the animation specified by the given string.
356animationByName :: Model -> String -> Maybe Animation 400animationByName :: Model -> String -> Maybe Animation
357animationByName model anim = 401animationByName model anim =
358 let anim' = B.pack anim in V.find ((==) anim' . name) $ animations model 402 let anim' = B.pack anim in S.find ((==) anim' . name) $ animations model
359 403
360 404
361-- | Return a copy of the model's triangles. 405-- | Return a copy of the model's triangles.
@@ -378,8 +422,8 @@ transformVerts :: Model -> (Vec3 -> Vec3) -> Model
378transformVerts model f = model { vertices = vertices' } 422transformVerts model f = model { vertices = vertices' }
379 where 423 where
380 n = numVerts model * numFrames model 424 n = numVerts model * numFrames model
381 vertices' = V.generate n f' 425 vertices' = S.generate n f'
382 f' i = f $ vertices model V.! i 426 f' i = f $ vertices model S.! i
383 427
384 428
385-- | Transform the model's normals. 429-- | Transform the model's normals.
@@ -387,14 +431,14 @@ transformNormals :: Model -> (Vec3 -> Vec3) -> Model
387transformNormals model f = model { normals = normals' } 431transformNormals model f = model { normals = normals' }
388 where 432 where
389 n = numVerts model * numFrames model 433 n = numVerts model * numFrames model
390 normals' = V.generate n f' 434 normals' = S.generate n f'
391 f' i = f $ normals model V.! i 435 f' i = f $ normals model S.! i
392 436
393 437
394-- | Translate the model such that its lowest point has y = 0. 438-- | Translate the model such that its lowest point has y = 0.
395toGround :: Model -> IO Model 439toGround :: Model -> IO Model
396toGround model = 440toGround model =
397 let model' = model { vertices = V.generate n $ \i -> vertices model V.! i } 441 let model' = model { vertices = S.generate n $ \i -> vertices model S.! i }
398 n = numVerts model * numFrames model 442 n = numVerts model * numFrames model
399 in 443 in
400 with model' model_to_ground >> return model' 444 with model' model_to_ground >> return model'
@@ -402,3 +446,28 @@ toGround model =
402 446
403foreign import ccall "Model.h model_to_ground" 447foreign import ccall "Model.h model_to_ground"
404 model_to_ground :: Ptr Model -> IO () 448 model_to_ground :: Ptr Model -> IO ()
449
450
451-- | Get the model's 2D bounding boxes.
452modelBoxes :: Model -> IO (V.Vector Box)
453modelBoxes model =
454 with model $ \modelPtr ->
455 allocaArray (numVerts model * numFrames model) $ \pointsPtr -> do
456 model_compute_boxes modelPtr pointsPtr
457 let n = numFrames model
458 getBoxes = peekBoxes pointsPtr n 0 0 $ return []
459 peekBoxes ptr n cur off l
460 | cur == n = l
461 | otherwise = do
462 f0 <- peekByteOff ptr off
463 f1 <- peekByteOff ptr $ off + sizeFloat
464 f2 <- peekByteOff ptr $ off + 2*sizeFloat
465 f3 <- peekByteOff ptr $ off + 3*sizeFloat
466 peekBoxes ptr n (cur+1) (off + 4*sizeFloat) $
467 fmap ((f3:) . (f2:) . (f1:) . (f0:)) l
468 fmap (V.fromList . reverse) getBoxes
469
470
471
472foreign import ccall "Model.h model_compute_boxes"
473 model_compute_boxes :: Ptr Model -> Ptr Vec2 -> IO ()
diff --git a/Spear/Assets/Model/Model.c b/Spear/Assets/Model/Model.c
index 4942566..6fa88c3 100644
--- a/Spear/Assets/Model/Model.c
+++ b/Spear/Assets/Model/Model.c
@@ -79,3 +79,30 @@ void model_copy_triangles (Model* model, unsigned frame, model_triangle* tris)
79 tris->t2 = t[tri->textureIndices[2]]; 79 tris->t2 = t[tri->textureIndices[2]];
80 } 80 }
81} 81}
82
83
84void model_box (Model* model, float* points)
85{
86 vec3* v = model->vertices;
87
88 unsigned f;
89 for (f = 0; f < model->numFrames; ++f)
90 {
91 float xmin = v->x;
92 float xmax = v->x;
93 float ymin = v->y;
94 float ymax = v->y;
95
96 unsigned i;
97 for (i = 0; i < model->numVertices; ++i, ++v)
98 {
99 xmin = fmin (xmin, v->x);
100 ymin = fmin (ymin, v->y);
101 xmax = fmax (xmax, v->x);
102 ymax = fmax (ymax, v->y);
103 }
104
105 *points++ = xmin; *points++ = ymin;
106 *points++ = xmax; *points++ = ymax;
107 }
108}
diff --git a/Spear/Assets/Model/Model.h b/Spear/Assets/Model/Model.h
index 0532322..eb9c39b 100644
--- a/Spear/Assets/Model/Model.h
+++ b/Spear/Assets/Model/Model.h
@@ -90,6 +90,9 @@ void model_to_ground (Model* model);
90/// Copy the triangles of the given frame from the Model into the given array. 90/// Copy the triangles of the given frame from the Model into the given array.
91void model_copy_triangles (Model* model, unsigned frame, model_triangle* tris); 91void model_copy_triangles (Model* model, unsigned frame, model_triangle* tris);
92 92
93/// Compute the model's 2d AABBs.
94void model_compute_boxes (Model* model, float* points);
95
93#ifdef __cplusplus 96#ifdef __cplusplus
94} 97}
95#endif 98#endif
diff --git a/Spear/Render/AnimatedModel.hs b/Spear/Render/AnimatedModel.hs
index cc31f12..8db87c3 100644
--- a/Spear/Render/AnimatedModel.hs
+++ b/Spear/Render/AnimatedModel.hs
@@ -9,6 +9,7 @@ module Spear.Render.AnimatedModel
9, currentAnimation 9, currentAnimation
10, bind 10, bind
11, render 11, render
12, update
12) 13)
13where 14where
14 15
@@ -18,10 +19,10 @@ import Spear.Render.Model
18import Spear.GLSL 19import Spear.GLSL
19import Spear.Render.Material 20import Spear.Render.Material
20import Spear.Render.Program 21import Spear.Render.Program
21import Spear.Updatable
22import Spear.Setup as Setup 22import Spear.Setup as Setup
23 23
24import Control.Applicative ((<$>), (<*>)) 24import Control.Applicative ((<$>), (<*>))
25import qualified Data.Vector as V
25import Graphics.Rendering.OpenGL.Raw.Core31 26import Graphics.Rendering.OpenGL.Raw.Core31
26import Unsafe.Coerce (unsafeCoerce) 27import Unsafe.Coerce (unsafeCoerce)
27 28
@@ -36,6 +37,7 @@ data AnimatedModelResource = AnimatedModelResource
36 , nVertices :: Int 37 , nVertices :: Int
37 , material :: Material 38 , material :: Material
38 , texture :: Texture 39 , texture :: Texture
40 , boxes :: V.Vector Box
39 , rkey :: Resource 41 , rkey :: Resource
40 } 42 }
41 43
@@ -89,6 +91,7 @@ animatedModelResource
89 RenderModel elements numFrames numVertices <- setupIO . renderModelFromModel $ model 91 RenderModel elements numFrames numVertices <- setupIO . renderModelFromModel $ model
90 elementBuf <- newBuffer 92 elementBuf <- newBuffer
91 vao <- newVAO 93 vao <- newVAO
94 boxes <- setupIO $ modelBoxes model
92 95
93 setupIO $ do 96 setupIO $ do
94 97
@@ -119,7 +122,8 @@ animatedModelResource
119 releaseBuffer elementBuf 122 releaseBuffer elementBuf
120 123
121 return $ AnimatedModelResource 124 return $ AnimatedModelResource
122 model vao (unsafeCoerce numFrames) (unsafeCoerce numVertices) material texture rkey 125 model vao (unsafeCoerce numFrames) (unsafeCoerce numVertices)
126 material texture boxes rkey
123 127
124 128
125-- | Release the given 'AnimatedModelResource'. 129-- | Release the given 'AnimatedModelResource'.
@@ -132,16 +136,17 @@ animatedModelRenderer :: AnimatedModelResource -> AnimatedModelRenderer
132animatedModelRenderer modelResource = AnimatedModelRenderer modelResource 0 0 0 0 0 136animatedModelRenderer modelResource = AnimatedModelRenderer modelResource 0 0 0 0 0
133 137
134 138
135instance Updatable AnimatedModelRenderer where 139-- | Update the 'AnimatedModelRenderer'.
136 140update dt (AnimatedModelRenderer model curAnim startFrame endFrame curFrame fp) =
137 update dt (AnimatedModelRenderer model curAnim startFrame endFrame curFrame fp) = 141 AnimatedModelRenderer model curAnim startFrame endFrame curFrame' fp'
138 AnimatedModelRenderer model curAnim startFrame endFrame curFrame' fp' 142 where f = fp + dt
139 where f = fp + dt 143 nextFrame = f >= 1.0
140 nextFrame = f >= 1.0 144 fp' = if nextFrame then f - 1.0 else f
141 fp' = if nextFrame then f - 1.0 else f 145 curFrame' = if nextFrame
142 curFrame' = if nextFrame 146 then
143 then let x = curFrame + 1 in if x > endFrame then startFrame else x 147 let x = curFrame + 1
144 else curFrame 148 in if x > endFrame then startFrame else x
149 else curFrame
145 150
146 151
147-- | Set the active animation to the given one. 152-- | Set the active animation to the given one.
diff --git a/Spear/Render/StaticModel.hs b/Spear/Render/StaticModel.hs
index 05e80e4..cefb7ed 100644
--- a/Spear/Render/StaticModel.hs
+++ b/Spear/Render/StaticModel.hs
@@ -18,6 +18,7 @@ import Spear.Render.Material
18import Spear.Render.Program 18import Spear.Render.Program
19import Spear.Setup as Setup 19import Spear.Setup as Setup
20 20
21import qualified Data.Vector as V
21import Graphics.Rendering.OpenGL.Raw.Core31 22import Graphics.Rendering.OpenGL.Raw.Core31
22import Unsafe.Coerce (unsafeCoerce) 23import Unsafe.Coerce (unsafeCoerce)
23 24
@@ -27,6 +28,7 @@ data StaticModelResource = StaticModelResource
27 , nVertices :: Int 28 , nVertices :: Int
28 , material :: Material 29 , material :: Material
29 , texture :: Texture 30 , texture :: Texture
31 , boxes :: V.Vector Box
30 , rkey :: Resource 32 , rkey :: Resource
31 } 33 }
32 34
@@ -61,6 +63,7 @@ staticModelResource (StaticProgramChannels vertChan normChan texChan) material t
61 RenderModel elements _ numVertices <- setupIO . renderModelFromModel $ model 63 RenderModel elements _ numVertices <- setupIO . renderModelFromModel $ model
62 elementBuf <- newBuffer 64 elementBuf <- newBuffer
63 vao <- newVAO 65 vao <- newVAO
66 boxes <- setupIO $ modelBoxes model
64 67
65 setupIO $ do 68 setupIO $ do
66 69
@@ -85,9 +88,9 @@ staticModelResource (StaticProgramChannels vertChan normChan texChan) material t
85 setupIO $ putStrLn "Releasing static model resource" 88 setupIO $ putStrLn "Releasing static model resource"
86 releaseVAO vao 89 releaseVAO vao
87 releaseBuffer elementBuf 90 releaseBuffer elementBuf
88 --sequence_ . fmap releaseBuffer $ [elementBuf, indexBuf]
89 91
90 return $ StaticModelResource vao (unsafeCoerce numVertices) material texture rkey 92 return $ StaticModelResource
93 vao (unsafeCoerce numVertices) material texture boxes rkey
91 94
92 95
93-- | Release the given 'StaticModelResource'. 96-- | Release the given 'StaticModelResource'.