How to program 3D graphics from scratch. Without 3D engines. (In Python)

DLC ENERGY
DLC ENERGY
154.6 هزار بار بازدید - 9 سال پیش - March 11th 2019 100k views!Hello!
March 11th 2019 100k views!
Hello! If you're having trouble, please read up on comments below, or type me one, and i will reply as soon as i can!

The pacman points: https://pastebin.com/XaDgrHkW
(fun fact, if you animated the pacman blocks loading from the first index to the last, that would be me drawing the grid)

some ideas for any of you 3d enthusiasts.

FACE SORTING METHOD:

Alright, so a simpler way than i showed you would be to just simply get the average z of each face.
(yep, just add the z's and divide by 4)

that's the most basic idea to sort with... Which i actually got from the internet when reading up on this kind of 3d stuff.
But just to mention, the help weren't all that great. they had different math to the projection (bad), an they didn't have anything on cameras. (that's why the projection they used were bad)

It took me a couple days figuring out translating the scene into perspective properly.

Anyway, the online help seemed good at the time, since all you got were a simple cube. just one cube, so you wouldn't notice any problems with it.

after implementing more cubes, (my pacman layout), i could see faces of adjacent cubes drawn over other cube faces. So i realized the sorting were terrible.

so i just had to spend ages figuring out a better way on my own. it happened kind of randomly. i decided to try implement some kind of point distance math, using the entire face. when i ran what i tried doing, it worked perfectly.

what i ended up coding was actually getting the (almost) average x's, y's, z's of the face (i didn't divide), and then sorting with the squared distances. (you don't need to sqrt both sides of the comparison)

but as i know, it's not a perfect solution. but it's good enough.

this problem hasn't got a perfect solution. it takes more thought, based on all the vertices of the faces... sorting is only needed for overlapping polygons. and if faces are colliding, there is no order. what a professional 3d engine does is use the depth of each pixel, which is a more perfect solution to rendering. it takes no face sorting. it's all done at the polygon renderer, in any order.


----------------------------------

FACE SORTING PROBLEM:

a face with 3 points... (a triangle)
a face with 4 points... (a quad)

apply the average properly
depth+=[sum(sum(vert_list[j][i]/len(face) for j in face)**2 for i in range(3))]

and yes, i'll state again, sorting quads with triangles, or whatever size polygon, still isn't going to be perfect. all you can do is get a rough closest poly.

So why did sorting still work when i didn't divide? firstly, because we were only sorting quads, and so that means we were essentially just scaling all the points (vectors) by 4. (its equal effect on each side of the comparison)

so all you need to know is, if you do have different size poly's, you need to average them. since all poly's don't average by the same amount.


also in professional 3d engines, every face is actually broken down into triangles. not for that silly sorting problem above, but for the interpolation. a triangle is always coplanar. (flat) so all the fragments can be calculated.

and yes... everything can be broken down into triangles.
(well, you're probably thinking, what about a sphere? yeah, you render a lot of strips of columns of triangles around in a circle. Polygons are actually the reason for calculating circles in the first place. which is why pi goes on forever. the algorithm is infinite. you can keep increasing the polygon size, but it's never quite a true circle.)

so your quads are made out of 2 triangles.

but in this pygame example, with flat faces, and having to sort faces, and the fact that python isn't for super fast rendering of many 3d calculated faces. i'd prefer to keep it as a single polygon draw to be faster...

anyway, incase you were having trouble, or are just interested, hope you enjoyed reading... =]


here's an image showing why you cant perfectly sort faces with some simple comparison.
https://www.khronos.org/opengl/wiki_o...
viewer A is looking at walls B and C
C is "infront" of B, which we want it to be drawn
but how would we sort them?
closest point? furthest point? center point?
even with all those options, it will calculate B as closer to viewer, so it will draw B ontop.


also, to add additional quality, to my polygon sorting
depth2+=[min(vert_list[j][2] for j in face)+max(vert_list[j][2] for j in face)]

i use min/max so faces which also have an equal min z, still don't get equally compared, if they had a higher max latched. to represent different sides of the cube for example.

update: new Poly Sorting concept i just thought of.
https://imgur.com/a/sswCd
it's about, in terms of the overlapping parts, which is infront?
9 سال پیش در تاریخ 1394/09/29 منتشر شده است.
154,605 بـار بازدید شده
... بیشتر