I am trying optimize routines for drawing primitive shapes for my custom 3D renderer.
What is the best data type for representing a coordinate in 3D space? I thought that integer would be the best choice since calculations involving integer are fast, compared to float or double. Also, Bresenham's algorithm loves integers. But the drawback for using integer is the loss of precision which leads to z fighting. Here is the attempt to draw overlapping 3D triangles if I use integer coordinates.
You can see the rough line on the area where triangles overlap. It is supposed to be straight. On the other hand, if I use float for coordinates,
It works as expected.
I wonder if it is possible to fix the z fighting issue when I use integer coordinates. Maybe there's no other way than using float for coordinates?
Here is my code for rendering a single pixel. I thought it might be relevant.
void Renderer::PutPixel(int x, int y, int z, const Colour& col)
int offset = y * width + x;
float depth = 1.0 / z;
if (zdepth[offset] < depth)
zdepth[offset] = depth;
pixels[offset] = col.argb;
The z buffer array
zdepth is a 1D array of floating point values and
pixels is an another 1D array of 32-bit unsigned RGB values.