平移多个网格 - HLSL
好吧,如果这有点模糊,请原谅我,但我整夜都在努力赶上一些编码。
我有一个相当大且定义的地形,并进行了最小程度的优化,并且我刚刚开始引入带有材质和纹理的预定义网格(.X 对象)。之前我采用的是固定功能管道方法,因为我最近才开始使用 DirectX9。很明显,FFP 很老派,在 DirectX10 中已弃用,因此我一直在将相关代码转移到使用 HLSL 方法。
在我最初的方法中,我将模型加载到模型的 std::vector 容器中,并创建了另一个对象的 std::vector ,其中包含对要显示的模型的引用。在我的渲染循环中,我将迭代此容器并检查对象是否在相机的视野内。如果是这样,我将首先使用 SetTransform() 调用将网格转换到它们的位置,然后使用 DrawSubset()。
然而,很明显 SetTransform() 不适用于 HLSL 方法;因此,我有点困惑如何将这些网格预先翻译到它们的相关位置,或者我是否应该在顶点着色器中翻译它们。网格体存储在 ID3DXMESH 类型中,似乎我可以访问这些网格体的索引和顶点缓冲区;我是否应该获取这些缓冲区的内容,翻译内容然后绘制它们?还是我这样做真的走错了路?
我熟悉顶点缓冲区方法,但不确定网格本身内的顶点格式是什么。
任何帮助将不胜感激,因为我即将把我的眼球撕下来。
编辑
我会接受 Sergio 的回答,因为它把我推向了正确的方向,尽管当我在调试输出中意识到有一行关于提交更改时,解决方案就出现了。
解决方案
转换网格后,我需要调用 g_pEffect->CommitChanges();
Okay forgive me if this is at all vague, but I've been up all night trying to catch up with some coding.
I have a reasonably large and defined terrain with minimal optimisations in place and I have just started to introduce predefined meshes (.X objects) which come with materials and textures. Previously I was working in a fixed-function-pipeline approach as I have only recently started working with DirectX9. It has become apparent that FFP is old school and deprecated in DirectX10, so I have been moving relevant code to using a HLSL approach.
In my initial approach I had loaded my models into a std::vector container of models and created another std::vector of objects which contain a reference to which model to display. In my render loop, I would iterate this container and check to see if the objects were within the Field-of-view of my camera. If so I would first translate the meshes to their positions, using a SetTransform() call, then DrawSubset().
However it has become clear that SetTransform() is not applicable to the HLSL approach; therefore I'm a little stumped to how I can pre-translate these meshes to their relevant positions, or whether I should be translating them within the vertex shader. The meshes are stored within an ID3DXMESH type and it seems that I can access the Index and Vertex Buffer of these meshes; Am I supposed to take the contents of these buffers, translate the contents then draw them? Or am I really going the wrong way about doing this?
I am familiar with the Vertex Buffer approach, but not sure what the vertex format is within the mesh itself.
Any help would be appreciated as I'm about to tear my eyeballs out.
Edit
I'll accept Sergio's answer as it pushed me in the right direction although the solution came when I realised in my debug output a line about committing changes.
Solution
After transforming my mesh I needed to call
g_pEffect->CommitChanges();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如有人之前所说,您应该在顶点着色器上进行转换,如果您的着色器中有一个 worldViewProjection 矩阵常量,那么您需要将顶点位置乘以该矩阵,并在移动到您的输出之前返回转换后的位置像素着色器。
确保您通过视图和投影传递的世界变换不仅仅是一个身份,因为这根本不会改变顶点。
这是一个关于如何在顶点着色器上实现此目的的示例,本质上是将传入的未变换顶点位置乘以 worldViewProj 矩阵。
As someone said earlier you should be doing the translation on your vertex shader, if you have a worldViewProjection matrix constant within your shader, then you need to multiply the vertex position by that matrix and return the transformed position on your output before you move onto your pixel shader.
Make sure the world transform you are passing in with your view and projection is not just an identity as this wont transform the verts at all.
This is a sample on how you can achieve this on your vertex shader, which is essentially multiplying the incoming untransformed vertex position by the worldViewProj matrix.