三角光栅化问题

时间:2012-12-17 07:04:51

标签: geometry polygon fill convention rasterizing

我正在尝试编写几个函数来绘制(填充)平顶和平底三角形。对于它们工作的大部分,然而有时在相邻三角形之间看到裂缝,这些三角形共有一侧。我使用插值技术,逐行栅格化三角形,计算每一步的新左右限制。其中大部分都在代码中进行了解释,但这是一般性的想法(对于平底):

  

1)找到dy(身高)

     

2)从顶点到每个底点找到dy / dx(斜率)

     

3)移动到起始行(floor(顶点)),找到初始x-start和x-end坐标

     

4)移至下一行,计算新的开始和结束限制......

我应该注意,只有在两侧连接的三角形之间才能看到裂缝,而不是顶部/底部连接。我已经这么久了,我不知道该怎么办了。我认为逻辑是可靠的,也许任何裂缝都是由于浮点错误造成的。我真的很感激一些反馈。

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()

1 个答案:

答案 0 :(得分:1)

浮动错误似乎很可能。您需要确保最终两个相邻边的值相同。当您以这种方式累积时,使用浮点值修复这可能有点棘手。根据您的需要,您有几种选择:

  • 改为使用固定点。这也可能会更快。
  • 如果过度抽取不是问题,您可以简单地为浮动值添加足够大的epsilon。