帮助理解这个?

时间:2010-08-21 15:11:03

标签: c++ c algorithm opengl

我在这里找到了这个算法,只有一件事让我感到困惑:

    Clear the stencil buffer to 1.
    Pick an arbitrary vertex v0, probably somewhere near the polygon to reduce floating-point errors.
    For each vertex v[i] of the polygon in clockwise order:
        let s be the segment v[i]->v[i+1] (where i+1 will wrap to 0 when the last vertex is reached)
        if v0 is to the "right" of s:
            draw a triangle defined by s, v[i], v[i+1] that adds 1 to the stencil buffer
        else
            draw a triangle defined by s, v[i], v[i+1] that subtracts 1 from the stencil buffer
    end for
    fill the screen with the desired color/texture, testing for stencil buffer values >= 2.

By "right of s" I mean from the perspective of someone standing on v[i] and facing v[i+1]. This can be tested by using a cross product:

cross(v0 - v[i], v[i+1] - v[i]) > 0

让我困惑的部分是我需要绘制由S,V [i],V [i + 1]定义的三角形。如果S是段v [i] - > v [i + 1]那么这怎么可能?

由于

3 个答案:

答案 0 :(得分:1)

如果我没记错的话,你必须画的三角形是v0 - v [i] - v [i + 1]

答案 1 :(得分:0)

如果我正确地读了你的例子,s是两个顶点之间的一个段,它是三角形的边缘。所以它以顺时针方式“绕”三角形“填充”顶点。

“绕组” - 顺时针或逆时针 - 决定三角形的法线。

答案 2 :(得分:0)

是的,它看起来像是一个错误,因为s是由 v [i] v [i + 1] 定义的段。这在绘制凹多边形的背景下是有意义的。使用 v0,v [i],v [i + 1]