Sign in to follow this  

C# gathering position and normal data only from .X file

Recommended Posts

Hello everyone, Just wanted to post some code regarding gathering only the position and normal data from an .X file becuase this is a question I had here, but figured out how to do this. I originally thought I was going to need to write my own .X parser specificaly for this job, but turns out its not needed. I'm using C# with MDX 1.1 So I basically load up an .X file as a mesh object and then gather the data I need from that mesh object. I still need to refactor the code a little bit, but it does what i need it to.

// Get the base mesh position & normal information
        private CustomVertex.PositionNormal[] meshVertices;

        // Position holder
        private float[] x;
        private float[] y;
        private float[] z;

        // Normals holder
        private float[] nx;
        private float[] ny;
        private float[] nz;

private void CopyData()
            int[] numberVertices = new int[1];
            numberVertices[0] = mesh.NumberVertices;
            Array getPoints;
            CustomVertex.PositionNormal pointMover = new CustomVertex.PositionNormal();

            meshVertices = new CustomVertex.PositionNormal[mesh.NumberVertices];
            x = new float[mesh.NumberVertices];
            y = new float[mesh.NumberVertices];
            z = new float[mesh.NumberVertices];

            nx = new float[mesh.NumberVertices];
            ny = new float[mesh.NumberVertices];
            nz = new float[mesh.NumberVertices];

            getPoints = mesh.LockVertexBuffer(pointMover.GetType(), LockFlags.None, numberVertices);

            for (int i = 0; i < mesh.NumberVertices; i++)
                pointMover = (CustomVertex.PositionNormal)getPoints.GetValue(i);

                meshVertices[i].Normal = pointMover.Normal;
                meshVertices[i].Position = pointMover.Position;

                x.SetValue(meshVertices[i].Position.X, i);
                y.SetValue(meshVertices[i].Position.Y, i);
                z.SetValue(meshVertices[i].Position.Z, i);

                nx.SetValue(meshVertices[i].Normal.X, i);
                ny.SetValue(meshVertices[i].Normal.Y, i);
                nz.SetValue(meshVertices[i].Normal.Z, i);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this