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

C++ 我的重心三角形光栅化器每三个像素绘制一次,c++,graphics,rendering,rasterizing,C++,Graphics,Rendering,Rasterizing,它还绘制了多个三角形,它们都被置换了,并且比例错误。 我正在尝试自己实现三角形光栅化器,可在以下位置找到: 我不知道我的代码出了什么问题 #include<fstream> #include<cmath> class Vertice { public: float x, y; Vertice(float x, float y) { this->x = x; this->y = y; }

它还绘制了多个三角形,它们都被置换了,并且比例错误。 我正在尝试自己实现三角形光栅化器,可在以下位置找到:

我不知道我的代码出了什么问题

#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;
}
#包括
#包括
职业眩晕
{
公众:
浮动x,y;
垂直(浮动x、浮动y)
{
这个->x=x;
这->y=y;
}
void fitToImage(int-imageWidth、int-imageHeight)
{
x=(x*(imageWidth/2))+(imageWidth/2);
y=(-y*(图像高度/2))+(图像高度/2);
}
};
阶级形象
{
公众:
int imageWidth,imageHeight;
无符号字符*像素;
图像(int-imageWidth、int-imageHeight)
{
此->图像宽度=图像宽度;
此->图像高度=图像高度;
像素=新的无符号字符[imageWidth*imageHeight*3];
}
~Image()
{
删除[]个像素;
}
无效设置像素(整数x、整数y、整数红色、整数绿色、整数蓝色)
{
int help_var=((y*imageHeight)+x)*3;
像素[帮助值+0]=(字符)红色;
像素[help_var+1]=(char)绿色;
像素[help_var+2]=(char)蓝色;
}
空填充像素(整数红色、整数绿色、整数蓝色)
{
int help_var=imageWidth*imageHeight*3;
for(int i=0;i=0&&w1>=0&&w2>=0){
设置像素(x,y,0,0,255);
}
}
}
}
//-----------------------------------------------------------------------------
};
int main()
{
图像(800600);
图像填充像素(255、255、255);
垂直a(0.2f,0.5f);
垂直b(-0.5f,0.0f);
垂直c(0.5f,-0.5f);
a、 fitToImage(image.imageWidth、image.imageHeight);
b、 fitToImage(image.imageWidth、image.imageHeight);
c、 fitToImage(image.imageWidth、image.imageHeight);
图像。填充三角形以方为中心(a、b、c);
std::流图像文件;
打开(“./drawing\u triangle\u test\u image.ppm”);
图像文件
绝对是代码的错误类型(可能有多个实例)。您需要将y位置乘以宽度。否则,您将在随机x位置交错三角形

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

绝对是代码的错误类型(可能有多个实例)。您需要将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));
}

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

除了@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));
}

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

感谢您的重播。我将函数setPixel中的help_var更改为:to:int help_var=((y*imageWidth)+x)*3;图像现在好多了。但三角形在右上角,比它应该的小。您是否仔细检查了fitToImage中的值和fitToImage中的值?感谢您的重播。我将函数setPixel中的help_var更改为:to:int help_var=((y*imageWidth)+x)*3;图像现在好多了。但三角形在右上角,比它应该的小。您是否仔细检查了fitToImage中的值和fitToImage中的值?