梯度算法产生很少的白点

时间:2010-06-09 18:49:52

标签: c++ c algorithm gradient

我正在研究一种算法来生成点对点线性渐变。我已经完成了概念实施的粗略证明:

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 个答案:

答案 0 :(得分:1)

我认为你有一个错误索引到pdata字节向量。您的x域是[0,宽度]但是当您将正在执行的索引乘以x * 4 * width时。它应该是x * 4 + y * 4 * widthx * 4 * height + y * 4,具体取决于您的数据是按行还是列排列。