module Graphics.GLUtil.Shaders (loadShader,
linkShaderProgram, linkShaderProgramWith,
namedUniform,
uniformScalar, uniformVec, uniformMat,
namedUniformMat, uniformGLMat4) where
import Control.Monad (unless)
import qualified Data.ByteString as BS
import Graphics.Rendering.OpenGL
import Graphics.Rendering.OpenGL.Raw.Core31
import Graphics.GLUtil.GLError
import Foreign.Ptr (Ptr)
import Unsafe.Coerce (unsafeCoerce)
loadShader :: ShaderType -> FilePath -> IO Shader
loadShader st filePath = do
shader <- createShader st
BS.readFile filePath >>= (shaderSourceBS shader $=)
compileShader shader
printError
ok <- get (compileStatus shader)
infoLog <- get (shaderInfoLog shader)
unless (null infoLog)
(mapM_ putStrLn
["Shader info log for '" ++ filePath ++ "':", infoLog, ""])
unless ok $ do
deleteObjectNames [shader]
ioError (userError "shader compilation failed")
return shader
linkShaderProgram :: [Shader] -> IO Program
linkShaderProgram shaders = linkShaderProgramWith shaders (const $ return ())
linkShaderProgramWith :: [Shader] -> (Program -> IO ()) -> IO Program
linkShaderProgramWith shaders prelink = do p <- createProgram
mapM_ (attachShader p) shaders
prelink p
linkProgram p
return p
namedUniform :: Uniform a => String -> StateVar a
namedUniform name = makeStateVar (loc >>= get) (\x -> loc >>= ($= x))
where loc = do Just p <- get currentProgram
l <- get (uniformLocation p name)
printError
return $ uniform l
withHMatrix :: [[GLfloat]] -> (Ptr GLfloat -> IO a) -> IO a
withHMatrix lstMat m = do
mat <- newMatrix RowMajor (concat lstMat) :: IO (GLmatrix GLfloat)
withMatrix mat (\_ -> m)
unUL :: UniformLocation -> GLint
unUL = unsafeCoerce
uniformScalar :: UniformComponent a => UniformLocation -> SettableStateVar a
uniformScalar loc = makeSettableStateVar $ (uniform loc $=) . Index1
uniformVec :: UniformLocation -> SettableStateVar [GLfloat]
uniformVec loc = makeSettableStateVar aux
where aux [x,y] = glUniform2f loc' x y
aux [x,y,z] = glUniform3f loc' x y z
aux [x,y,z,w] = glUniform4f loc' x y z w
aux _ = ioError . userError $
"Only 2, 3, and 4 dimensional vectors are supported"
loc' = unUL loc
namedUniformMat :: String -> SettableStateVar [[GLfloat]]
namedUniformMat var = makeSettableStateVar (\m -> loc >>= ($= m) . uniformMat)
where loc = do Just p <- get currentProgram
location <- get (uniformLocation p var)
printError
return location
uniformMat :: UniformLocation -> SettableStateVar [[GLfloat]]
uniformMat loc = makeSettableStateVar aux
where aux mat = do withHMatrix mat $ \ptr ->
case length mat of
4 -> glUniformMatrix4fv loc' 1 1 ptr
3 -> glUniformMatrix3fv loc' 1 1 ptr
_ -> ioError . userError $
"Only 3x3 and 4x4 matrices are supported"
loc' = unUL loc
uniformGLMat4 :: UniformLocation -> SettableStateVar (GLmatrix GLfloat)
uniformGLMat4 loc = makeSettableStateVar aux
where aux m = withMatrix m $ \_ -> glUniformMatrix4fv loc' 1 1
loc' = unUL loc