我正在尝试编写几个函数来绘制(填充)平顶和平底三角形。它们在很大程度上起作用,但有时在共享一侧的相邻三角形之间仍会出现裂缝。我使用插值技术逐行光栅化三角形,在每一步计算新的左右限制。其中大部分在代码中进行了解释,但这里是一般的想法(对于平底):
1)找到dy(高度)
2)从顶点到每个底部点找到dy/dx(斜率)
3) 移动到起始行( floor(top point) ),并找到初始 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()