# Software rendering 3d triangles in the proper order

I'm implementing a basic 3d rendering engine in software (for education purposes, please don't mention to use an API). When I project a triangle from 3d to 2d coordinates, I draw the triangle. However, it's in a random order and so whatever gets drawn last draws on top of all other triangles (which might be in front of triangles it shouldn't be in front of)...

Intuitively, seems I need to draw the triangles in the correct order. So I can calculate all their distances to the camera and sort by that. The objects furthest away get drawn last. Is this the proper way to render triangles? If I'm sorting all the objects, this is n*log(n) now. Is this the most efficient way to do this?

-

I just compute 3 points when projecting a 3d triangle onto 2d space. Then I use an API's `draw_triangle` method. I think the filled 2d triangle is drawn using OpenGL, so it's very fast. You're right though, if I have to draw every pixel anyway, making an additional depth calculation isn't too bad I guess.. `sqrt((x-camx)**2 + (y-camy)**2 + (z-camz)**2)` – at. Nov 21 '12 at 21:25