diff options
author | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-30 20:24:50 +0200 |
---|---|---|
committer | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-30 20:24:50 +0200 |
commit | 7068585f77bab617d6d688a98c4ff72329b325d9 (patch) | |
tree | 499f28ab50345ab32abc8439d65f450c85915711 | |
parent | 38cf114a03d0c7d5a9c154af9e095dfdb4c4c2f5 (diff) |
Added support for custom programs
-rw-r--r-- | Spear/Scene/Loader.hs | 94 | ||||
-rw-r--r-- | Spear/Scene/SceneResources.hs | 25 |
2 files changed, 80 insertions, 39 deletions
diff --git a/Spear/Scene/Loader.hs b/Spear/Scene/Loader.hs index 0ef5333..e3b9546 100644 --- a/Spear/Scene/Loader.hs +++ b/Spear/Scene/Loader.hs | |||
@@ -107,28 +107,32 @@ loadResource key field modifyResources load = do | |||
107 | return resource | 107 | return resource |
108 | 108 | ||
109 | 109 | ||
110 | addShader name shader = | 110 | addShader name shader = modify $ \sceneData -> |
111 | modify $ \sceneData -> sceneData { shaders = M.insert name shader $ shaders sceneData } | 111 | sceneData { shaders = M.insert name shader $ shaders sceneData } |
112 | 112 | ||
113 | 113 | ||
114 | addStaticProgram name prog = | 114 | addCustomProgram name prog = modify $ \sceneData -> |
115 | modify $ \sceneData -> sceneData { staticPrograms = M.insert name prog $ staticPrograms sceneData } | 115 | sceneData { customPrograms = M.insert name prog $ customPrograms sceneData } |
116 | 116 | ||
117 | 117 | ||
118 | addAnimatedProgram name prog = | 118 | addStaticProgram name prog = modify $ \sceneData -> |
119 | modify $ \sceneData -> sceneData { animatedPrograms = M.insert name prog $ animatedPrograms sceneData } | 119 | sceneData { staticPrograms = M.insert name prog $ staticPrograms sceneData } |
120 | 120 | ||
121 | 121 | ||
122 | addTexture name tex = | 122 | addAnimatedProgram name prog = modify $ \sceneData -> |
123 | modify $ \sceneData -> sceneData { textures = M.insert name tex $ textures sceneData } | 123 | sceneData { animatedPrograms = M.insert name prog $ animatedPrograms sceneData } |
124 | 124 | ||
125 | 125 | ||
126 | addStaticModel name model = | 126 | addTexture name tex = modify $ \sceneData -> |
127 | modify $ \sceneData -> sceneData { staticModels = M.insert name model $ staticModels sceneData } | 127 | sceneData { textures = M.insert name tex $ textures sceneData } |
128 | 128 | ||
129 | 129 | ||
130 | addAnimatedModel name model = | 130 | addStaticModel name model = modify $ |
131 | modify $ \sceneData -> sceneData { animatedModels = M.insert name model $ animatedModels sceneData } | 131 | \sceneData -> sceneData { staticModels = M.insert name model $ staticModels sceneData } |
132 | |||
133 | |||
134 | addAnimatedModel name model = modify $ | ||
135 | \sceneData -> sceneData { animatedModels = M.insert name model $ animatedModels sceneData } | ||
132 | 136 | ||
133 | 137 | ||
134 | -- Get the given resource from the data pool. | 138 | -- Get the given resource from the data pool. |
@@ -237,33 +241,33 @@ newShaderProgram (SceneLeaf _ props) = do | |||
237 | (fsName, fragShader) <- Spear.Scene.Loader.loadShader GLSL.FragmentShader props | 241 | (fsName, fragShader) <- Spear.Scene.Loader.loadShader GLSL.FragmentShader props |
238 | name <- asString $ mandatory' "name" props | 242 | name <- asString $ mandatory' "name" props |
239 | stype <- asString $ mandatory' "type" props | 243 | stype <- asString $ mandatory' "type" props |
240 | texChan <- fmap read $ asString $ mandatory' "texture-channel" props | ||
241 | ambient <- asString $ mandatory' "ambient" props | ||
242 | diffuse <- asString $ mandatory' "diffuse" props | ||
243 | specular <- asString $ mandatory' "specular" props | ||
244 | shininess <- asString $ mandatory' "shininess" props | ||
245 | texture <- asString $ mandatory' "texture" props | ||
246 | modelview <- asString $ mandatory' "modelview" props | ||
247 | normalmat <- asString $ mandatory' "normalmat" props | ||
248 | projection <- asString $ mandatory' "projection" props | ||
249 | prog <- loaderSetup $ GLSL.newProgram [vertShader, fragShader] | 244 | prog <- loaderSetup $ GLSL.newProgram [vertShader, fragShader] |
250 | 245 | ||
251 | let getUniformLoc name = | 246 | let getUniformLoc name = loaderSetup $ (setupIO . SV.get $ GLSL.uniformLocation prog name) `GLSL.assertGL` name |
252 | loaderSetup $ (setupIO . SV.get $ GLSL.uniformLocation prog name) `GLSL.assertGL` name | ||
253 | |||
254 | ka <- getUniformLoc ambient | ||
255 | kd <- getUniformLoc diffuse | ||
256 | ks <- getUniformLoc specular | ||
257 | shi <- getUniformLoc shininess | ||
258 | tex <- getUniformLoc texture | ||
259 | mview <- getUniformLoc modelview | ||
260 | nmat <- getUniformLoc normalmat | ||
261 | proj <- getUniformLoc projection | ||
262 | 247 | ||
263 | case stype of | 248 | case stype of |
264 | "static" -> do | 249 | "static" -> do |
250 | ambient <- asString $ mandatory' "ambient" props | ||
251 | diffuse <- asString $ mandatory' "diffuse" props | ||
252 | specular <- asString $ mandatory' "specular" props | ||
253 | shininess <- asString $ mandatory' "shininess" props | ||
254 | texture <- asString $ mandatory' "texture" props | ||
255 | modelview <- asString $ mandatory' "modelview" props | ||
256 | normalmat <- asString $ mandatory' "normalmat" props | ||
257 | projection <- asString $ mandatory' "projection" props | ||
258 | |||
259 | ka <- getUniformLoc ambient | ||
260 | kd <- getUniformLoc diffuse | ||
261 | ks <- getUniformLoc specular | ||
262 | shi <- getUniformLoc shininess | ||
263 | tex <- getUniformLoc texture | ||
264 | mview <- getUniformLoc modelview | ||
265 | nmat <- getUniformLoc normalmat | ||
266 | proj <- getUniformLoc projection | ||
267 | |||
265 | vertChan <- fmap read $ asString $ mandatory' "vertex-channel" props | 268 | vertChan <- fmap read $ asString $ mandatory' "vertex-channel" props |
266 | normChan <- fmap read $ asString $ mandatory' "normal-channel" props | 269 | normChan <- fmap read $ asString $ mandatory' "normal-channel" props |
270 | texChan <- fmap read $ asString $ mandatory' "texture-channel" props | ||
267 | 271 | ||
268 | let channels = StaticProgramChannels vertChan normChan texChan | 272 | let channels = StaticProgramChannels vertChan normChan texChan |
269 | uniforms = StaticProgramUniforms ka kd ks shi tex mview nmat proj | 273 | uniforms = StaticProgramUniforms ka kd ks shi tex mview nmat proj |
@@ -273,10 +277,29 @@ newShaderProgram (SceneLeaf _ props) = do | |||
273 | return () | 277 | return () |
274 | 278 | ||
275 | "animated" -> do | 279 | "animated" -> do |
280 | ambient <- asString $ mandatory' "ambient" props | ||
281 | diffuse <- asString $ mandatory' "diffuse" props | ||
282 | specular <- asString $ mandatory' "specular" props | ||
283 | shininess <- asString $ mandatory' "shininess" props | ||
284 | texture <- asString $ mandatory' "texture" props | ||
285 | modelview <- asString $ mandatory' "modelview" props | ||
286 | normalmat <- asString $ mandatory' "normalmat" props | ||
287 | projection <- asString $ mandatory' "projection" props | ||
288 | |||
289 | ka <- getUniformLoc ambient | ||
290 | kd <- getUniformLoc diffuse | ||
291 | ks <- getUniformLoc specular | ||
292 | shi <- getUniformLoc shininess | ||
293 | tex <- getUniformLoc texture | ||
294 | mview <- getUniformLoc modelview | ||
295 | nmat <- getUniformLoc normalmat | ||
296 | proj <- getUniformLoc projection | ||
297 | |||
276 | vertChan1 <- fmap read $ asString $ mandatory' "vertex-channel1" props | 298 | vertChan1 <- fmap read $ asString $ mandatory' "vertex-channel1" props |
277 | vertChan2 <- fmap read $ asString $ mandatory' "vertex-channel2" props | 299 | vertChan2 <- fmap read $ asString $ mandatory' "vertex-channel2" props |
278 | normChan1 <- fmap read $ asString $ mandatory' "normal-channel1" props | 300 | normChan1 <- fmap read $ asString $ mandatory' "normal-channel1" props |
279 | normChan2 <- fmap read $ asString $ mandatory' "normal-channel2" props | 301 | normChan2 <- fmap read $ asString $ mandatory' "normal-channel2" props |
302 | texChan <- fmap read $ asString $ mandatory' "texture-channel" props | ||
280 | fp <- asString $ mandatory' "fp" props | 303 | fp <- asString $ mandatory' "fp" props |
281 | p <- getUniformLoc fp | 304 | p <- getUniformLoc fp |
282 | 305 | ||
@@ -286,6 +309,13 @@ newShaderProgram (SceneLeaf _ props) = do | |||
286 | loadResource name animatedPrograms addAnimatedProgram $ | 309 | loadResource name animatedPrograms addAnimatedProgram $ |
287 | return $ AnimatedProgram prog channels uniforms | 310 | return $ AnimatedProgram prog channels uniforms |
288 | return () | 311 | return () |
312 | |||
313 | _ -> do | ||
314 | loadResource name customPrograms addCustomProgram $ return prog | ||
315 | return () | ||
316 | |||
317 | |||
318 | |||
289 | 319 | ||
290 | 320 | ||
291 | loadShader :: GLSL.ShaderType -> [Property] -> Loader (String, GLSL.GLSLShader) | 321 | loadShader :: GLSL.ShaderType -> [Property] -> Loader (String, GLSL.GLSLShader) |
diff --git a/Spear/Scene/SceneResources.hs b/Spear/Scene/SceneResources.hs index 037e3aa..ab96dc6 100644 --- a/Spear/Scene/SceneResources.hs +++ b/Spear/Scene/SceneResources.hs | |||
@@ -1,10 +1,14 @@ | |||
1 | module Spear.Scene.SceneResources | 1 | module Spear.Scene.SceneResources |
2 | ( | 2 | ( |
3 | -- * Data types | ||
3 | SceneResources(..) | 4 | SceneResources(..) |
4 | , StaticProgram(..) | 5 | , StaticProgram(..) |
5 | , AnimatedProgram(..) | 6 | , AnimatedProgram(..) |
7 | -- * Construction | ||
6 | , emptySceneResources | 8 | , emptySceneResources |
9 | -- * Accessors | ||
7 | , getShader | 10 | , getShader |
11 | , getCustomProgram | ||
8 | , getStaticProgram | 12 | , getStaticProgram |
9 | , getAnimatedProgram | 13 | , getAnimatedProgram |
10 | , getTexture | 14 | , getTexture |
@@ -28,6 +32,7 @@ import Data.Map as M | |||
28 | 32 | ||
29 | data SceneResources = SceneResources | 33 | data SceneResources = SceneResources |
30 | { shaders :: Map String GLSLShader | 34 | { shaders :: Map String GLSLShader |
35 | , customPrograms :: Map String GLSLProgram | ||
31 | , staticPrograms :: Map String StaticProgram | 36 | , staticPrograms :: Map String StaticProgram |
32 | , animatedPrograms :: Map String AnimatedProgram | 37 | , animatedPrograms :: Map String AnimatedProgram |
33 | , textures :: Map String Texture | 38 | , textures :: Map String Texture |
@@ -38,34 +43,40 @@ data SceneResources = SceneResources | |||
38 | 43 | ||
39 | 44 | ||
40 | -- | Build an empty instance of 'SceneResources'. | 45 | -- | Build an empty instance of 'SceneResources'. |
41 | emptySceneResources = SceneResources M.empty M.empty M.empty M.empty M.empty M.empty [] | 46 | emptySceneResources = |
47 | SceneResources M.empty M.empty M.empty M.empty M.empty M.empty M.empty [] | ||
42 | 48 | ||
43 | 49 | ||
44 | -- | Get the 'GLSLShader' specified by the given 'String' from the given 'SceneResources'. | 50 | -- | Get the shader specified by the given string. |
45 | getShader :: SceneResources -> String -> Maybe GLSLShader | 51 | getShader :: SceneResources -> String -> Maybe GLSLShader |
46 | getShader res key = M.lookup key $ shaders res | 52 | getShader res key = M.lookup key $ shaders res |
47 | 53 | ||
48 | 54 | ||
49 | -- | Get the 'StaticProgram' specified by the given 'String' from the given 'SceneResources'. | 55 | -- | Get the custom program specified by the given string. |
56 | getCustomProgram :: SceneResources -> String -> Maybe GLSLProgram | ||
57 | getCustomProgram res key = M.lookup key $ customPrograms res | ||
58 | |||
59 | |||
60 | -- | Get the static program specified by the given string. | ||
50 | getStaticProgram :: SceneResources -> String -> Maybe StaticProgram | 61 | getStaticProgram :: SceneResources -> String -> Maybe StaticProgram |
51 | getStaticProgram res key = M.lookup key $ staticPrograms res | 62 | getStaticProgram res key = M.lookup key $ staticPrograms res |
52 | 63 | ||
53 | 64 | ||
54 | -- | Get the 'AnimatedProgram' specified by the given 'String' from the given 'SceneResources'. | 65 | -- | Get the animated program specified by the given string. |
55 | getAnimatedProgram :: SceneResources -> String -> Maybe AnimatedProgram | 66 | getAnimatedProgram :: SceneResources -> String -> Maybe AnimatedProgram |
56 | getAnimatedProgram res key = M.lookup key $ animatedPrograms res | 67 | getAnimatedProgram res key = M.lookup key $ animatedPrograms res |
57 | 68 | ||
58 | 69 | ||
59 | -- | Get the 'Texture' specified by the given 'String' from the given 'SceneResources'. | 70 | -- | Get the texture specified by the given string. |
60 | getTexture :: SceneResources -> String -> Maybe Texture | 71 | getTexture :: SceneResources -> String -> Maybe Texture |
61 | getTexture res key = M.lookup key $ textures res | 72 | getTexture res key = M.lookup key $ textures res |
62 | 73 | ||
63 | 74 | ||
64 | -- | Get the 'StaticModelResource' specified by the given 'String' from the given 'SceneResources'. | 75 | -- | Get the static model resource specified by the given string. |
65 | getStaticModel :: SceneResources -> String -> Maybe StaticModelResource | 76 | getStaticModel :: SceneResources -> String -> Maybe StaticModelResource |
66 | getStaticModel res key = M.lookup key $ staticModels res | 77 | getStaticModel res key = M.lookup key $ staticModels res |
67 | 78 | ||
68 | 79 | ||
69 | -- | Get the 'AnimatedModelResource' specified by the given 'String' from the given 'SceneResources'. | 80 | -- | Get the animated model resource specified by the given string. |
70 | getAnimatedModel :: SceneResources -> String -> Maybe AnimatedModelResource | 81 | getAnimatedModel :: SceneResources -> String -> Maybe AnimatedModelResource |
71 | getAnimatedModel res key = M.lookup key $ animatedModels res | 82 | getAnimatedModel res key = M.lookup key $ animatedModels res |