From de43ca2ffd38449ce3c9ea0741c5792e35fe1ac1 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 27 Dec 2020 16:28:37 +0000 Subject: Rename to Vivant.hs --- learnopengl-haskell.cabal | 34 ------------- src/Main.hs | 126 ---------------------------------------------- src/Vivant.hs | 126 ++++++++++++++++++++++++++++++++++++++++++++++ vivant.cabal | 37 ++++++++++++++ 4 files changed, 163 insertions(+), 160 deletions(-) delete mode 100644 learnopengl-haskell.cabal delete mode 100644 src/Main.hs create mode 100644 src/Vivant.hs create mode 100644 vivant.cabal diff --git a/learnopengl-haskell.cabal b/learnopengl-haskell.cabal deleted file mode 100644 index 807395b..0000000 --- a/learnopengl-haskell.cabal +++ /dev/null @@ -1,34 +0,0 @@ -cabal-version: >=1.10 --- Initial package description 'learnopengl-haskell.cabal' generated by --- 'cabal init'. For further documentation, see --- http://haskell.org/cabal/users-guide/ - -name: learnopengl-haskell -version: 0.1.0.0 --- synopsis: --- description: --- bug-reports: -license: GPL-3 -license-file: LICENSE -author: Yann Herklotz -maintainer: git@yannherklotz.com --- copyright: --- category: -build-type: Simple -extra-source-files: CHANGELOG.md, - README.md - -data-files: shaders/*.vert, - shaders/*.frag - -executable learnopengl-haskell - main-is: Main.hs - other-modules: Paths_learnopengl_haskell - default-extensions: OverloadedStrings - build-depends: base >=4.14 && <4.15, - sdl2 >=2.5.3.0 && <2.6, - OpenGL >=3.0.3.0 && <3.1, - bytestring >=0.10 && <0.11, - vector >=0.12.1.2 && <0.13 - hs-source-dirs: src - default-language: Haskell2010 diff --git a/src/Main.hs b/src/Main.hs deleted file mode 100644 index f5100e3..0000000 --- a/src/Main.hs +++ /dev/null @@ -1,126 +0,0 @@ -{- - OpenGL examples in Haskell. - Copyright (C) 2020 Yann Herklotz - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. --} - -module Main where - -import Control.Monad -import Foreign.C.Types -import SDL.Vect -import qualified Data.ByteString as BS -import qualified Data.Vector.Storable as V -import System.Exit (exitFailure) -import System.IO - -import SDL (($=)) -import qualified SDL -import qualified Graphics.Rendering.OpenGL as GL - -import Paths_learnopengl_haskell (getDataDir) - -screenWidth, screenHeight :: CInt -(screenWidth, screenHeight) = (640, 480) - -main :: IO () -main = do - SDL.initialize [SDL.InitVideo] - SDL.HintRenderScaleQuality $= SDL.ScaleLinear - do renderQuality <- SDL.get SDL.HintRenderScaleQuality - when (renderQuality /= SDL.ScaleLinear) $ - putStrLn "Warning: Linear texture filtering not enabled!" - - window <- - SDL.createWindow - "SDL / OpenGL Example" - SDL.defaultWindow {SDL.windowInitialSize = V2 screenWidth screenHeight, - SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL} - SDL.showWindow window - - _ <- SDL.glCreateContext window - (prog, attrib) <- initResources - - let loop = do - events <- SDL.pollEvents - let quit = elem SDL.QuitEvent $ map SDL.eventPayload events - - GL.clear [GL.ColorBuffer] - draw prog attrib - SDL.glSwapWindow window - - unless quit loop - - loop - - SDL.destroyWindow window - SDL.quit - -initResources :: IO (GL.Program, GL.AttribLocation) -initResources = 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) - -draw :: GL.Program -> GL.AttribLocation -> IO () -draw program attrib = do - GL.clearColor $= GL.Color4 1 1 1 1 - GL.clear [GL.ColorBuffer] - GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral screenWidth) (fromIntegral screenHeight)) - - GL.currentProgram $= Just program - GL.vertexAttribArray attrib $= GL.Enabled - V.unsafeWith vertices $ \ptr -> - GL.vertexAttribPointer attrib $= - (GL.ToFloat, GL.VertexArrayDescriptor 2 GL.Float 0 ptr) - GL.drawArrays GL.Triangles 0 3 -- 3 is the number of vertices - GL.vertexAttribArray attrib $= GL.Disabled - -vertices :: V.Vector Float -vertices = V.fromList [ 0.0, 0.8 - , -0.8, -0.8 - , 0.8, -0.8 - ] diff --git a/src/Vivant.hs b/src/Vivant.hs new file mode 100644 index 0000000..f5100e3 --- /dev/null +++ b/src/Vivant.hs @@ -0,0 +1,126 @@ +{- + OpenGL examples in Haskell. + Copyright (C) 2020 Yann Herklotz + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +-} + +module Main where + +import Control.Monad +import Foreign.C.Types +import SDL.Vect +import qualified Data.ByteString as BS +import qualified Data.Vector.Storable as V +import System.Exit (exitFailure) +import System.IO + +import SDL (($=)) +import qualified SDL +import qualified Graphics.Rendering.OpenGL as GL + +import Paths_learnopengl_haskell (getDataDir) + +screenWidth, screenHeight :: CInt +(screenWidth, screenHeight) = (640, 480) + +main :: IO () +main = do + SDL.initialize [SDL.InitVideo] + SDL.HintRenderScaleQuality $= SDL.ScaleLinear + do renderQuality <- SDL.get SDL.HintRenderScaleQuality + when (renderQuality /= SDL.ScaleLinear) $ + putStrLn "Warning: Linear texture filtering not enabled!" + + window <- + SDL.createWindow + "SDL / OpenGL Example" + SDL.defaultWindow {SDL.windowInitialSize = V2 screenWidth screenHeight, + SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL} + SDL.showWindow window + + _ <- SDL.glCreateContext window + (prog, attrib) <- initResources + + let loop = do + events <- SDL.pollEvents + let quit = elem SDL.QuitEvent $ map SDL.eventPayload events + + GL.clear [GL.ColorBuffer] + draw prog attrib + SDL.glSwapWindow window + + unless quit loop + + loop + + SDL.destroyWindow window + SDL.quit + +initResources :: IO (GL.Program, GL.AttribLocation) +initResources = 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) + +draw :: GL.Program -> GL.AttribLocation -> IO () +draw program attrib = do + GL.clearColor $= GL.Color4 1 1 1 1 + GL.clear [GL.ColorBuffer] + GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral screenWidth) (fromIntegral screenHeight)) + + GL.currentProgram $= Just program + GL.vertexAttribArray attrib $= GL.Enabled + V.unsafeWith vertices $ \ptr -> + GL.vertexAttribPointer attrib $= + (GL.ToFloat, GL.VertexArrayDescriptor 2 GL.Float 0 ptr) + GL.drawArrays GL.Triangles 0 3 -- 3 is the number of vertices + GL.vertexAttribArray attrib $= GL.Disabled + +vertices :: V.Vector Float +vertices = V.fromList [ 0.0, 0.8 + , -0.8, -0.8 + , 0.8, -0.8 + ] diff --git a/vivant.cabal b/vivant.cabal new file mode 100644 index 0000000..518d95c --- /dev/null +++ b/vivant.cabal @@ -0,0 +1,37 @@ +cabal-version: >=1.10 +-- Initial package description 'learnopengl-haskell.cabal' generated by +-- 'cabal init'. For further documentation, see +-- http://haskell.org/cabal/users-guide/ + +name: vivant +version: 0.1.0.0 +-- synopsis: +-- description: +-- bug-reports: +license: GPL-3 +license-file: LICENSE +author: Yann Herklotz +maintainer: git@yannherklotz.com +-- copyright: +-- category: +build-type: Simple +extra-source-files: CHANGELOG.md, + README.md + +data-files: shaders/*.vert, + shaders/*.frag + +executable vivant + main-is: Vivant.hs + other-modules: Paths_vivant, + Vivant.Noise, + Vivant.Shader + default-extensions: OverloadedStrings + build-depends: base >=4.14 && <4.15, + sdl2 >=2.5.3.0 && <2.6, + OpenGL >=3.0.3.0 && <3.1, + bytestring >=0.10 && <0.11, + vector >=0.12.1.2 && <0.13, + JuicyPixels >=3.3 && <3.4 + hs-source-dirs: src + default-language: Haskell2010 -- cgit