简体   繁体   English

如何在x或y轴上反射顶点后恢复CCW缠绕顺序?

[英]How to restore CCW winding order after reflecting vertices in the x or y axis?

I'm rendering a polygon in OpenGL with a vertex array called vertices and a final index buffer called DRAW_ORDER with CCW winding. 我在OpenGL中渲染一个多边形,其顶点数组称为verticesfinal索引缓冲区称为DRAW_ORDER带有CCW绕组。 I have back-face culling enabled, and I make draw calls using glDrawElements(GL_TRIANGLES, DRAW_ORDER.capacity(), GL_UNSIGNED_SHORT, DRAW_ORDER) . 我启用了背面剔除,并使用glDrawElements(GL_TRIANGLES, DRAW_ORDER.capacity(), GL_UNSIGNED_SHORT, DRAW_ORDER)进行绘制调用。

When I reflect vertices in the x or y axis via matrix transformation, the polygon gets culled because the reflection reverses the orientation of the vertices, so that they no longer match the CCW winding order of DRAW_ORDER . 当我通过矩阵变换反映x或y轴上的vertices时,多边形会被剔除,因为反射会反转顶点的方向,因此它们不再与DRAW_ORDER的CCW缠绕顺序相DRAW_ORDER

I can prevent the problem by disabling culling, but for performance sake I would rather find a way to restore the orientation of vertices via permutation. 我可以通过禁用剔除来防止这个问题,但出于性能考虑,我宁愿找到一种方法来通过置换来恢复vertices的方向。 For example, if the polygon were a triangle, I could simply swap the second and third vertices after a reflection to restore CCW orientation. 例如,如果多边形是三角形,我可以简单地在反射后交换第二个和第三个顶点以恢复CCW方向。 How can I extend this approach to a polygon with an arbitrary number of vertices and indices? 如何将此方法扩展到具有任意数量的顶点和索引的多边形?

  //PSEUDO-CODE FOR TRIANGLE:
    final DRAW_ORDER = {0,1,2};
    vertices = { {0,0}, {1,0}, {0,1} };
    reflect(vertices);
    swap(vertices,1,2);

EDIT: Here's a solution that seems to work for convex polygons, but not concave. 编辑:这是一个似乎适用于凸多边形但不是凹面的解决方案。

    //Reverse the order of the vertices so, for example, 
    //vertices {v1,v2,v3,v4,v5} become {v5,v4,v3,v2,v1}
    for(int start = 0, end = vertices.length-1; start<end; start++, end--){
        swap(vertices,start,end);
    }

You can see in the image below how the solution works for an ellipse (which is convex) but not a star (which is concave). 您可以在下图中看到解决方案如何适用于椭圆(凸出)但不是星形(凹入)。

在此输入图像描述

To invert the winding order by a permutation of the indices, you can simply reverse the order of the indices. 要通过索引的排列来反转绕组顺序,您可以简单地反转索引的顺序。

So for your triangle example, if the original order is (0, 1, 2), the reverse order is (2, 1, 0). 因此,对于您的三角形示例,如果原始顺序为(0,1,2),则反向顺序为(2,1,0)。 Since all cyclic permutations are equivalent for defining a polygon, other valid orders would be (1, 0, 2) and (0, 2, 1). 由于所有循环排列对于定义多边形都是等效的,因此其他有效顺序将是(1,0,2)和(0,2,1)。 But using the reverse is as good as anything. 但使用反向就像任何东西一样好。

As another example, for a polygon with 5 vertices, using indices (3, 17, 6, 8, 11), the reverse is (11, 8, 6, 17, 3), which can be used as the set of indices when you want to render the polygon with the opposite winding order. 作为另一个例子,对于具有5个顶点的多边形,使用索引(3,17,6,8,11),反之为(11,8,6,17,3),当可以用作索引集时您想要以相反的缠绕顺序渲染多边形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM