C++ 如何在三角形的第三点之间插值?

C++ 如何在三角形的第三点之间插值?,c++,math,C++,Math,我试图在三角形的三个点之间插值,以创建颜色混合效果。我成功地在三角形的两点之间插值,效果非常好。以下是FillFlatBottom函数的完整代码。对于平顶三角形,我也在做同样的事情。我在涉及颜色混合的行的末尾留下了//以便更容易理解 //c will contain an array of 3 non-normalized rgb colors static void FillFlatBottom(const Triangle& tri, olc::PixelGameEngine* pg

我试图在三角形的三个点之间插值,以创建颜色混合效果。我成功地在三角形的两点之间插值,效果非常好。以下是
FillFlatBottom
函数的完整代码。对于平顶三角形,我也在做同样的事情。我在涉及颜色混合的行的末尾留下了//以便更容易理解

//c will contain an array of 3 non-normalized rgb colors
static void FillFlatBottom(const Triangle& tri, olc::PixelGameEngine* pge, const Color::frgb c[3], const bool blend) noexcept
    {
        const float m0 = (tri.p0.x - tri.p1.x) / (tri.p0.y - tri.p1.y);
        const float m1 = (tri.p0.x - tri.p2.x) / (tri.p0.y - tri.p2.y);

        const int starty = (int)std::ceil(tri.p0.y - 0.5f); \\
        const int endy = (int)std::ceil(tri.p1.y - 0.5f); \\

        float colorStarty = 0; \\
        float colorEndy = (float)endy - (float)starty; \\

        for (int y = starty; y < endy; y++, colorStarty++)\\
        {
            const float px1 = m0 * ((float)y + 0.5f - tri.p1.y) + tri.p1.x;
            const float px2 = m1 * ((float)y + 0.5f - tri.p2.y) + tri.p2.x;

            const int startx = (int)std::ceil(px1 - 0.5f);
            const int endx = (int)std::ceil(px2 - 0.5f);

            float r1 = 1.0f;\\
            float r2 = 1.0f;\\
            float r3 = 1.0f\\;

            Color::frgb rColor[3];\\
            Color::frgb finalColor = c[0];\\

            if (blend)
            {
                r1 = colorStarty / colorEndy;\\
                r2 = 1.0f - r1;\\
                finalColor.r = (r1 * c[0].r) + (r2 * c[1].r);\\
                finalColor.g = (r1 * c[0].g) + (r2 * c[1].g);\\
                finalColor.b = (r1 * c[0].b) + (r2 * c[1].b);\\
            }

            float colorStartx = 0;\\
            float colorEndx = (float)endx - (float)startx;\\
            for (int x = startx; x < endx; x++)\\
            {
                if (blend) \\
                {
                    r3 = colorStartx / colorEndx;\\
                    finalColor.r += (r3 * c[2].r);\\
                    finalColor.g += (r3 * c[2].g);\\
                    finalColor.b += (r3 * c[2].b);\\
                }
                pge->Draw(x, y, olc::Pixel
                (
                (int)(finalColor.r),
                (int)(finalColor.g),
                (int)(finalColor.b))
                );
            }
        }
    }
但是对于第三点,这很棘手,因为
r1+r2
已经加起来是1,所以我不能从
finalColor
中添加或删除任何数字,但我需要通过沿x轴插值并计算
r3
来调整
finalColor
的值

我试过一些肮脏的把戏,比如

 r1 = colorStarty / colorEndy;
 r2 = 0.67f - r1; \\ 1.0f - 0.33 = 0.67 \\ 1 / 3 = 0.33
r3
留出空间,但它总是给出不完美的结果


在研究如何解决这个问题时,我确实读到了一些关于重心坐标的东西,但我不理解它,我不想复制我不理解的东西。因此,如果可能的话,一个不涉及这一点的解决方案会更好。谢谢

请参见古兰和冯阴影。这就是你在这里要做的。简而言之,不需要直接插值3个点。沿顶点之间的边插值,然后使用这些值来解释三角形上的像素。请参见,您只需按照与
x
相同的方式插值颜色(通过为每个颜色通道添加一个缓冲区)。如果您有大量的并行处理,那么使用它会更快
 r1 = colorStarty / colorEndy;
 r2 = 0.67f - r1; \\ 1.0f - 0.33 = 0.67 \\ 1 / 3 = 0.33