Julia Set
This sample draws an animated julia set in real time using the pixel shader for the computation.
// The 4x4 world view projection matrix. float4x4 worldViewProjection : WORLDVIEWPROJECTION; // The seed for the julia set (c in the expression z(n+1) = z(n)^2+c). float2 seed; // input parameters for our vertex shader struct VertexShaderInput { float4 position : POSITION; float2 texCoord : TEXCOORD0; }; // input parameters for our pixel shader // also the output parameters for our vertex shader struct PixelShaderInput { float4 position : POSITION; float2 texCoord : TEXCOORD0; }; /** * vertexShaderMain - Multiplies position by world-view-projection matrix, and * passes on texture coordinates scaled to put the origin in the center of the * quad and reveal a nicely sized portion of the plane to show the julia set. */ PixelShaderInput vertexShaderMain(VertexShaderInput input) { PixelShaderInput output; output.position = mul(input.position, worldViewProjection); output.texCoord = 4.0 * (input.texCoord - float2(0.5, 0.5)); return output; } /** * pixelShaderMain - Calculates the color of the pixel by iterating on the * formula z = z*z + seed. After some number of iterations, the magnitude of z * determines the color. */ float4 pixelShaderMain(PixelShaderInput input) : COLOR { float2 Z = input.texCoord; // Number of iterations hardcoded here. The more iterations, the crisper the // image. for(int i = 0; i < 10; ++i) { Z = float2(Z.x * Z.x - Z.y * Z.y, 2.0 * Z.x * Z.y) + seed; // Some graphics cards and some software renderers don't appreciate large // floating point values, so we clamp to prevent Z from getting that big. if (i > 7) { Z = clamp(Z, -25, 25); } } return (1 - length(Z)) * float4(0.5, 1, 2, 1); } // Here we tell our effect file *which* functions are // our vertex and pixel shaders. // #o3d VertexShaderEntryPoint vertexShaderMain // #o3d PixelShaderEntryPoint pixelShaderMain // #o3d MatrixLoadOrder RowMajor