Skip to content

Commit

Permalink
Tidy up comments, organize code detritus, make reviewers frown less
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-PH committed Mar 27, 2019
1 parent 3d3565e commit a34d4a8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 146 deletions.
211 changes: 99 additions & 112 deletions resources/fractal.glslf
Original file line number Diff line number Diff line change
@@ -1,148 +1,135 @@
#version 420 core

This comment has been minimized.

Copy link
@Ben-PH

Ben-PH Apr 2, 2019

Author Owner

#13 is there a way to handle it here?

vec3 hsv2rgb( vec3 c);
bool fast_check( dvec2 z);
dvec2 coord_to_im(dvec2 coord);

in vec4 gl_FragCoord;
#define ESCAPE_BOUNDARY 18
#define FREQUENCY 0.1

in highp vec4 gl_FragCoord;
out vec4 Target0;

layout (std140) uniform Mandel {
uniform dvec2 u_MousePos;
uniform dvec2 u_Center;
uniform dvec2 u_Dimension;
uniform dvec2 u_Resolution;
uniform float u_Time;
uniform int u_MaxIter;
uniform int u_IsMandel;
};

out vec4 Target0;
dvec2 vec2_to_dvec2(vec2 floatvec2); // Naive approach to guaranteeing double precision
dvec2 viewport_coord_to_complex(dvec2 coord); // moves and scales gl_FragCoord to desired complex number

dvec2 get_c() {
dvec2 ret;
if(u_IsMandel != 0) {
ret = coord_to_im(gl_FragCoord.xy);
} else {
ret = coord_to_im(u_MousePos);
}

return ret;
}
// For this iterative escape-fractal, we need to select start value, and iterative modifier
dvec2 get_c();
dvec2 get_z();

vec3 hsv2rgb( vec3 c);

layout (std140) uniform Mandel {
uniform dvec2 u_Center; // where on the complex plain the user wants the screen-center
uniform dvec2 u_Dimension; // the width and height user wants the view onto the complex plane
uniform dvec2 u_Resolution; // user interogates the pixel coverage of the view, and provides it here

uniform vec2 u_MousePos; // Used for julia set, assumes gl_FragCoord format
uniform float u_Time; // Time since start of program in seconds
uniform int u_MaxIteration; // User defined limit of iteration count
uniform int u_IsMandel; // (A bit hacky, TODO) select between MandelBrot and Julia
};

dvec2 get_z() {
dvec2 ret;
if(u_IsMandel != 0) {
ret = dvec2(0.0, 0.0);
} else {
ret = coord_to_im(gl_FragCoord.xy);
}

return ret;
}

dvec2 coord_to_im(dvec2 coord) {
dvec2 ret = coord - u_Resolution/2.0;
ret /= u_Resolution/(u_Dimension);
ret += u_Center;
// ret -= u_Dimension/2.0;
// ret -= u_Center/2.0;
// dvec2 scale = u_Resolution/(dvec2(3.5, 2.0));
// ret /= scale;
return ret;
}

void main() {


// load up our iteration values
dvec2 z = get_z();
dvec2 c = get_c();
int i;


int step_count;
dvec2 tmp;
// dvec2 zsq = dvec2(z.x*z.x - z.y*z.y, 2*z.x*z.y);
for (i = 0; i < u_MaxIter; i++) {
for (step_count = 0; step_count < u_MaxIteration; step_count++) {

// Vast majority of performance gets absorbed here

// TODO micro-optimise
// `z = z * z + c`, with z and c being complex numbers

tmp.x = z.x*z.x - z.y*z.y;
tmp.y = 2.0*z.x*z.y;
tmp.y = 2.0 * z.x*z.y;
z.x = tmp.x;
z.y = tmp.y;
z.x += c.x;
z.y += c.y;

// z = z * z + c;
// z = dvec2(pow(z.x, 2) - pow(z.y, 2), 2 * z.x * z.y) + c;

// z.y = (z.x + z.y)*(z.x + z.y) - zsq.x - zsq.y;
// z.y += c.y;
// z.x = zsq.x - zsq.y + c.x;
// zsq.x = z.x * z.x;
// zsq.y = z.y * z.y;
if((z.x*z.x + z.y*z.y) > ESCAPE_BOUNDARY) {
break;
}
}


// a different fast-check
// if((pow(D.x, 2) + pow(D.y, 2)) < 0.001) {
// Target0 = vec4(i/u_MaxIter, 0.0, 0.0, 1.0);
// return;
// }
if (step_count == u_MaxIteration) {
Target0 = vec4(1.0, 1.0, 1.0, 1.0);
} else {
float dist = float(length(z));

float two = float(2.0);
dist = log(log(dist)) / log(two);

// dvec2 tmp;
// tmp.x = D.x*z.x - D.y*z.y;
// tmp.y = D.x*z.y + D.y*z.x;
// D.x = 2.0*tmp.x;
// D.y = 2.0*tmp.y;
float val = float(step_count) - dist;
float hue = val/10.0 - u_Time * FREQUENCY;

if((z.x*z.x + z.y*z.y) > 18) {
break;
}
// if (!fast_check(z)) { return;}
Target0 = vec4(hsv2rgb(vec3((hue), 1.0, 1.0 )), 1.0);
}
}
// enhance...
dvec2 vec2_to_dvec2(vec2 floatvec2) {
return dvec2(floatvec2);
}

// center yourself, THEN grow your mind, then you will be moved on the plane of imagination
dvec2 viewport_coord_to_complex(dvec2 coord) {

if (i == u_MaxIter) {
Target0 = vec4(1.0, 1.0, 1.0, 1.0);
// set the origin to the center of the screen
dvec2 result = dvec2(coord) - u_Resolution/2.0;


// Scale down to a 1.0 by 1.0 view of the complex plane
result /= u_Resolution;

// match the real and im dimensions to user-input
result *= u_Dimension;

// shift the center reference to where the user asked it to be
result += u_Center;
return result;
}


// TODO set it up so that responsibility between mandel and julia decoupled
// at the moment seperate areoas are tightly coupled
dvec2 get_c() {
dvec2 result;

// for mandel, the iter-step is where that pixel sits in the complex plane
// but for julia, it's based on mouse position
if(u_IsMandel != 0) {
result = viewport_coord_to_complex(gl_FragCoord.xy);
} else {
float dist = float(z.x*z.x + z.y*z.y);
result = viewport_coord_to_complex(u_MousePos);
}

dist = log(log(dist))/log(2.0);
float val = float(i) - dist;
// float val = i/ float(u_MaxIter);
Target0 = vec4(hsv2rgb( vec3((val*0.1 - u_Time/5.0), 1.0, 1.0 )), 1.0);
return result;
}

// TODO see get_c()
dvec2 get_z() {
dvec2 result;

// for mandel, iterate starting from 0, for julia, the start relates to location in
// the complex plane
if(u_IsMandel != 0) {
result = dvec2(0.0, 0.0);
} else {
result = viewport_coord_to_complex(gl_FragCoord.xy);
}

return result;
}
/*z.r = 0;
z.i = 0;
zrsqr = z.r * z.r;
zisqr = z.i * z.i;
while (zrsqr + zisqr <= 4.0)
{
z.i = z.r * z.i;
z.i += z.i; // Multiply by two
z.i += c.i;
z.r = zrsqr – zisqr + c.r;
zrsqr = square(z.r);
zisqr = square(z.i);
}*/

// dirty check that coveres points close enough to an attractor
// bool fast_check( dvec2 Z) {
// float r = sqrt(pow(Z.x - 0.25, 2) + pow(Z.y, 2));
// if (Z.x < r - 2 * pow(r, 2) + 0.25) {
// Target0 = vec4(0, 1.0, 1.0, 1.0);
// return false;
// }

// if (pow(Z.x + 1, 2) + pow(Z.y, 2) < 0.0625) {
// Target0 = vec4(1.0, 1.0, 1.0, 1.0);
// return false;
// }

// return true;
// }



// hue-rotates an rgb-value
vec3 hsv2rgb( vec3 c) {


// hue, saturation, value to reg, green, blue
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www );
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
Expand Down
1 change: 0 additions & 1 deletion src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl Direction {
}

mod test {
use super::*;

#[test]
fn key_code_to_direction() {
Expand Down
Loading

0 comments on commit a34d4a8

Please sign in to comment.