Jump to content

Recommended Posts

Posted

I am working on a platform that does not have z buffering hardware support.  I am trying to sort polygons in a scene according to z distance so I can implement the painter's algorithm, but I am having no luck.  Currently I divide the game into regions and sort the polygons manually in each region according to what looks right.  But I would like to know the mathematical solution.  I don't have any cyclical overlaps and I don't have any intersecting objects but I do have polygons that touch.  

Thank you for your help.

Posted (edited)

Z-buffering is more reliable than sorting by polygon's z-position...

Sorting by z-position is very slow, the more polygons, the worse it gets.

 

Each polygon has a minimum distance from the camera and a maximum distance from the camera. The center of z will be at distance (min+max)/2.

What z value are you using as a parameter for the sort routine?

 

Edited by Sensei
Posted (edited)
4 hours ago, Richard Baker said:

but I do have polygons that touch.  

attach screen-shot..

 

BTW, if you don't support two-sided polygons, you can reject polygons with an incorrect dot-product between them and the camera, i.e. > 0 (which means that the normal of the polygon is facing the same direction as the camera, so it's facing away from the camera)

Edited by Sensei
Posted

I already used back face culling on some polygons but not all of them.  I tried using the average z value of the vertices of the polygon but that doesn't work because some polygons have closer points but are further away from the average. 

 

Thank you.  Be back later in a rush right now.

Posted (edited)

Sort by z is not a perfect algorithm..

don't expect realistic results...

 

Edited by Sensei
Posted
On 6/16/2022 at 5:27 PM, Richard Baker said:

I tried using the average z value of the vertices of the polygon but that doesn't work because some polygons have closer points but are further away from the average. 

Tessellate triangles more.. won't work as good as z-buffer, but you might get better results than without..

 

Posted (edited)

Scanline's algorithm is:

clear the screen

clear the z-buffer (set Z to maximum)

for each triangle, draw a triangle, check and fill the z-buffer with lower and lower values.

https://en.wikipedia.org/wiki/Scanline_rendering

Go from min-Y to max-Y of each triangle. Go from min-X to max-X of each triangle.

 

If you start by sorting the polygons by their Z values,

you will fill the Z buffer and the RGBA buffers only when Z is closest to the camera. Thus faster.

 

However, if the geometry has transparency, you must start by sorting by Z in reverse order, that is, the pixels farthest from the camera are rendered first.

(most 3D games do this because it is quickly implemented in the hardware)

The pixel of the transparent polygon is a mixture of the old value (which is further away from the camera) and the semi-transparent, semi-transparent color of the triangle.

(note it does not support refractive index!).

 

Someone who doesn't want to use the Z buffer can change the algorithm to go from the top left pixel of the screen to the bottom right pixel of the screen, and then perform a for loop for each triangle. This will be slower, but the Z buffer will not be needed at all.

Edited by Sensei
Posted

After a night, I was reminded of one trick on how to improve transparency support:

- render the opaque polygons first (sorted or not (sorting a large number of elements is slow, but rendering things that are not visible and will be replaced by the closest one is slow too))

- sort only transparent polygons in reverse Z order (from farthest to closest to the camera)

- render the transparent polygons.

This means that only a small number of transparent polygons are sorted.

 

It is good to have a user editable option to enable/disable sorting (this can be done for CPU, different algorithms and GPU accelerated version). Option to disable/enable transparency support.

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.