aboutsummaryrefslogtreecommitdiffstats
path: root/src/Vivant.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Vivant.hs')
-rw-r--r--src/Vivant.hs42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/Vivant.hs b/src/Vivant.hs
index 45ed667..323602f 100644
--- a/src/Vivant.hs
+++ b/src/Vivant.hs
@@ -33,7 +33,7 @@ import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.Rendering.OpenGL.GL.Shaders.Uniform as GL (UniformLocation (..))
import Linear
import Paths_vivant (getDataDir)
-import SDL (($=))
+import Graphics.Rendering.OpenGL (($=))
import qualified SDL
import SDL.Raw.Enum as SDL
import SDL.Raw.Video as SDL (glSetAttribute)
@@ -42,6 +42,8 @@ import SDL.Vect
import System.Exit (exitFailure)
import System.IO
import Vivant.Shader (createProgram)
+import Vivant.Renderer
+import Vivant.Terrain
screenWidth, screenHeight :: CInt
(screenWidth, screenHeight) = (640, 480)
@@ -73,7 +75,8 @@ data Game = Game
gameModelP :: Ptr (M44 Float),
gameViewP :: Ptr (M44 Float),
gameProjectionP :: Ptr (M44 Float),
- gameCamera :: Camera
+ gameCamera :: Camera,
+ gameTerrain :: Maybe Terrain
}
deriving (Show)
@@ -85,7 +88,8 @@ initialGameState =
gameModelP = nullPtr,
gameViewP = nullPtr,
gameProjectionP = nullPtr,
- gameCamera = initialCamera
+ gameCamera = initialCamera,
+ gameTerrain = Nothing
}
data MouseInputs = MouseInputs
@@ -232,23 +236,6 @@ updateMouse mouse game =
newPos = rotation . cameraPos $ gameCamera game
newUp = rotation . cameraUp $ gameCamera game
-setVertexAttribute :: GL.Program -> String -> Int -> Int -> Int -> IO ()
-setVertexAttribute program name vertices stride offset = do
- let floatSize = sizeOf (1.0 :: Float)
- attrib <- GL.get $ GL.attribLocation program name
- GL.vertexAttribPointer attrib
- $= ( GL.ToFloat,
- GL.VertexArrayDescriptor
- (fromIntegral vertices)
- GL.Float
- (fromIntegral $ stride * floatSize)
- (plusPtr nullPtr (offset * floatSize))
- )
- GL.vertexAttribArray attrib $= GL.Enabled
-
-getUniformLocation :: GL.UniformLocation -> GL.GLint
-getUniformLocation (GL.UniformLocation i) = i
-
initResources :: IO Game
initResources = do
vao <- GL.genObjectName
@@ -267,13 +254,16 @@ initResources = do
viewP <- malloc
projectionP <- malloc
+ terrain <- initTerrain [[1, 2, 3, 4], [3, 2, 1, 4], [2, 5, 2, 3], [3, 1, 2, 4]] prog
+
return $
initialGameState
{ gameProgram = Just prog,
gameVao = Just vao,
gameModelP = modelP,
gameViewP = viewP,
- gameProjectionP = projectionP
+ gameProjectionP = projectionP,
+ gameTerrain = Just terrain
}
-- | Convert degrees to radians
@@ -284,13 +274,13 @@ scaledMat :: V4 (V4 Float) -> V4 (V4 Float)
scaledMat n = ((n * identity) & _w . _w .~ 1)
draw :: Game -> IO ()
-draw game@(Game {gameProgram = Just p, gameVao = Just v}) = do
+draw game@(Game {gameProgram = Just p, gameVao = Just v, gameTerrain = Just t}) = do
tick <- ticks
GL.clearColor $= GL.Color4 1 1 1 1
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
GL.currentProgram $= gameProgram game
- GL.bindVertexArrayObject $= gameVao game
+ GL.bindVertexArrayObject $= rendererVao (terrainRenderer t)
model <- getUniformLocation <$> GL.uniformLocation p "model"
view <- getUniformLocation <$> GL.uniformLocation p "view"
@@ -311,11 +301,7 @@ draw game@(Game {gameProgram = Just p, gameVao = Just v}) = do
ourColorLoc <- GL.uniformLocation p "ourColour"
GL.uniform ourColorLoc $= (GL.Vector4 (1 :: Float) 0.5 1 1)
- forM_ locs $ \l -> do
- let modelMatrix = mkTransformation (axisAngle (V3 0 0 0) 0) l !*! scaledMat 5
- poke (gameModelP game) modelMatrix
- GL.glUniformMatrix4fv model 1 1 (castPtr (gameModelP game))
- GL.drawArrays GL.Triangles 0 36
+ render $ terrainRenderer t
GL.bindVertexArrayObject $= Nothing
return ()