Last active
October 3, 2019 20:06
-
-
Save Lifelovinglight/a139c76f01cdd7f36c28c8182d9b94d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform vec2 resolution; | |
uniform float zoom, panx, pany; | |
void main(void) | |
{ | |
// vec2 res = vec2(1366.0, 768.0); | |
vec2 uv = gl_FragCoord.xy / resolution.xy * zoom; | |
float scale = resolution.y / resolution.x; | |
uv=((uv-0.5)*5.5); | |
uv.y*=scale; | |
uv.y+=pany; | |
uv.x-=0.5; | |
uv.x+=panx; | |
vec2 z = vec2(0.0, 0.0); | |
vec3 c = vec3(0.0, 0.0, 0.0); | |
float v; | |
int depth = 170 + int(zoom); | |
for(int i=0;i < depth;i++) | |
{ | |
if(((z.x*z.x+z.y*z.y) >= 4.0)) break; | |
z = vec2(z.x*z.x - z.y*z.y, 2.0*z.y*z.x) + uv; | |
if((z.x*z.x+z.y*z.y) >= 2.0) | |
{ | |
c.b = 1.0; | |
c.r=float(i)/20.0; | |
c.g=sin((float(i)/5.0)); | |
} else { | |
c.r = 1.0; | |
c.g = 1.0; | |
c.b = 1.0; | |
} | |
} | |
gl_FragColor = vec4(c,1.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (main) where | |
import Control.Monad | |
import Linear | |
import Data.StateVar | |
import Foreign.C.Types (CInt) | |
import qualified SDL | |
import Control.Monad.Loops | |
import qualified Graphics.Rendering.OpenGL as OpenGL | |
import Data.Text (Text) | |
import qualified Data.Text as Text | |
myVertices :: [OpenGL.Vertex3 OpenGL.GLfloat] | |
myVertices = [ OpenGL.Vertex3 x y 0.0 | (x,y) <- zip [-1.0, 1.0, 1.0, -1.0] [-1.0, -1.0, 1.0, 1.0] ] | |
screenWidth, screenHeight :: CInt | |
(screenWidth, screenHeight) = (1366, 768) | |
myWindowConfig :: SDL.WindowConfig | |
myWindowConfig = SDL.defaultWindow { SDL.windowInitialSize = V2 screenWidth screenHeight | |
, SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL } | |
keyPressed :: SDL.Keycode -> SDL.Event -> Bool | |
keyPressed code event = | |
case SDL.eventPayload event of | |
SDL.KeyboardEvent keyboardEvent -> | |
SDL.keyboardEventKeyMotion keyboardEvent == SDL.Pressed | |
&& SDL.keysymKeycode (SDL.keyboardEventKeysym keyboardEvent) == code | |
_ -> False | |
setUniform :: (OpenGL.Uniform a) => OpenGL.Program -> String -> a -> IO () | |
setUniform program name value = get (OpenGL.uniformLocation program name) >>= ($= value) . OpenGL.uniform | |
modUniform :: (OpenGL.Uniform a) => OpenGL.Program -> String -> (a -> a) -> IO () | |
modUniform program name fn = get (OpenGL.uniformLocation program name) >>= ($~ fn) . OpenGL.uniform | |
main :: IO () | |
main = do | |
SDL.initialize [SDL.InitVideo] | |
window <- SDL.createWindow (Text.pack "Mandelbrot") myWindowConfig | |
SDL.showWindow window | |
SDL.glCreateContext window | |
frag <- OpenGL.createShader OpenGL.FragmentShader | |
fragSource <- readFile "Main.frag" | |
OpenGL.shaderSourceBS frag $= OpenGL.packUtf8 fragSource | |
OpenGL.compileShader frag | |
status <- get $ OpenGL.compileStatus frag | |
unless status $ putStrLn "Error compiling fragment shader." | |
vert <- OpenGL.createShader OpenGL.VertexShader | |
vertSource <- readFile "Main.vert" | |
OpenGL.shaderSourceBS vert $= OpenGL.packUtf8 vertSource | |
OpenGL.compileShader vert | |
status <- get $ OpenGL.compileStatus vert | |
unless status $ putStrLn "Error compiling vertex shader." | |
program <- OpenGL.createProgram | |
OpenGL.attachShader program frag | |
OpenGL.attachShader program vert | |
OpenGL.linkProgram program | |
status <- get $ OpenGL.linkStatus program | |
unless status $ putStrLn "Error linking shader program." | |
OpenGL.validateProgram program | |
status <- get $ OpenGL.validateStatus program | |
log <- get $ OpenGL.programInfoLog program | |
unless status $ putStrLn log | |
OpenGL.currentProgram $= Just program | |
setUniform program "resolution" (OpenGL.Vector2 | |
((fromIntegral screenWidth) :: OpenGL.GLfloat) | |
((fromIntegral screenHeight) :: OpenGL.GLfloat)) | |
setUniform program "zoom" (1.0 :: OpenGL.GLfloat) | |
setUniform program "panx" (0.0 :: OpenGL.GLfloat) | |
setUniform program "pany" (0.0 :: OpenGL.GLfloat) | |
iterateWhile not $ do | |
events <- unfoldM SDL.pollEvent | |
when (any (keyPressed SDL.KeycodeW) events) $ modUniform program "zoom" (subtract (0.01 :: OpenGL.GLfloat)) | |
when (any (keyPressed SDL.KeycodeS) events) $ modUniform program "zoom" (+ (0.01 :: OpenGL.GLfloat)) | |
when (any (keyPressed SDL.KeycodeUp) events) $ modUniform program "pany" (+ (0.01 :: OpenGL.GLfloat)) | |
when (any (keyPressed SDL.KeycodeDown) events) $ modUniform program "pany" (subtract (0.01 :: OpenGL.GLfloat)) | |
when (any (keyPressed SDL.KeycodeRight) events) $ modUniform program "panx" (+ (0.01 :: OpenGL.GLfloat)) | |
when (any (keyPressed SDL.KeycodeLeft) events) $ modUniform program "panx" (subtract (0.01 :: OpenGL.GLfloat)) | |
OpenGL.clear [OpenGL.ColorBuffer] | |
OpenGL.currentColor $= OpenGL.Color4 1.0 0.5 0.5 0.5 | |
OpenGL.renderPrimitive OpenGL.Quads (mapM_ OpenGL.vertex myVertices) | |
OpenGL.flush | |
SDL.glSwapWindow window | |
return $ any ((== SDL.QuitEvent) . SDL.eventPayload) events | |
SDL.destroyWindow window | |
SDL.quit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attribute vec2 coord2d; | |
void main(void) | |
{ | |
gl_Position = vec4(coord2d, 0.0, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment