Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 1 | // |
| 2 | // Fragment shader for drawing the Mandelbrot set |
| 3 | // |
| 4 | // Authors: Dave Baldwin, Steve Koren, Randi Rost |
| 5 | // based on a shader by Michael Rivero |
| 6 | // |
| 7 | // Copyright (c) 2002-2005: 3Dlabs, Inc. |
| 8 | // |
| 9 | // See 3Dlabs-License.txt for license information |
| 10 | // |
| 11 | |
| 12 | varying vec3 Position; |
| 13 | varying float LightIntensity; |
| 14 | |
| 15 | uniform float MaxIterations; |
| 16 | uniform float Zoom; |
| 17 | uniform float Xcenter; |
| 18 | uniform float Ycenter; |
| 19 | uniform vec3 InnerColor; |
| 20 | uniform vec3 OuterColor1; |
| 21 | uniform vec3 OuterColor2; |
| 22 | |
| 23 | void main() |
| 24 | { |
| 25 | float real = Position.x * Zoom + Xcenter; |
| 26 | float imag = Position.y * Zoom + Ycenter; |
| 27 | float Creal = real; // Change this line... |
| 28 | float Cimag = imag; // ...and this one to get a Julia set |
| 29 | |
| 30 | float r2 = 0.0; |
| 31 | float iter; |
| 32 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 33 | // for (iter = 0.0; iter < MaxIterations && r2 < 4.0; ++iter) |
Vinson Lee | 0444284 | 2009-11-17 23:15:25 -0800 | [diff] [blame] | 34 | for (iter = 0.0; iter < 12.0 && r2 < 4.0; ++iter) |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 35 | { |
| 36 | float tempreal = real; |
| 37 | |
| 38 | real = (tempreal * tempreal) - (imag * imag) + Creal; |
| 39 | imag = 2.0 * tempreal * imag + Cimag; |
| 40 | r2 = (real * real) + (imag * imag); |
| 41 | } |
| 42 | |
| 43 | // Base the color on the number of iterations |
| 44 | |
| 45 | vec3 color; |
| 46 | |
| 47 | if (r2 < 4.0) |
| 48 | color = InnerColor; |
| 49 | else |
| 50 | color = mix(OuterColor1, OuterColor2, fract(iter * 0.05)); |
| 51 | |
| 52 | color *= LightIntensity; |
| 53 | |
| 54 | gl_FragColor = vec4(color, 1.0); |
| 55 | } |