Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Mandelbrot Set with PixelShader in WPF

, 23 Jul 2011
This article explains how to use pixel shaders for fast generation of Mandelbrot Sets.
complex_hlsl.zip
shadermandel_demo.zip
shadermandel_src.zip
ShaderMandelbrot
julii.ps
mandel.ps
Properties
Settings.settings
ShaderMandelbrot.csproj.user
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

TeapotDev

Poland Poland

My homepage
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 23 Jul 2011
Article Copyright 2011 by TeapotDev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid