GL.hs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {-- GL.hs - some semi-imperative opengl mess
  2. -- Copyright (C) 2018-2019 caryoscelus
  3. --
  4. -- This program is free software: you can redistribute it and/or modify
  5. -- it under the terms of the GNU General Public License as published by
  6. -- the Free Software Foundation, either version 3 of the License, or
  7. -- (at your option) any later version.
  8. --
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. --}
  17. {-# LANGUAGE
  18. NoMonomorphismRestriction,
  19. ScopedTypeVariables,
  20. TypeFamilies,
  21. LambdaCase,
  22. GADTs,
  23. FlexibleContexts
  24. #-}
  25. module GL where
  26. import Graphics.GPipe
  27. data OnTexture what c = OnTexture
  28. { targetTexture :: Image (Format c)
  29. , putOnTexture :: what
  30. }
  31. data RenderTexture os c p a = RenderTexture
  32. { screenArea :: PrimitiveArray p a
  33. , screenTex :: Texture2D os (Format c)
  34. }
  35. sameV3 :: a -> V3 a
  36. sameV3 x = V3 x x x
  37. -- adopted from http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
  38. hsvToRgb (V3 hue sat val) =
  39. let
  40. k = V3 1.0 (2.0 / 3.0) (1.0 / 3.0)
  41. (V3 r g b) =
  42. abs (fract' (sameV3 hue + k) * 6.0 - sameV3 3.0) - sameV3 1.0
  43. in
  44. (sameV3 val) *
  45. mix
  46. (sameV3 1.0)
  47. (V3 (saturate r) (saturate g) (saturate b))
  48. (sameV3 sat)
  49. hsvTrianglesOnTextureShader
  50. :: Int -> Int
  51. -> Shader os
  52. (OnTexture (PrimitiveArray Triangles (B4 Float, B3 Float)) RGBFloat)
  53. ()
  54. hsvTrianglesOnTextureShader w h = do
  55. stream <- toPrimitiveStream putOnTexture
  56. rasterized <- rasterize
  57. (const (FrontAndBack, ViewPort (V2 0 0) (V2 w h), DepthRange 0 1))
  58. stream
  59. draw (const NoBlending) rasterized $ \hsv ->
  60. drawColor
  61. (\s -> (targetTexture s, (V3 True True True), False))
  62. (hsvToRgb hsv ** 2.2)
  63. colorTrianglesOnTextureShader
  64. :: Int -> Int
  65. -> Shader os
  66. (OnTexture (PrimitiveArray Triangles (B4 Float, B3 Float)) RGBFloat)
  67. ()
  68. colorTrianglesOnTextureShader w h = do
  69. stream <- toPrimitiveStream putOnTexture
  70. rasterized <- rasterize
  71. (const (FrontAndBack, ViewPort (V2 0 0) (V2 w h), DepthRange 0 1))
  72. stream
  73. draw (const NoBlending) rasterized $ \color ->
  74. drawColor
  75. (\s -> (targetTexture s, (V3 True True True), False))
  76. (color ** 2.2) -- gamma correction, bah
  77. singleTextureOnWindowShader
  78. :: Window os RGBFloat ds
  79. -> Int -> Int
  80. -> Shader os
  81. (RenderTexture os RGBFloat Triangles (B2 Float))
  82. ()
  83. singleTextureOnWindowShader win w h = do
  84. let
  85. filter = SamplerNearest
  86. edge = (pure ClampToEdge, 0)
  87. primStream <- toPrimitiveStream screenArea
  88. fragments <- rasterize
  89. (const (FrontAndBack, ViewPort (V2 0 0) (V2 w h), DepthRange 0 1))
  90. (fmap (\(V2 x y) -> (V4 (x*2-1) (y*2-1) 0 1, V2 x y)) primStream)
  91. samp <- newSampler2D (\s -> (screenTex s, filter, edge))
  92. let
  93. sampleTexture = sample2D samp SampleAuto Nothing Nothing
  94. fragments' = fmap sampleTexture fragments
  95. drawWindowColor
  96. (const (win, ContextColorOption NoBlending (pure True))) fragments'