Sign in to follow this  
leandra

problem optimizing, cleaning and simplifying meshes c#

Recommended Posts

hi I wanted to optimize my meshes removing some unseen vertex and i find a tutorial that has this code but when I implemented my app crashes when rendering the mesh :S. What is what Im doing wrong??? heres is the code :::::::::::::::::::::: public void setMeshFromFile(string filename) { GraphicsStream adj; m = Mesh.FromFile(dirMeshes + filename,MeshFlags.Managed,device,out adj,out materials); m.ComputeNormals(); meshMaterials = new Material[materials.Length]; meshTextures = new Texture[materials.Length]; for( int i=0; i< materials.Length; i++ ) { meshMaterials[i] = materials[i].Material3D; if ((materials[i].TextureFilename != null) && ((materials[i].TextureFilename != string.Empty))) { meshTextures[i] = TextureLoader.FromFile(device, dirMeshes + materials[i].TextureFilename); } } Mesh tempMesh; if ((m.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal) { tempMesh = m.Clone(m.Options.Value,m.VertexFormat | VertexFormats.Normal, device); tempMesh.ComputeNormals(); m.Dispose(); m = tempMesh; } // optimize mesh tempMesh = m.Optimize (MeshFlags.Managed | MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeDoNotSplit, adj); m.Dispose(); m = tempMesh; // Clean our main mesh Mesh tempMesh = Mesh.Clean(CleanType.Simplification,m, adj, adj); m.Dispose(); m = tempMesh; simplifiedMesh = Mesh.Simplify(m, adj, null, 1, MeshFlags.SimplifyVertex); }

Share this post


Link to post
Share on other sites
Ouch, this early in the morning, reading code without indenting is quite difficult ^^'' please check the FAQ to post some codes with indenting :


public void setMeshFromFile(string filename)
{
GraphicsStream adj;
m = Mesh.FromFile(dirMeshes + filename,MeshFlags.Managed,device,out adj,out materials);
m.ComputeNormals();
meshMaterials = new Material[materials.Length];
meshTextures = new Texture[materials.Length];
for( int i=0; i< materials.Length; i++ )
{
meshMaterials[i] = materials[i].Material3D;
if ((materials[i].TextureFilename != null) && ((materials[i].TextureFilename != string.Empty)))
{
meshTextures[i] = TextureLoader.FromFile(device, dirMeshes + materials[i].TextureFilename);
}
}

Mesh tempMesh;

if ((m.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal)
{
tempMesh = m.Clone(m.Options.Value, m.VertexFormat | VertexFormats.Normal, device);
tempMesh.ComputeNormals();
m.Dispose();
m = tempMesh;
}

// optimize mesh
tempMesh = m.Optimize (MeshFlags.Managed | MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeDoNotSplit, adj);
m.Dispose();
m = tempMesh;

// Clean our main mesh
Mesh tempMesh = Mesh.Clean(CleanType.Simplification, m, adj, adj);
m.Dispose();
m = tempMesh;

simplifiedMesh = Mesh.Simplify(m, adj, null, 1, MeshFlags.SimplifyVertex);
}



Well, I would use OptimizeInPlace instead of Optimize. That would save the m.Dispose() and the m = tempMesh; after that. Because I'm not too sure (it's C# ^^) but is it right to do tempMesh = m.Optimize(...); although just before that, you have m = tempMesh; ?

And ... is it also right to do Mesh tempMesh twice ???? In C++, it shouldn't even compile (i think) So maybe try to replace the seconde Mesh tempMesh = ...; by a Mesh tempMesh2 = ...; or something like that.

Again, I'm not good with C#, maybe I missed something ...

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