diff options
author | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-22 21:55:34 -0600 |
---|---|---|
committer | Joshua Rahm <joshua.rahm@colorado.edu> | 2014-04-22 21:55:34 -0600 |
commit | 4d62a6d631fb9703d818654c5b9722e747cfe190 (patch) | |
tree | 97104038db6cd8e3a13eb0e529975fd2edddc4ce | |
parent | 14b6f5cee8236f7c42065813c5dd6a659d4d5d65 (diff) | |
download | terralloc-4d62a6d631fb9703d818654c5b9722e747cfe190.tar.gz terralloc-4d62a6d631fb9703d818654c5b9722e747cfe190.tar.bz2 terralloc-4d62a6d631fb9703d818654c5b9722e747cfe190.zip |
fixed most uniforms
-rw-r--r-- | Graphics/Glyph/Shaders.hs | 23 | ||||
-rw-r--r-- | Resources.hs | 40 | ||||
-rw-r--r-- | shaders/basic.frag | 4 | ||||
-rw-r--r-- | shaders/basic.vert | 6 | ||||
-rw-r--r-- | shaders/forest.frag | 6 | ||||
-rw-r--r-- | shaders/forest.vert | 8 |
6 files changed, 47 insertions, 40 deletions
diff --git a/Graphics/Glyph/Shaders.hs b/Graphics/Glyph/Shaders.hs index 296e4a8..fa8712c 100644 --- a/Graphics/Glyph/Shaders.hs +++ b/Graphics/Glyph/Shaders.hs @@ -77,22 +77,13 @@ createShaderProgramSafe shaders = createShaderProgram $ workingShaders shaders -{- Get the uniform form a program. -} -getUniform :: Uniform a => String -> IO (Maybe (StateVar a)) -getUniform name = - get currentProgram >>= (\pr -> case pr of - Just p -> liftM (Just . uniform) (get $ uniformLocation p name) - Nothing -> return Nothing ) - -getUniformForProgram :: Uniform a => String -> Program -> IO (StateVar a) -getUniformForProgram name prog = - liftM uniform (get $ uniformLocation prog name) - - -getUniformLocation :: String -> IO (Maybe UniformLocation) -getUniformLocation name = - get currentProgram >>= maybe (return Nothing) (\prog -> - liftM Just (get $ uniformLocation prog name) ) +getUniformLocationsSafe :: Program -> [String] -> IO [ Maybe UniformLocation ] +getUniformLocationsSafe prog uniforms = + forM uniforms $ \uniform -> do + tmp <- get $ uniformLocation prog uniform + case tmp of + UniformLocation (-1) -> return $ Nothing + _ -> return $Just tmp loadProgramFullSafe :: (IsShaderSource tc, diff --git a/Resources.hs b/Resources.hs index 6754742..1d5d4c9 100644 --- a/Resources.hs +++ b/Resources.hs @@ -84,6 +84,16 @@ data ResourcesClosure = ResourcesClosure { $(declareSetters ''Resources) +getUniformsSafe :: Program -> [String] -> IO [UniformLocation] +getUniformsSafe prog uniforms = + forM uniforms $ \uniform -> do + tmp <- get $ uniformLocation prog uniform + case tmp of + UniformLocation (-1) -> do + putStrLn $ "No uniform with name: "++uniform + exitWith (ExitFailure 112) + _ -> return tmp + buildMVMatrix :: CameraPosition -> Mat4 GLfloat buildMVMatrix (CameraPosition eye th ph) = let up = if ph' >= 90 && ph' < 270 then Vec3 (0,-1,0) else Vec3 (0,1,0) @@ -231,14 +241,17 @@ buildTerrainObject builder = do uniform dXlocation $= Index1 (dx::GLfloat) uniform dYlocation $= Index1 (dy::GLfloat) printErrors "terrainObjectClosure" + + [lightposU, globalAmbientU, pjMatrixU, mvMatrixU, normalMatrixU] + <- getUniformsSafe terrainProg ["lightPos","globalAmbient","pjMatrix","mvMatrix","normalMatrix"] return $ \rc -> do draw $ prepare obj $ \_ -> do cullFace $= Just Front - uniform (UniformLocation 5) $= rcMVMatrix rc - uniform (UniformLocation 4) $= rcPMatrix rc - uniform (UniformLocation 6) $= rcLightPos rc - uniform (UniformLocation 7) $= rcNormalMatrix rc - uniform (UniformLocation 8) $= rcGlobalAmbient rc + uniform mvMatrixU $= rcMVMatrix rc + uniform pjMatrixU $= rcPMatrix rc + uniform lightposU $= rcLightPos rc + uniform normalMatrixU $= rcNormalMatrix rc + uniform globalAmbientU $= rcGlobalAmbient rc buildForestObject :: Seq.Seq GLfloat -> String -> String -> IO (ResourcesClosure -> IO ()) buildForestObject seq obj tex = @@ -259,9 +272,12 @@ buildForestObject seq obj tex = dXlocation <- get $ uniformLocation forestProg "dX" dYlocation <- get $ uniformLocation forestProg "dY" + [textureU,lightU,globalAmbientU,pjMatrixU,mvMatrixU,timeU,normalMatrixU] <- + getUniformsSafe forestProg ["texture","light","globalAmbient","pjMatrix","mvMatrix","time","normalMatrix"] + obj' <- newDefaultGlyphObjectWithClosure treeF () $ \_ -> do currentProgram $= Just forestProg - setupTexturing woodTexture (UniformLocation 6) 0 + setupTexturing woodTexture textureU 0 uniform dXlocation $= (Index1 $ (dx::GLfloat)) uniform dYlocation $= (Index1 $ (dy::GLfloat)) @@ -286,12 +302,12 @@ buildForestObject seq obj tex = return $ \rc -> do draw $ (prepare obj'') $ \_ -> do - uniform (UniformLocation 5) $= rcMVMatrix rc - uniform (UniformLocation 4) $= rcPMatrix rc - uniform (UniformLocation 7) $= rcLightPos rc - uniform (UniformLocation 8) $= (Index1 $ rcTime rc) - uniform (UniformLocation 9) $= rcNormalMatrix rc - uniform (UniformLocation 10) $= rcGlobalAmbient rc + uniform mvMatrixU $= rcMVMatrix rc + uniform pjMatrixU $= rcPMatrix rc + uniform lightU $= rcLightPos rc + uniform timeU $= (Index1 $ rcTime rc) + uniform normalMatrixU $= rcNormalMatrix rc + uniform globalAmbientU $= rcGlobalAmbient rc buildWaterObject :: BuilderM GLfloat a -> IO (ResourcesClosure -> IO ()) buildWaterObject builder = do diff --git a/shaders/basic.frag b/shaders/basic.frag index 835efbf..a97caf8 100644 --- a/shaders/basic.frag +++ b/shaders/basic.frag @@ -3,8 +3,8 @@ #extension GL_ARB_explicit_uniform_location : enable layout(location = 0) out vec4 frag_color ; -layout(location = 6) uniform vec4 lightPos ; -layout(location = 8) uniform vec4 globalAmbient ; +uniform vec4 lightPos ; +uniform vec4 globalAmbient ; uniform float dX ; uniform float dY ; diff --git a/shaders/basic.vert b/shaders/basic.vert index 36e0c48..a51115c 100644 --- a/shaders/basic.vert +++ b/shaders/basic.vert @@ -7,9 +7,9 @@ layout(location = 2) in vec4 in_color ; layout(location = 1) in vec3 in_normal ; layout(location = 3) in vec2 in_texcoord ; -layout(location = 4) uniform mat4 pjMatrix ; -layout(location = 5) uniform mat4 mvMatrix ; -layout(location = 7) uniform mat3 normalMatrix ; +uniform mat4 pjMatrix ; +uniform mat4 mvMatrix ; +uniform mat3 normalMatrix ; out vec2 texcoord ; out vec4 position ; diff --git a/shaders/forest.frag b/shaders/forest.frag index 1cad806..7392a00 100644 --- a/shaders/forest.frag +++ b/shaders/forest.frag @@ -6,9 +6,9 @@ layout(location = 0) out vec4 frag_color ; uniform mat4 mvMatrix ; uniform mat4 pjMatrix ; -layout(location = 6) uniform sampler2D texture ; -layout(location = 7) uniform vec4 light ; -layout(location = 10) uniform vec4 globalAmbient ; +uniform sampler2D texture ; +uniform vec4 light ; +uniform vec4 globalAmbient ; uniform float dX ; uniform float dY ; diff --git a/shaders/forest.vert b/shaders/forest.vert index 8fb9528..ba2cfc4 100644 --- a/shaders/forest.vert +++ b/shaders/forest.vert @@ -7,10 +7,10 @@ layout(location = 2) in vec4 in_color ; layout(location = 1) in vec3 in_normal ; layout(location = 3) in vec2 in_texcoord ; -layout(location = 4) uniform mat4 pjMatrix ; -layout(location = 5) uniform mat4 mvMatrix ; -layout(location = 8) uniform float time ; -layout(location = 9) uniform mat3 normalMatrix ; +uniform mat4 pjMatrix ; +uniform mat4 mvMatrix ; +uniform float time ; +uniform mat3 normalMatrix ; layout(location = 10) in vec3 in_translation ; layout(location = 11) in vec3 in_scale ; |