gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > 2D-in-3D: overcoming z-fighting, other questions

#143276 - Hyouko - Fri Oct 19, 2007 2:51 am

Hello, all; bit of a noob here, working on a simple-ish homebrew game with libnds and devkitARM. As I have some past experience with OpenGL, I figured I'd try to base my engine on that, and it's working well enough so far. I ran into the same z-fighting issue with depth-test that several others have come across in the past, and I've implemented a simple hack to get around it. Two questions regarding that: has anybody figured out a more elegant way to get around the depth test, and (if not) what's the smallest incremental change in depth that will register? (I'm presently using 1000th of a unit, which works, but some of the sprites are noticeably off by one pixel).

On a different note - can anybody recommend a decent Linux-based tool for saving DS textures? I'm currently using the psptex GIMP plugin, which can save to a 5551 raw format - it works, but I'd like to be able to use sprites with a proper alpha channel and take advantage of the smaller paletted texture formats available. I've found a tool from NomadTeam that could do this, but it's apparently Windows-only at present.

#143293 - ingramb - Fri Oct 19, 2007 8:36 am

Not 100% sure if this is your problem, but if you set an orthographic projection (rather than perspective), then a given poly's depth will have no effect on its pixel size. This may make your question about depth resolution less important.

#143359 - Peter - Sat Oct 20, 2007 9:06 am

Hyouko wrote:
has anybody figured out a more elegant way to get around the depth test

I read a common approach is to use a modified projection matrix (with slightly changed znear/zfar values) to render polys where you know they lie on the same plane with another poly. For example for bullets on walls.

Hyouko wrote:
and (if not) what's the smallest incremental change in depth that will register? (I'm presently using 1000th of a unit, which works, but some of the sprites are noticeably off by one pixel).

Depth buffer usually ranges from 0..1 or 0..0x7fff in fixed point. The scene depth is projected into the depth buffer, so when you setup your project matrix, znear=5, zfar=20 the depth buffer has to deal with a range of 15units. Where zdepth=0 is at the near plane and zdepth 0x7fff (the 15th unit) is the far plane.

However, the z-buffer doesn't have a linear distribution of its depth-values, it's more accurate closer to the near plane and loses accuaracy the more it comes to the far plane.
The formula to compute the depth value(copied from wikipedia).
Code:

z' = (far + near / far - near) + 1/z * (-2 * far + near / far - near)

I guess this makes it rather hard to give a "working depth value which will register" at every possible depth in this case.

What you could try is to use the w-buffer. The w-buffer has distrubuted its depth values linearily across the whole depth-range, so you can calculate depending on your scene depth how many units you have to move an object to make a change in the depth buffer. But the resolution closer at the near plane is not as good as for the z-buffer.

Hope it makes sense :)
_________________
Kind Regards,
Peter