变换 Direct3D 网格
我尝试编写一个 TransformMesh 函数。 该函数接受一个 Mesh
对象和一个 Matrix
对象。 这个想法是使用矩阵来变换网格。 为此,我锁定了顶点缓冲区,并在每个顶点上调用 Vector3::TransformCooperative。 它没有产生预期的结果。 生成的网格无法识别。
我究竟做错了什么?
// C++/CLI code. My apologies.
int n = verts->Length;
for(int i = 0; i < n; i++){
verts[i].Position = DX::Vector3::TransformCoordinate(verts[i].Position, matrix);
}
I tried to write a TransformMesh
function. The function accepts a Mesh
object and a Matrix
object. The idea is to transform the mesh using the matrix. To do this, I locked the vertex buffer, and called Vector3::TransformCoordinate on each vertex. It did not produce expected results. The resulting mesh was unrecognizable.
What am I doing wrong?
// C++/CLI code. My apologies.
int n = verts->Length;
for(int i = 0; i < n; i++){
verts[i].Position = DX::Vector3::TransformCoordinate(verts[i].Position, matrix);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有围绕您正在做的事情的上下文代码,可能很难知道确切的问题。 网格是如何创建的? verts[]怎么读,怎么写? 您是否尝试从只写顶点缓冲区中读取?
我的建议是首先尝试使用一个非常简单的平移矩阵,然后调试代码并查看顶点输入和输出。 查看您是否收到了良好的数据以及数据是否已正确转换。 如果是这样,则问题出在顶点步幅、流声明或 DirectX 管道中更深层次的其他内容中。
正如我所说,需要更多代码来查明问题的根源。
Without contextual code around what you are doing, it might be hard to know the exact problem. How is the mesh created? How is verts[] read, how is it written? Are you trying to read from a write only vertex buffer?
My recommendation would be to try with a very simple translation matrix first and debug the code and see the vertex input and output. See if you receive good data and if it's transformed correctly. If so, the problem is in vertex stride, stream declaration or something else deeper in the DirectX pipeline.
As I said, more code would be needed to pinpoint the origin of the problem.
我完全同意 Coincoin,上下文代码会有所帮助。
如果您只想将变换后的网格绘制到屏幕上,则不需要以这种方式变换网格。 您只需更改世界矩阵、视图矩阵和投影矩阵之一。 这会产生预期的结果。 就像下面的示例代码一样。
此示例取自此处。
I totally agree with Coincoin, contextual code would help.
And if you want just to draw transformed mesh to the screen, you don't need to transform the mesh in this way. You can just change one of world, view, and projection matrices. This produces expected result. Like in the following sample code.
This sample is taken from here.
我建议使用函数 D3DXConcatenateMeshes。 通过一个网格和一个矩阵。 结果将被转换为网格。 这很容易。
I recommend to use function D3DXConcatenateMeshes. Pass one mesh and one matrix. Result will be transformed mesh. It's quite easy.