aboutsummaryrefslogtreecommitdiffstats
path: root/src/Vivant/Terrain.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Vivant/Terrain.hs')
-rw-r--r--src/Vivant/Terrain.hs89
1 files changed, 56 insertions, 33 deletions
diff --git a/src/Vivant/Terrain.hs b/src/Vivant/Terrain.hs
index befee88..b73469a 100644
--- a/src/Vivant/Terrain.hs
+++ b/src/Vivant/Terrain.hs
@@ -1,24 +1,22 @@
-module Vivant.Terrain
- (Terrain(..), initTerrain)
-where
+module Vivant.Terrain (Terrain (..), initTerrain) where
-import Vivant.Renderer
import qualified Data.Vector.Storable as V
-import qualified Graphics.Rendering.OpenGL as GL
-import SDL (($=))
import Foreign.Marshal.Alloc (free, malloc)
import Foreign.Ptr
-import Vivant.Common (vectorSize, uncurry3)
import Foreign.Storable (poke)
+import qualified Graphics.Rendering.OpenGL as GL
import Linear
+import SDL (($=))
+import Vivant.Common (uncurry3, vectorSize)
+import Vivant.Renderer
data Terrain = Terrain
- {
- terrainWidth :: Int,
+ { terrainWidth :: Int,
terrainHeight :: Int,
terrainGeometry :: [[Float]],
terrainRenderer :: Renderer
- } deriving (Show)
+ }
+ deriving (Show)
normal :: Num c => (c, c, c) -> (c, c, c) -> (c, c, c) -> (c, c, c)
normal a b c =
@@ -26,30 +24,53 @@ normal a b c =
where
transform (V3 a b c) = (a, b, c)
-partition
- :: (V.Storable a1, Enum a1, Num a1) => [[a1]] -> V.Vector a1
+partition ::
+ (V.Storable a1, Enum a1, Num a1) => [[a1]] -> V.Vector a1
partition r =
- V.fromList . concat . concat .
- fmap (\(e, y) -> fmap (generateP y) $ zip (zip (uncurry zip e) $ tail (uncurry zip e)) [0..])
- $ zip (zip r $ tail r) [0..]
+ V.fromList . concat . concat
+ . fmap (\(e, y) -> fmap (generateP y) $ zip (zip (uncurry zip e) $ tail (uncurry zip e)) [0 ..])
+ $ zip (zip r $ tail r) [0 ..]
where
generateP y (((z1, z2), (z3, z4)), x) =
- let (n1x, n1y, n1z) = normal (x, y, z1) (x, y+1, z2) (x+1, y, z3)
- (n2x, n2y, n2z) = normal (x+1, y+1, z4) (x, y+1, z2) (x+1, y, z3)
- in
- [ x, y, z1
- , -n1x, -n1y, -n1z
- , x, y+1, z2
- , -n1x, -n1y, -n1z
- , x+1, y, z3
- , -n1x, -n1y, -n1z
- , x, y+1, z2
- , n2x, n2y, n2z
- , x+1, y, z3
- , n2x, n2y, n2z
- , x+1, y+1, z4
- , n2x, n2y, n2z
- ]
+ let (n1x, n1y, n1z) = normal (x, y, z1) (x, y + 1, z2) (x + 1, y, z3)
+ (n2x, n2y, n2z) = normal (x + 1, y + 1, z4) (x, y + 1, z2) (x + 1, y, z3)
+ in [ x,
+ y,
+ z1,
+ - n1x,
+ - n1y,
+ - n1z,
+ x,
+ y + 1,
+ z2,
+ - n1x,
+ - n1y,
+ - n1z,
+ x + 1,
+ y,
+ z3,
+ - n1x,
+ - n1y,
+ - n1z,
+ x,
+ y + 1,
+ z2,
+ n2x,
+ n2y,
+ n2z,
+ x + 1,
+ y,
+ z3,
+ n2x,
+ n2y,
+ n2z,
+ x + 1,
+ y + 1,
+ z4,
+ n2x,
+ n2y,
+ n2z
+ ]
initTerrain :: [[Float]] -> GL.Program -> IO Terrain
initTerrain g p = do
@@ -70,8 +91,10 @@ initTerrain g p = do
let h = length g
w = length (head g)
- modelMatrix = mkTransformation (axisAngle (V3 0 0 0) 0)
- (V3 (- fromIntegral w / 2) (- fromIntegral h / 2) 0)
+ modelMatrix =
+ mkTransformation
+ (axisAngle (V3 0 0 0) 0)
+ (V3 (- fromIntegral w / 2) (- fromIntegral h / 2) 0)
modelP <- malloc
poke modelP modelMatrix