renderer-tutorial/src/Main.hs

212 lines
5 KiB
Haskell
Raw Normal View History

2020-05-17 04:40:30 +00:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Strict #-}
2020-05-15 18:04:18 +00:00
module Main where
2020-05-17 04:40:30 +00:00
import SDL (($=), get)
import qualified SDL
import qualified SDL.Raw.Video as SDL (glSetAttribute)
import qualified SDL.Raw.Enum as SDL
import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.GL as GLRaw
import Control.Concurrent.MVar
import Control.Monad
import Control.Monad.Loops
import Foreign hiding (void)
import Foreign.Ptr
import Foreign.Marshal.Array
import Foreign.Storable
import Data.Word
import qualified Data.ByteString as B
2020-05-17 08:59:15 +00:00
import qualified Data.ByteString.Char8 as B8
import qualified Data.List as L
2020-05-17 04:40:30 +00:00
import System.Random (randomRIO)
2020-05-18 02:58:45 +00:00
-- internal imports
2020-05-21 16:17:38 +00:00
import BindableClass
2020-05-18 02:58:45 +00:00
import BufferClass
2020-05-20 03:54:14 +00:00
import VertexArray
2020-05-18 02:58:45 +00:00
import VertexBuffer
import IndexBuffer
2020-05-21 19:04:05 +00:00
import Shader
2020-05-22 00:29:16 +00:00
import Renderer
2020-05-22 14:16:06 +00:00
import Texture
2020-05-18 02:58:45 +00:00
2020-05-15 18:04:18 +00:00
main :: IO ()
2020-05-17 04:40:30 +00:00
main = do
-- -- INITIALIZATION
-- Initialize all the things! (including wacky stuff like joysticks and so on)
SDL.initializeAll
-- create an actual window with OpenGL 3.3
window <- SDL.createWindow "renderer" (SDL.defaultWindow
{ SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 3
}
}
)
-- enable context sharing (this is not absolutely neccessary)
void $ SDL.glSetAttribute SDL.SDL_GL_SHARE_WITH_CURRENT_CONTEXT 1
-- create ancutal context
context <- SDL.glCreateContext window
-- window should be a window, not fullscreen or so
SDL.setWindowMode window SDL.Windowed
2020-05-17 15:27:18 +00:00
-- synchronize buffer swapping with monitor's vsync
SDL.swapInterval $= SDL.SynchronizedUpdates
2020-05-17 04:40:30 +00:00
-- show the window
SDL.showWindow window
-- print OpenGL version of active context
version <- peekArray0 (0 :: Word8) =<< GLRaw.glGetString GLRaw.GL_VERSION
print (B.pack version)
-- -- VERTICES
-- first, create and bind a vertex array object
2020-05-17 19:27:35 +00:00
vao <- GL.genObjectName
GL.bindVertexArrayObject $= Just vao
2020-05-17 04:40:30 +00:00
2020-05-22 14:16:06 +00:00
-- define vertices (positions of the rectangle corners) as List of Floats
-- with added in texture coordinates
2020-05-17 04:40:30 +00:00
let vertexPositions =
2020-05-22 14:16:06 +00:00
-- 3D positions | texture coordinates
[ (-0.5), (-0.5), 0, 0, 0 -- 0
, 0.5 , (-0.5), 0, 1, 0 -- 1
, 0.5 , 0.5 , 0, 1, 1 -- 2
, (-0.5), 0.5 , 0, 0, 1 -- 3
2020-05-17 11:04:13 +00:00
] :: [GL.GLfloat]
2020-05-17 04:40:30 +00:00
2020-05-17 10:26:57 +00:00
-- create draw order indices
2020-05-17 11:04:13 +00:00
indices = [0, 1, 2, 2, 3, 0] :: [GL.GLuint]
2020-05-17 10:26:57 +00:00
2020-05-18 02:58:45 +00:00
-- construct new VertexBuffer and fill it with data
2020-05-20 03:54:14 +00:00
vao <- newVertexArray
2020-05-18 02:58:45 +00:00
vbo <- newVertexBuffer vertexPositions
-- rebind the vertex buffer
bind vbo
2020-05-20 03:54:14 +00:00
2020-05-17 04:40:30 +00:00
-- enable and specify data layout of the in-memory vertices
2020-05-20 03:54:14 +00:00
layout <- newVertexBufferLayout
2020-05-22 14:16:06 +00:00
-- push vertex positions
pushElements layout GL.Float 3
-- pusht texture coordinates
2020-05-20 03:54:14 +00:00
pushElements layout GL.Float 2
addBuffer vao vbo layout
2020-05-22 14:16:06 +00:00
-- -- TEXTURE
-- generate and bind texture
tex <- newTexture "res/textures/lynx.jpg" 0
bind tex
2020-05-17 04:40:30 +00:00
2020-05-18 02:58:45 +00:00
-- construct new IndexBuffer and fill it with data
ibo <- newIndexBuffer indices
2020-05-17 10:26:57 +00:00
2020-05-17 04:40:30 +00:00
-- -- SHADERS
2020-05-21 19:04:05 +00:00
sp <- newShader
[ ShaderSource GL.VertexShader "./res/shaders/vert.shader"
, ShaderSource GL.FragmentShader "./res/shaders/frag.shader"
]
[ "u_color"
2020-05-22 14:16:06 +00:00
, "u_texture"
2020-05-21 19:04:05 +00:00
]
2020-05-17 11:37:59 +00:00
2020-05-22 14:16:06 +00:00
-- -- tell the shader where to find the texture
bind sp
setUniform sp "u_texture" (texSlot tex)
2020-05-17 11:37:59 +00:00
-- -- UNIFORMS
2020-05-21 19:04:05 +00:00
-- create MVars for pulsating red channel
2020-05-17 15:28:39 +00:00
red <- newMVar 0
increment <- newMVar 0.05
2020-05-17 11:37:59 +00:00
2020-05-17 11:06:09 +00:00
err <- get GL.errors
print $ "pre-loop errors: " <> show err
2020-05-17 04:40:30 +00:00
-- -- EVENTING AND DRAWING
2020-05-17 19:27:35 +00:00
-- -- UNBINDING ALL BUFFERS AND VERTEX ARRAYS
2020-05-22 14:16:06 +00:00
unbind vao
2020-05-18 02:58:45 +00:00
unbind vbo
unbind ibo
2020-05-21 19:04:05 +00:00
unbind sp
2020-05-17 19:27:35 +00:00
-- -- LOOPING
2020-05-17 04:40:30 +00:00
-- initial poll for events
evs <- newMVar =<< SDL.pollEvents
-- Loop running until window closes
whileM_ (notElem (SDL.WindowClosedEvent (SDL.WindowClosedEventData window))
<$> map SDL.eventPayload <$> readMVar evs) $ do
-- poll again
void $ swapMVar evs =<< SDL.pollEvents
-- -- OBEY THE HYPNOTOAD!
-- clearcol <- GL.Color4
-- <$> (randomRIO (0, 1))
-- <*> (randomRIO (0, 1))
-- <*> (randomRIO (0, 1))
-- <*> pure 1
-- GL.clearColor $= clearcol
2020-05-17 15:28:39 +00:00
incrementValue <- takeMVar increment
2020-05-21 19:04:05 +00:00
redValue <- takeMVar red
2020-05-17 15:28:39 +00:00
let newRed = redValue + incrementValue
2020-05-21 19:04:05 +00:00
-- write data to the uniform
2020-05-22 00:38:32 +00:00
bind sp
2020-05-21 19:04:05 +00:00
setUniform sp "u_color" (GL.Color4 newRed 0.5 0 1 :: GL.Color4 GL.GLfloat)
2020-05-17 15:28:39 +00:00
2020-05-17 10:35:29 +00:00
-- the actual drawing happens here
2020-05-22 00:29:16 +00:00
draw vao ibo sp
2020-05-17 11:06:09 +00:00
err <- get GL.errors
when (not $ null err) (print $ "loop errors: " <> show err)
2020-05-17 04:40:30 +00:00
2020-05-17 15:30:05 +00:00
-- update MVAr values
putMVar red newRed
-- cycle the increment for red if neccessary
if (newRed + incrementValue > 1 || newRed + incrementValue < 0)
then
putMVar increment (-incrementValue)
else
putMVar increment incrementValue
2020-05-17 04:40:30 +00:00
-- draw context on screen
SDL.glSwapWindow window
-- -- CLEAN UP
-- wrapping things up
putStrLn "loop exited. shutting down."
-- delete context
SDL.glDeleteContext context
-- destroy window
SDL.destroyWindow window
-- quit SDL
SDL.quit
-- This is the end.
putStrLn "Bye!"