Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef GL_ES
- precision highp float;
- #endif
- //
- varying vec2 pos;
- uniform float minx;
- uniform float maxx;
- uniform float miny;
- uniform float maxy;
- const float MAX_ITERATIONS = 250.0;
- const float BOUNDARY = 4.0;
- //temp placement here, but these are our complex number functions which we will use instead
- vec2 cx_mul(vec2 a, vec2 b) {return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
- vec2 cx_add(vec2 a, vec2 b) {return vec2(a.x + b.x, a.y + b.y);}
- //creating our own power function by for looping cx_mul cuz i'm not smart enough to math this out yet
- vec2 cx_pow(vec2 a, float b) {
- vec2 n = vec2(1.0, 0.0);
- for (float i = 1.0; i <= b; i += 1.0){
- n = cx_mul(n, a);
- }
- return n;
- }
- //performs our main iteration processing
- float doIterate(vec2 pixel){
- //initialize our real and imaginary values of c and z
- vec2 z = vec2(0.0, 0.0);
- vec2 c = vec2(pixel.x, pixel.y);
- //perform iterations
- for (float i = 0.0; i < MAX_ITERATIONS; i += 1.0){
- z = cx_pow(z, 2.0);
- z = cx_add(z, c);
- //check if the point is outside the defined boundary. If so, just return the number of
- //iterations it took to exceed this boundary
- if (z.x * z.x + z.y * z.y > BOUNDARY){
- //return Z - the point at which our iteration stopped,
- //and i - the final number of iterations we spent before stopping
- return i / MAX_ITERATIONS;
- }
- }
- //at this point, we've hit our max iteration limit, so we assume the point to be in the set. We return
- //-1 so that our coloring functions don't confuse a return of 0 with a point in the set.
- //also return our max iteration number, since we no longer have access to i
- return 1.0;
- }
- /**
- * our render logic starts here. This will be performed for all pixels
- */
- void main(){
- //adjust our min and max coordinates for our canvas
- float x = ((maxx - minx) * pos.x) + minx;
- float y = ((maxy - miny) * pos.y) + miny;
- //perform iteration
- float isInSet = doIterate(vec2(x, y));
- //finally, color the point
- gl_FragColor = vec4(isInSet, isInSet, isInSet, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment