Create world matrix uniform

This commit is contained in:
2021-02-25 23:30:02 +01:00
parent 37e26dcb07
commit 9575eab230
9 changed files with 86 additions and 61 deletions
+1
View File
@@ -42,6 +42,7 @@ dependencies:
- deepseq - deepseq
- transformers - transformers
- foldl - foldl
- linear
library: library:
source-dirs: src source-dirs: src
+2 -18
View File
@@ -9,27 +9,11 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform vec2 translation; uniform vec2 translation;
uniform float rotation; uniform float rotation;
uniform mat4 worldMat;
vec2 tran(vec2 v)
{
return (v - translation);
}
vec2 rot(vec2 v)
{
return vec2 ( v.x * cos(-rotation) - v.y * sin(-rotation)
, v.x * sin(-rotation) + v.y * cos(-rotation)
);
}
vec2 scal(vec2 v)
{
return vec2 ( 2 * v.x * zoom / winSize.x
, 2 * v.y * zoom / winSize.y
);
}
void main() void main()
{ {
gl_Position = vec4(scal(rot(tran(position.xy))),position.z,1); gl_Position = worldMat * vec4(position.xyz,1);
vColor = color; vColor = color;
vparams = saEaRadWdth; vparams = saEaRadWdth;
} }
+1
View File
@@ -8,6 +8,7 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform vec2 translation; uniform vec2 translation;
uniform float rotation; uniform float rotation;
uniform mat4 worldMat;
void main() void main()
{ {
+18 -16
View File
@@ -7,26 +7,28 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform float rotation; uniform float rotation;
uniform vec2 translation; uniform vec2 translation;
uniform mat4 worldMat;
vec2 tran(vec2 v) mat4 tranMat = mat4
{ ( 1.0, 0.0, 0.0, 0.0
return (v - translation); , 0.0, 1.0, 0.0, 0.0
} , 0.0, 0.0, 1.0, 0.0
vec2 rot(vec2 v) , -translation.x, -translation.y, 0.0, 1.0
{
return vec2 ( v.x * cos(-rotation) - v.y * sin(-rotation)
, v.x * sin(-rotation) + v.y * cos(-rotation)
) ; ) ;
} mat4 rotMat = mat4
vec2 scal(vec2 v) ( cos(rotation), sin(-rotation), 0.0, 0.0
{ , sin(rotation), cos(rotation), 0.0, 0.0
return vec2 ( 2 * v.x * zoom / winSize.x , 0.0, 0.0, 1.0, 0.0
, 2 * v.y * zoom / winSize.y , 0.0, 0.0, 0.0, 1.0
) ;
mat4 scalMat = mat4
( 2*zoom/winSize.x, 0.0, 0.0, 0.0
, 0.0, 2*zoom/winSize.y, 0.0, 0.0
, 0.0, 0.0, 1.0, 0.0
, 0.0, 0.0, 0.0, 1.0
) ; ) ;
}
void main() void main()
{ {
gl_Position = vec4(scal(rot(tran(position.xy))),position.z,1); gl_Position = worldMat * vec4(position.xyz,1);
vColor = color; vColor = color;
} }
+18 -15
View File
@@ -9,26 +9,29 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform vec2 translation; uniform vec2 translation;
uniform float rotation; uniform float rotation;
uniform mat4 worldMat;
vec2 tran(vec2 v) mat4 tranMat = mat4
{ ( 1.0, 0.0, 0.0, 0.0
return (v - translation); , 0.0, 1.0, 0.0, 0.0
} , 0.0, 0.0, 1.0, 0.0
vec2 rot(vec2 v) , -translation.x, -translation.y, 0.0, 1.0
{
return vec2 ( v.x * cos(-rotation) - v.y * sin(-rotation)
, v.x * sin(-rotation) + v.y * cos(-rotation)
) ; ) ;
} mat4 rotMat = mat4
vec2 scal(vec2 v) ( cos(rotation), sin(-rotation), 0.0, 0.0
{ , sin(rotation), cos(rotation), 0.0, 0.0
return vec2 ( 2 * v.x * zoom / winSize.x , 0.0, 0.0, 1.0, 0.0
, 2 * v.y * zoom / winSize.y , 0.0, 0.0, 0.0, 1.0
) ;
mat4 scalMat = mat4
( 2*zoom/winSize.x, 0.0, 0.0, 0.0
, 0.0, 2*zoom/winSize.y, 0.0, 0.0
, 0.0, 0.0, 1.0, 0.0
, 0.0, 0.0, 0.0, 1.0
) ; ) ;
}
void main() void main()
{ {
gl_Position = vec4(scal(rot(tran(position.xy))),position.z,1); gl_Position = scalMat * rotMat * tranMat * vec4(position.xyz,1);
vColor = color; vColor = color;
vRad = rad; vRad = rad;
} }
+1
View File
@@ -6,6 +6,7 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform vec2 translation; uniform vec2 translation;
uniform float rotation; uniform float rotation;
uniform mat4 worldMat;
void main() void main()
{ {
+1
View File
@@ -8,6 +8,7 @@ uniform vec2 winSize;
uniform float zoom; uniform float zoom;
uniform float rotation; uniform float rotation;
uniform vec2 translation; uniform vec2 translation;
uniform mat4 worldMat;
void main() void main()
{ {
+39 -7
View File
@@ -4,23 +4,27 @@ module Picture.Render
where where
import Control.Lens import Control.Lens
import Control.Monad import Control.Monad
import Control.Monad.Trans.State --import Control.Monad.Trans.State
--
--
import Linear.Matrix
import Linear.V4
import qualified Control.Foldl as F import qualified Control.Foldl as F
import Data.Bifunctor import Data.Bifunctor
import Picture import Picture.Data
import Geometry import Geometry
import Picture.Preload import Picture.Preload
--import Control.Lens --import Control.Lens
import Foreign import Foreign hiding (rotate)
import Codec.Picture import Codec.Picture
import Graphics.Rendering.OpenGL hiding (Line,get,translate,scale,imageHeight,imageWidth,Polygon,Color,T) import Graphics.Rendering.OpenGL hiding (Line,translate,scale,imageHeight,imageWidth,Polygon,Color,T)
import qualified Graphics.Rendering.OpenGL as GL import qualified Graphics.Rendering.OpenGL as GL
import Data.Foldable import Data.Foldable
@@ -30,6 +34,9 @@ import qualified Data.IntMap as IM
import Control.DeepSeq import Control.DeepSeq
white = (1,1,1,1)
black = (0,0,0,1)
polyToTris :: [s] -> [s] polyToTris :: [s] -> [s]
{-# INLINE polyToTris #-} {-# INLINE polyToTris #-}
polyToTris (a:b:c:as) = a : intercalate [a] (zipWith (\x y->[x,y]) (init (b:c:as)) (c:as)) polyToTris (a:b:c:as) = a : intercalate [a] (zipWith (\x y->[x,y]) (init (b:c:as)) (c:as))
@@ -311,6 +318,26 @@ renderPicture' :: RenderData -> Float -> Float -> (Float,Float) -> (Float,Float)
renderPicture' pdata rot zoom (tranx,trany) (winx,winy) wallPoints lightPoints pic = do renderPicture' pdata rot zoom (tranx,trany) (winx,winy) wallPoints lightPoints pic = do
depthFunc $= Just Lequal depthFunc $= Just Lequal
-- calculate world transformation matrix
let scalMat = Linear.Matrix.transpose $
V4 (V4 (2*zoom/winx) 0 0 (0::GLfloat))
(V4 0 (2*zoom/winy) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
let rotMat = Linear.Matrix.transpose $
V4 (V4 (cos rot) (sin (-rot)) 0 0)
(V4 (sin rot) (cos rot) 0 0)
(V4 0 0 1 0)
(V4 0 0 0 1)
let tranMat = Linear.Matrix.transpose $
V4 (V4 1 0 0 0)
(V4 0 1 0 0)
(V4 0 0 1 0)
(V4 (-tranx) (-trany) 0 1)
let wmat = scalMat !*! rotMat !*! tranMat
vToL (V4 a b c d) = [a,b,c,d]
wmata <- (newMatrix RowMajor $ concatMap vToL $ vToL wmat) :: IO (GLmatrix GLfloat)
-- set common uniforms -- set common uniforms
forM_ [_basicShader pdata forM_ [_basicShader pdata
,_textShader pdata ,_textShader pdata
@@ -326,6 +353,11 @@ renderPicture' pdata rot zoom (tranx,trany) (winx,winy) wallPoints lightPoints p
uniform (snd shad !! 2) $= rot uniform (snd shad !! 2) $= rot
uniform (snd shad !! 3) $= Vector2 tranx trany uniform (snd shad !! 3) $= Vector2 tranx trany
-- matrixMode $= Projection
-- loadIdentity
uniform (snd shad !! 4) $= wmata
-- draw lightmap -- draw lightmap
bindVertexArrayObject $= Just (_vao $ _wallVAO pdata) bindVertexArrayObject $= Just (_vao $ _wallVAO pdata)
let wallPtr = (\(_,x,_) -> x) $ head $ _vaoBufferTargets $ _wallVAO pdata let wallPtr = (\(_,x,_) -> x) $ head $ _vaoBufferTargets $ _wallVAO pdata
@@ -370,9 +402,9 @@ renderPicture' pdata rot zoom (tranx,trany) (winx,winy) wallPoints lightPoints p
,_textShader pdata ,_textShader pdata
,_circShader pdata ,_circShader pdata
,_arcShader pdata ,_arcShader pdata
,_fadeCircleShader pdata -- ,_fadeCircleShader pdata
,_backShader pdata -- ,_backShader pdata
,_wallShadowShader pdata -- ,_wallShadowShader pdata
] $ \shad -> do ] $ \shad -> do
currentProgram $= Just (fst shad) currentProgram $= Just (fst shad)
uniform (snd shad !! 0) $= Vector2 (2::Float) 2 uniform (snd shad !! 0) $= Vector2 (2::Float) 2
+1 -1
View File
@@ -22,7 +22,7 @@ makeSourcedShader :: String -> [ShaderType] -> IO (Program, [UniformLocation])
makeSourcedShader s sts = do makeSourcedShader s sts = do
sources <- forM sts $ \st -> BS.readFile ("shader/" ++ s ++ shaderTypeExt st) sources <- forM sts $ \st -> BS.readFile ("shader/" ++ s ++ shaderTypeExt st)
prog <- makeShaderProgram' s $ zip sts sources prog <- makeShaderProgram' s $ zip sts sources
uniformLocations <- forM ["winSize","zoom","rotation","translation"] uniformLocations <- forM ["winSize","zoom","rotation","translation","worldMat"]
$ \uniString -> uniformLocation prog uniString $ \uniString -> uniformLocation prog uniString
return (prog,uniformLocations) return (prog,uniformLocations)