Transform heightmap to Static Mesh

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Menyueru
Posts: 17
Joined: Sun Jun 09, 2013 11:28 pm

Transform heightmap to Static Mesh

Post by Menyueru »

I've been using a model up till now to create the terrain, but as i've seen that can get troublesome, so i've decided to use a heightmap to generate the map similar to the multitextured one from Riemers, here are the properties i'm using for the construction of the heightmap

Code: Select all

 
        public struct VertexMultitextured
        {
            public Vector3 Position;
            public Vector3 Normal;
            public Vector4 TextureCoordinate;
            public Vector4 TexWeights;

            public static int SizeInBytes = (3 + 3 + 4 + 4) * sizeof(float);

            public static VertexElement[]
                VertexElements = new[]
                                     {
                                         new VertexElement(0, VertexElementFormat.Vector3,
                                                           VertexElementUsage.Position, 0),
                                         new VertexElement(sizeof (float)*3,
                                                           VertexElementFormat.Vector3,
                                                           VertexElementUsage.TextureCoordinate, 0),
                                         new VertexElement(sizeof (float)*6,
                                                           VertexElementFormat.Vector4,
                                                           VertexElementUsage.TextureCoordinate, 1),
                                         new VertexElement(sizeof (float)*10,
                                                           VertexElementFormat.Vector4,
                                                           VertexElementUsage.TextureCoordinate, 2),
                                     };
        }


        public int Width { get; private set; }
        public int Height { get; private set; }

        private Effect effect;
        private Texture2D bitmap;
        private Texture2D[] textures;
        private string asset;
        private string[] textureAssets;

        private float[,] data;
        private int minheight;
        private int maxheight;
        private VertexMultitextured[] vertices;
        private int[] indices;
        private VertexDeclaration vertexDeclaration;

        private Matrix world;
And i want to use a StaticMesh because i'm planning to have drastic changes in height. Basically i want to know how can i construct this static mesh?? and i'm using xna draw methods instead of BepuphysicsDrawer
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Transform heightmap to Static Mesh

Post by Norbo »

A StaticMesh is only interested in the position of each vertex and the indices which specify the triangles. So, if you've got the set of VertexMultitextured that compose the model, you can create a parallel set of Vector3s which are then given to the StaticMesh along with the indices in the same way that the StaticMeshDemo works.

If memory is a pressing concern, you could also modify the StaticMesh to accept the VertexMultitextured array directly, though this is generally not necessary.
Menyueru
Posts: 17
Joined: Sun Jun 09, 2013 11:28 pm

Re: Transform heightmap to Static Mesh

Post by Menyueru »

I am taking the positions and saving them in a temporary Vector3 arrray like this:

Code: Select all

Vector3[] verts = new Vector3[vertices.Length];
for (int i = 0; i < verts.Length; i++)
{
    verts[i] = vertices[i].Position;
}
But the problem is with the affine transform which i don't know what to do with.

Edit: i fixed it now all i had to do was after i created the world matrix start the Static Mesh this way

Code: Select all

mesh = new StaticMesh(verts, indices,new AffineTransform(world.Translation));
Thanks for the help Norbo :)
Post Reply