我试图按照"On the Hardware Implementation of Triangle Traversal Algorithms for Graphics Processing" (Royer, Ituero, Lopez-Vallejo, & Barrio)(第4页)实现三角形三角/光栅化的Zig-Zag遍历算法。然而,论文中的解释对我来说是违反直觉的,我无法使其发挥作用。
我试图实现一个有限状态机,但我无法弄清楚确切的状态。现在,我有(方向,e_1,e_2,e_3),其中e_n表示每个边缘的边缘测试输出。伪代码:
/// Zig Zag (not working)
int top_row = floor(fmin(y0, fmin(y1, y2)));
int bot_row = floor(fmax(y0, fmax(y1, y2)));
if (y0 > y1) {
swap(x0, x1); swap(y0, y1);
}
if (y0 > y2) {
swap(x0, x2); swap(y0, y2);
}
if (y1 > y2) {
swap(x1, x2); swap(y1, y2);
}
assert(top_row == floor(y0));
assert(bot_row == floor(y2));
bool direction = true;
bool changed = false;
int x = floor(x0); int y = floor(y0);
while (y <= bot_row) {
bool e1, e2, e3;
e1 = edge_test((float)x+0.5, (float)y+0.5, x0, y0, x1, y1) < 0.0f;
e2 = edge_test((float)x+0.5, (float)y+0.5, x1, y1, x2, y2) < 0.0f;
e3 = edge_test((float)x+0.5, (float)y+0.5, x2, y2, x0, y0) < 0.0f;
if ((e1 == e2) && (e2 == e3)) {
if ( x < 0 || x >= width ) continue;
if ( y < 0 || y >= height ) continue;
samplebuffer[y][x].fill_pixel(color);
if (direction) x++;
else x--;
} else if (changed) {
y++;
changed = false;
} else {
direction = !direction;
changed = true;
if (direction) x++;
else x--;
}
}
任何帮助将不胜感激!
编辑:到目前为止我的努力: 边缘测试正常工作时,图表中只有少数部分被栅格化。
Run > External Tools > External Tools Configurations ...
答案 0 :(得分:0)
我如何看到状态机:
Dir = +1 / -1
State 0: (moving inside)
EdgeTest:
0 => State 1 ; y++
1 => State 0 ; x = x + Dir
State 1: (outside)
EdgeTest:
0 => State 3 ; Dir = - Dir
1 => State 2 ;
State 2: (inside moving the same dir)
EdgeTest:
0 => State 3 ; Dir = - Dir
1 => State 2 ; x= x + Dir
State 3: (outside)
EdgeTest:
0 => State 3 ; x = x + Dir
1 => State 0 ;