I am doing a small project, where there is a 3d engine, however I do not know how to draw something if it is like the image below. Usually I would draw from farthest back to closest. But here there is no order? How would modern 3d Engines draw this?

OverLapping Triangles

  • \$\begingroup\$ Just to make sure, you want to know how you could draw something like that in the 3d engine that you're writing, is that right? \$\endgroup\$ – Vaillancourt Oct 4 '18 at 15:54
  • \$\begingroup\$ Yes, or at least how it is down in other engines. \$\endgroup\$ – teclnol Oct 5 '18 at 0:22

The keyword you are looking for is z-buffering.

When a polygon-based rendering engine calculates the pixels covered by a triangle, it doesn't just calculate its x- and y-coordinates on the screen for each pixel, but also it's z-coordinate. The z-coordinate is its distance from the screen.

The engine also creates a z-buffer for each frame it renders. The z-buffer remembers if the engine already drew something to each x/y coordinate, and if it did, what z-coordinate that pixel had. Before the engine calculates the color of a new pixel, it checks if there is already a value in the z-buffer for these coordinates with a lower value than that of the current pixel. When there is, the pixel gets skipped. When there is not, both z-buffer and the image on the screen get overwritten by the current pixel.

  • \$\begingroup\$ Yes, this looks like what I want, it seems a bit expensive though as you have to find the co-ordinate of each pixel and compare it. \$\endgroup\$ – teclnol Oct 5 '18 at 0:26
  • \$\begingroup\$ @teclnol Not really. The z-buffer is basically a two-dimensional array of floats with the size of the screen and the z-coordinate of a pixel is usually a by-product of the perspective calculations you have anyway. It in fact works so well that 3d engines usually try to render scenes from front to back to avoid unnecessary overdraw. \$\endgroup\$ – Philipp Oct 5 '18 at 7:44
  • \$\begingroup\$ Yeah that part isn't, I was just unsure of how to find the z value of each polygon. Before, for each triangle I would find each of the three vertices of that triangle on the the 2d screen, then draw a triangle screen, which means I wouldn't know the z-values of the center of the triangle, just the vertexes. It seems a bit expensive to find the z-value of each pixel in the triangle, I am actually a bit unsure how to in fact. \$\endgroup\$ – teclnol Oct 5 '18 at 15:28
  • \$\begingroup\$ @teclnol Have you heard about projection matrices? The standard method to convert 3d vertices to 2d screen positions is by multiplying it with a matrix, and the z-coordinate is a natural by-product of it. \$\endgroup\$ – Philipp Oct 5 '18 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.