PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Rasterizing issue

I read posts pointing to this fatmap article and was wondering if anyone could give me some insights about a change I'm trying to do.

The conversion from subpixel precision is made via a CEIL method ( x = x + 0xFFFF >> 16). Using this, I'm loosing many of the top and left pixels of the screen. So I've tried to use a ROUND method (x = x + 0x8000 >> 16) instead. This create huge artefacts with (almost) horizontal triangles' edge.

eg.:
Having an edge from (0,1.5) to (10,1.4) ==> m = -0.01. Rounding y makes a 1 pixel high edge from y=2 to y=1.
The rightmost x will then be computed as 10 + (ROUND(1.4)-1.4) / -0.01. That is x=40. :(
I'd leave the CEIL alone and instead look at your clipping to fix the top/left issues.

Part of the document that may be pertinent is this:


11. Avoiding divide overflow in slope calculations
--------------------------------------------------

For some sections that are very thin, the slope calculation might
overflow.

Look at the following case:

v1.x = 0000:0000 v2.x = 0002:0000
v1.y = 0000:0000 v2.y = 0000:0001

ceil(v2.y) - ceil(v1.y) will report that this is one scanline even if
the actual height is just a fraction of a pixel.

height = v2.y - v1.y = 0000:00001
width = v2.x - v1.y = 0002:00000

so performing (width << 16) / height will cause divide overflow. We
are aiming for nearly perfect affine mapping so we can't just take some
default value for the slope. One way to avoid the overflow is to
multiply width by the inverse of height using only 18:14 bit precision.

long height = v2.y - v1.y;
long inv_height = (0x10000 << 14) / height;
long dxdy = ((v2.x - v1.x) * inv_height) >> 14;

Note that this method can only be used for this special case where the
section height is less than one pixel. Other sections that are more
than one pixel should of course be calculated as usual.
I've read this section, but it does not seems related to my problem. I don't suffer from any overflow. The result is what it should be, an extrapolation of the slope. But out of the triangle boundary... :?

The clipping should effectively return an absolute 0 (rather than current 0.000??). I was looking for some reason as to why a CEIL works fine while a ROUND blow everythings apart.

Thanks anyway.
That must be a bug in your rasterizer.

If you use the ceil-based fixed to integer conversion but subtract 0x7fff from all coordinates prior to rasterization you effectively have switched from ceil to a rounding-mode.

My guess is that you only fixed the ceil to round conversion for the y calculation but didn't touched the subpixel correction in x.

Btw, before you start messing with the rasterizer again: leave it in ceil-mode, that's the way it ought to be.
torus wrote:My guess is that you only fixed the ceil to round conversion for the y calculation but didn't touched the subpixel correction in x.


I've achieved the best results to date using ceil in y and round in x. That is, I am missing pixels only at the top line, and no artefact are caused by the round in x.

torus wrote:Btw, before you start messing with the rasterizer again: leave it in ceil-mode, that's the way it ought to be.


It seems I will have to use ceiling function, but I can't figure why... Round should have similar behavior, while fixing my clipping issue. Is it something to do with the vertex ordering being made on y values?
This doesn't make sense. Why would you want something different for each axis (x and y). You want to use ceil() for both.
This will not be the final solution either... :? I've done many tests and rounding x pixels value did not result in huge artefacts as for the y values.
And this doesn't make sense. There is no difference between the x and y axis when rasterizing triangles. Something else must be wrong in your implementation.
The problem was in the fixed point's inverse method. I was loosing too much precision there.

Thanks for the help.