Thomas Harte | 25 Aug 15:09

Thinking aloud on polygon filling

I've not actually done any coding on my Sam 3d experiments in a while,  
but I have been thinking a lot about the best way to implement polygon  
filling. I had a polygon filler working in a much earlier version of  
my code (see, e.g. http://uk.youtube.com/watch?v=kr_Lz98qVjE from  
about 0:39) but it only did triangles, didn't clip and generally  
wasn't really up to snuff in several ways that hit the frame rate.

Anyway, I've been playing around with a few ideas and was wondering if  
someone with a more instinctive grasp for this sort of thing might  
help out.

I previously tried using a span buffer. Each pixel span (i.e. a  
horizontal line of pixels) was inserted into a linear list of existing  
spans for that scan line, by clipping it against the spans that were  
already there then shuffling things around in memory. So there was a  
binary search and then often a small LDIR. The entire frame was drawn  
at the end. I even tried a system whereby each span list was compared  
with the previous span list and only the differences were plotted.  
That was much faster than just dumbly drawing the whole list, but  
slower than just plotting the spans to the screen and not doing the  
list search and insert. Furthermore, as you add more polygons to the  
scene, the insert gets more expensive very quickly.

Idea 1 is to divide the screen into 8×8 blocks, keep an overview that  
tags each as either block filled with a certain colour or messy. The  
polygon filler tags as many blocks as possible as completely filled,  
and consults & modifies a 1 bit mask for each run of 8 pixels in a  
messy block. So searching the existing structures for each individual  
container is faster because it's O(1), and you're doing exactly as  
much searching as you were for a 64×64 rectangle, then maybe less or  
(Continue reading)

Tobermory | 30 Aug 13:29
Favicon

RE: Thinking aloud on polygon filling

No expert here! Just my two cents.

I considered both your Ideas 1 and 2, both came out to be more expensive
with maintenance than I wanted.  Especially Idea 1 (which is a basic space
partitioning tree), the masks are very inefficient when you have many
overlapping/joining polygons in the same 8*8 pix square.  This happens an
awful lot for objects in the distance, I ended up having to redraw to the
same 8*8 square many times.

As I'm heavily using the stack pointer to draw to the screen I wanted to
render the whole object right-to-left line-by-line.  Like Braben, I'm using
convex polys and convex objects only.  (This isn't a problem when you can
glue convex objects together afterwards).  For each object, there are two
kinds of mask to use - (A) one at the left and right sides of the object, ie
the background meets the object here, and (B) where the polys on the object
meet each other in the middle.

(A) is done traditionally, with a bitwise-mask to ensure crisp edges.
(B) only has 4 or 5 different flavours so is hardcoded.

I use a 'shared points' system to ensure calculations on points and vertices
are only done once, this helps ordering the rendering process as well, so I
know which polygons I'm rendering on each line.  In the end the code looks a
bit like this:

Render:
	PUSH HL		;Poly 1 - 4 bytes per push. Mmm, efficient-y.
	PUSH HL
	...
	PUSH DE		;Joining bit
(Continue reading)


Gmane