Compare commits
5 Commits
8cabc29195
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
602507a605 | ||
|
|
4d59cd7569 | ||
|
|
5474012f89 | ||
|
|
2a3c9bdafb | ||
|
|
a62275f853 |
15
CHANGELOG.md
15
CHANGELOG.md
@@ -20,6 +20,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- use `Double` rather than `FLoat` for internal calculations
|
- use `Double` rather than `FLoat` for internal calculations
|
||||||
- `cursorPos`, `dt` natively `Double` already
|
- `cursorPos`, `dt` natively `Double` already
|
||||||
|
|
||||||
|
## [0.4.0] - 2025-12-21
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- cube as array of points
|
||||||
|
- debug feature where `t` and `g` raise and lower speed respectively
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `initResources` now takes `[[V3 GL.GLfloat]]` rather than `[V3 GL.GLfloat]`
|
||||||
|
- to migrate: rather than passing `a` pass `[a]`; behaviour is the same
|
||||||
|
- reduce view distance to 1'000 from 10'000
|
||||||
|
- color pixels based on normalized coordinates
|
||||||
|
- use `haskellPackages.callCabal2nix` in `flake.nix` for brevity, same behaviour
|
||||||
|
|
||||||
## [0.3.0] - 2025-12-08
|
## [0.3.0] - 2025-12-08
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
36
flake.nix
36
flake.nix
@@ -7,45 +7,21 @@
|
|||||||
self,
|
self,
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
versionString = "0.3.0";
|
|
||||||
package = {
|
|
||||||
mkDerivation,
|
|
||||||
base,
|
|
||||||
bytestring,
|
|
||||||
GLFW-b,
|
|
||||||
lens,
|
|
||||||
lib,
|
|
||||||
linear,
|
|
||||||
OpenGL,
|
|
||||||
}:
|
|
||||||
mkDerivation {
|
|
||||||
pname = "hs-game";
|
|
||||||
version = versionString;
|
|
||||||
src = ./.;
|
|
||||||
isLibrary = false;
|
|
||||||
isExecutable = true;
|
|
||||||
executableHaskellDepends = [
|
|
||||||
base
|
|
||||||
bytestring
|
|
||||||
GLFW-b
|
|
||||||
lens
|
|
||||||
linear
|
|
||||||
OpenGL
|
|
||||||
];
|
|
||||||
homepage = "https://git.mtgmonkey.net/Andromeda/hs-game";
|
|
||||||
license = lib.licenses.bsd3;
|
|
||||||
mainProgram = "hs-game";
|
|
||||||
};
|
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
in {
|
in {
|
||||||
packages.${system} = {
|
packages.${system} = {
|
||||||
default =
|
default =
|
||||||
pkgs.haskellPackages.callPackage package {};
|
pkgs.haskell.packages.ghc912.callCabal2nix "hs-game" ./. {
|
||||||
|
};
|
||||||
};
|
};
|
||||||
devShells.${system} = {
|
devShells.${system} = {
|
||||||
default = pkgs.mkShell {
|
default = pkgs.mkShell {
|
||||||
packages = [
|
packages = [
|
||||||
|
# dev stuff
|
||||||
|
pkgs.haskellPackages.ghcide
|
||||||
|
pkgs.haskellPackages.ormolu
|
||||||
|
|
||||||
pkgs.cabal-install
|
pkgs.cabal-install
|
||||||
pkgs.libGL
|
pkgs.libGL
|
||||||
pkgs.xorg.libX11
|
pkgs.xorg.libX11
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
cabal-version: 3.0
|
cabal-version: 3.0
|
||||||
name: hs-game
|
name: hs-game
|
||||||
version: 0.3.0
|
version: 0.5.0
|
||||||
-- synopsis:
|
-- synopsis:
|
||||||
-- description:
|
-- description:
|
||||||
homepage: https://git.mtgmonkey.net/Andromeda/hs-game
|
homepage: https://git.mtgmonkey.net/Andromeda/hs-game
|
||||||
|
|||||||
@@ -1,95 +1,155 @@
|
|||||||
{-# LANGUAGE DisambiguateRecordFields, NamedFieldPuns, OverloadedRecordDot #-}
|
{-# LANGUAGE DisambiguateRecordFields #-}
|
||||||
{- |
|
{-# LANGUAGE MultilineStrings #-}
|
||||||
- Module : Game.Internal
|
{-# LANGUAGE NamedFieldPuns #-}
|
||||||
- Description : internal functions
|
{-# LANGUAGE OverloadedRecordDot #-}
|
||||||
- Copyright : 2025 Andromeda
|
|
||||||
- License : BSD 3-clause
|
|
||||||
- Maintainer : Matrix @Andromeda:tchncs.de
|
|
||||||
- Stability : Experimental
|
|
||||||
-}
|
|
||||||
module Game.Internal
|
|
||||||
( cursorPosHandler
|
|
||||||
, drawObjects
|
|
||||||
, initResources
|
|
||||||
, keyPressed
|
|
||||||
, loop
|
|
||||||
, resizeWindow
|
|
||||||
, shutdownWindow
|
|
||||||
, updateCursorPos
|
|
||||||
, updateKeyPressed
|
|
||||||
, updateKeyReleased
|
|
||||||
)
|
|
||||||
where
|
|
||||||
|
|
||||||
|
-- |
|
||||||
|
-- - Module : Game.Internal
|
||||||
|
-- - Description : internal functions
|
||||||
|
-- - Copyright : 2025 Andromeda
|
||||||
|
-- - License : BSD 3-clause
|
||||||
|
-- - Maintainer : Matrix @Andromeda:tchncs.de
|
||||||
|
-- - Stability : Experimental
|
||||||
|
module Game.Internal
|
||||||
|
( cursorPosHandler,
|
||||||
|
drawObjects,
|
||||||
|
initResources,
|
||||||
|
keyPressed,
|
||||||
|
loop,
|
||||||
|
resizeWindow,
|
||||||
|
shutdownWindow,
|
||||||
|
updateCursorPos,
|
||||||
|
updateKeyPressed,
|
||||||
|
updateKeyReleased,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
|
import Control.Monad (when)
|
||||||
|
import Data.IORef (IORef, modifyIORef', readIORef)
|
||||||
|
import Data.List (delete)
|
||||||
|
import Foreign.Marshal.Array (withArray)
|
||||||
|
import Foreign.Ptr (nullPtr, plusPtr)
|
||||||
|
import Foreign.Storable (Storable, sizeOf)
|
||||||
|
import GHC.Float (double2Float)
|
||||||
import Game.Internal.LoadShaders
|
import Game.Internal.LoadShaders
|
||||||
import Game.Internal.Types
|
import Game.Internal.Types
|
||||||
|
import Graphics.Rendering.OpenGL (($=))
|
||||||
import Control.Concurrent (threadDelay)
|
|
||||||
import Control.Monad (when)
|
|
||||||
import Data.IORef (IORef, modifyIORef', readIORef)
|
|
||||||
import Data.List (delete)
|
|
||||||
import Foreign.Marshal.Array (withArray)
|
|
||||||
import Foreign.Ptr (nullPtr, plusPtr)
|
|
||||||
import Foreign.Storable (sizeOf, Storable)
|
|
||||||
import GHC.Float (double2Float)
|
|
||||||
|
|
||||||
import qualified Graphics.UI.GLFW as GLFW
|
|
||||||
import qualified Graphics.Rendering.OpenGL as GL
|
import qualified Graphics.Rendering.OpenGL as GL
|
||||||
import Graphics.Rendering.OpenGL as GL (($=))
|
import qualified Graphics.UI.GLFW as GLFW
|
||||||
|
import Linear (V3 (..), V4 (..))
|
||||||
import Linear (V3(..))
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Shader creation and object initialisation
|
-- Shader creation and object initialisation
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | loads models, shaders
|
initResources :: [V4 GL.GLfloat] -> IO (Object, GL.Program)
|
||||||
initResources :: [V3 GL.GLfloat] -> IO ([Object], GL.Program)
|
initResources arr = do
|
||||||
initResources array = do
|
object <-
|
||||||
-- create objects
|
createObject arr 4 GL.Triangles (GL.AttribLocation 0)
|
||||||
testObject0 <- createObject (map (+(V3 (-1) (-1) (-1))) array) 3 GL.TriangleStrip
|
|
||||||
testObject1 <- createObject (map (+(V3 (1) (1) (1))) array) 3 GL.TriangleStrip
|
|
||||||
testObject2 <- createObject array 3 GL.TriangleStrip
|
|
||||||
let objects = [testObject0, testObject1, testObject2]
|
|
||||||
|
|
||||||
-- load shaders
|
-- compile shader program
|
||||||
program <- loadShaders
|
program <-
|
||||||
[ ShaderInfo GL.VertexShader (StringSource vertShader)
|
loadShaders
|
||||||
, ShaderInfo GL.FragmentShader (StringSource fragShader)
|
[ ShaderInfo GL.VertexShader (StringSource vertShader),
|
||||||
]
|
ShaderInfo GL.FragmentShader (StringSource fragShader)
|
||||||
|
]
|
||||||
GL.currentProgram $= Just program
|
GL.currentProgram $= Just program
|
||||||
|
|
||||||
return (objects, program)
|
-- alpha
|
||||||
|
GL.blend $= GL.Enabled
|
||||||
|
GL.blendFunc $= (GL.SrcAlpha, GL.OneMinusSrcAlpha)
|
||||||
|
|
||||||
|
return (object, program)
|
||||||
|
|
||||||
|
listIOsToIOlist :: [IO a] -> [a] -> IO [a]
|
||||||
|
listIOsToIOlist [] out = return out
|
||||||
|
listIOsToIOlist (io : ios) out = do
|
||||||
|
ioVal <- io
|
||||||
|
listIOsToIOlist ios (ioVal : out)
|
||||||
|
|
||||||
-- a_ vertex shader input
|
-- a_ vertex shader input
|
||||||
-- v_ varying
|
-- v_ varying
|
||||||
-- u_ uniform
|
-- u_ uniform
|
||||||
-- o_ fragment shader output
|
-- o_ fragment shader output
|
||||||
|
|
||||||
-- | vertex shader
|
|
||||||
vertShader :: String
|
vertShader :: String
|
||||||
vertShader =
|
vertShader =
|
||||||
"#version 330 core\n" ++
|
"""
|
||||||
"layout (location = 0) in vec3 a_vPos;\n" ++
|
#version 330 core
|
||||||
"uniform mat4 u_view;\n" ++
|
|
||||||
"uniform mat4 u_projection;\n" ++
|
layout (location = 0) in vec4 a_vPos;
|
||||||
"out vec3 v_pos;\n" ++
|
|
||||||
"void main()\n" ++
|
uniform mat4 u_view;
|
||||||
"{\n" ++
|
uniform mat4 u_projection;
|
||||||
" gl_Position = u_projection * u_view * vec4(a_vPos.xyz, 1.0);\n" ++
|
uniform vec4 u_cam;
|
||||||
" v_pos = a_vPos;\n" ++
|
|
||||||
"}"
|
out vec3 v_pos;
|
||||||
|
out float v_w;
|
||||||
-- | fragment shader
|
out float v_alpha;
|
||||||
|
|
||||||
|
vec3 orthoFrom4d(vec4 point)
|
||||||
|
{
|
||||||
|
return point.xyz;
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates a simple 3d coordinate from a 4d
|
||||||
|
vec3 projectFrom4d(vec4 point)
|
||||||
|
{
|
||||||
|
// TODO don't do camera ops in shader, prefer linear algebra
|
||||||
|
// also use a reasonable projection for god's sake
|
||||||
|
vec4 view = abs(u_cam - point);
|
||||||
|
float perspective = 1.0 / abs(u_cam.w - view.w);
|
||||||
|
|
||||||
|
return perspective * (point.xyz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 vPos = orthoFrom4d(a_vPos);
|
||||||
|
|
||||||
|
// TODO don't set constant inside of shader :/
|
||||||
|
float wHorizon = 3;
|
||||||
|
float alpha = (wHorizon - abs(u_cam.w - a_vPos.w)) / wHorizon;
|
||||||
|
|
||||||
|
// cull invisible things
|
||||||
|
if (alpha < -1) {
|
||||||
|
gl_Position = vec4(0.0);
|
||||||
|
alpha = 0.0;
|
||||||
|
} else {
|
||||||
|
alpha = max(alpha, 0.0);
|
||||||
|
gl_Position = u_projection * u_view * vec4(vPos, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
v_pos = vPos;
|
||||||
|
v_w = a_vPos.w;
|
||||||
|
v_alpha = alpha;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
fragShader :: String
|
fragShader :: String
|
||||||
fragShader =
|
fragShader =
|
||||||
"#version 330 core\n" ++
|
"""
|
||||||
"out vec4 o_vColor;\n" ++
|
#version 330 core
|
||||||
"in vec3 v_pos;\n" ++
|
|
||||||
"void main()\n" ++
|
uniform vec4 u_cam;
|
||||||
"{\n" ++
|
|
||||||
" o_vColor = vec4(0.5 + 0.5 * v_pos, 1);\n" ++
|
out vec4 o_vColor;
|
||||||
"}"
|
|
||||||
|
in vec3 v_pos;
|
||||||
|
in float v_w;
|
||||||
|
in float v_alpha;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
// the normal vector of the face
|
||||||
|
// yoinked from https://stackoverflow.com/questions/14980712/how-to-get-flat-normals-on-a-cube/14981446#14981446
|
||||||
|
vec3 norm = normalize(cross(dFdx(v_pos), dFdy(v_pos)));
|
||||||
|
|
||||||
|
// creates a color based on the normal direction
|
||||||
|
o_vColor = vec4((0.5 + 0.5 * norm) / 2, v_alpha);
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Objects
|
-- Objects
|
||||||
@@ -98,133 +158,111 @@ fragShader =
|
|||||||
-- | calculates the size in memory of an array
|
-- | calculates the size in memory of an array
|
||||||
sizeOfArray :: (Storable a, Num b) => [a] -> b
|
sizeOfArray :: (Storable a, Num b) => [a] -> b
|
||||||
sizeOfArray [] = 0
|
sizeOfArray [] = 0
|
||||||
sizeOfArray (x:xs) = fromIntegral $ (*) (1 + length xs) $ sizeOf x
|
sizeOfArray (x : xs) = fromIntegral $ (*) (1 + length xs) $ sizeOf x
|
||||||
|
|
||||||
-- | loads a given array into a given attribute index
|
-- | loads a given array into a given attribute index
|
||||||
createVBO
|
createVBO ::
|
||||||
:: Storable (a GL.GLfloat)
|
(Storable (a GL.GLfloat)) =>
|
||||||
=> [a GL.GLfloat]
|
[a GL.GLfloat] ->
|
||||||
-> GL.NumComponents
|
GL.NumComponents ->
|
||||||
-> GL.AttribLocation
|
GL.AttribLocation ->
|
||||||
-> IO GL.BufferObject
|
IO GL.BufferObject
|
||||||
createVBO array numComponents attribLocation = do
|
createVBO array numComponents attribLocation = do
|
||||||
-- vbo for buffer
|
-- vbo for buffer
|
||||||
buffer <- GL.genObjectName
|
buffer <- GL.genObjectName
|
||||||
GL.bindBuffer GL.ArrayBuffer $= Just buffer
|
GL.bindBuffer GL.ArrayBuffer $= Just buffer
|
||||||
|
|
||||||
-- populate buffer
|
-- populate buffer
|
||||||
withArray
|
withArray array $ \ptr ->
|
||||||
array
|
GL.bufferData GL.ArrayBuffer $= (sizeOfArray array, ptr, GL.StaticDraw)
|
||||||
$ \ptr ->
|
|
||||||
GL.bufferData GL.ArrayBuffer $= (sizeOfArray array, ptr, GL.StaticDraw)
|
|
||||||
|
|
||||||
-- create attribute pointer to buffer
|
-- create attribute pointer to buffer
|
||||||
GL.vertexAttribPointer attribLocation $=
|
GL.vertexAttribPointer attribLocation
|
||||||
( GL.ToFloat
|
$= ( GL.ToFloat,
|
||||||
, GL.VertexArrayDescriptor
|
GL.VertexArrayDescriptor numComponents GL.Float 0 (plusPtr nullPtr 0)
|
||||||
numComponents
|
)
|
||||||
GL.Float
|
|
||||||
0
|
|
||||||
(plusPtr nullPtr 0)
|
|
||||||
)
|
|
||||||
GL.vertexAttribArray attribLocation $= GL.Enabled
|
GL.vertexAttribArray attribLocation $= GL.Enabled
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
-- | creates an object from a given array; deals with vbos and everything
|
-- | creates an object from a given array; deals with vbos and everything
|
||||||
createObject
|
createObject ::
|
||||||
:: Storable (a GL.GLfloat)
|
(Storable (a GL.GLfloat)) =>
|
||||||
=> [a GL.GLfloat]
|
[a GL.GLfloat] ->
|
||||||
-> GL.NumComponents
|
GL.NumComponents ->
|
||||||
-> GL.PrimitiveMode
|
GL.PrimitiveMode ->
|
||||||
-> IO Object
|
GL.AttribLocation ->
|
||||||
createObject array numComponents primitiveMode = do
|
IO Object
|
||||||
|
createObject array numComponents primitiveMode attrLocation = do
|
||||||
-- vao for object
|
-- vao for object
|
||||||
vao <- GL.genObjectName
|
vao <- GL.genObjectName
|
||||||
GL.bindVertexArrayObject $= Just vao
|
GL.bindVertexArrayObject $= Just vao
|
||||||
|
|
||||||
-- vbo for vertices
|
-- vbo for vertices
|
||||||
_ <- createVBO array numComponents $ GL.AttribLocation 0
|
_ <- createVBO array numComponents attrLocation
|
||||||
|
return (Object vao (fromIntegral $ length array) numComponents primitiveMode)
|
||||||
return
|
|
||||||
(Object
|
|
||||||
vao
|
|
||||||
(fromIntegral $ length array)
|
|
||||||
numComponents
|
|
||||||
primitiveMode
|
|
||||||
)
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Elm-like data structures
|
-- Elm-like data structures
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | gameloop
|
-- | gameloop
|
||||||
loop
|
loop ::
|
||||||
:: GLFW.Window -- ^ window to display on
|
-- | window to display on
|
||||||
-> Float -- ^ dt
|
GLFW.Window ->
|
||||||
-> (Float -> Model -> Model) -- ^ update function
|
-- | dt
|
||||||
-> (GLFW.Window -> Model -> IO ()) -- ^ view function
|
Float ->
|
||||||
-> IORef Model -- ^ model
|
-- | update function
|
||||||
-> IO ()
|
(Float -> Model -> Model) ->
|
||||||
|
-- | view function
|
||||||
|
(GLFW.Window -> Model -> IO ()) ->
|
||||||
|
-- | model
|
||||||
|
IORef Model ->
|
||||||
|
IO ()
|
||||||
loop window dt update view modelRef = do
|
loop window dt update view modelRef = do
|
||||||
-- start frame timer
|
-- start frame timer
|
||||||
Just frameStart <- GLFW.getTime
|
Just frameStart <- GLFW.getTime
|
||||||
|
|
||||||
-- tick model
|
-- tick model
|
||||||
modifyIORef' modelRef $ update dt
|
modifyIORef' modelRef $ update dt
|
||||||
model' <- readIORef modelRef
|
model' <- readIORef modelRef
|
||||||
|
|
||||||
-- view new model
|
-- view new model
|
||||||
view window model'
|
view window model'
|
||||||
|
|
||||||
-- end frame timer, wait the difference between expected and actual
|
-- end frame timer, wait the difference between expected and actual
|
||||||
Just frameEnd <- GLFW.getTime
|
Just frameEnd <- GLFW.getTime
|
||||||
let
|
let drawTime = double2Float $ frameEnd - frameStart
|
||||||
drawTime = double2Float $ frameEnd - frameStart
|
target = 1 / 60 :: Float
|
||||||
target = 1 / 60 :: Float
|
|
||||||
when (drawTime < target) $ threadDelay $ floor $ (target - drawTime) * 1000000
|
when (drawTime < target) $ threadDelay $ floor $ (target - drawTime) * 1000000
|
||||||
Just frameEnd' <- GLFW.getTime
|
Just frameEnd' <- GLFW.getTime
|
||||||
let
|
let dt' = double2Float $ frameEnd' - frameStart
|
||||||
dt' = double2Float $ frameEnd' - frameStart
|
|
||||||
|
|
||||||
loop window dt' update view modelRef
|
loop window dt' update view modelRef
|
||||||
|
|
||||||
-- | updates given a keypress. escape case is probably caught by GLFW in the
|
-- | updates given a keypress. escape case is probably caught by GLFW in the
|
||||||
-- handler function itself
|
-- handler function itself
|
||||||
updateKeyPressed :: GLFW.Key -> Model -> Model
|
updateKeyPressed :: GLFW.Key -> Model -> Model
|
||||||
updateKeyPressed key model =
|
updateKeyPressed key model = model {keys = key : model.keys}
|
||||||
model { keys = key:model.keys }
|
|
||||||
|
|
||||||
-- | updates given a keyrelease. escape case is probably caught by GLFW in the
|
-- | updates given a keyrelease. escape case is probably caught by GLFW in the
|
||||||
-- handler function itself
|
-- handler function itself
|
||||||
updateKeyReleased :: GLFW.Key -> Model -> Model
|
updateKeyReleased :: GLFW.Key -> Model -> Model
|
||||||
updateKeyReleased key model =
|
updateKeyReleased key model = model {keys = (delete key model.keys)}
|
||||||
model { keys = (delete key model.keys) }
|
|
||||||
|
|
||||||
applyToTuples :: (a -> b -> c) -> (a, a) -> (b, b) -> (c, c)
|
applyToTuples :: (a -> b -> c) -> (a, a) -> (b, b) -> (c, c)
|
||||||
applyToTuples f (x, y) (a, b) = (f x a, f y b)
|
applyToTuples f (x, y) (a, b) = (f x a, f y b)
|
||||||
|
|
||||||
-- | updates cursor
|
-- | updates cursor
|
||||||
updateCursorPos :: Double -> Double -> Model -> Model
|
updateCursorPos :: Double -> Double -> Model -> Model
|
||||||
updateCursorPos x y model =
|
updateCursorPos x y model =
|
||||||
let
|
let pyth =
|
||||||
pyth = (((fst model.cursorPos) - x) ** 2 + ((snd model.cursorPos) - y) ** 2) ** 0.5
|
(((fst model.cursorPos) - x) ** 2 + ((snd model.cursorPos) - y) ** 2)
|
||||||
in
|
** 0.5
|
||||||
if pyth < 16 then
|
in if pyth < 16
|
||||||
model
|
then
|
||||||
{ cursorPos = (x, y)
|
model
|
||||||
, cursorDeltaPos = applyToTuples (-) model.cursorPos (x, y)
|
{ cursorPos = (x, y),
|
||||||
}
|
cursorDeltaPos = applyToTuples (-) model.cursorPos (x, y)
|
||||||
else
|
}
|
||||||
model
|
else model {cursorPos = (x, y)}
|
||||||
{ cursorPos = (x, y)
|
|
||||||
}
|
|
||||||
|
|
||||||
-- | draws objects
|
-- | draws objects
|
||||||
drawObjects :: [Object] -> IO ([Object])
|
drawObjects :: [Object] -> IO ([Object])
|
||||||
drawObjects [] = return []
|
drawObjects [] = return []
|
||||||
drawObjects
|
drawObjects ((Object vao numVertices _ primitiveMode) : objects) = do
|
||||||
((Object vao numVertices _ primitiveMode):objects) = do
|
|
||||||
GL.bindVertexArrayObject $= Just vao
|
GL.bindVertexArrayObject $= Just vao
|
||||||
GL.drawArrays primitiveMode 0 numVertices
|
GL.drawArrays primitiveMode 0 numVertices
|
||||||
drawObjects objects
|
drawObjects objects
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- |
|
-- |
|
||||||
-- Module : LoadShaders
|
-- Module : LoadShaders
|
||||||
-- Copyright : (c) Sven Panne 2013
|
-- Copyright : (c) Sven Panne 2013
|
||||||
@@ -10,12 +13,12 @@
|
|||||||
--
|
--
|
||||||
-- Utilities for shader handling, adapted from LoadShaders.cpp which is (c) The
|
-- Utilities for shader handling, adapted from LoadShaders.cpp which is (c) The
|
||||||
-- Red Book Authors.
|
-- Red Book Authors.
|
||||||
--
|
module Game.Internal.LoadShaders
|
||||||
--------------------------------------------------------------------------------
|
( ShaderSource (..),
|
||||||
|
ShaderInfo (..),
|
||||||
module Game.Internal.LoadShaders (
|
loadShaders,
|
||||||
ShaderSource(..), ShaderInfo(..), loadShaders
|
)
|
||||||
) where
|
where
|
||||||
|
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
@@ -25,15 +28,14 @@ import Graphics.Rendering.OpenGL
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | The source of the shader source code.
|
-- | The source of the shader source code.
|
||||||
|
data ShaderSource
|
||||||
data ShaderSource =
|
= -- | The shader source code is directly given as a 'B.ByteString'.
|
||||||
ByteStringSource B.ByteString
|
ByteStringSource B.ByteString
|
||||||
-- ^ The shader source code is directly given as a 'B.ByteString'.
|
| -- | The shader source code is directly given as a 'String'.
|
||||||
| StringSource String
|
StringSource String
|
||||||
-- ^ The shader source code is directly given as a 'String'.
|
| -- | The shader source code is located in the file at the given 'FilePath'.
|
||||||
| FileSource FilePath
|
FileSource FilePath
|
||||||
-- ^ The shader source code is located in the file at the given 'FilePath'.
|
deriving (Eq, Ord, Show)
|
||||||
deriving ( Eq, Ord, Show )
|
|
||||||
|
|
||||||
getSource :: ShaderSource -> IO B.ByteString
|
getSource :: ShaderSource -> IO B.ByteString
|
||||||
getSource (ByteStringSource bs) = return bs
|
getSource (ByteStringSource bs) = return bs
|
||||||
@@ -43,21 +45,20 @@ getSource (FileSource path) = B.readFile path
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | A description of a shader: The type of the shader plus its source code.
|
-- | A description of a shader: The type of the shader plus its source code.
|
||||||
|
data ShaderInfo
|
||||||
data ShaderInfo = ShaderInfo ShaderType ShaderSource
|
= ShaderInfo ShaderType ShaderSource
|
||||||
deriving ( Eq, Ord, Show )
|
deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | Create a new program object from the given shaders, throwing an
|
-- | Create a new program object from the given shaders, throwing an
|
||||||
-- 'IOException' if something goes wrong.
|
-- 'IOException' if something goes wrong.
|
||||||
|
|
||||||
loadShaders :: [ShaderInfo] -> IO Program
|
loadShaders :: [ShaderInfo] -> IO Program
|
||||||
loadShaders infos =
|
loadShaders infos =
|
||||||
createProgram `bracketOnError` deleteObjectName $ \program -> do
|
createProgram `bracketOnError` deleteObjectName $ \program -> do
|
||||||
loadCompileAttach program infos
|
loadCompileAttach program infos
|
||||||
linkAndCheck program
|
linkAndCheck program
|
||||||
return program
|
return program
|
||||||
|
|
||||||
linkAndCheck :: Program -> IO ()
|
linkAndCheck :: Program -> IO ()
|
||||||
linkAndCheck = checked linkProgram linkStatus programInfoLog "link"
|
linkAndCheck = checked linkProgram linkStatus programInfoLog "link"
|
||||||
@@ -65,25 +66,26 @@ linkAndCheck = checked linkProgram linkStatus programInfoLog "link"
|
|||||||
loadCompileAttach :: Program -> [ShaderInfo] -> IO ()
|
loadCompileAttach :: Program -> [ShaderInfo] -> IO ()
|
||||||
loadCompileAttach _ [] = return ()
|
loadCompileAttach _ [] = return ()
|
||||||
loadCompileAttach program (ShaderInfo shType source : infos) =
|
loadCompileAttach program (ShaderInfo shType source : infos) =
|
||||||
createShader shType `bracketOnError` deleteObjectName $ \shader -> do
|
createShader shType `bracketOnError` deleteObjectName $ \shader -> do
|
||||||
src <- getSource source
|
src <- getSource source
|
||||||
shaderSourceBS shader $= src
|
shaderSourceBS shader $= src
|
||||||
compileAndCheck shader
|
compileAndCheck shader
|
||||||
attachShader program shader
|
attachShader program shader
|
||||||
loadCompileAttach program infos
|
loadCompileAttach program infos
|
||||||
|
|
||||||
compileAndCheck :: Shader -> IO ()
|
compileAndCheck :: Shader -> IO ()
|
||||||
compileAndCheck = checked compileShader compileStatus shaderInfoLog "compile"
|
compileAndCheck = checked compileShader compileStatus shaderInfoLog "compile"
|
||||||
|
|
||||||
checked :: (t -> IO ())
|
checked ::
|
||||||
-> (t -> GettableStateVar Bool)
|
(t -> IO ()) ->
|
||||||
-> (t -> GettableStateVar String)
|
(t -> GettableStateVar Bool) ->
|
||||||
-> String
|
(t -> GettableStateVar String) ->
|
||||||
-> t
|
String ->
|
||||||
-> IO ()
|
t ->
|
||||||
|
IO ()
|
||||||
checked action getStatus getInfoLog message object = do
|
checked action getStatus getInfoLog message object = do
|
||||||
action object
|
action object
|
||||||
ok <- get (getStatus object)
|
ok <- get (getStatus object)
|
||||||
unless ok $ do
|
unless ok $ do
|
||||||
infoLog <- get (getInfoLog object)
|
infoLog <- get (getInfoLog object)
|
||||||
fail (message ++ " log: " ++ infoLog)
|
fail (message ++ " log: " ++ infoLog)
|
||||||
|
|||||||
@@ -1,141 +1,122 @@
|
|||||||
{-# LANGUAGE NamedFieldPuns, OverloadedRecordDot #-}
|
{-# LANGUAGE NamedFieldPuns #-}
|
||||||
{- |
|
{-# LANGUAGE OverloadedRecordDot #-}
|
||||||
- Module : Game.Internal.Types
|
|
||||||
- Description :
|
-- |
|
||||||
- Copyright : 2025 Andromeda
|
-- - Module : Game.Internal.Types
|
||||||
- License : BSD 3-clause
|
-- - Description :
|
||||||
- Maintainer : Matrix @Andromeda:tchncs.de
|
-- - Copyright : 2025 Andromeda
|
||||||
- Stability : Experimental
|
-- - License : BSD 3-clause
|
||||||
-}
|
-- - Maintainer : Matrix @Andromeda:tchncs.de
|
||||||
|
-- - Stability : Experimental
|
||||||
module Game.Internal.Types
|
module Game.Internal.Types
|
||||||
( Object(..)
|
( Object (..),
|
||||||
|
toGLMatrix,
|
||||||
|
Model (camera, objects, cursorDeltaPos, cursorPos, keys, program, wprop),
|
||||||
|
mkModel,
|
||||||
|
Camera (camPos, camPitch, camYaw, camReference, mouseSensitivity, camVel, strafeStrength, jumpStrength, hasJumped, airTime),
|
||||||
|
mkCamera,
|
||||||
|
WorldProperties (g, friction, up),
|
||||||
|
mkWorldProperties,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
, toGLMatrix
|
|
||||||
|
|
||||||
, Model ( camera
|
|
||||||
, objects
|
|
||||||
, cursorDeltaPos
|
|
||||||
, cursorPos
|
|
||||||
, program
|
|
||||||
, keys
|
|
||||||
, wprop
|
|
||||||
)
|
|
||||||
, mkModel
|
|
||||||
|
|
||||||
, Camera ( camPos
|
|
||||||
, camPitch
|
|
||||||
, camYaw
|
|
||||||
, camReference
|
|
||||||
, mouseSensitivity
|
|
||||||
, camVel
|
|
||||||
, strafeStrength
|
|
||||||
, jumpStrength
|
|
||||||
, hasJumped
|
|
||||||
, airTime
|
|
||||||
)
|
|
||||||
, mkCamera
|
|
||||||
|
|
||||||
, WorldProperties (g, friction, up)
|
|
||||||
, mkWorldProperties
|
|
||||||
|
|
||||||
) where
|
|
||||||
|
|
||||||
import qualified Graphics.UI.GLFW as GLFW
|
|
||||||
import qualified Graphics.Rendering.OpenGL as GL
|
import qualified Graphics.Rendering.OpenGL as GL
|
||||||
|
import qualified Graphics.UI.GLFW as GLFW
|
||||||
|
import Linear (V3 (..), V4 (..))
|
||||||
import qualified Linear as L
|
import qualified Linear as L
|
||||||
import Linear (V3, V3(..), V4(..))
|
|
||||||
|
|
||||||
-- | represents a single draw call
|
-- | represents a single draw call
|
||||||
data Object =
|
data Object = Object
|
||||||
Object
|
{ -- | vao of vertex buffer
|
||||||
{ vao :: GL.VertexArrayObject -- ^ vao of vertex buffer
|
vao :: GL.VertexArrayObject,
|
||||||
, numIndicies :: GL.NumArrayIndices -- ^ number of vertices
|
-- | number of vertices
|
||||||
, numComponents :: GL.NumComponents -- ^ dimensionallity; vec3, vec4, etc.
|
numIndicies :: GL.NumArrayIndices,
|
||||||
, primitiveMode :: GL.PrimitiveMode -- ^ primitive mode to be drawn with
|
-- | dimensionallity; vec3, vec4, etc.
|
||||||
}
|
numComponents :: GL.NumComponents,
|
||||||
deriving Show
|
-- | primitive mode to be drawn with
|
||||||
|
primitiveMode :: GL.PrimitiveMode
|
||||||
|
}
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
-- | converts M44 to a 16array for OpenGL
|
-- | converts M44 to a 16array for OpenGL
|
||||||
toGLMatrix :: L.M44 GL.GLfloat -> [GL.GLfloat]
|
toGLMatrix :: L.M44 GL.GLfloat -> [GL.GLfloat]
|
||||||
toGLMatrix
|
toGLMatrix (V4 (V4 c00 c01 c02 c03) (V4 c10 c11 c12 c13) (V4 c20 c21 c22 c23) (V4 c30 c31 c32 c33)) =
|
||||||
(V4
|
[ c00,
|
||||||
(V4 c00 c01 c02 c03)
|
c01,
|
||||||
(V4 c10 c11 c12 c13)
|
c02,
|
||||||
(V4 c20 c21 c22 c23)
|
c03,
|
||||||
(V4 c30 c31 c32 c33)) =
|
c10,
|
||||||
[ c00, c01, c02, c03
|
c11,
|
||||||
, c10, c11, c12, c13
|
c12,
|
||||||
, c20, c21, c22, c23
|
c13,
|
||||||
, c30, c31, c32, c33
|
c20,
|
||||||
|
c21,
|
||||||
|
c22,
|
||||||
|
c23,
|
||||||
|
c30,
|
||||||
|
c31,
|
||||||
|
c32,
|
||||||
|
c33
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | gamestate
|
-- | gamestate
|
||||||
data Model =
|
data Model = Model
|
||||||
Model
|
{ camera :: Camera,
|
||||||
{ camera :: Camera
|
-- | frame-on-frame delta mouse position
|
||||||
, cursorDeltaPos :: (Double, Double) -- ^ frame-on-frame delta mouse position
|
cursorDeltaPos :: (Double, Double),
|
||||||
, cursorPos :: (Double, Double) -- ^ current mouse position
|
-- | current mouse position
|
||||||
, keys :: [GLFW.Key] -- ^ currently pressed keys
|
cursorPos :: (Double, Double),
|
||||||
, objects :: [Object] -- ^ draw calls
|
-- | currently pressed keys
|
||||||
, program :: GL.Program -- ^ shader program
|
keys :: [GLFW.Key],
|
||||||
, wprop :: WorldProperties
|
-- | draw calls
|
||||||
}
|
objects :: [Object],
|
||||||
deriving Show
|
program :: GL.Program,
|
||||||
|
wprop :: WorldProperties
|
||||||
|
}
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
-- | smart constructor for Model
|
-- | smart constructor for Model
|
||||||
mkModel
|
mkModel :: Camera -> [Object] -> GL.Program -> WorldProperties -> Model
|
||||||
:: Camera
|
|
||||||
-> [Object]
|
|
||||||
-> GL.Program
|
|
||||||
-> WorldProperties
|
|
||||||
-> Model
|
|
||||||
mkModel camera objects program wprop =
|
mkModel camera objects program wprop =
|
||||||
Model
|
Model camera (0, 0) (0, 0) [] objects program wprop
|
||||||
camera
|
|
||||||
(0,0)
|
|
||||||
(0,0)
|
|
||||||
[]
|
|
||||||
objects
|
|
||||||
program
|
|
||||||
wprop
|
|
||||||
|
|
||||||
-- | camera
|
-- | camera
|
||||||
data Camera =
|
data Camera = Camera
|
||||||
Camera
|
{ -- | position in world space
|
||||||
{ camPos :: V3 Float -- ^ position in world space
|
camPos :: V4 Float,
|
||||||
, camPitch :: Float -- ^ pitch in radians, up positive
|
-- | pitch in radians, up positive
|
||||||
, camYaw :: Float -- ^ yaw in radians, right positive
|
camPitch :: Float,
|
||||||
, camReference :: V3 Float -- ^ reference direction; orientation applied to
|
-- | yaw in radians, right positive
|
||||||
, camVel :: V3 Float -- ^ velocity in world space
|
camYaw :: Float,
|
||||||
, mouseSensitivity :: Float -- ^ scale factor for mouse movement
|
-- | reference direction; orientation applied to
|
||||||
, strafeStrength :: Float -- ^ scale factor for strafe
|
camReference :: V3 Float,
|
||||||
, jumpStrength :: Float -- ^ scale factor for jump initial velocity
|
-- | velocity in world space
|
||||||
, hasJumped :: Bool -- ^ whether the camera still has jumping state
|
camVel :: V3 Float,
|
||||||
, airTime :: Float -- ^ time since jumping state entered in seconds
|
-- | scale factor for mouse movement
|
||||||
}
|
mouseSensitivity :: Float,
|
||||||
deriving Show
|
-- | scale factor for strafe
|
||||||
|
strafeStrength :: Float,
|
||||||
|
-- | scale factor for jump initial velocity
|
||||||
|
jumpStrength :: Float,
|
||||||
|
-- | whether the camera still has jumping state
|
||||||
|
hasJumped :: Bool,
|
||||||
|
-- | time since jumping state entered in seconds
|
||||||
|
airTime :: Float
|
||||||
|
}
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
-- | smart constructor for Camera
|
-- | smart constructor for Camera
|
||||||
mkCamera
|
mkCamera ::
|
||||||
:: V3 Float
|
V4 Float ->
|
||||||
-> Float
|
Float ->
|
||||||
-> Float
|
Float ->
|
||||||
-> V3 Float
|
V3 Float ->
|
||||||
-> V3 Float
|
V3 Float ->
|
||||||
-> Float
|
Float ->
|
||||||
-> Float
|
Float ->
|
||||||
-> Float
|
Float ->
|
||||||
-> Camera
|
Camera
|
||||||
mkCamera
|
mkCamera camPos camPitch camYaw camReference camVel mouseSensitivity strafeStrength jumpStrength =
|
||||||
camPos
|
|
||||||
camPitch
|
|
||||||
camYaw
|
|
||||||
camReference
|
|
||||||
camVel
|
|
||||||
mouseSensitivity
|
|
||||||
strafeStrength
|
|
||||||
jumpStrength =
|
|
||||||
Camera
|
Camera
|
||||||
camPos
|
camPos
|
||||||
camPitch
|
camPitch
|
||||||
@@ -149,15 +130,16 @@ mkCamera
|
|||||||
0
|
0
|
||||||
|
|
||||||
-- | physical properties of the world
|
-- | physical properties of the world
|
||||||
data WorldProperties =
|
data WorldProperties = WorldProperties
|
||||||
WorldProperties
|
{ -- | gravity `g`
|
||||||
{ g :: Float -- ^ gravity `g`
|
g :: Float,
|
||||||
, friction :: Float -- ^ scale factor for floor friction
|
-- | scale factor for floor friction
|
||||||
, up :: V3 Float -- ^ global up vector
|
friction :: Float,
|
||||||
}
|
-- | global up vector
|
||||||
deriving Show
|
up :: V3 Float
|
||||||
|
}
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
-- | smart constructor for WorldProperties
|
-- | smart constructor for WorldProperties
|
||||||
mkWorldProperties :: Float -> Float -> V3 Float-> WorldProperties
|
mkWorldProperties :: Float -> Float -> V3 Float -> WorldProperties
|
||||||
mkWorldProperties g friction up =
|
mkWorldProperties g friction up = WorldProperties g friction (L.normalize up)
|
||||||
WorldProperties g friction (L.normalize up)
|
|
||||||
|
|||||||
442
src/Main.hs
442
src/Main.hs
@@ -1,98 +1,179 @@
|
|||||||
{-# LANGUAGE DisambiguateRecordFields, NamedFieldPuns, OverloadedRecordDot #-}
|
{-# LANGUAGE DisambiguateRecordFields #-}
|
||||||
{- |
|
{-# LANGUAGE NamedFieldPuns #-}
|
||||||
- Module : Game
|
{-# LANGUAGE OverloadedRecordDot #-}
|
||||||
- Description : runs game
|
|
||||||
- Copyright : 2025 Andromeda
|
|
||||||
- License : BSD 3-clause
|
|
||||||
- Maintainer : Matrix @Andromeda:tchncs.de
|
|
||||||
- Stability : Experimental
|
|
||||||
-}
|
|
||||||
module Main (main) where
|
|
||||||
|
|
||||||
import Game.Internal.Types
|
-- |
|
||||||
|
-- - Module : Game
|
||||||
|
-- - Description : runs game
|
||||||
|
-- - Copyright : 2025 Andromeda
|
||||||
|
-- - License : BSD 3-clause
|
||||||
|
-- - Maintainer : Matrix @Andromeda:tchncs.de
|
||||||
|
-- - Stability : Experimental
|
||||||
|
module Main
|
||||||
|
( main,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
import Control.Lens ((^.))
|
||||||
|
import Data.IORef (newIORef)
|
||||||
|
import GHC.Float (double2Float)
|
||||||
import Game.Internal
|
import Game.Internal
|
||||||
|
import Game.Internal.Types
|
||||||
import Control.Lens ((^.))
|
import Graphics.Rendering.OpenGL (($=))
|
||||||
import Data.IORef (newIORef)
|
|
||||||
import GHC.Float (double2Float)
|
|
||||||
|
|
||||||
import qualified Graphics.UI.GLFW as GLFW
|
|
||||||
import qualified Graphics.Rendering.OpenGL as GL
|
import qualified Graphics.Rendering.OpenGL as GL
|
||||||
import Graphics.Rendering.OpenGL as GL (($=))
|
import qualified Graphics.UI.GLFW as GLFW
|
||||||
|
import Linear (V3 (..), V4 (..), (*^), _w, _xyz, _y)
|
||||||
import qualified Linear as L
|
import qualified Linear as L
|
||||||
import Linear ( V3(..), _y )
|
|
||||||
|
|
||||||
-- | Main function runs game
|
-- | Main function runs game
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
_ <- GLFW.init
|
_ <- GLFW.init
|
||||||
GLFW.defaultWindowHints
|
GLFW.defaultWindowHints
|
||||||
|
|
||||||
-- OpenGL core >=3.3
|
-- OpenGL core >=3.3
|
||||||
GLFW.windowHint $ GLFW.WindowHint'ContextVersionMajor 3
|
GLFW.windowHint $ GLFW.WindowHint'ContextVersionMajor 3
|
||||||
GLFW.windowHint $ GLFW.WindowHint'ContextVersionMinor 3
|
GLFW.windowHint $ GLFW.WindowHint'ContextVersionMinor 3
|
||||||
GLFW.windowHint $ GLFW.WindowHint'OpenGLProfile GLFW.OpenGLProfile'Core
|
GLFW.windowHint $ GLFW.WindowHint'OpenGLProfile GLFW.OpenGLProfile'Core
|
||||||
|
|
||||||
-- MSAA
|
-- MSAA
|
||||||
GLFW.windowHint $ GLFW.WindowHint'Samples $ Just 8
|
GLFW.windowHint $ GLFW.WindowHint'Samples $ Just 8
|
||||||
|
-- alpha
|
||||||
-- create window
|
-- create window
|
||||||
monitor <- GLFW.getPrimaryMonitor
|
monitor <- GLFW.getPrimaryMonitor
|
||||||
Just window <- GLFW.createWindow 256 256 "hs-game" monitor Nothing
|
Just window <- GLFW.createWindow 256 256 "hs-game" monitor Nothing
|
||||||
GLFW.makeContextCurrent $ Just window
|
GLFW.makeContextCurrent $ Just window
|
||||||
|
|
||||||
-- add callbacks
|
-- add callbacks
|
||||||
GLFW.setWindowCloseCallback window $ Just shutdownWindow
|
GLFW.setWindowCloseCallback window $ Just shutdownWindow
|
||||||
GLFW.setWindowSizeCallback window $ Just resizeWindow
|
GLFW.setWindowSizeCallback window $ Just resizeWindow
|
||||||
GLFW.setKeyCallback window $ Just (keyPressed Nothing)
|
GLFW.setKeyCallback window $ Just (keyPressed Nothing)
|
||||||
GLFW.setCursorInputMode window GLFW.CursorInputMode'Hidden
|
GLFW.setCursorInputMode window GLFW.CursorInputMode'Hidden
|
||||||
GLFW.setCursorPosCallback window $ Just (cursorPosHandler Nothing)
|
GLFW.setCursorPosCallback window $ Just (cursorPosHandler Nothing)
|
||||||
|
(object, program) <-
|
||||||
(objects, program) <- initResources testVertices
|
initResources $
|
||||||
|
concat
|
||||||
-- init model
|
( [hCube]
|
||||||
let
|
++ [map (\v -> (V4 a 0 0 a) + (rotate4 0 1 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
model =
|
++ [map (\v -> (V4 a 2 0 a) + (rotate4 0 2 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
mkModel
|
++ [map (\v -> (V4 a 4 0 a) + (rotate4 0 3 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
(mkCamera
|
++ [map (\v -> (V4 a (-2) 0 a) + (rotate4 1 0 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
(V3 0 0 3) -- camPos
|
++ [map (\v -> (V4 a (-4) 0 a) + (rotate4 2 0 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
0 -- pitch
|
++ [map (\v -> (V4 a (-6) 0 a) + (rotate4 3 0 (a * g90 / 24) v)) hCube | a <- take 1000 [0, 2 ..]]
|
||||||
0 -- yaw
|
|
||||||
(V3 0 0 (-1)) -- reference vector
|
|
||||||
(V3 0 0 0) -- velocity
|
|
||||||
2 -- mouse sensitivity
|
|
||||||
16 -- strafe strength
|
|
||||||
12 -- jump strength
|
|
||||||
)
|
|
||||||
objects
|
|
||||||
program
|
|
||||||
(mkWorldProperties
|
|
||||||
2
|
|
||||||
0.16
|
|
||||||
(V3 0 1 0)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let model =
|
||||||
|
mkModel
|
||||||
|
( mkCamera
|
||||||
|
(V4 0 0 3 0) -- camPos
|
||||||
|
0 -- pitch
|
||||||
|
0 -- yaw
|
||||||
|
(V3 0 0 (-1)) -- reference vector
|
||||||
|
(V3 0 0 0) -- velocity
|
||||||
|
2 -- mouse sensitivity
|
||||||
|
16 -- strafe strength
|
||||||
|
12 -- jump strength
|
||||||
|
)
|
||||||
|
[object]
|
||||||
|
program
|
||||||
|
(mkWorldProperties 2 0.16 (V3 0 1 0))
|
||||||
modelRef <- newIORef model
|
modelRef <- newIORef model
|
||||||
|
|
||||||
-- add callbacks with io ref to model
|
-- add callbacks with io ref to model
|
||||||
GLFW.setKeyCallback window $ Just $ keyPressed $ Just modelRef
|
GLFW.setKeyCallback window $ Just $ keyPressed $ Just modelRef
|
||||||
GLFW.setCursorPosCallback window $ Just $ cursorPosHandler $ Just modelRef
|
GLFW.setCursorPosCallback window $ Just $ cursorPosHandler $ Just modelRef
|
||||||
|
|
||||||
loop window 0 update view modelRef
|
loop window 0 update view modelRef
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Arrays
|
-- Arrays
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | centered unit square
|
m = (0 - p)
|
||||||
testVertices :: [V3 GL.GLfloat]
|
|
||||||
testVertices =
|
p = 0.5
|
||||||
[ V3 (-0.5) (-0.5) 0
|
|
||||||
, V3 0.5 (-0.5) 0
|
g90 = pi / 2
|
||||||
, V3 (-0.5) 0.5 0
|
|
||||||
, V3 0.5 0.5 0
|
v3tov4 :: GL.GLfloat -> V3 GL.GLfloat -> V4 GL.GLfloat
|
||||||
|
v3tov4 w (V3 x y z) = V4 x y z w
|
||||||
|
|
||||||
|
v3tov4' :: GL.GLfloat -> V3 GL.GLfloat -> V4 GL.GLfloat
|
||||||
|
v3tov4' w (V3 x y z) = V4 x y w z
|
||||||
|
|
||||||
|
v3tov4'' :: GL.GLfloat -> V3 GL.GLfloat -> V4 GL.GLfloat
|
||||||
|
v3tov4'' w (V3 x y z) = V4 x w y z
|
||||||
|
|
||||||
|
v3tov4''' :: GL.GLfloat -> V3 GL.GLfloat -> V4 GL.GLfloat
|
||||||
|
v3tov4''' w (V3 x y z) = V4 w x y z
|
||||||
|
|
||||||
|
rotate :: V3 GL.GLfloat -> GL.GLfloat -> V3 GL.GLfloat -> V3 GL.GLfloat
|
||||||
|
rotate axis angle point = L.fromQuaternion (L.axisAngle axis angle) L.!* point
|
||||||
|
|
||||||
|
rotate4 :: Int -> Int -> GL.GLfloat -> V4 GL.GLfloat -> V4 GL.GLfloat
|
||||||
|
rotate4 i0 i1 angle (V4 x y z w) =
|
||||||
|
let coords = [x, y, z, w]
|
||||||
|
cos' = cos angle
|
||||||
|
sin' = sin angle
|
||||||
|
xi = coords !! i0
|
||||||
|
xj = coords !! i1
|
||||||
|
coords' =
|
||||||
|
[ if k == i0
|
||||||
|
then cos' * xi - sin' * xj
|
||||||
|
else
|
||||||
|
if k == i1
|
||||||
|
then sin' * xi + cos' * xj
|
||||||
|
else coords !! k
|
||||||
|
| k <- [0 .. 3]
|
||||||
|
]
|
||||||
|
in V4 (coords' !! 0) (coords' !! 1) (coords' !! 2) (coords' !! 3)
|
||||||
|
|
||||||
|
cycle3r :: V3 GL.GLfloat -> V3 GL.GLfloat
|
||||||
|
cycle3r (V3 a b c) = V3 c a b
|
||||||
|
|
||||||
|
cycle3l :: V3 GL.GLfloat -> V3 GL.GLfloat
|
||||||
|
cycle3l (V3 a b c) = V3 b c a
|
||||||
|
|
||||||
|
face :: [V3 GL.GLfloat]
|
||||||
|
face =
|
||||||
|
[ V3 m m 0,
|
||||||
|
V3 p m 0,
|
||||||
|
V3 m p 0,
|
||||||
|
V3 m p 0,
|
||||||
|
V3 p m 0,
|
||||||
|
V3 p p 0
|
||||||
]
|
]
|
||||||
|
|
||||||
|
-- | cube, side length 1, centered on 0 0 0
|
||||||
|
cube :: [V3 GL.GLfloat]
|
||||||
|
cube =
|
||||||
|
concatMap
|
||||||
|
( \faceSpec ->
|
||||||
|
map
|
||||||
|
(\v -> (rotate (fst faceSpec) g90 v) L.^+^ (rotate (fst faceSpec) g90 (snd faceSpec)))
|
||||||
|
face
|
||||||
|
)
|
||||||
|
[ (V3 0 0 1, V3 0 0 p),
|
||||||
|
(V3 0 1 0, V3 0 0 p),
|
||||||
|
(V3 1 0 0, V3 0 0 p),
|
||||||
|
(V3 0 0 (-1), V3 0 0 m), -- no clue
|
||||||
|
(V3 0 (-1) 0, V3 0 0 p),
|
||||||
|
(V3 (-1) 0 0, V3 0 0 p)
|
||||||
|
]
|
||||||
|
|
||||||
|
hCube :: [V4 GL.GLfloat]
|
||||||
|
hCube =
|
||||||
|
concatMap
|
||||||
|
( \(w, i0, i1) ->
|
||||||
|
map
|
||||||
|
(rotate4 i0 i1 g90 . v3tov4 w)
|
||||||
|
cube
|
||||||
|
)
|
||||||
|
[ (p, 3, 0),
|
||||||
|
(p, 0, 3),
|
||||||
|
(p, 1, 3),
|
||||||
|
(p, 2, 3),
|
||||||
|
(m, 3, 0),
|
||||||
|
(m, 0, 3),
|
||||||
|
(m, 1, 3),
|
||||||
|
(m, 2, 3)
|
||||||
|
]
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Elm-like data structures
|
-- Elm-like data structures
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
@@ -100,84 +181,151 @@ testVertices =
|
|||||||
-- | update function
|
-- | update function
|
||||||
update :: Float -> Model -> Model
|
update :: Float -> Model -> Model
|
||||||
update dt model =
|
update dt model =
|
||||||
updateVelocity
|
updateW dt $
|
||||||
dt
|
updateVelocity dt $
|
||||||
$ updateAcceleration
|
updateAcceleration dt $
|
||||||
dt
|
updateSpeed dt $
|
||||||
$ updateCameraAngle
|
updateCameraAngle dt model
|
||||||
dt
|
|
||||||
model
|
updateW :: Float -> Model -> Model
|
||||||
|
updateW dt model =
|
||||||
|
if elem GLFW.Key'R model.keys
|
||||||
|
then
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ camPos = model.camera.camPos + (V4 0 0 0 (dt * dt * model.camera.strafeStrength))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if elem GLFW.Key'F model.keys
|
||||||
|
then
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ camPos = model.camera.camPos - (V4 0 0 0 (dt * dt * model.camera.strafeStrength))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else model
|
||||||
|
|
||||||
|
updateSpeed :: Float -> Model -> Model
|
||||||
|
updateSpeed dt model =
|
||||||
|
if elem GLFW.Key'T model.keys
|
||||||
|
then
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ jumpStrength = model.camera.jumpStrength * 1.1,
|
||||||
|
strafeStrength = model.camera.strafeStrength * 1.1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if elem GLFW.Key'G model.keys
|
||||||
|
then
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ jumpStrength = model.camera.jumpStrength * 0.99,
|
||||||
|
strafeStrength = model.camera.strafeStrength * 0.99
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else model
|
||||||
|
|
||||||
updateAcceleration :: Float -> Model -> Model
|
updateAcceleration :: Float -> Model -> Model
|
||||||
updateAcceleration dt model =
|
updateAcceleration dt model =
|
||||||
let
|
let zp =
|
||||||
zp = if elem GLFW.Key'S model.keys then 1 else 0
|
if elem GLFW.Key'S model.keys
|
||||||
zn = if elem GLFW.Key'W model.keys then 1 else 0
|
then 1
|
||||||
xp = if elem GLFW.Key'D model.keys then 1 else 0
|
else 0
|
||||||
xn = if elem GLFW.Key'A model.keys then 1 else 0
|
zn =
|
||||||
x = xp - xn
|
if elem GLFW.Key'W model.keys
|
||||||
z = zp - zn
|
then 1
|
||||||
friction = V3 (1 - model.wprop.friction) 1 (1 - model.wprop.friction)
|
else 0
|
||||||
movement = L.normalize (V3 x 0 z) L.^* (dt * model.camera.strafeStrength)
|
xp =
|
||||||
movement' = L.rotate (L.axisAngle model.wprop.up model.camera.camYaw) movement
|
if elem GLFW.Key'D model.keys
|
||||||
jump =
|
then 1
|
||||||
if model.camera.hasJumped then
|
else 0
|
||||||
V3 0 (0 - model.wprop.g * model.camera.airTime) 0
|
xn =
|
||||||
else
|
if elem GLFW.Key'A model.keys
|
||||||
V3 0 0 0
|
then 1
|
||||||
camVel' = friction * (model.camera.camVel + movement' + jump)
|
else 0
|
||||||
aboveGround = (model.camera.camPos + dt L.*^ camVel') ^. _y > 0
|
x = xp - xn
|
||||||
in
|
z = zp - zn
|
||||||
if
|
friction = V3 (1 - model.wprop.friction) 1 (1 - model.wprop.friction)
|
||||||
(elem GLFW.Key'Space model.keys)
|
movement = L.normalize (V3 x 0 z) L.^* (dt * model.camera.strafeStrength)
|
||||||
&& (model.camera.hasJumped == False)
|
movement' =
|
||||||
then
|
L.rotate (L.axisAngle model.wprop.up model.camera.camYaw) movement
|
||||||
updateAcceleration dt $ model { camera = model.camera { airTime = dt, camVel = model.camera.camVel + (V3 0 model.camera.jumpStrength 0), hasJumped = True } }
|
jump =
|
||||||
else
|
if model.camera.hasJumped
|
||||||
if aboveGround then
|
then V3 0 (0 - model.wprop.g * model.camera.airTime) 0
|
||||||
model
|
else V3 0 0 0
|
||||||
{ camera = model.camera
|
camVel' = friction * (model.camera.camVel + movement' + jump)
|
||||||
{ airTime = model.camera.airTime + dt
|
aboveGround = ((model.camera.camPos ^. _xyz) + dt L.*^ camVel') ^. _y > 0
|
||||||
, camVel = camVel'
|
in if (elem GLFW.Key'Space model.keys) && (model.camera.hasJumped == False)
|
||||||
, hasJumped = aboveGround
|
then
|
||||||
}
|
updateAcceleration dt $
|
||||||
}
|
model
|
||||||
else
|
{ camera =
|
||||||
model
|
model.camera
|
||||||
{ camera = model.camera
|
{ airTime = dt,
|
||||||
{ airTime = 0
|
camVel =
|
||||||
, camVel = camVel' * (V3 1 0 1)
|
model.camera.camVel
|
||||||
, camPos = model.camera.camPos * (V3 1 0 1)
|
+ (V3 0 model.camera.jumpStrength 0),
|
||||||
, hasJumped = aboveGround
|
hasJumped = True
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
if aboveGround
|
||||||
|
then
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ airTime = model.camera.airTime + dt,
|
||||||
|
camVel = camVel',
|
||||||
|
hasJumped = aboveGround
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
model
|
||||||
|
{ camera =
|
||||||
|
model.camera
|
||||||
|
{ airTime = 0,
|
||||||
|
camVel = camVel' * (V3 1 0 1),
|
||||||
|
camPos = model.camera.camPos * (V4 1 0 1 1),
|
||||||
|
hasJumped = aboveGround
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updateVelocity :: Float -> Model -> Model
|
updateVelocity :: Float -> Model -> Model
|
||||||
updateVelocity dt model =
|
updateVelocity dt model =
|
||||||
model
|
model
|
||||||
{ camera = model.camera
|
{ camera =
|
||||||
{ camPos = model.camera.camPos + dt L.*^ model.camera.camVel
|
model.camera
|
||||||
}
|
{ camPos = V4 1 1 1 (model.camera.camPos ^. _w) * (L.point $ (model.camera.camPos ^. _xyz) + dt L.*^ model.camera.camVel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCameraAngle :: Float -> Model -> Model
|
updateCameraAngle :: Float -> Model -> Model
|
||||||
updateCameraAngle dt model =
|
updateCameraAngle dt model =
|
||||||
let
|
let scaleFactor = model.camera.mouseSensitivity * dt
|
||||||
scaleFactor = model.camera.mouseSensitivity * dt
|
newPitch =
|
||||||
newPitch = model.camera.camPitch -
|
model.camera.camPitch
|
||||||
scaleFactor * (double2Float $ snd model.cursorDeltaPos) -- mouse sensitivity, update pitch
|
- scaleFactor * (double2Float $ snd model.cursorDeltaPos) -- mouse sensitivity, update pitch
|
||||||
newPitch' = if newPitch > 1.56 then 1.56 else newPitch
|
newPitch' =
|
||||||
newPitch'' = if newPitch' < (-1.56) then (-1.56) else newPitch'
|
if newPitch > 1.56
|
||||||
newYaw = model.camera.camYaw +
|
then 1.56
|
||||||
scaleFactor * (double2Float $ fst model.cursorDeltaPos)
|
else newPitch
|
||||||
in
|
newPitch'' =
|
||||||
model
|
if newPitch' < (-1.56)
|
||||||
{ cursorDeltaPos = (0, 0)
|
then (-1.56)
|
||||||
, camera = model.camera
|
else newPitch'
|
||||||
{ camPitch = newPitch''
|
newYaw =
|
||||||
, camYaw = newYaw
|
model.camera.camYaw
|
||||||
}
|
+ scaleFactor * (double2Float $ fst model.cursorDeltaPos)
|
||||||
}
|
in model
|
||||||
|
{ cursorDeltaPos = (0, 0),
|
||||||
|
camera = model.camera {camPitch = newPitch'', camYaw = newYaw}
|
||||||
|
}
|
||||||
|
|
||||||
-- | views the model
|
-- | views the model
|
||||||
view :: GLFW.Window -> Model -> IO ()
|
view :: GLFW.Window -> Model -> IO ()
|
||||||
@@ -185,39 +333,51 @@ view window model = do
|
|||||||
-- fit viewport to window
|
-- fit viewport to window
|
||||||
(w, h) <- GLFW.getFramebufferSize window
|
(w, h) <- GLFW.getFramebufferSize window
|
||||||
GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral w) (fromIntegral h))
|
GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral w) (fromIntegral h))
|
||||||
|
|
||||||
-- clear screen
|
-- clear screen
|
||||||
GL.clearColor $= GL.Color4 1 0 1 1
|
GL.clearColor $= GL.Color4 1 0 1 1
|
||||||
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
|
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
|
||||||
|
|
||||||
-- depth
|
-- depth
|
||||||
GL.depthFunc $= Just GL.Less
|
GL.depthFunc $= Just GL.Less
|
||||||
|
|
||||||
-- apply transforms
|
-- apply transforms
|
||||||
let
|
let pitch = model.camera.camPitch
|
||||||
pitch = model.camera.camPitch
|
yaw = model.camera.camYaw
|
||||||
yaw = model.camera.camYaw
|
forward = V3 (cos pitch * sin yaw) (sin pitch) (cos pitch * cos yaw)
|
||||||
forward = V3 (cos pitch * sin yaw) (sin pitch) (cos pitch * cos yaw)
|
viewMatrix =
|
||||||
viewMatrix =
|
L.lookAt
|
||||||
L.lookAt
|
(model.camera.camPos ^. _xyz)
|
||||||
model.camera.camPos
|
((model.camera.camPos ^. _xyz) - forward)
|
||||||
(model.camera.camPos - forward)
|
model.wprop.up
|
||||||
model.wprop.up
|
projectionMatrix =
|
||||||
projectionMatrix = L.perspective 1.5 (fromIntegral w / fromIntegral h) 0.01 10000
|
L.perspective 1.2 (fromIntegral w / fromIntegral h) 0.01 1000
|
||||||
|
|
||||||
viewGLMatrix <- GL.newMatrix GL.RowMajor $ toGLMatrix viewMatrix :: IO (GL.GLmatrix GL.GLfloat)
|
viewGLMatrix <-
|
||||||
|
GL.newMatrix GL.RowMajor $ toGLMatrix viewMatrix ::
|
||||||
|
IO
|
||||||
|
(GL.GLmatrix GL.GLfloat)
|
||||||
|
|
||||||
|
-- load 3d view matrix
|
||||||
viewLocation <- GL.get $ GL.uniformLocation model.program "u_view"
|
viewLocation <- GL.get $ GL.uniformLocation model.program "u_view"
|
||||||
GL.uniform viewLocation $= viewGLMatrix
|
GL.uniform viewLocation $= viewGLMatrix
|
||||||
|
|
||||||
projectionGLMatrix <- GL.newMatrix GL.RowMajor $ toGLMatrix projectionMatrix :: IO (GL.GLmatrix GL.GLfloat)
|
projectionGLMatrix <-
|
||||||
|
GL.newMatrix GL.RowMajor $ toGLMatrix projectionMatrix ::
|
||||||
|
IO
|
||||||
|
(GL.GLmatrix GL.GLfloat)
|
||||||
|
|
||||||
|
-- load 3d projection matrix
|
||||||
projectionLocation <- GL.get $ GL.uniformLocation model.program "u_projection"
|
projectionLocation <- GL.get $ GL.uniformLocation model.program "u_projection"
|
||||||
GL.uniform projectionLocation $= projectionGLMatrix
|
GL.uniform projectionLocation $= projectionGLMatrix
|
||||||
|
|
||||||
|
let camx = (\(V4 x _ _ _) -> x) model.camera.camPos
|
||||||
|
camy = (\(V4 _ y _ _) -> y) model.camera.camPos
|
||||||
|
camz = (\(V4 _ _ z _) -> z) model.camera.camPos
|
||||||
|
camw = (\(V4 _ _ _ w) -> w) model.camera.camPos
|
||||||
|
camWLocation <- GL.get $ GL.uniformLocation model.program "u_cam"
|
||||||
|
GL.uniform camWLocation $= GL.Vector4 camx camy camz camw
|
||||||
|
|
||||||
-- draw objects; returns IO []
|
-- draw objects; returns IO []
|
||||||
_ <- drawObjects model.objects
|
_ <- drawObjects model.objects
|
||||||
|
|
||||||
-- swap to current buffer
|
-- swap to current buffer
|
||||||
GLFW.swapBuffers window
|
GLFW.swapBuffers window
|
||||||
|
|
||||||
-- check for interrupts
|
-- check for interrupts
|
||||||
GLFW.pollEvents
|
GLFW.pollEvents
|
||||||
|
|||||||
Reference in New Issue
Block a user