Loading a Collada File in a DirectX application
Last week I did show a small code sample that was loading some information from a Collada file and displaying 3D objects in a WPF application. Here is another small sample which process the same information from the Collada file but which this time generates the DirectX Index, Vertex buffer and Textures to display the same 3D objects easily in a DirectX application. This code sample is meant to be used with C#, .Net 2.0 and MDX1.1.
Again the code included in the zip file only process triangles from the Collada file and doesn’t make any attempt at reading complex material properties. Still I tend to think it can be easily modified to server any purpose you might have in mind.
Here is a small screenshot of the result:
And here is the code to writte in your own application to use the class called ColladaLoader included in the zip file :
collada = new ColladaLoader(e.Device);
collada.Load("Villada.dae");
Puis utiliser le code suivant pour effectuer le rendu de ce qui a été chargé:
IDictionaryEnumerator enumGeometrie = collada.colladaGeometries.GetEnumerator();
while (enumGeometrie.MoveNext())
{
ColladaGeometry collGeom = enumGeometrie.Value as ColladaGeometry;
if (collGeom == null)
continue;
foreach (LoadedEntity entity in collGeom.models)
{
if (entity.materials.Length == 0)
continue;
if (entity.textures.Length == 0)
continue;
device.SetTransform(TransformType.World,
Matrix.Multiply(Matrix.Scaling(0.1f, 0.1f, 0.1f), Matrix.Translation(0.0f, 5.0f, 0.0f)));
device.Indices = entity.iBuff;
device.SetStreamSource(0, entity.vBuff, 0);
device.Material = entity.materials[0];
device.SetTexture(0, entity.textures[0]);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
entity.numVertices, 0, entity.numPrimitives);
}
}
Feel free to contact me if you have any remark or question at guillara@microsoft.com . |