We’ve implemented a mandelbrot shader on the Wildcat VP using OGL2’s fragment shader capabilities. Here’s the source.
//
// mandel.vert: Vertex shader for drawing the Mandelbrot set
//
// author: Dave Baldwin, Steve Koren
// based on a shader by Michael Rivero
//
// Copyright © 2002: 3Dlabs, Inc.
//
varying float lightIntensity;
varying vec3 Position;
uniform vec3 LightPosition;
const float specularContribution = 0.2;
const float diffuseContribution = (1.0 - specularContribution);
void main(void) {
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
Position = vec3(gl_Vertex);
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
vec3 lightVec = normalize(LightPosition - vec3(pos));
vec3 reflectVec = reflect(lightVec, tnorm);
vec3 viewVec = normalize(vec3(pos));
float spec = clamp(dot(reflectVec, viewVec), 0.0, 1.0);
spec = spec * spec;
spec = spec * spec;
spec = spec * spec;
spec = spec * spec;
lightIntensity = abs(diffuseContribution * dot(lightVec, tnorm) +
specularContribution * spec);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
//
// mandel.frag: Fragment shader for drawing the Mandelbrot set
//
// author: Dave Baldwin, Steve Koren
// based on a shader by Michael Rivero
//
// Copyright © 2002: 3Dlabs, Inc.
//
varying vec3 Position;
varying float lightIntensity;
uniform float maxIterations;
uniform float zoom;
uniform float xCenter;
uniform float yCenter;
void main (void)
{
vec3 pos = fract(Position);
float real = ((pos.x - 0.5) * zoom) - xCenter;
float imag = ((pos.y - 0.5) * zoom) - yCenter;
float Creal = real;
float Cimag = imag;
float r2 = 0.0;
float iter;
for (iter = 0.0; iter < maxIterations && r2 < 4.0; ++iter) {
float tempreal = real;
real = (tempreal * tempreal) - (imag * imag) + Creal;
imag = 2.0 * tempreal * imag + Cimag;
r2 = (real * real) + (imag * imag);
}
// Base the color on the number of iterations
vec3 color;
if (r2 < 4.0) {
color = vec3(1.2, 0.0, 0.0);
} else {
float tmpval = fract (iter * 0.05);
vec3 color1 = vec3(0.5, 0.0, 1.5);
vec3 color2 = vec3(0.0, 1.5, 0.0);
color = mix(color1, color2, tmpval);
}
color = clamp(color * lightIntensity, 0.0, 1.0);
gl_FragColor = vec4(color, 1.0);
}
Barthold