Skip to content

Commit

Permalink
Mandelbrot GLSL shader!
Browse files Browse the repository at this point in the history
  • Loading branch information
rafraser committed Nov 7, 2019
1 parent f3f1016 commit 1ce5c4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions opengl/src/shaders/mandelbrot.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
uniform vec2 resolution;

vec4 colorize(int i) {
if(i == 0 || i == 50) {
return vec4(0.0, 0.0, 0.0, 1.0);
} else {
float strength = float(i)/50.0;
return vec4(0.5*strength, 1.5*strength, 2.0*strength, 1.0);
}
}

void main(void)
{
vec2 position = gl_FragCoord.xy / resolution;
vec2 z, c;
c.x = -0.5 + (position.x - 0.5) * 3.0;
c.y = (position.y - 0.5) * 3.0;

z = c;
int i;
for(i=0; i<50; i++) {
float real = (z.x * z.x - z.y * z.y) + c.x;
float imag = (z.y * z.x + z.x * z.y) + c.y;

if((real*real + imag*imag) > 4.0) break;

z.x = real;
z.y = imag;
}

gl_FragColor = colorize(i);
}
11 changes: 11 additions & 0 deletions opengl/src/shaders/mandelbrot.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

void main(){

gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;

}

0 comments on commit 1ce5c4a

Please sign in to comment.