首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >梯度算法产生小白点

梯度算法产生小白点
EN

Stack Overflow用户
提问于 2010-06-10 02:49:52
回答 1查看 356关注 0票数 0

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

代码语言:javascript
复制
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

白点可能是什么原因造成的?如果无法解决此问题,是否还有更好的方法来生成我的梯度?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-10 03:04:01

我认为在索引pdata字节向量时有一个bug。您的x域是[0,宽度),但是当您将索引相乘时,您正在执行x * 4 * width。它可能应该是x * 4 + y * 4 * widthx * 4 * height + y * 4,这取决于你的数据是以行还是列为主排列的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3008878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档