0
我寫了下面這段代碼洪水填充的畫圖程序我開發的一部分:洪水填充GLUT代碼不工作
void setPixel(int x, int y)
{
glColor4f(red, green, blue, alpha);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glFlush();
}
void floodFill(int x, int y)
{
unsigned char pick_col[3];
float R, G, B;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pick_col);
R = (float) pick_col[0]/255.0;
G = (float) pick_col[1]/255.0;
B = (float) pick_col[2]/255.0;
//std::cout<<R<<" "<<G<<" "<<B<<"\n";
if(R!=0.0 || G!=0.0 || B!=0.0) //true for any non-black colour (BTW, my canvas is black)
return;
setPixel(x,y);
floodFill(x+1,y);
floodFill(x,y+1);
floodFill(x,y-1);
floodFill(x-1,y);
}
不幸的是,這個代碼是行不通的。例如,我首先畫了一個矩形並試圖填充矩形。實際發生的事情是,從我點擊填充的位置開始,右邊的像素開始被填滿,最終右邊緣附近的像素開始被繪製。但這就是發生的一切。我不明白爲什麼。
你爲什麼要將RGB值轉換爲float?爲什麼不只是檢查零點並完成它呢? –
@CaptainObvlious好吧,所以我修改了代碼以避免轉換,但代碼仍然不起作用。我感覺我的算法是不正確的,但當我看代碼時看起來並不是這樣。 – Ricky