Sunday, October 19, 2008

The Optimization of the Month

There is this character which is composed of about 2200 objects. Most being small gemstones.
Because those gemstones are animated individually, there is no simple way for me to merge all of them into a single object.
So I'm left with about 2000 objects made of 100-200 vertices that at some point are rendered so far away that one can't see them.

I optimized by simply not rendering them if their area is smaller than one pixel 8)

For the area I consider the screen-projected bounding box of an object. It's a it's fast estimate, but it's not the proper way of doing it.

Let's say that I have a wall made of bricks, with each brick being an individual object with its own bounding box.
This wall could be located so far away that the area of each brick would be smaller than one pixel.
In this case, my assumption/optimization would simply not render any of the bricks.. and the the wall would disappear !

The  proper way of rendering the wall, would be to collect all bricks belonging to a pixel and to use the average of the color of every one of those bricks. This is clearly overkill to do in real-time.

The less accurate but more efficient way of rendering the wall, would be to calculate the bounding boxes' extremities as they would get rasterized. Not simply taking the area of a brick's bounding box, but taking the area of the bounding box whose extremities are snapped to the pixel grid at fixed integer intervals.
When snapping those bounding boxes, some would end up collapsing on the same edge giving a 0 area, while some others would snap at opposite edges and conquer a whole pixel worth of screen real-estate.

..now, Imagine if those bricks were actually texels from a texture map. In that case one would obviously use precalculated mip-maps. Very easy, because of the regular nature of texture maps: samples distributed at regular intervals, unlike for polygonal geometry, where samples (vertices) are distributed irregularly in space.

..that's when one starts realizing that rasterizing 2D polygons at every frame doesn't have much of a future.

zzzzzzzzzzzzzzzzzzzzzzzzz

P.S. I wanted to draw something, but it's very hard to draw things by hand, let alone using a computer  ...Sometimes I really miss Deluxe Paint !!