|
| 1 | +module Example01 where |
| 2 | + |
| 3 | +import qualified Data.ByteString as B |
| 4 | +import qualified Data.Vector.Storable as VS |
| 5 | +import Data.IORef |
| 6 | + |
| 7 | +import Data.SizedVector |
| 8 | +import Data.HList |
| 9 | + |
| 10 | +import Graphics.LambdaGL.Buffer |
| 11 | +import Graphics.LambdaGL.Program |
| 12 | +import Graphics.LambdaGL.Draw |
| 13 | +import Graphics.LambdaGL.Shader |
| 14 | +import Graphics.LambdaGL.Texture |
| 15 | +import Graphics.LambdaGL.Uniform |
| 16 | +import Graphics.LambdaGL.Types.Shared |
| 17 | + |
| 18 | +example01 :: IO (IO ()) |
| 19 | +example01 = do |
| 20 | + -- Timer to use as a uniform. |
| 21 | + timer <- newIORef 0 |
| 22 | + draw <- createDrawAction |
| 23 | + pure $ readIORef timer >>= draw >> modifyIORef timer (+1) |
| 24 | + |
| 25 | +createDrawAction :: IO (Int -> IO ()) |
| 26 | +createDrawAction = do |
| 27 | + vertexBuffer :: Buffer '[(D2, Float)] <- createBuffer |
| 28 | + writeBuffer vertexBuffer $ VS.fromList $ [HostData (V2 (-1) (-1)) <:> Nil, HostData (V2 (1) (-1)) <:> Nil, HostData (V2 1 1) <:> Nil, HostData (V2 (-1) 1) <:> Nil, HostData (V2 (-1) (-1)) <:> Nil] |
| 29 | + |
| 30 | + vertexShader :: CompiledShader 'Vertex '[] '[] '[(D2, Float)] '[(D2, Float)] <- compileShader $ Shader $ ShaderSource vertexShader |
| 31 | + |
| 32 | + fragmentShader :: CompiledShader 'Fragment '[Named "time" Float] '[Texture NoMipMap TextureNormal D2 RGBA8] '[(D2, Float)] '[] <- compileShader $ Shader $ ShaderSource fragmentShader |
| 33 | + |
| 34 | + texture <- newTexture @NoMipMap @TextureNormal @D2 @RGBA8 |
| 35 | + |
| 36 | + bindTexture texture $ do |
| 37 | + writeTexture (V2 2 2) $ VS.fromList [V4 0 0 0 255, V4 255 255 255 255, V4 255 255 255 255, V4 0 0 0 255] |
| 38 | + |
| 39 | + program <- createProgram $ TwoShader vertexShader fragmentShader |
| 40 | + |
| 41 | + let uniforms x = |
| 42 | + (makeDrawUniform $ (Named x :: Named "time" Float)) |
| 43 | + |
| 44 | + let initial = toDrawInput vertexBuffer 0 |
| 45 | + drawObj <- createDraw program (uniforms 0) initial |
| 46 | + pure $ \i -> do |
| 47 | + let pI = skipDrawInput @'[(D2, Float)] |
| 48 | + runDrawEnv $ do |
| 49 | + clearColor $ V4 1 1 1 1 |
| 50 | + bindLastTexture texture $ |
| 51 | + draw drawObj (uniforms $ fromIntegral i) pI (DrawOptions TriangleStrip 0 5) |
| 52 | + pure () |
| 53 | + |
| 54 | +fragmentShader :: B.ByteString |
| 55 | +fragmentShader = |
| 56 | + mconcat . fmap (<> "\n") $ |
| 57 | + [ "#version 450 core", |
| 58 | + "in vec2 fragCoord;", |
| 59 | + "out vec4 fragColor;", |
| 60 | + "uniform float time;", |
| 61 | + "uniform sampler2D tex;", |
| 62 | + "void main()", |
| 63 | + "{", |
| 64 | + "vec4 c = texture(tex, fragCoord);", |
| 65 | + "fragColor = vec4(abs(fragCoord) * (sin(time/21) * sin(time/13) + abs(vec2(sin(time/47)))), c.z, 1.0);", |
| 66 | + "}" |
| 67 | + ] |
| 68 | + |
| 69 | +vertexShader :: B.ByteString |
| 70 | +vertexShader = |
| 71 | + mconcat . fmap (<> "\n") $ |
| 72 | + [ "#version 430 core", |
| 73 | + "layout(location = 0) in vec2 vPosition;", |
| 74 | + "out vec2 fragCoord;", |
| 75 | + "void main()", |
| 76 | + "{", |
| 77 | + "gl_Position = vec4(vPosition, 1, 1);", |
| 78 | + "fragCoord = vPosition;", |
| 79 | + "}" |
| 80 | + ] |
0 commit comments