Archived

This topic is now archived and is closed to further replies.

langguy

Mesh Translation in C#

Recommended Posts

I have been looking into this for two days now and has become very elusive: How can I translate/scale/rotate a mesh after its been loaded without moving the camera in C# (DX9)? I''m pretty sure there isn''t a magical function like mesh.Translate(x, y, z);. I saw in a post on here some code for C++ that kinda did what I am looking for: locked the vertex buffer (one of the parameters was a pointer to the array of vertices in the buffer) translated each vertex individually: eg, vert->X += 1; vert->Y += 2; vert->Z += 3; unlocked the vertex buffer Of course, this is psuedo code, I can''t remember exactly word for word what the code was. I know that one of the members of the Mesh class is a VertexBuffer, so I am hoping I can do something similar to this in C#. If not, how can I otherwise translate a mesh without moving the camera?

Share this post


Link to post
Share on other sites
Well, if you want instance based transformations, then you could simply do:

(My matrix multiplication and order of transformations may be out of order, but you''ll be able to tell if that is the case real quick)

Matrix m = Matrix.Translate(x,y,z);
m = Matrix.Multiply(m, Matrix.RotateYawPitchRoll(y, p, r));
m = Matrix.Multiply(m, Matrix.Scale(xfactor, yfactor, zfactor));
Device.SetTransform(TransformType.World, m);
DrawMesh();
Device.SetTransform(TransformType.World, Matrix.Identity);

Otherwise, I think you need to call ProcessVertices using the mesh''s vb, and use the matrix above).

This code aint gauranteed to work out of the box, it''s too early in the morning. Look on MSDN for more C++ examples, which can be ported to C# lickety split.

Share this post


Link to post
Share on other sites
Thanks man...the first part works just fine. I tried the second option first cause it sounded easier [it wasn''t, and it had nothing to do with moving vertices.] The only thing I''m concerned about is performance issues:

What if you were to render a traffic jam with 1000 cars involved, all moving roughly 5MPH at the same time. Does transforming the world 1000+ times per frame slow down the game, or is this something I shouldn''t worry about?

Share this post


Link to post
Share on other sites