I have implemented Z-buffer algorithm but sometimes I get weird results, ie visibility is not solved correctly. My Z-buffer algorithm works like that:
-------Step 0:
Initialize Z-buffer with smallest possible negative numbers
Initialize frame buffer to background colour
-------Step 1:
Projection of all polygons to 2D:
x_2d = x_3d
y_2d = y_3d
(ie projection to xy plane, z coordinates are ommited, more in the remarks section)
------Step 2 (for each 2d polygon - ie for each projection)
Scan conversion of the 2d polygon -> for each pixel of that polygon:
calculate z-value: z=-(a*x2d + b*x2d + d)/ c
a, b, c, d are taken from the equation of the plane in which the original 3d polygon lies - and a, b, c are coordinates of the 3d polygon's normal (calculated as P1P2 x P1P3 where P1, P2, P3 are vertices of the 3d polygon)
compare z-buffer w/ z-value, update z-buffer and frame buffer if z-value > z-buffer[x2d, y2d]
-------Step 3
Write frame buffer to screen
IMPORTANT REMARKS:
1) I am be unable to project polygons to 2d in a different way than by ommiting z-coordinates because the expression for calculating the z-value in Step 2 wouldn't work in different case, I think
2) Moreover - from the same reason - I am unable to do any transformations and eg. have [0;0] in the middle of the screen
WELL, FINALLY, HERE ARE MY QUESTIONS:
Where (on the internet) can I find a complete, accurate, step-by-step description of the z-buffer algorithm, with all the necessary expression, equations etc. and perhaps some sample code in C/C++/Pascal or VB? Everything I have found do far are vague descriptions with weird pseudo-code, there is not a word about when I work with 3d polygon, when with its 2d projection etc. - only John Carmack could understand that))))
Is anything described in my implementation wrong?
Thank you very much