Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Forked from Mon-Ouie/mandelbrot_glsl.rb
Created August 28, 2011 13:54
Show Gist options
  • Save bil-bas/1176687 to your computer and use it in GitHub Desktop.
Save bil-bas/1176687 to your computer and use it in GitHub Desktop.
require 'ray'
ImageWidth = 640
ImageHeight = 480
image = Ray::Image.new [ImageWidth, ImageHeight]
Ray::ImageTarget.new image do |target|
target.shader.compile(:frag => StringIO.new(<<eof))
#version 110
uniform sampler2D in_Texture;
const int max_it = 50;
const float width = #{ImageWidth.to_f};
const float height = #{ImageHeight.to_f};
varying vec4 var_Color;
vec2 mandelbrot_pos() {
return vec2(((gl_FragCoord.x) / width) * 2.7 - 2.1,
((gl_FragCoord.y) / height) * 2.4 - 1.2);
}
vec2 complex_square(vec2 complex) {
return vec2(complex.x * complex.x - complex.y * complex.y,
complex.x * complex.y + complex.x * complex.y);
}
vec4 inverse(vec4 color) {
return vec4(vec3(1,1,1) - color.rgb, 1.0);
}
void main() {
vec2 c = mandelbrot_pos();
vec2 z = vec2(0, 0);
int i = 0;
for (i = 0; i < max_it; i++) {
z = complex_square(z) + c;
if (dot(z, z) > 4.0) break;
}
gl_FragColor = i == max_it ? var_Color : inverse(var_Color);
}
eof
target.draw Ray::Polygon.rectangle([0, 0, ImageWidth, ImageHeight],
Ray::Color.black)
target.update
end
Ray.game "Mandelbrot", :size => image.size do
register { add_hook :quit, method(:exit!) }
scene :mandelbrot do
@sprite = sprite(image)
render do |win|
win.draw @sprite
end
end
scenes << :mandelbrot
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment