**0**

# Triangle rasterization troubles

Started by thewhiteaussie, Dec 16 2012 11:14 PM

3 replies to this topic

###
#1
Members - Reputation: **157**

Posted 16 December 2012 - 11:14 PM

I'm trying to write a couple of functions to draw (filled) flat-top and flat-bottom triangles. For the large part they work however cracks are still sometimes seen between adjacent triangles. I use an interpolating technique whereby I raster the triangle line by line, calculating the new left and right limits at each step. Much of this is explained in the code, but here is the general idea (for a flat bottom):

1) Find dy (height)

2) Find dy/dx (slope) from the top point to each bottom point

3) Move to the starting row ( floor(top point) ), and find initial x-start and x-end coordinates

4) Interpolate down the triangle

I should note that cracks are only seen between triangles that join at the sides, not top/bottom joins. I've been at this so long I don't know what to try anymore. I think the logic is solid, maybe any cracks are due to floating point error. I'd really appreciate some feedback.

[source lang="cpp"]typedef unsigned int uint32;Line(uint32 y, uint32 x_left, uint32 x_right); //Draws a horizontal line at these coordsstruct vector2{ float x,y;};//--------------------------------------------------------------------------------// Draws a flat bottom triangle from top to bottom//--------------------------------------------------------------------------------void Draw_Bottom_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Bottom left: p0 //Bottom right: p1 //Top point: p2 //calculate dy float dy = p2.y - p0.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Since we start the raster process at floor(p2.y) //we need to shift the x start and x end postions along //by this factor: float y_bump = p2.y - floor(p2.y); //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p0.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p2.y); //y top, use casting instead of std::floor //Draw lines for (uint32 i = yt; i >= yb; i--) { //Set left and right limits, use casting instead of std::floor uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can also be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Bottom_Tri_SOLID()//--------------------------------------------------------------------------------// Draws a flat top triangle from bottom to top//--------------------------------------------------------------------------------void Draw_Top_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Top left: p0 //Top right: p1 //Bottom point: p2 //calculate dy (height) float dy = p0.y - p2.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Find shifting factor float y_bump = ceil(p2.y) - p2.y; //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p2.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p0.y) ; //y top //Draw lines for (uint32 i = yb; i <= yt; i++) { //Set left and right limits uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Top_Tri_SOLID()[/source]

1) Find dy (height)

2) Find dy/dx (slope) from the top point to each bottom point

3) Move to the starting row ( floor(top point) ), and find initial x-start and x-end coordinates

4) Interpolate down the triangle

I should note that cracks are only seen between triangles that join at the sides, not top/bottom joins. I've been at this so long I don't know what to try anymore. I think the logic is solid, maybe any cracks are due to floating point error. I'd really appreciate some feedback.

[source lang="cpp"]typedef unsigned int uint32;Line(uint32 y, uint32 x_left, uint32 x_right); //Draws a horizontal line at these coordsstruct vector2{ float x,y;};//--------------------------------------------------------------------------------// Draws a flat bottom triangle from top to bottom//--------------------------------------------------------------------------------void Draw_Bottom_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Bottom left: p0 //Bottom right: p1 //Top point: p2 //calculate dy float dy = p2.y - p0.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Since we start the raster process at floor(p2.y) //we need to shift the x start and x end postions along //by this factor: float y_bump = p2.y - floor(p2.y); //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p0.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p2.y); //y top, use casting instead of std::floor //Draw lines for (uint32 i = yt; i >= yb; i--) { //Set left and right limits, use casting instead of std::floor uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can also be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Bottom_Tri_SOLID()//--------------------------------------------------------------------------------// Draws a flat top triangle from bottom to top//--------------------------------------------------------------------------------void Draw_Top_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2){ //Point order: //Top left: p0 //Top right: p1 //Bottom point: p2 //calculate dy (height) float dy = p0.y - p2.y; //dx/dy for the left and right edges float dxdy_left = (p0.x - p2.x)/dy; float dxdy_right = (p1.x - p2.x)/dy; //Find shifting factor float y_bump = ceil(p2.y) - p2.y; //Initial start and end x values float xs = p2.x + dxdy_left*y_bump; //x left (start) float xe = p2.x + dxdy_right*y_bump; //x right (end) uint32 yb = uint32(p2.y) + 1; //y bottom, +1 for top left fill convention uint32 yt = uint32(p0.y) ; //y top //Draw lines for (uint32 i = yb; i <= yt; i++) { //Set left and right limits uint32 left = uint32(xs) + 1; //+1 for top left fill convention uint32 right = uint32(xe); //Draw line, can be std::fill or simply a for loop. Line(i, left, right); //Increment limits xs += dxdy_left; xe += dxdy_right; }} //End: Draw_Top_Tri_SOLID()[/source]

Sponsor:

###
#2
Crossbones+ - Reputation: **3631**

Posted 17 December 2012 - 10:26 AM

there are quite some possible reasons and they lead to different kind of bugs, if you could show the wireframe and the flatshaded rendering with cracks, it's easier to guess what might go wrong.

one problem might be line 50 and 51, every step you add something to xs/xe, every step after the add there is some rounding, this leads to an increasing error at every step. if you modify it to something like xs=y*dxdy_left+bs; you might already decrease the amount of error.

is that all the code? seems like it's just the top part of the triangle, going on in the 2nd part of the triangle with the wrong data is a common bug that leads to cracks.

I suggest you to check out http://devmaster.net/forums/topic/1145-advanced-rasterization/

it describes how to draw leak free solid triangles with a very simple algorithm, might not be as fast as yours, but it's good to have a working reference, right?

one problem might be line 50 and 51, every step you add something to xs/xe, every step after the add there is some rounding, this leads to an increasing error at every step. if you modify it to something like xs=y*dxdy_left+bs; you might already decrease the amount of error.

is that all the code? seems like it's just the top part of the triangle, going on in the 2nd part of the triangle with the wrong data is a common bug that leads to cracks.

I suggest you to check out http://devmaster.net/forums/topic/1145-advanced-rasterization/

it describes how to draw leak free solid triangles with a very simple algorithm, might not be as fast as yours, but it's good to have a working reference, right?

###
#3
Members - Reputation: **559**

Posted 19 December 2012 - 06:09 PM

Typically the way to fix this is to quantize your screen-space vertices to some fixed grid. This grid can be finer than the size of a pixel. Then when creating your interpolants you have a finer resolution so can you step using a finer resolution than this quantized grid. If done correctly then the scanline interpolation should never under or over shoot the endpoints. I use fixed point math to store screen space positions and interpolants, though you can use fp math when calculating intermediate values.

###
#4
Members - Reputation: **157**

Posted 04 January 2013 - 10:51 PM

Thanks guys. After much experimentation, the problem was with rounding errors. I have since switch to using a fixed point number representation for greater accuracy and have solved the issue, pretty much as AB suggested.

Also, Krypt0n you migh be right about the error introduced by incrementing xs and xe each iteration, I'll check it out.