我的重心三角形光栅器每三个像素绘制一次

My barycentric triangle rasterizer draws every third pixel

它还画了多个三角形,它们都错位了而且比例错误。 我正在尝试自己实现三角形光栅化器,网址为:

https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage

我不知道我的代码有什么问题。

#include<fstream>
#include<cmath>

class Vertice
{
public:
    float x, y;
    Vertice(float x, float y)
    {
        this->x = x;
        this->y = y;
    }
    void fitToImage(int imageWidth, int imageHeight)
    {
        x = (x * (imageWidth / 2)) + (imageWidth / 2);
        y = (-y * (imageHeight / 2)) + (imageHeight / 2);
    }
};

class Image
{
public:
    int imageWidth, imageHeight;
    unsigned char* pixels;
    Image(int imageWidth, int imageHeight)
    {
        this->imageWidth = imageWidth;
        this->imageHeight = imageHeight;
        pixels = new unsigned char[imageWidth * imageHeight * 3];
    }
    ~Image()
    {
        delete[] pixels;
    }
    void setPixel(int x, int y, int red, int green, int blue)
    {
        int help_var = ((y * imageHeight) + x) * 3;
        pixels[help_var + 0] = (char)red;
        pixels[help_var + 1] = (char)green;
        pixels[help_var + 2] = (char)blue;
    }
    void fillPixels(int red, int green, int blue)
    {
        int help_var = imageWidth * imageHeight * 3;
        for (int i = 0; i < help_var; i += 3) {
            pixels[i + 0] = (char)red;
            pixels[i + 1] = (char)green;
            pixels[i + 2] = (char)blue;
        }
    }
    //-------------------BARYCENTRIC TRIANGLE RASTERISATION------------------------
    float edgeFunction(const Vertice& A, const Vertice& B, const Vertice& P)
    {
        return ((P.x - A.x)*(B.y - A.y) + (P.y - A.y)*(B.x - A.x));
    }   

    void fillTriangleBarycentric(const Vertice& v0, const Vertice& v1, const Vertice& v2)
    {
        Vertice p(0.0f, 0.0f);
        for (int x = 0; x < imageWidth; x++) {
            for (int y = 0; y < imageHeight; y++) {
                p.x = x + 0.5f; p.y = y + 0.5f;
                float w0 = edgeFunction(v1, v2, p);
                float w1 = edgeFunction(v2, v0, p);
                float w2 = edgeFunction(v0, v1, p);
                if (w0 >= 0 && w1 >= 0 && w2 >= 0) {
                    setPixel(x, y, 0, 0, 255);
                }
            }
        }
    }
    //-----------------------------------------------------------------------------
};

int main()
{
    Image image(800, 600);
    image.fillPixels(255, 255, 255);

    Vertice a(0.2f, 0.5f);
    Vertice b(-0.5f, 0.0f);
    Vertice c(0.5f, -0.5f);

    a.fitToImage(image.imageWidth, image.imageHeight);
    b.fitToImage(image.imageWidth, image.imageHeight);
    c.fitToImage(image.imageWidth, image.imageHeight);

    image.fillTriangleBarycentric(a, b, c);

    std::ofstream imageFile;
    imageFile.open("./drawing_triangle_test_image.ppm");
    imageFile << "P6\n" << image.imageWidth << " " << image.imageHeight << "\n255\n";
    imageFile.write((char*)image.pixels, image.imageWidth * image.imageHeight * 3);
    imageFile.close();

    return 0;
}

这是我在 运行 我的程序后得到的图像。

感谢您的帮助!

这是更好的结果(其中 setPixel 使用 imageWidth 而不是 imageHeight):

y * imageHeight

绝对是您的代码出现的错误类型(可能有多个实例)。您需要将 y 位置乘以 宽度 。否则,您最终会在随机 x 位置交错三角形。

你得到四个三角形的事实与 800/600 简化为 4/3 有关。如果你用 603 渲染到 797,你可能会得到一些随机的水平线。

除了@Jeffrey 的更正外,您的边缘功能也不太正确。应该是

float edgeFunction(const Vertice& A, const Vertice& B, const Vertice& P)
{
    return ((P.x - A.x)*(B.y - A.y) - (P.y - A.y)*(B.x - A.x));
}

即两项之间应该有一个负号(因为它是两个位置向量AB和AP的叉积)。