Original Post
Hi I am working on a software renderer based on the renderer described here: http://www.devmaster.net/forums/showthread.php?t=1884 I have got to adding a z-buffer but I have a problem with accuracy. Some triangles are showing through when they shouldn't. Here is my code for working out the gradient to use for the Z value: my z-test is as follows: for each increment of x I adjust z by (when scanning over the triangles bounding box): z += dzdx; and for y: z += dzdy; I have uploaded a screenshot,(using software renderer is in square on the top left, the same thing rendered through OpenGL is in the background). I know there are triangles missing, but thats a different issue. http://img2.freeimagehosting.net/image.php?65ee60069f.png Does anyone have any ideas on what I am doing that is causing it not to be 100% accurate? Thanks Mike
vec3<float> pv1(v1.mPosition.x,v1.mPosition.y,v1.mPosition.z);
vec3<float> pv2(v2.mPosition.x,v2.mPosition.y,v2.mPosition.z);
vec3<float> pv3(v3.mPosition.x,v3.mPosition.y,v3.mPosition.z);
Plane<float> p(pv1,pv2,pv3);
float dzdx = (-p.a/p.c);
float dzdy = (-p.b/p.c);
float origz = (v1.mPosition.z + dzdx * (minx - v1.mPosition.x) + dzdy * (miny - v1.mPosition.y));
origz = 1/2 * origz + 1/2;
if(z < zbuffer[comm]) {
unsigned char icolour[4];
icolour[0] = (unsigned char) rcolour[0];
icolour[1] = (unsigned char) rcolour[1];
icolour[2] = (unsigned char) rcolour[2];
icolour[3] = 255;
memcpy(&framebuffer[comm],icolour,4);
zbuffer[comm] = z;
}