2017-02-08 90 views
0

Zig-Zag Traversal
锯齿形三角形遍历实现

我试图按照"On the Hardware Implementation of Triangle Traversal Algorithms for Graphics Processing" (Royer, Ituero, Lopez-Vallejo, & Barrio)(第4页)来实现锯齿形遍历算法三角triversal /光栅化。但是,论文中的解释对我来说是违反直觉的,我无法使其工作。

我试图实现一个有限状态机,但我无法弄清楚确切的状态。现在,我有(方向,e_1,e_2,e_3)其中e_n表示每个边缘的边缘测试输出。伪代码:

if (right, true, true, true): 
    x++; // Move towards right 
else if (left, true, true, true): 
    x--; // Move towards left 
else: 
    // This is where I stuck. There should be two cases where in one of them 
    // y goes down and x doesn't change direction, in the other case x simply 
    // flips its direction. But I wasn't able to figure it out. 

任何帮助,将不胜感激!

编辑:我的努力到目前为止: 虽然边缘测试工作正常,但只有少部分图形被光栅化。

/// 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--; 
    } 
} 

回答

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 ; 
+1

我试图实现这个,似乎它适用于常见的情况。然而,对于一些边缘情况,例如想象两个垂直堆叠的像素,例如$ p_1 $,$ p_2 $,并且三角形非常细,以至于它的两条边通过$ x_1 + 0.5,y_1 + 0.5 $和$ x_2 + 0.5,y_2 + 0.5 $。两个像素的边缘测试都会失败,并且当(x,y)从$ p_1 $到$ p_2 $时,状态_1被触发并且方向反转,可能导致x继续增加它自己与三角形的距离。我如何处理这个问题? –