From 8428f08ebddd8f05bd0649f1844bcde0f6438df9 Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 18 Nov 2025 10:55:46 +0000 Subject: [PATCH] Work on distortions --- shader/texture/barrel.frag | 42 +++++++++++++++++++-------- shader/texture/barrel.vert | 55 +++++++++++++++++++++--------------- src/Dodge/Data/Distortion.hs | 5 +++- src/Dodge/Distortion.hs | 4 +-- src/Dodge/Event/Test.hs | 18 ++++++++---- src/Dodge/Render.hs | 9 +++--- 6 files changed, 86 insertions(+), 47 deletions(-) diff --git a/shader/texture/barrel.frag b/shader/texture/barrel.frag index 98a4dedff..e06fd6f45 100644 --- a/shader/texture/barrel.frag +++ b/shader/texture/barrel.frag @@ -4,20 +4,38 @@ in vec2 distmask; in vec2 pinch; out vec4 fColor; layout (binding = 1) uniform sampler2D screenTexture; -float x = 1 - min(1, dot(distmask,distmask)); +float x = 1 - min(1, sqrt(distance(distmask,vec2(0,0)))); float xab = abs(distmask.x); float yab = abs(distmask.y); +vec3 toYUV (vec3 rgb) +{ return rgb * mat3 + ( 0.299 , 0.587 , 0.114 + ,-0.14713,-0.28886, 0.436 + , 0.615 ,-0.51499,-0.10001 + ) ; +} +vec3 toRGB (vec3 yuv) +{ return yuv * mat3 + (1, 0 , 1.13983 + ,1,-0.39465,-0.58060 + ,1, 2.03211, 0 + ) ; +} void main() { - fColor = (xab < 1 && yab < 1 - //? vec4 (0.5*(distmask.x+1),0.5*(distmask.y+1),0,1) - ? vec4 (0.5*(distmask.x+1),0.5*(distmask.y+1),0,1) - : texture(screenTexture, texPos)); - //fColor = texture(screenTexture, (texPos + x*x*pinch)); - //fColor = vec4 - // ( texture(screenTexture, (texPos + x*x*pinch)).r - // , texture(screenTexture, (texPos - x*x*pinch)).g - // , texture(screenTexture, (texPos - x*x*(pinch.yx))).b - // , texture(screenTexture, texPos).a - // ) ; + fColor = texture(screenTexture, (texPos + x * pinch)); + //fColor = texture(screenTexture, (texPos + x*pinch)); + //fColor = texture(screenTexture, (texPos + x*pinch)); +// fColor = vec4 +// ( texture(screenTexture, (texPos + x*pinch)).r +// , texture(screenTexture, (texPos + x*(-pinch))).g +// , texture(screenTexture, texPos).ba +// ) ; +// fColor = vec4 +// ( toRGB(vec3( toYUV(texture(screenTexture, texPos).rgb).r +// , toYUV(texture(screenTexture, (texPos + x*pinch)).rgb).g +// , toYUV(texture(screenTexture, (texPos + x*(pinch))).rgb).b +// ) ) +// , 1 +// ) ; } diff --git a/shader/texture/barrel.vert b/shader/texture/barrel.vert index ae9a5db3e..f51725bc3 100644 --- a/shader/texture/barrel.vert +++ b/shader/texture/barrel.vert @@ -1,5 +1,5 @@ #version 450 core -struct DataS { vec2 pos; vec2 v1; vec2 v2; vec2 rad; }; +struct DataS { vec2 pos; vec2 v1; vec2 v2; vec2 t1; vec2 t2; vec2 t3; }; layout (std430, binding = 9) readonly buffer Data { DataS data[]; }; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; out vec2 texPos; @@ -15,32 +15,41 @@ const vec2 cs[4] = , vec2 (-1, 1) , vec2 ( 1, 1) }; +vec2 f (vec2 p) // transforms a vector from "world" into "screen" space +{ return (theMat * vec4(p,0,1) - theMat * vec4(0,0,0,1)).xy; } +vec2 g (vec2 p) +{ return normalize(p); } +vec2 ncToTex (vec2 p) {return 0.5 * (p + 1);} +mat4 toTransMat (vec2 p) +{ return mat4 + ( 1, 0, 0 ,0 + , 0, 1, 0 ,0 + , 0, 0, 1 ,0 + ,p.x,p.y, 0 ,1 + ) ; +} void main() { int k = ks[gl_VertexID]; - vec4 off = (theMat * vec4(data[gl_VertexID/6].pos, 0,1)); - vec2 r1 = (theMat * vec4(data[gl_VertexID/6].v1, 0,1)).xy - - (theMat * vec4(0,0,0,1)).xy; - vec2 r2 = (theMat * vec4(data[gl_VertexID/6].v2, 0,1)).xy - - (theMat * vec4(0,0, 0,1)).xy; - pinch = - (theMat * vec4(data[gl_VertexID/6].rad, 0,1)).xy - - (theMat * vec4(0,0, 0,1)).xy; -// mat2 distT = mat2(-1/r1.x, -1/r2.x -// ,1/r1.y, -1/r2.y -// ); -// mat2 distT = mat2(2, 0 -// ,-1/sqrt(2), 1/sqrt(2) -// ); -// mat2 distT = mat2(1/r1.x, 2*sqrt(2) -// ,1/r1.y, 2*sqrt(2) -// ); + DataS thedata = data[gl_VertexID/6]; + vec4 off = theMat * vec4(data[gl_VertexID/6].pos, 0,1); + vec2 r1 = f(thedata.v1); + vec2 r2 = f(thedata.v2); mat2 distT = inverse (mat2(r1,r2)); -// mat2 distT = mat2(1/r1.x, 1/r2.x -// ,1/r1.y, 1/r2.y -// ); - //distmask = (cs[k] - off.xy)/r; distmask = distT * (cs[k] - off.xy); - texPos = 0.5 * (cs[k]+vec2(1,1)); + vec4 pinchx = theMat + * toTransMat(thedata.t3) + * toTransMat(thedata.pos) + * mat4 + (thedata.t1 , 0, 0 + ,thedata.t2 , 0, 0 + , 0, 0 , 0, 0 + , 0, 0 , 0, 1 + ) + * inverse(toTransMat(thedata.pos)) + * inverse(theMat) + * (vec4(cs[k],0,1)); + pinch = ncToTex(pinchx.xy) - ncToTex(cs[k]); + texPos = ncToTex(cs[k]); gl_Position = vec4 (cs[k],0,1); } diff --git a/src/Dodge/Data/Distortion.hs b/src/Dodge/Data/Distortion.hs index b2aa24ebd..ce9636ea1 100644 --- a/src/Dodge/Data/Distortion.hs +++ b/src/Dodge/Data/Distortion.hs @@ -7,7 +7,10 @@ import Geometry import Data.Aeson import Data.Aeson.TH -data Distortion = RadialDistortion Point2 Point2 Point2 Point2 Int +data Distortion = RadialDistortion + Point2 Point2 Point2 + Point2 Point2 Point2 + Int deriving (Eq, Ord, Show, Read) --Generic, Flat) deriveJSON defaultOptions ''Distortion diff --git a/src/Dodge/Distortion.hs b/src/Dodge/Distortion.hs index 3cdadd945..571ba540b 100644 --- a/src/Dodge/Distortion.hs +++ b/src/Dodge/Distortion.hs @@ -5,6 +5,6 @@ import Dodge.Data.Distortion updateDistortion :: Distortion -> Maybe Distortion updateDistortion = \case - RadialDistortion a b c d t - | t > 0 -> Just $ RadialDistortion a b c d $ t-1 + RadialDistortion a b c d e f t + | t > 0 -> Just $ RadialDistortion a b c d e f $ t-1 | otherwise -> Nothing diff --git a/src/Dodge/Event/Test.hs b/src/Dodge/Event/Test.hs index 07da7521d..3765cf11c 100644 --- a/src/Dodge/Event/Test.hs +++ b/src/Dodge/Event/Test.hs @@ -1,15 +1,23 @@ module Dodge.Event.Test (testEvent) where --import Dodge.Creature.State -import Dodge.Data.World -import Linear -import LensHelp + import Data.Maybe +import Dodge.Data.World +import LensHelp +import Linear testEvent :: World -> World testEvent w = fromMaybe w $ do cpos <- w ^? cWorld . lWorld . creatures . ix 0 . crPos . _xy let distR = 50 - distortionBulge = RadialDistortion cpos (V2 (0.5*distR) (-0.5*distR)) (V2 0 distR) - (V2 (0.5* distR) 0) 20 + distortionBulge = + RadialDistortion + cpos + (V2 distR 0) + (V2 0 distR) + (V2 2 0) + (V2 0 0.5) + (V2 0 0) + 20 return $ w & cWorld . lWorld . distortions .~ [distortionBulge] diff --git a/src/Dodge/Render.hs b/src/Dodge/Render.hs index d5a3c34ac..758eb11af 100644 --- a/src/Dodge/Render.hs +++ b/src/Dodge/Render.hs @@ -366,12 +366,13 @@ doDrawing' win pdata u = do glBindFramebuffer GL_FRAMEBUFFER 0 glUseProgram (pdata ^. fullscreenShader) glDrawArrays GL_TRIANGLES 0 6 - (RadialDistortion (V2 a b) (V2 c d) (V2 e f) (V2 g h) _:_) -> do + --(RadialDistortion (V2 a b) (V2 c d) (V2 e f) (V2 g h) _:_) -> do + (RadialDistortion a b c d e f _:_) -> do glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fbo2 . _1 . unFBO) - pokeArray (pdata ^. barrelShader . _2 . vboPtr) [a, b, c, d, e, f, g, h] - withArray [a, b, c, d, e, f, g] $ +-- pokeArray (pdata ^. barrelShader . _2 . vboPtr) [a, b, c, d, e, f, g, h] + withArray [a, b, c, d, e, f] $ glNamedBufferSubData (pdata ^. barrelShader . _2 . vboName) 0 - (6 * fromIntegral (sizeOf (0 :: Float))) + (12 * fromIntegral (sizeOf (0 :: Float))) glBindTextureUnit 1 $ pdata ^. fboBase . _2 . _1 . unTO glUseProgram (pdata ^. barrelShader . _1) glDrawArrays GL_TRIANGLES 0 6