Skip to content

Commit

Permalink
Tweaks to mandelbrot (palette)
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-romes committed Oct 26, 2023
1 parent 9e26f62 commit 2808196
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
6 changes: 3 additions & 3 deletions examples/mandlebrot-set/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ viewportIndices
, 2, 1, 3
]

-- pattern WINDOW_SIZE = (2560, 1600)
pattern WINDOW_SIZE = (1920, 1200)
-- | we should be getting the window size dynamically (in ghengin utils we can even pass it automatically)
pattern WINDOW_SIZE = (2560, 1600)
-- pattern WINDOW_SIZE = (1920, 1200)

makeMainPipeline :: Renderer (RenderPipeline _ '[MousePos])
makeMainPipeline = makeRenderPipeline (shaderPipeline WINDOW_SIZE)
Expand Down Expand Up @@ -103,7 +104,6 @@ main = do

(freeRenderQueue rq )

-- This is all done in the freeRenderQueue!
-- In fact, freeing these again is a type error. Woho!
-- (destroyRenderPipeline pipeline ↑)

Expand Down
66 changes: 53 additions & 13 deletions examples/mandlebrot-set/Shaders.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,31 @@ type FragmentDefs =
#define N 64
#define B 4

mandel :: CodeComplex Float -> CodeComplex Float -> CodeComplex Float
mandel z c = z * z + c

burning_ship :: CodeComplex Float -> CodeComplex Float -> CodeComplex Float
burning_ship (a :+: b) c = (a :+: abs b) + c

fragment :: (Float, Float) -> G.FragmentShaderModule FragmentDefs _
fragment (width,height) = shader do

~( Vec4 x y _ _ ) <- #gl_FragCoord

let uv = (2 *^ (Vec2 x y) ^-^ (Vec2 (Lit width) (Lit height)) ^-^ Vec2 1 1) ^/ (Lit height)
let uv = (1.5 *^ (2 *^ (Vec2 x y) ^-^ (Vec2 (Lit width) (Lit height)) ^-^ Vec2 1 1)) ^/ (Lit height) ^-^ Vec2 0.4 0

i <- iterate (CodeComplex uv)
i <- iterate mandel (CodeComplex uv)

let r = if i == N then 1 else i / N
let (Vec3 r g b) = if i == N then Vec3 0 0 0 else color (i / N)

#out_colour .= Vec4 r r r 1
#out_colour .= Vec4 r g b 1

iterate :: _ => CodeComplex Float -> Program _s _s (Code Float)
iterate c = locally do
iterate :: _
=> (CodeComplex Float -> CodeComplex Float -> CodeComplex Float)
-- ^ Fractal series function, taking complex numbers @z@ and @c@ as input
-> CodeComplex Float
-> Program _s _s (Code Float)
iterate fractal_s c = locally do
#z #= (Vec2 0 0 :: Code (V 2 Float))
#depth #= (0 :: Code Float) -- float incremented as an integer

Expand All @@ -97,18 +107,48 @@ iterate c = locally do
if dot zv zv > B*B || depth >= N
then break @1
else do
#z .= codeComplex (z * z + c)
#z .= codeComplex (fractal_s z c)
#depth .= depth + 1

zv <- #z
depth <- #depth
-- return (depth - log (log (dot zv zv) / log B / log 2.0))
return depth

-- iterate' :: Code Float -> CodeComplex Float -> CodeComplex Float -> Code Float
-- iterate' !i z c = do
-- let z' = z * z + c
-- if dot (codeComplex z') (codeComplex z') > B*B
-- then i
-- else iterate' (i+1) z' c
color :: Code Float -> Code (V 3 Float)
color t
= palette t
-- 3rd palette from https://darkeclipz.github.io/fractals/paper/Fractals%20&%20Rendering%20Techniques.html
(Vec3 0.66 0.56 0.68)
(Vec3 0.718 0.438 0.72)
(Vec3 0.52 0.8 0.52)
(Vec3 (-0.43) (-0.397) (-0.083))

palette :: Code Float -> Code (V 3 Float) -> Code (V 3 Float) -> Code (V 3 Float) -> Code (V 3 Float) -> Code (V 3 Float)
palette t a b c d = a ^+^ (b .* cos3 (6.28318*^(c^*t ^+^ d)))

random v = fract(sin(dot(v, Vec2 12.989 78.233)) * 43758.543)


------------------------------------------------
-- utils

fract x = x - floor x

-- hadarmard product, element wise vector multiplication
(.*) :: Code (V 3 Float) -> Code (V 3 Float) -> Code (V 3 Float)
(.*) v1 v2 = toDiagMat v1 !*^ v2
-- (.*) (Vec3 a b c) (Vec3 d e f) = Vec3 (a*d) (b*e) (c*f)

-- turn a vector into the diagonal of a matrix otherwise filled with 0s
toDiagMat :: Code (V 3 Float) -> Code (M 3 3 Float)
toDiagMat (Vec3 a b c) = Mat33 a 0 0
0 b 0
0 0 c

-- cos applied to all elements of a vector
cos3 :: Code (V 3 Float) -> Code (V 3 Float)
cos3 (Vec3 a b c) = Vec3 (cos a) (cos b) (cos c)

------------------------------------------------
-- pipeline
Expand Down
1 change: 1 addition & 0 deletions examples/mandlebrot-set/TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It'd be cool if there was some sort of Hadamard product to define element-wise multiplication of vectors?

0 comments on commit 2808196

Please sign in to comment.