3D calculatig.
ZXNet echo conference «code.zx»
From Aleksey Malov → To All 6 April 2000
Greetings, All!
When drawing 3d polygons, a situation arises when it is necessary to decide in which
order to display polygons on the screen.
In the case of convex objects, everything is simple: it is enough to display only those faces
whose normals are directed towards the camera, and the order in which the faces are displayed is not
matters.
For non-convex objects, the ones furthest from the camera should be displayed first
polygons.
In Demo or die it is recommended to sort polygons by distance Z coordinate
center points of polygons. However, this method will fail if the calculation is very
simple scene:
──────────────────── ─────────────────────
/ ───────┐ \n
/ . │ \n
/ │ \n
/ │ \n
/\n
/\n
/\n
/\n
───────────────────────────── ──────────────────────────────
Although the center point of the small triangle is further away than
rectangle, it must be drawn later than the rectangle, however, the above
the sorting method will not notice this, and as a result, the small one will not be visible
triangle.The method I will write about is devoid of this drawback and works for several
orders of magnitude faster than the bubble sort method for polygon center points.
Consider an object of the "dumbbell" type, consisting of three objects: 2 spheres and
cylinder Instead of sorting all the polygons of a given object,
it is enough to introduce two pseudo-polygons, specified by the directions of their vectors
normals (we don’t need the coordinates of the vertices of the pseudo-polygons themselves, because
we won't use them), which divide the object into 3 CONVEX objects (2 spheres and
cylinder):
WWWWW WWWWW
WW WW WW WW
W W W W
W W W W
W WWWWWWWWWWWW W
W W
W S1 C S2 W
W W
W WWWWWWWWWWWW W
W W W W
W W W W
WW WW WW WW
WWWWW WWWWW
│N1 │N2
├──> ├───>
│ │
Now it is enough to determine the direction of the normals of the pseudo-polygons. If
normal N1 is directed towards the camera, then first we draw the polygons of the sphere
S1. Otherwise, we draw sphere S1 last, afterpolygons of sphere S2 and cylinder C (which of these two objects should be displayed first,
solve in a similar way using the normal N2).
In this example, it would be possible to get by with only the normal N1, because N1 and N2
parallel to each other.
In a similar way, you can sort more complex scenes by entering
a larger number of pseudo-polygons (just add the required number of vectors
normals of pseudo-polygons).
By the way, to set some normals, you can get by with the ones that already exist in
stage tops.
With dynamically changing 3D scenes (in which the relative position of
and/or shape of objects) will be a little more difficult. Maybe someone knows how to resolve this
problem? However, in demos for the Spectrum you can get by with unchanged
objects.
I wish you health, happiness and creative Uzbeks.
Aleksey Malov aka VIVID/Brainwave.