ssloy / tinyrenderer

A brief computer graphics / rendering course
https://github.com/ssloy/tinyrenderer/wiki
Other
20.68k stars 1.98k forks source link

About the function of face() #136

Open plallallla opened 1 year ago

plallallla commented 1 year ago

i make some adjust to overcome the missing function face() in lesson 1. here is my code,I encapsulated x and y using the Point structure in the simplest way,so it may looks a lttle different from lesson 1.

struct Point
{
    int x = 0;
    int y = 0;
};

Model mFace("../_Input/african_head/african_head.obj");

int width = 800;
int height = 800;
TGAImage imageobj(width, height, TGAImage::RGB);
for (int i = 0; i < mFace.nfaces(); i++)
{
    for (int j = 0; j < 3; j++)
    {
        vec3 v1 = mFace.vert(i, j);
        vec3 v2 = mFace.vert(i, (j + 1) % 3);;
        Point pt1{ (v1.x + 1.0f) * width / 2.0f,(v1.y + 1.0f) * height / 2.0f };
        Point pt2{ (v2.x + 1.0f) * width / 2.0f,(v2.y + 1.0f) * height / 2.0f };// view transport

        line(pt1, pt2, imageobj, white);
    }
}
imageobj.write_tga_file("../_Output/lesson1_obj.tga");

according to the class Model,the Model class has an overloaded function 'vert' that allows you to access a specified vertex or visit a specified node in a specified face.

vec3 Model::vert(const int i) const {
    return verts[i];
}
vec3 Model::vert(const int iface, const int nthvert) const {
    return verts[facet_vrt[iface*3+nthvert]];
}
plallallla commented 1 year ago

here is my output image