views:

1070

answers:

3

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);
}
+2  A: 

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
+1  A: 

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.

avp
A: 

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

Blackfighter