tamara-schmitz / CPPsoftwareRenderer

3D SoftwareRenderer written in C++11 using SDL
MIT License
0 stars 0 forks source link

Do backface clipping based on triangle normal vector #13

Open tamara-schmitz opened 7 years ago

tamara-schmitz commented 7 years ago

Probably requires #12 first. Comparison has to happen in view space. Eye vector would then be a forward vector.

Possible implementation of backface culling:

bool cullBackFacingTriangle(const Vec3 & eye, const Vec3 & v0,
                            const Vec3 & v1,  const Vec3 & v2)
{
    // Plane equation of the triangle will give us its orientation
    // which can be compared with the viewer's world position.
    //
    const Vec3 d = crossProduct(v2 - v0, v1 - v0);
    const Vec3 c = v0 - eye;
    return dotProduct3(c, d) <= 0.0f; // Returns true if it should be discarded
}