Skip to content

Instantly share code, notes, and snippets.

@agrif
Created December 2, 2012 22:14
Show Gist options
  • Save agrif/4191297 to your computer and use it in GitHub Desktop.
Save agrif/4191297 to your computer and use it in GitHub Desktop.
import sys
class IndentOutput:
def __init__(self, backend):
self.level = 0
self.backend = backend
self.buffer = ""
def indent(self):
self.level += 1
def dedent(self):
self.level -= 1
def __enter__(self):
self.indent()
def __exit__(self, exc_type, exc_value, traceback):
self.dedent()
def write(self, s):
self.buffer += s
if s == '\n':
for p in self.buffer.splitlines():
print(" " * self.level + p, file=self.backend)
self.buffer = ""
n = 8
o = IndentOutput(sys.stdout)
sys.stdout = o
print("#version 400 core")
print("#extension GL_ARB_gup_shader_fp64 : enable")
print()
print("in vec2 txcoords;")
print("uniform double zoom;")
print("uniform dvec2 offset;")
print("out vec4 fragColor;")
print("uniform int iter;")
print()
print("uniform sampler1D palette;")
print()
print("void main() {")
with o:
print("double c_real = double(txcoords.x) * zoom + offset.x;")
print("double c_imag = double(txcoords.y) * zoom + offset.y;")
print("double z_real_0 = c_real;")
print("double z_imag_0 = c_imag;")
for i in range(1, n):
print("double z_real_{0} = z_real_{1} * z_real_{1} - z_imag_{1} * z_imag_{1} + c_real;".format(i, i-1))
print("double z_imag_{0} = 2.0 * z_real_{1} * z_imag_{1} + c_imag;".format(i, i-1))
print("int i;")
print("for (i = 0; i < iter; i += {})".format(n), "{")
with o:
print("double z_r_q = z_real_{0} * z_real_{0};".format(n - 1))
print("double z_i_q = z_imag_{0} * z_imag_{0};".format(n - 1))
print("if (z_i_q + z_r_q > 4.0) {")
with o:
print("break;")
print("}")
for i in range(n):
src = i - 1
if src < 0:
src = n - 1
if i == 0:
print("z_real_0 = z_r_q - z_i_q + c_real;")
else:
print("z_real_{0} = z_real_{1} * z_real_{1} - z_imag_{1} * z_imag_{1} + c_real;".format(i, src))
print("z_imag_{0} = 2.0 * z_real_{1} * z_imag_{1} + c_imag;".format(i, src))
print("}")
for i in range(n - 1):
if i == 0:
print("if ", end='')
else:
print("} else if ", end='')
print("(z_imag_{0} * z_imag_{0} + z_real_{0} * z_real_{0} > 4.0)".format(i), "{")
with o:
print("i -= {};".format(n - i - 1))
print("z_real_{0} = z_real_{1};".format(n - 1, i))
print("z_imag_{0} = z_imag_{1};".format(n - 1, i))
print("}")
print("if (i == iter) {")
with o:
print("fragColor = vec4(0.0, 0.0, 0.0, 1.0);")
print("} else {")
with o:
print("float ni = float(i) + 1.0 - log(log(float(sqrt(z_imag_{0} * z_imag_{0} + z_real_{0} * z_real_{0})))) / log(2.0);".format(n - 1))
print("fragColor = texture(palette, abs(ni / float(iter)));")
print("}")
print("}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment