我试图画一个阴影三角形,但我得到了错误的颜色。这是我得到的例子,如果我用红色、绿色和蓝色调用一个函数:
它应该是左上角的红色,右上角的绿色和底部的蓝色。但正如你所看到的,颜色不是那样的。我应该得到这样的东西:
这是代码:
unsigned RGB(unsigned char R, unsigned char G, unsigned B){
return (R << 16) | (G << 8) | B;
}
float get_dist(int x1, int y1, int x2, int y2){
int xproj = x2-x1;
int yproj = y2-y1;
float dist = sqrt(pow(xproj, 2) + pow(yproj, 2));
return dist;
}
unsigned char getR(unsigned c){
return (c >> 16) & 0xFF;
}
unsigned char getG(unsigned c){
return (c >> 8) & 0xFF;
}
unsigned char getB(unsigned c){
return c & 0xFF;
}
void draw_ftriangle(SDL_Surface * s, int x0, int y0, unsigned c0, int x1, int y1, unsigned c1, int x2, int y2, unsigned c2){
int x;
signed d0, d1, d2;
unsigned R, G, B, color;
int ymin = min(y0, min(y1, y2));
int xmin = min(x0, min(x1, x2));
int ymax = max(y0, max(y1, y2));
int xmax = max(x0, max(x1, x2));
draw_line(s, x0, y0, x1, y1, color);
draw_line(s, x1, y1, x2, y2, color);
draw_line(s, x0, y0, x2, y2, color);
for(; ymin < ymax; ymin++)
for(x=xmin; x < xmax; x++)
if(ptintr(x, ymin, x0, y0, x1, y1, x2, y2)){
d0 = get_dist(x, ymin, x0, y0);
d1 = get_dist(x, ymin, x1, y1);
d2 = get_dist(x, ymin, x2, y2);
R = d0+d1+d2 == 1 ? getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2 :
(getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2)/(d0+d1+d2);
G = d0+d1+d2 == 1 ? getG(c0)*d0 + getG(c1)*d1 + getG(c2)*d2 :
(getG(c0)*d0 + getG(c1)*d1 + getG(c2)*d2)/(d0+d1+d2);
B = d0+d1+d2 == 1 ? getB(c0)*d0 + getB(c1)*d1 + getB(c2)*d2 :
(getB(c0)*d0 + getB(c1)*d1 + getB(c2)*d2)/(d0+d1+d2);
color = RGB(R, G, B);
put_pixel(s, x, ymin, color);
}
}
我像这样调用函数drraw_ftriangle:
draw_ftriangle(表面, 100, 100, RGB(255, 0, 0), 235, 150, RGB(0, 255, 0), 4, 254, RGB(0, 0, 255));
我不确定,但我认为问题应该在这里:
R = (d0+d1+d2) == 1 ? getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2 :
(getR(c0)*d0 + getR(c1)*d1 + getR(c2)*d2)/(d0+d1+d2);
与 G 和 B 变量相同...
我希望其他人解释我,问题出在哪里,因为我整天都在寻找它......