2010-06-09 59 views
0

我正在研究一種算法來生成點對點線性漸變。我的概念實現的粗略證明完成:漸變算法產生的小白點

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

這可能是導致白點? 如果沒有可行的解決方案,是否有更好的方法來生成我的漸變?

謝謝

+0

'diroffset.x = static_cast (ndir.x * 0 * 0.5);'erp?我們可以看到「GetLinearBezier」的代碼嗎? – 2010-06-09 20:12:54

+0

如果不觸摸像素,是白色的默認顏色? – Gabe 2010-06-09 20:47:12

回答

1

我想你有一個索引到pdata字節向量的bug。你的x域是[0,寬度),但是當你將指數乘以x * 4 * width。它應該可能是x * 4 + y * 4 * widthx * 4 * height + y * 4,這取決於您的數據是排列在行還是列主要。