Gradient algororithm produces little white dots

Posted by user146780 on Stack Overflow See other posts from Stack Overflow or by user146780
Published on 2010-06-09T18:49:51Z Indexed on 2010/06/09 18:52 UTC
Read the original article Hit count: 268

Filed under:
|
|
|

I'm working on an algorithm to generate point to point linear gradients. I have a rough, proof of concept implementation done:

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);
}

It works as I would expect most of the time, but at certain angles it produces little white dots. I can't figure out what does this.

This is what it looks like at most angles (good) http://img9.imageshack.us/img9/5922/goodgradient.png

But once in a while it looks like this (bad): http://img155.imageshack.us/img155/760/badgradient.png

What could be causing the white dots? Is there maybe also a better way to generate my gradients if no solution is possible for this?

Thanks

© Stack Overflow or respective owner

Related posts about c++

Related posts about c