OpenGL计算给定点列表的正确法线
对于这个问题,我有:a)有序的点列表b)组成每个多边形的点的列表
例如
point1 = (1, 2, 2)
point2 = (1, 2, 3)
point3 = (1, 3, 3)
polygon1 = [ point1, point2, point3 ]
=> Polygon1 是一个三角形,可以是模型外部的一部分。
我需要计算 OpenGL 中光照的正确法线。
我知道我可以根据给定的点计算平面上的向量,并进行叉积以获得垂直于平面的A,但是OpenGL要求法线指向正确的方向(即向外) 。
该过程需要自动化,因为我有很多多边形。
如果我在平面上选择两个向量,我无法确定如何确定法线已计算出正确方向的照明点(即位于模型的外部)。
我想我可以将计算出的法线添加到平面上的一个点,看看它是否离原点更远,看看它是否正确,但是模型非常复杂,一些法线可能需要指向原点(ish)
如果有帮助,结果是所有多边形都是三角形(虽然不明白它有什么帮助,但应该能够任何多边形的抽象)
For the problem I have: a) An ordered list of points b) A list of points that make up each polygon
For example
point1 = (1, 2, 2)
point2 = (1, 2, 3)
point3 = (1, 3, 3)
polygon1 = [ point1, point2, point3 ]
=> polygon1 is a triangle which could be part of the outside of the model.
I need to calculate the correct normals for lighting in OpenGL.
I know that I can calculate vectors on the plane from the points given, and cross-product to get A normal to the plane, but OpenGL requires the normals to be pointing in the correct direction (ie outward).
The process needs to be automated because I have LOTS of polygons.
If I pick two vectors on the plane I can't tell how I can be sure the normal I have calculated points in the correct direction for lighting (ie is on the outside of the model.)
I figured I could add the calculated normal to a point on the plane and see if its further from the origin to see if its correct, but the model is quite complex and some normals may need to point towards the origin(ish)
If it helps, it turns out all the polygons are triangles (dont see how it helps though, should be able to abstract for any polygon)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题通常称为“歧管表面方向”。在封闭流形的情况下,它可以明确地解决,在开放但可定向流形的情况下,您必须手动确定补片的哪一侧是“外部”。
然而,有一些流形,例如克莱因瓶或莫比斯带,问题无法解决(请注意,如果您在莫比斯带上循环两次,您实际上可以定向表面,如果您假设只能看到一侧)。
好的,关于你的三角汤问题:这通常是使用所谓的半边来解决的。即,对于每个三角形,您都会构建组成它们的顶点列表。这为每个三角形提供了 3 个有向半边。现在,对于每对顶点 v1、v2,您创建一个连接它们的边列表(您应该使用顶点 ID 对作为哈希映射的键,其中 key((v1, v2)) == key((v2, v1) ),最简单的方法是对它们进行排序)。对于每一对这样的顶点,您应该只找到一条半边,或者两条反平行的半边。如果半边超过 2 个,则该表面不可定向。如果半边平行,则属于半边之一的三角形的方向必须翻转。选择一个起始三角形,并构建一棵连接三角形的树,然后将这些三角形构建到下一个三角形,依此类推。在树中为每个三角形存储一个翻转计数器。如果计数器为奇数,则树下方的所有三角形也将被视为翻转。任何三角形都不能使其显式翻转计数器递增超过 1。合并分支的累积翻转计数在合并点必须是偶数。
Your problem is usually called "manifold surface orientation". In the case of closed manifolds it can solved unambigously, in the case of open, but orientable manifolds you must manually decide which side of the patch is "outside".
However there are some manifolds, like Klein bottles or Moebius strips for which the problem cannot be solved (note that if you cycle two times over the Moebius strip you actually can orient the surface, if you assume that you can see only one side).
Okay, regarding your triangle soup problem: This is usually solved using so called half edges. I.e. for every triangle you build the list of vertices that make them up. This gives you 3 directed half-edges for each triangle. Now for each pair of vertices v1, v2 you make a list of edges connecting them (you should use the vertex ID pair as key into a hash map, where key((v1, v2)) == key((v2, v1)), most simply done by sorting them). For each such pair of vertices you should find either only one half edge, or two antiparallel half-edges. If there are more than 2 half-edges then the surface is not orientable. If the half-edges are parallel, the orientation of the triangle that belongs to one of the half-edges must be flipped. Choose one starting triangle, and build a tree of connected triangles, then of those triangles to the next and so on. In the tree store for each triangle a flip counter. If the counter is odd all triangles further down the tree are to be considered flipped as well. No triangle must have it's explicit flip counter being incremented beyond 1. Cumulative flipcounts of merging branches must be even at the merge point.
通常顺时针方向定义的点对应于物体的外表面。所以正常的计算是有用的。
Usually points defined in clockwise direction correspond to the outer surface of the object. So normal calculations will be useful.