Click here to Skip to main content
13,249,035 members (46,137 online)
Click here to Skip to main content

Stats

31K views
2.5K downloads
38 bookmarked
Posted 15 Jul 2011

Mandelbrot Set with PixelShader in WPF

, 23 Jul 2011
This article explains how to use pixel shaders for fast generation of Mandelbrot Sets.
float maxIter : register(c0);
float2 power : register(c1);
float bailout : register(c2);
float2 offset : register(c3);
float2 size : register(c4);

#include "complex.txt"

float4 getColor(float i)
{
    float k = 1.0 / 3.0;
    float k2 = 2.0 / 3.0;
    float cr = 0.0;
    float cg = 0.0;
    float cb = 0.0;
    if (i >= k2)
    {
        cr = i - k2;
        cg = (k-1) - cr;
    }
    else if (i >= k)
    {
        cg = i - k;
        cb = (k-1) - cg;
    }
    else
    {
        cb = i;
    }
    return float4(cr * 3, cg * 3, cb * 3, 1.0);
}

float4 main(float2 uv : TEXCOORD) : COLOR
{
	float2 xy = float2(uv.x / size.x + offset.x, uv.y / size.y + offset.y);
	float2 z = float2(xy.x, xy.y);
	float i = 0;

	while (i < maxIter && c_abs(z) <= bailout)
	{
		z = c_add(c_pow(z, power), xy);
		i++;
	}
	if (i < maxIter)
	{
		i -= log(log(c_abs(z))) / log(c_abs(power));
		return getColor(i / maxIter);
	}
	else
		return float4(0.0, 0.0, 0.0, 1.0);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 23 Jul 2011
Article Copyright 2011 by TeapotDev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid