0

I'm working on a simple "3D engine". I have studied a lot and everything till now is fine. But, there is a problem I haven't solved yet. How to determine which solid to draw first and which last. The solids more far from the camera should be drawn first, the solids near, last. But this is difficult to be implemented in code. I tried different ways. The first was calculating the distance from the camera to the center of the solid and compare all values. It works partially. But, for example, if you have a large solid near, and a small one back of this, the small one will be drawn last because it's center is near than the center of the large one... This is an error... Take a look to the attached picture... Thanks. Far solid is drawn last I want to sort the solids in order to get the correct drawing order. Thanks for any help!

Daniel
  • 11
  • 1
  • 6
  • see: **depth buffering** easy to implement, `O(1)` fast and pixel perfect ... however in case of transparency you need also **depth sorting** which is slow, more or less comlplex as you need split triangles ... – Spektre Feb 07 '23 at 16:45
  • Hi, thanks. I've already heard about depth buffering. But I'm using a canvas library for drawing lines and other faces... I'm trying to figure out how can I draw by each pixel an entire drawing. It is possible? Thank you – Daniel Feb 07 '23 at 22:01
  • if you have fast pixel access then yes it is possible you just use [N-dimensinal DDA](https://stackoverflow.com/a/54018102/2521214) for lines and also as building block for [polygons](https://stackoverflow.com/a/19078088/2521214) and simply add the depth buffer condition [here simple example](https://stackoverflow.com/a/65664918/2521214) for lines and [here](https://stackoverflow.com/q/66503903/2521214) even polygons and other stuff – Spektre Feb 08 '23 at 07:34
  • Thanks. With depth buffering how anti-aliasing is handled? – Daniel Feb 08 '23 at 10:57
  • That has nothing to do with depth buffering ... antialliasing is about transparency not depth so the same algorithms work the same the only thing is you might have depth artifacts on the edges of antialliased stuff but that is concerning only direct intersections between polygons and usually using `<=` or `>=` instead of `<` or `>` for the depth test solves the problems – Spektre Feb 08 '23 at 12:47
  • Ok. Thank you! I will make a try – Daniel Feb 08 '23 at 16:35
  • Depth buffering seems difficult to be implemented. It is necessary to calculate the depth of each pixel of each polygon in the 3D world. But how to calculate the depth of the pixels that lies inside the polygon but that aren't present on the list of vertices? – Daniel Feb 09 '23 at 10:31
  • see the links I posted the answer is in there ... – Spektre Feb 09 '23 at 16:23
  • You can perfectly sort your polygons with a BSP tree. However some polygons would need to be splitted. Check https://people.eecs.berkeley.edu/~jrs/274/bsptreefaq.html – Mauricio Cele Lopez Belon Feb 09 '23 at 22:48

0 Answers0