backface culling error (in world space)
- by acrilige
I write simple software renderer. In my pipeline i have stage of backface culling.
But looks like it has some error (see picture).
I perform culling right after world transformation (is it correct?).
(i can't insert picture in post coz i don't have enough points, so i just upload it (cube model):
http://imageshack.us/photo/my-images/705/bcerror.png/)
Vector3F view_dir(0.0f, 0.0f, 1.0f);
std::vector<Triangle> to_remove;
for (Triangle &t : m_triangles)
{
Vector4F e1 = t.v2 - t.v1;
Vector4F e2 = t.v3 - t.v1;
Vector3F normal(
e1.y * e2.z - e1.z * e2.y,
e1.z * e2.x - e1.x * e2.z,
e1.x * e2.y - e1.y * e2.x
);
normal.Normalize();
float dot = Dot(view_dir, normal);
if (dot <= 0)
to_remove.push_back(t);
}
for (Triangle& t : to_remove)
m_triangles.erase(std::remove(m_triangles.begin(), m_triangles.end(), t), m_triangles.end());
Camera sits in origin and points in screen (RH).
What is the reason?
For better explanation i upload picture with cube rotation screenshots: http://imageshack.us/photo/my-images/842/bcmove.png/
UPDATED: The error occurs only when triangle has non-zero offset from origin
UPDATED 2: If i process backface culling in clip space (after transforming all vertices with view and projection matrix), and just check z coordinate of triangle normal - it works perfect... Can i perform culing RIGHT BEFORE view/proj transforms? In this case looks like culling will not depends of projection and it's not right?..
UPDATED 3: I found answer and will post it in two hours - again coz of reputation lack.