变换 Direct3D 网格

发布于 2024-07-09 18:24:47 字数 425 浏览 8 评论 0原文

我尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱影子 2024-07-16 18:24:48

如果没有围绕您正在做的事情的上下文代码,可能很难知道确切的问题。 网格是如何创建的? 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.

静待花开 2024-07-16 18:24:48

我完全同意 Coincoin,上下文代码会有所帮助。
如果您只想将变换后的网格绘制到屏幕上,则不需要以这种方式变换网格。 您只需更改世界矩阵、视图矩阵和投影矩阵之一。 这会产生预期的结果。 就像下面的示例代码一样。

      // Clear the backbuffer to a Blue color.
  device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue,   
     1.0f, 0);

  // Begin the scene.
  device.BeginScene();

  device.Lights[0].Enabled = true;

  // Setup the world, view, and projection matrices.
  Matrix m = new Matrix();

  if( destination.Y != 0 )
     y += DXUtil.Timer(DirectXTimer.GetElapsedTime) * (destination.Y 
          * 25);

  if( destination.X != 0 )
     x += DXUtil.Timer(DirectXTimer.GetElapsedTime) * (destination.X 
          * 25);

  m = Matrix.RotationY(y);
  m *= Matrix.RotationX(x);

  device.Transform.World = m;
  device.Transform.View = Matrix.LookAtLH(
      new Vector3( 0.0f, 3.0f,-5.0f ),
      new Vector3( 0.0f, 0.0f, 0.0f ),
      new Vector3( 0.0f, 1.0f, 0.0f ) );   
  device.Transform.Projection = Matrix.PerspectiveFovLH(
      (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );

  // Render the teapot.
  teapot.DrawSubset(0);

  // End the scene.
  device.EndScene();

此示例取自此处

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.

      // Clear the backbuffer to a Blue color.
  device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue,   
     1.0f, 0);

  // Begin the scene.
  device.BeginScene();

  device.Lights[0].Enabled = true;

  // Setup the world, view, and projection matrices.
  Matrix m = new Matrix();

  if( destination.Y != 0 )
     y += DXUtil.Timer(DirectXTimer.GetElapsedTime) * (destination.Y 
          * 25);

  if( destination.X != 0 )
     x += DXUtil.Timer(DirectXTimer.GetElapsedTime) * (destination.X 
          * 25);

  m = Matrix.RotationY(y);
  m *= Matrix.RotationX(x);

  device.Transform.World = m;
  device.Transform.View = Matrix.LookAtLH(
      new Vector3( 0.0f, 3.0f,-5.0f ),
      new Vector3( 0.0f, 0.0f, 0.0f ),
      new Vector3( 0.0f, 1.0f, 0.0f ) );   
  device.Transform.Projection = Matrix.PerspectiveFovLH(
      (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );

  // Render the teapot.
  teapot.DrawSubset(0);

  // End the scene.
  device.EndScene();

This sample is taken from here.

笑咖 2024-07-16 18:24:48

我建议使用函数 D3DXConcatenateMeshes。 通过一个网格和一个矩阵。 结果将被转换为网格。 这很容易。

I recommend to use function D3DXConcatenateMeshes. Pass one mesh and one matrix. Result will be transformed mesh. It's quite easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文