简体   繁体   English

阴影三角形上的颜色错误

[英]Wrong colors on shaded triangle

I was trying to draw a shaded triangle, but I get wrong colors. 我试图绘制阴影三角形,但是颜色错误。 Here's the example of what I get, if I call a function with red, green and blue colors: 这是我得到的示例,如果我调用具有红色,绿色和蓝色的函数:

在此处输入图片说明

It should be red on the top left, green on the top right and blue on the top bottom. 它在左上角应为红色,在右上角应为绿色,而在最下端应为蓝色。 But as you can see the colors are not like that. 但是正如您所看到的,颜色并不是那样。 I should get something like this: 我应该得到这样的东西:

在此处输入图片说明

Here's the code: 这是代码:

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

I call a function drraw_ftriangle like that: 我这样调用函数drraw_ftriangle:

draw_ftriangle(surface, 100, 100, RGB(255, 0, 0), 235, 150, RGB(0, 255, 0), 4, 254, RGB(0, 0, 255)); draw_ftriangle(surface,100,100,RGB(255,0,0),235,150,RGB(0,255,0),4,254,RGB(0,0,255));

I'm not sure, but I think the problem should be here: 我不确定,但是我认为问题应该在这里:

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

Same with G and B variables... 与G和B变量相同...

I'd like other people to explain me, where's the problem, because I'm searching for it all day long... 我希望其他人向我解释问题所在,因为我整天都在寻找它...

The R and G parameters to your RGB function are unsigned char , and you are not casting them to unsigned int before shifting. RGB函数的RG参数是unsigned char ,并且在移位之前不会将它们强制转换为unsigned int That means, you're shifting an 8-bit number. 这意味着您要转移一个8位数字。 You know what happens... 你知道会发生什么...

So, either convert the parameters to unsigned int or cast the values before shifting. 因此,将参数转换为unsigned int或在移位之前强制转换值。

PS: Whatever you do, it would be nice if you made all three parameters the same type! PS:无论您做什么,将三个参数都设置为同一类型会很好!

As in comments, turns out it was this: 如评论中所示,结果是这样的:

So I don't think your (x,y) coordinates are normalised in that get_dist function. 因此,我认为您的(x,y)坐标未在该get_dist函数中标准化。 You compute the length of the hypoteneuse connecting those two vectors, but you don't give it any base lengths to normalise on. 您可以计算连接这两个向量的斜边的长度,但不给它任何基长来进行归一化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM