I’ll post another example from the eleventh chapter: a Julia set renderer. I won’t explain what a Julia set is, but I’ll post the vertex and fragment shaders used to create it ;).

Vertex Shader:

#version 330

precision highp float;

// Attributes per vertex: position and normal
in vec4 position;
in vec2 texCoords;

uniform mat4 MVP;
uniform float zoom;
uniform vec2 offset;

out Fragment
{
    vec2 texCoord;
} fragment;

out vec2 initialZ;

void main(void)
{
    // Don't forget to transform the geometry!
    gl_Position = MVP * position;

    fragment.texCoord = texCoords;

    initialZ = (position.xy * zoom) + offset;
}

Fragment Shader:

#version 330

precision highp float;

in Fragment
{
    vec2 texCoord;
} fragment;

in vec2 initialZ;

uniform sampler1D texGradient;
uniform vec2 C;
uniform int maxIterations = 1000;

out vec4 outColor;

void main (void)
{
    vec2 Z = initialZ;
    int iterations = 0;
    const float threshold_squared = 16.f;

    while (iterations < maxIterations && dot (Z, Z) < threshold_squared)
    {
        vec2 z_squared;
        z_squared.x = Z.x * Z.x - Z.y * Z.y;
        z_squared.y = 2.f * Z.x * Z.y;
        Z = z_squared + C;
        ++iterations;
    }

    if (iterations == maxIterations)
        outColor = vec4 (0.f, 0.f, 0.f, 1.f);
    else
        outColor = texture (texGradient, float (iterations) / float
                (maxIterations));
}

And some cool screenshots!

About these ads