문제

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:

enter image description here

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:

enter image description here

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:

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...

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. That means, you're shifting an 8-bit number. You know what happens...

So, either convert the parameters to unsigned int or cast the values before shifting.

PS: Whatever you do, it would be nice if you made all three parameters the same type!

As in comments, turns out it was this:

So I don't think your (x,y) coordinates are normalised in that get_dist function. 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top