Мандельброт в пиксельном шейдере - PullRequest
2 голосов
/ 11 января 2020

Я работаю уже несколько дней над версией набора Мандельброта для DirectX 11. То, что я сделал до сих пор, - это создание четырехугольника с текстурой на нем. Я могу раскрасить точки с помощью пиксельного шейдера, но по какой-то причине набор Мандельброта в пиксельном шейдере не возвращает ожидаемый результат. Я протестировал logi c в простом коде C ++, и у меня тот же ошибочный результат. Есть идеи, что случилось с кодом? У меня есть правильная версия, работающая в Python, и я только что повторил код, но, кажется, чего-то не хватает.

Ширина набора составляет 2,5 (немного растянет изображение). Предполагается 1024 * 960 окна и макс. итерация 1000. Я скомпилировал с Shader Model 5.0. Он начинается со значения по умолчанию, установленного как

RealStart = -2.0;
ImagStart = -1.25;

Передается через постоянный буфер

cbuffer cBuffer
{
    double RealStart; 'equals -2.5 from the default view of the set
    double ImagStart; 'equals -1.25 from the default view of the set
};

// Pixel Shader
float4 main(float4 position : SV_POSITION) : SV_TARGET
{
    double real, imag;
    double real2, imag2;
    int ite = 0;
    float4 CalcColor = { 1.0f , 1.0f, 1.0f, 1.0f };
    'position is the position of the pixel from 1.0f to 0.0f
    real = RealStart + (double) position.x / 1024 * 2.5;
    imag = ImagStart + (double) position.y / 960 * 2.5;

    for (int i = 0; i < 1000; i++)
    {
        'breaking down the complex number by its constituents
        real2 = real * real;
        imag2 = imag * imag;

        if (real2 + imag2 > 4.0)
        {
            break;
        }
        else {
            imag = 2 * real * imag + ImagStart;
            real = real2 - imag2 + RealStart;
            ite++;
        }
    }

    CalcColor[0] = (float) (ite % 333) / 333 ;
    CalcColor[1] = (float) (ite % 666) / 666 ;
    CalcColor[2] = (float) (ite % 1000) / 1000;

    return CalcColor;
}

Редактировать Python версия

def Mandelbrot(creal, cimag, maxNumberOfIterations):
    real = creal
    imag = cimag

    for numberOfIterations in range(maxNumberOfIterations):
        real2 = real * real
        imag2 = imag * imag

        if real2 + imag2 > 4.0:
            return numberOfIterations

        imag = 2 * real * imag + cimag
        real = real2 - imag2 + creal

    return maxNumberOfIterations

Creal, cimag и являются созданный таким образом, а затем просто зацикленный.

realAxis = np.linspace(realStart, realStart + width, dim)
imagAxis = np.linspace(imagStart, imagStart + width, dim)

Он возвращает maxNumberOfIterations в двухмерный массив, который является сюжетом для рисования набора Мандельброта.

1 Ответ

0 голосов
/ 12 января 2020

Ошибка заключалась в том, что ImagStart и RealStart в остальном также необходимо масштабировать. Код в шейдере был изменен следующим образом:

cbuffer cBuffer
{
    double2 C;
    float2 Param;
    float MaxIt;
};


// Pixel Shader
float4 main(float4 position : SV_POSITION, float2 texcoord : TEXCOORD) : SV_TARGET
{
    double real, imag;
    double real2, imag2;
    uint ite = 0;
    float4 CalcColor = { 1.0f , 1.0f, 1.0f, 1.0f };

    real = C.x + ((double) texcoord.x - 0.5) * 2.0 * 2.5;
    imag = C.y + ((double) texcoord.y - 0.5) * 2.0 * 2.5;

    for (int i = 0; i < 100; i++)
    {
        real2 = real * real;
        imag2 = imag * imag;

        if (real2 + imag2 > 4.0)
        {
            break;
        }
        else {
            imag = 2 * real * imag + C.y + ((double) texcoord.y - 0.5) * 2.0 * 2.5;
            real = real2 - imag2 + C.x +   ((double) texcoord.x - 0.5) * 2.0 * 2.5;
            ite++;
        }
    }

    if (ite > 100)
        ite = 100;

    CalcColor[0] = (float)(ite % 33) / 33;
    CalcColor[1] = (float)(ite % 66) / 66;
    CalcColor[2] = (float)(ite % 100) / 100;

    return CalcColor;
}

Мандельброт установлен правильно. Mandelbrot set

...