Градиентный алгоритм выдает маленькие белые точки - PullRequest
0 голосов
/ 09 июня 2010

Я работаю над алгоритмом генерации линейных градиентов точка-точка. У меня есть грубое доказательство реализации концепции:

GLuint OGLENGINEFUNCTIONS::CreateGradient( std::vector<ARGBCOLORF> &input,POINTFLOAT start, POINTFLOAT end, int width, int height,bool radial )
{
    std::vector<POINT> pol;
    std::vector<GLubyte> pdata(width * height * 4);
    std::vector<POINTFLOAT> linearpts;
    std::vector<float> lookup;
    float distance = GetDistance(start,end);
    RoundNumber(distance);
    POINTFLOAT temp;

    float incr = 1 / (distance + 1);
    for(int l = 0; l < 100; l ++)
    {

    POINTFLOAT outA;
    POINTFLOAT OutB;

    float dirlen;
    float perplen;
    POINTFLOAT dir;
    POINTFLOAT ndir;
    POINTFLOAT perp;
    POINTFLOAT nperp;

    POINTFLOAT perpoffset;
    POINTFLOAT diroffset;


    dir.x = end.x - start.x;
    dir.y = end.y - start.y;

    dirlen = sqrt((dir.x * dir.x) + (dir.y * dir.y));

    ndir.x = static_cast<float>(dir.x * 1.0 / dirlen);
    ndir.y = static_cast<float>(dir.y * 1.0 / dirlen);

    perp.x = dir.y;
    perp.y = -dir.x;

    perplen = sqrt((perp.x * perp.x) + (perp.y * perp.y));

    nperp.x = static_cast<float>(perp.x * 1.0 / perplen);
    nperp.y = static_cast<float>(perp.y * 1.0 / perplen);

    perpoffset.x = static_cast<float>(nperp.x * l * 0.5);
    perpoffset.y = static_cast<float>(nperp.y * l * 0.5);

    diroffset.x = static_cast<float>(ndir.x * 0 * 0.5);
    diroffset.y = static_cast<float>(ndir.y * 0 * 0.5);

    outA.x = end.x + perpoffset.x + diroffset.x;
    outA.y = end.y + perpoffset.y + diroffset.y;

    OutB.x = start.x + perpoffset.x - diroffset.x;
    OutB.y = start.y + perpoffset.y - diroffset.y;


    for (float i = 0; i < 1; i += incr)
    {
        temp = GetLinearBezier(i,outA,OutB);
        RoundNumber(temp.x);
        RoundNumber(temp.y);

        linearpts.push_back(temp);
        lookup.push_back(i);
    }

    for (unsigned int j = 0; j < linearpts.size(); j++) {
        if(linearpts[j].x < width && linearpts[j].x >= 0 &&
            linearpts[j].y < height && linearpts[j].y >=0)
        {
            pdata[linearpts[j].x * 4 * width + linearpts[j].y * 4 + 0] = (GLubyte) j;
            pdata[linearpts[j].x * 4 * width + linearpts[j].y  * 4 + 1] = (GLubyte) j;
            pdata[linearpts[j].x * 4 * width + linearpts[j].y  * 4 + 2] = (GLubyte) j;
            pdata[linearpts[j].x * 4 * width + linearpts[j].y  * 4 + 3] = (GLubyte) 255;
        }

    }
    lookup.clear();
    linearpts.clear();
    }


    return CreateTexture(pdata,width,height);
}

Он работает так, как я ожидал бы большую часть времени, но под определенными углами он производит маленькие белые точки. Я не могу понять, что это делает.

Вот как это выглядит под большинством углов (хорошо) http://img9.imageshack.us/img9/5922/goodgradient.png

Но время от времени это выглядит так (плохо): http://img155.imageshack.us/img155/760/badgradient.png

Что может быть причиной белых точек? Может быть, есть также лучший способ для создания моих градиентов, если для этого не возможно решение?

Спасибо

1 Ответ

1 голос
/ 09 июня 2010

Я думаю, у вас есть ошибка при индексации в векторе байтов pdata.Ваш домен х равен [0, ширина), но когда вы умножаете индексы, вы делаете x * 4 * width.Вероятно, оно должно быть x * 4 + y * 4 * width или x * 4 * height + y * 4, в зависимости от того, располагаются ли ваши данные по строкам или основным столбцам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...