<p>I am trying optimize routines for drawing primitive shapes for my custom 3D renderer.</p>
<p>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.</p>
<p><a href="https://i.sstatic.net/UaULM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UaULM.png" alt="f1" /></a></p>
<p>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,</p>
<p><a href="https://i.sstatic.net/UaULM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZOqiA.png" alt="f2" /></a></p>
<p>It works as expected.</p>
<p>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?</p>
<p>Edit:</p>
<p>Here is my code for rendering a single pixel. I thought it might be relevant.</p>
<pre><code>void Renderer::PutPixel(int x, int y, int z, const Colour&amp; col)
{
    int offset = y * width + x;
    float depth = 1.0 / z;

    if (zdepth[offset] &lt; depth)
    {
         zdepth[offset] = depth;
         pixels[offset] = col.argb;
    }
}
</code></pre>
<p>The z buffer array <code>zdepth</code> is a 1D array of floating point values and <code>pixels</code> is an another 1D array of 32-bit unsigned RGB values.</p>

CC BY-SA 4.0
2022-06-09T11:25:46.620Z
32 bit floats are pretty standard and their performance is comparable to integer on a modern machine.
pmw1234
CC BY-SA 4.0
2022-06-09T15:39:21.643Z
@pmw1234 Yeah that's what I ended up but I am still wondering if it is still possible to use integers.
Jimmy Yang
CC BY-SA 4.0
2022-06-09T18:07:24.900Z
doing everything with integers is possible, the easiest approach is to make the each step size very small. So a smallish model might be 1000 units across, while the same object when done with floating point might only be 1 or 2 units across.
pmw1234
<z-buffer>
Data type for coordinates
Jimmy Yang
# Data type for coordinates

(Asked by: username_0 on 2022-06-07)

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.

[![f1](https://i.sstatic.net/UaULM.png)](https://i.sstatic.net/UaULM.png)

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,

[![f2](https://i.sstatic.net/ZOqiA.png)](https://i.sstatic.net/UaULM.png)

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?

Edit:

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.

**Tags:** z-buffer

> **Comments:**
> **username_1**: 32 bit floats are pretty standard and their performance is comparable to integer on a modern machine.  
> **username_0**: @username_1 Yeah that's what I ended up but I am still wondering if it is still possible to use integers.  
> **username_1**: doing everything with integers is possible, the easiest approach is to make the each step size very small. So a smallish model might be 1000 units across, while the same object when done with floating point might only be 1 or 2 units across.  

---

## 0 answers

