Sign in to follow this  
CodeBoyCjy

how to get the vertex format of a model in XNA??

Recommended Posts

hey: I want to get the vertex position from the Model. But the vertex buffer provides me the data with the form of an array of BYTE. I think I need to know the vertex format to parse the array. Actually , I just need to know the size of each vertex is enough. How could I get the info?? Any help will be really appreciate.

Share this post


Link to post
Share on other sites
You'll have to loop through all the ModelMeshParts in all of the ModelMeshes that are part of the Model. ModelMeshPart has a VertexDeclaration property that will let you access the declaration, or you can just just the VertexStride property to find the number of bytes per vertex.

Share this post


Link to post
Share on other sites
I just figured out how to do this. I did it in a way that gives the maximum and minimum vertices.


VertexPositionNormalTexture[] vertices =
new VertexPositionNormalTexture[map.model.Meshes[i].VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes];
model.Meshes[i].VertexBuffer.GetData<VertexPositionNormalTexture>(vertices);
Vector3[] vertexs = new Vector3[vertices.Length];
for (int k = 0; k < vertexs.Length; k++)
{
vertexs[k] = Vector3.Transform(vertices[k].Position, model.Meshes[i].ParentBone.Transform);
}
Vector3 max = maxVector(vertexs);
Vector3 min = minVector(vertexs);
Matrix m = Matrix.CreateFromYawPitchRoll(
model.rotation.Y, model.rotation.X, model.rotation.Z) *
Matrix.CreateScale(model.scale) * Matrix.CreateTranslation(model.position);
max = Vector3.Transform(max, m);
min = Vector3.Transform(min, m);



The vertexes array ends up being 3 times longer than the amount of actual vertices because each vertex is stored 3 times (for a reason I couldn't explain to you).

Share this post


Link to post
Share on other sites
(The vertexes array ends up being 3 times longer than the amount of actual vertices because each vertex is stored 3 times (for a reason I couldn't explain to you).)

I'm realy sorry that I want to know the reason of the 3 times space. I found that last night , I'm a little confused. In the .fbx , there are only 8 vertexes which is what I want , but when I load them in the XNA's Model , the vertex number turned out to be 24 which means there is no vertex reuse. Why?? And each triangle gets three vertexes with the same normal , I think it's a waste of space. And if the 3 times space vertexes exist for a reason , why do we need index? Why don't we just order the vertexes rightly??

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