Domanda

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 which share a side. 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) Move to next line, calculate new start and end limits...

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.

typedef unsigned int uint32;

void Line(uint32 y, uint32 x_left, uint32 x_right);  //Draws a horizontal line

struct 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 initial 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()
È stato utile?

Soluzione

Float errors seem probable. You need to make sure that you end up with the same value for two adjacent edges. This can be a bit tricky to fix with float values when you are accumulating in this manner. You have a couple of options depending on your needs:

  • Use fixed point instead. This would also likely be faster.
  • If overdraw isn't an issue, you can simply add a large enough epsilon to your float values.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top