计算法线并将其应用到三角形网格
我有一个三角形网格,我正在尝试计算法线,以便在绘制网格时应用它们。我正在使用即时模式(当我有时间了解它们如何工作时,可能会更改为顶点数组)并使用 GL_TRIANGLE_STRIP 绘制网格。
我在计算顶点法线时遇到问题。更准确地说,决定在计算中使用哪些相邻顶点,然后决定何时设置这些法线。考虑一下:
1_2
|/| Supposedly a square where the numbers represent the vertex number in a
3 4 triangle strip.
我知道你必须计算属于一个平面的 2 个向量的叉积才能获得平面法线。因此,在该示例中,顶部三角形的法线可以通过执行 (2-1)x(3-1) 来计算,第二个三角形的法线可以通过执行 (2-4)x(3-4) 来计算。在立即模式下绘制三角形带时如何应用法线?
我所做的是在设置 vtx 1 时设置第一个法线,在设置 vtx 4 时设置第二个法线,在设置 vtx 5 时设置第三个法线,等等。然而,这会带来问题,因为显然最终每个法线都有不同的法线三角形的顶点(当它们应该全部相同时)。例如,三角形|2,3,4|只会有具有正确法线的顶点 4(因为对于顶点 2 和 3,法线将是第一个三角形的法线)。
那么应该怎么做呢?有没有办法,或者我需要更改为GL_TRIANGLES
? (我现在不想停止使用立即模式,因为我没有时间)。
I have a triangle mesh and I'm trying to calculate the normals so I can apply them when drawing the mesh. I'm using immediate mode (will probably change to vertex arrays when I get time to understand how they work) and drawing the mesh with GL_TRIANGLE_STRIP.
I am having trouble calculating the vertex normals. More precisely deciding which neighbouring vertices to use in the calculations and then deciding when to set those normals. Consider this:
1_2
|/| Supposedly a square where the numbers represent the vertex number in a
3 4 triangle strip.
I know you have to compute the cross product of 2 vectors belonging to a plane in order to get the plane normal. So in that example the top triangle's normal could be calculated by doing (2-1)x(3-1), and the second one by doing (2-4)x(3-4). How do you then apply the normals when drawing the triangle strip in immediate mode?
What I was doing was setting the first normal when vtx 1 is set, the second when vtx 4 is set, the third when vtx 5 is set, etc. This however gives issues as you obviously end up by having different normals for each of the vertices of a triangle (when they should all be the same). For instance, triangle |2,3,4| would only have vertex 4 with the correct normal (since for vertices 2 and 3 the normal would be the one of the first triangle).
So how should it be done? Is there a way, or do I need to change to GL_TRIANGLES
? (I don't want to stop using immediate mode for now as I don't have time).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我是正确的,你仍然只计算每个三角形的法线?这是正确的,但之后您应该计算每个顶点的法线。这只是特定顶点所附加的所有三角形法线的归一化总和。
完成后,您可以继续进行即时模式绘图,指定每个顶点的法线。
If I'm correct you're still only computing a normal per triangle? This is correct, but after that you should computed what the normal is per vertex. This is simply the normalized sum of all triangle normals that the specific vertex is attached to.
Once completed you can proceed with your immediate mode drawing, specifying a normal per vertex.