为什么我写的这个三角形着色器会分成四个?

问题描述 投票:0回答:1

我编写了一个带有边缘函数的三角形着色器,并尝试使用权重为其着色,但由于某种原因,颜色分成 4 个独立的三角形,每个三角形都有自己较小版本的着色器。绘图函数只是颜色和位置的两行代码,不应导致此错误。

triangle

int edgeFunction(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3){
        return (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1);
}

std::vector<fragment> RGBtri(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3){
        std::vector<fragment> tri; //fragment is a struct with xyrgba

        int minX = std::min(x1, std::min(x2,x3));
        int maxX = std::max(x1, std::max(x2,x3));
        int minY = std::min(y1, std::min(y2,y3));
        int maxY = std::max(y1, std::max(y2,y3));

        int p = edgeFunction(x1,y2,x2,y2,x3,y3);

        for(uint16_t y = minY;y<=maxY;y++){
                for(uint16_t x = minX;x<=maxX;x++){
                        int a = edgeFunction(x2,y2,x3,y3,x,y);
                        int b = edgeFunction(x3,y3,x1,y1,x,y);
                        int c = edgeFunction(x1,y1,x2,y2,x,y);

                        if((a|b|c)>=0){
                                float d = ((float)a*255/(float)p);
                                float e = ((float)b*255/(float)p);
                                float f = ((float)c*255/(float)p);

                                uint8_t r = static_cast<uint8_t>(d);
                                uint8_t g = static_cast<uint8_t>(e);
                                uint8_t b = static_cast<uint8_t>(f);

                                 tri.push_back({x,y,r,g,b,255});
                        }
                }
         }
          return tri;
}

我得到了很多信息这里

我尝试了一些变化,但最多我得到了 rgb 和 cmy 部分来交换。

我的目标是在主三角形的顶点有红绿和蓝色,并且没有分裂

c++ graphics shader sdl-2 rasterizing
1个回答
0
投票

找到答案了,点p是x1 y2对于第一个点不是x1 y1,abc不应该乘以255,结果应该乘以255然后转换为uint8

std::vector<fragment> RGBtri(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3){
        std::vector<fragment> tri;

        int minX = std::min(x1, std::min(x2,x3));
        int maxX = std::max(x1, std::max(x2,x3));
        int minY = std::min(y1, std::min(y2,y3));
        int maxY = std::max(y1, std::max(y2,y3));

        int p = edgeFunction(x1,y1,x2,y2,x3,y3);

        for(uint16_t y = minY;y<=maxY;y++){
                for(uint16_t x = minX;x<=maxX;x++){
                        int a = edgeFunction(x2,y2,x3,y3,x,y);
                        int b = edgeFunction(x3,y3,x1,y1,x,y);
                        int c = edgeFunction(x1,y1,x2,y2,x,y);

                        if(a>=0&&b>=0&&c>=0){
                                float w1 = ((float)a/(float)p);
                                float w2 = ((float)b/(float)p);
                                float w3 = ((float)c/(float)p);

                                uint8_t R = red.r*w1+green.r*w2+blue.r*w3;
                                uint8_t G = red.g*w1+green.g*w2+blue.g*w3;
                                uint8_t B = red.b*w1+green.b*w2+blue.b*w3;

                                tri.push_back({x,y,R,G,B,255});
                        }
                }
        }

        return tri;

}
© www.soinside.com 2019 - 2024. All rights reserved.