I'm trying to experiment with computer graphics and would like to implement Z Buffer algorithm for rendering in Software.
So I'm trying to use the following plane equation:
z = -(ax + by + d)/c
To compute the Z coordinate of a pixel using the plane form equation should I compute the Face normal of the triangle ? or a normal of a vertex is enough ?
Here is how I compute it:
double zValueOfPoint(float vx, float vy, float vz, float x, float y, float nx, float ny, float nz)
{
float A = nx;
float B = ny;
float C = nz;
float D = -(nx*vx, +ny * vy + nz * vz);
float z = -(A*x + B * y + D) / C;
return z;
}
vx,vy,vz vertex, x,y pixel coordinate, nx,ny,nz normal of a vertex
Now for each TOP or Bottom Triangle I check the Z Pixel to ZBuffer pixel
// Top of the triangle
for (int y = y0; y<y2; y++)
{
for (int x = xl_edge; x<xr_edge; x++)
{
float zOfPixel = zValueOfPoint(vx, vy, vz, x, y, nx, ny, nz);
if (zOfPixel < zbuffer[int(x + y * m_Width)]) {
zbuffer[int(x + y*m_Width)] = zOfPixel;
SetPixel((unsigned int)x, (unsigned int)y, color);
}
}//end for loop x
The same for bottom triangle
Right now I get completely broken model. The Z Buffer is initialized correctly.

float D = -(nx*vx, +ny * vy + nz * vz)has an extra comma in the middle. Also, once you have implemented perspective interpolation in your rasterizer, this comes for free.zis interpolated from the face vertexes directly the same way asx,y... while rasterizing. No need to use plane equation...