aboutsummaryrefslogtreecommitdiffstats
path: root/src/Vivant
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2020-12-27 16:28:53 +0000
committerYann Herklotz <git@yannherklotz.com>2020-12-27 16:28:58 +0000
commit466cba7c57aa9d39221d8c8c6b2bb26b040007b8 (patch)
treebec86542b03de831bce9bda58f8e4a69a2378cfb /src/Vivant
parentde43ca2ffd38449ce3c9ea0741c5792e35fe1ac1 (diff)
downloadVivant-466cba7c57aa9d39221d8c8c6b2bb26b040007b8.tar.gz
Vivant-466cba7c57aa9d39221d8c8c6b2bb26b040007b8.zip
Add haskell files
Diffstat (limited to 'src/Vivant')
-rw-r--r--src/Vivant/Noise.hs2
-rw-r--r--src/Vivant/Shader.hs49
-rw-r--r--src/Vivant/Texture.hs19
3 files changed, 70 insertions, 0 deletions
diff --git a/src/Vivant/Noise.hs b/src/Vivant/Noise.hs
new file mode 100644
index 0000000..fafd21f
--- /dev/null
+++ b/src/Vivant/Noise.hs
@@ -0,0 +1,2 @@
+module Vivant.Noise where
+
diff --git a/src/Vivant/Shader.hs b/src/Vivant/Shader.hs
new file mode 100644
index 0000000..503d1b7
--- /dev/null
+++ b/src/Vivant/Shader.hs
@@ -0,0 +1,49 @@
+module Vivant.Shader (initShaders) where
+
+import SDL (($=))
+import qualified Graphics.Rendering.OpenGL as GL
+import qualified Data.ByteString as BS
+import System.Exit (exitFailure)
+import System.IO (stderr, hPutStrLn)
+import Control.Monad (unless)
+import Paths_vivant (getDataDir)
+
+initShaders :: IO (GL.Program, GL.AttribLocation)
+initShaders = do
+ datadir <- getDataDir
+ -- compile vertex shader
+ vs <- GL.createShader GL.VertexShader
+ vsSource <- BS.readFile $ datadir <> "/shaders/triangle.vert"
+ GL.shaderSourceBS vs $= vsSource
+ GL.compileShader vs
+ vsOK <- GL.get $ GL.compileStatus vs
+ unless vsOK $ do
+ hPutStrLn stderr "Error in vertex shader\n"
+ exitFailure
+
+ -- Do it again for the fragment shader
+ fs <- GL.createShader GL.FragmentShader
+ fsSource <- BS.readFile $ datadir <> "/shaders/triangle.frag"
+ GL.shaderSourceBS fs $= fsSource
+ GL.compileShader fs
+ fsOK <- GL.get $ GL.compileStatus fs
+ unless fsOK $ do
+ hPutStrLn stderr "Error in fragment shader\n"
+ exitFailure
+
+ program <- GL.createProgram
+ GL.attachShader program vs
+ GL.attachShader program fs
+ GL.attribLocation program "coord2d" $= GL.AttribLocation 0
+ GL.linkProgram program
+ linkOK <- GL.get $ GL.linkStatus program
+ GL.validateProgram program
+ status <- GL.get $ GL.validateStatus program
+ unless (linkOK && status) $ do
+ hPutStrLn stderr "GL.linkProgram error"
+ plog <- GL.get $ GL.programInfoLog program
+ putStrLn plog
+ exitFailure
+ GL.currentProgram $= Just program
+
+ return (program, GL.AttribLocation 0)
diff --git a/src/Vivant/Texture.hs b/src/Vivant/Texture.hs
new file mode 100644
index 0000000..7328c87
--- /dev/null
+++ b/src/Vivant/Texture.hs
@@ -0,0 +1,19 @@
+module Vivant.Texture (initTexture, loadTexture) where
+
+import Codec.Picture.Jpg (decodeJpeg)
+import Codec.Picture.Types (convertImage)
+import qualified Graphics.Rendering.OpenGL as GL
+import qualified Data.ByteString as BS
+import qualified Data.Vector.Storable as V
+
+import Paths_vivant (getDataDir)
+
+initTexture = do
+ GL.textureWrapMode GL.Texture2D GL.S $= (GL.Mirrored, GL.Repeat)
+ GL.textureWrapMode GL.Texture2D GL.T $= (GL.Mirrored, GL.Repeat)
+ GL.textureBorderColor GL.Texture2D $= GL.Color4 1 1 1 1
+ GL.textureFilter GL.Texture2D $= ((GL.Nearest, Nothing), GL.Linear')
+
+loadTexture = do
+ image <- BS.readFile $ datadir <> "/assets/wall.jpg"
+ convertImage $ decodeJpeg image