计算DirectX中的顶点法线

时间:2022-09-10 21:23:53

As a learning experience, I'm writing an Immediate mode managed DirectX 9 application.

作为一种学习经历,我正在编写一个即时模式管理的DirectX 9应用程序。

I'm manually calculating Vertex normals across all triangles in a scene to allow smooth Gouraud shading.

我正在手动计算场景中所有三角形的顶点法线以允许平滑的Gouraud着色。

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?

这可以按预期工作,但我猜这不是最有效的方法。有可能让GPU为我做这个吗?

2 个解决方案

#1


Check this and this.

检查这个和这个。

#2


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).

如果你的意思是你想在渲染之前生成它们,但是使用GPU而不是CPU,我会说加速你只会做一次的事情是不值得的。此外,我不确定DX9是否有办法从着色器返回计算出的顶点信息(DX10)。

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.

总而言之,在大多数情况下最好的做法是传统的:在程序中计算顶点法线,以保存包含网格的数据文件 - 将其作为预计算步骤。通常你有网格来自像Max或Maya这样的3d包,因为法线中有艺术信息,除非你知道整个网格应该是完全平滑的(或刻面的),在一般情况下它是不可计算的。

#1


Check this and this.

检查这个和这个。

#2


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).

如果你的意思是你想在渲染之前生成它们,但是使用GPU而不是CPU,我会说加速你只会做一次的事情是不值得的。此外,我不确定DX9是否有办法从着色器返回计算出的顶点信息(DX10)。

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.

总而言之,在大多数情况下最好的做法是传统的:在程序中计算顶点法线,以保存包含网格的数据文件 - 将其作为预计算步骤。通常你有网格来自像Max或Maya这样的3d包,因为法线中有艺术信息,除非你知道整个网格应该是完全平滑的(或刻面的),在一般情况下它是不可计算的。