DirectX 中顶点法线的计算
作为学习经验,我正在编写一个立即模式管理的 DirectX 9 应用程序。
我手动计算场景中所有三角形的顶点法线,以实现平滑的 Gouraud 着色。
这按预期工作,但我猜这不是最有效的方法。 是否可以让 GPU 帮我做这件事?
As a learning experience, I'm writing an Immediate mode managed DirectX 9 application.
I'm manually calculating Vertex normals across all triangles in a scene to allow smooth Gouraud shading.
This works as expected, but I'm guessing this is not the most efficient approach. Is it possible to get the GPU to do this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上,您可以在顶点着色器内生成顶点法线。 不过,每次使用该着色器渲染网格时都需要进行计算,所以为什么不提前生成它们呢。
如果你的意思是你想在渲染之前生成它们,但使用 GPU 而不是 CPU,我会说,不值得为你只要做一次的事情而费心加速。 此外,我不确定 DX9 是否有办法从着色器获取计算出的顶点信息(DX10 有)。
总而言之,在大多数情况下最好的做法是传统的:在保存包含网格的数据文件的程序中计算顶点法线 - 将其作为预计算步骤进行。 通常,如果网格来自 Max 或 Maya 等 3D 包,则您会拥有它们,因为法线中存在艺术信息,除非您知道整个网格应该完全平滑(或有面),否则在一般情况下它是不可计算的。
You could in theory generate the vertex normals inside the vertex shader. That involves computation every single time you render a mesh using that shader though, so why not generate them in advance.
If you mean you want to generate them in advance of rendering, but use the GPU instead of the CPU, I would say that it's not worth the bother of speeding up something you are only going to do once. Besides, I'm not sure if DX9 has a way to get computed vertex information back from a shader (DX10 does).
All in all, the best thing to do in most cases is the traditional: compute vertex normals in the program that saves the data files that contain the meshes - do it as a pre-computation step. Usually you have them if the mesh came from a 3d package like Max or Maya, because there is artistic information in the normals, unless you know the whole mesh is supposed to be perfectly smooth (or faceted), it's not computable in the general case.