private void LoadMesh(string filename)
{
int lastBackslash = filename.LastIndexOf('\\');
string clippedFilename = filename.Substring(lastBackslash + 1);
string srcDir = filename.Substring(0, lastBackslash + 1);
SetAnimation(null);
UpdateAnimationListBox();
if (boneDisplay != null) {
boneDisplay.Close();
boneDisplay = null;
}
if (loadedModel != null)
{
// clean up the last model if we are loading a new one
RemoveCollisionObjects(0);
CleanupSockets();
CleanupBones();
CleanupNormalsRenderable();
Debug.Assert(attachedNodes.Count == 0);
modelNode.DetachObject(loadedModel);
sceneManager.RemoveEntity(loadedModel);
}
CleanupGroundPlaneRenderable();
Mesh mesh = ReadMesh(Matrix4.Identity, srcDir, clippedFilename);
SetupPoseAnimation(mesh);
// // Create the list of triangles used to query mouse hits
// mesh.CreateTriangleIntersector();
loadedMesh = mesh;
loadedModel = sceneManager.CreateEntity("model", mesh);
Text = "ModelViewer: " + filename;
// move the camera focus to the middle of the new model
ModelHeight = loadedModel.BoundingBox.Maximum.y - loadedModel.BoundingBox.Minimum.y;
if (CameraRadius < loadedModel.BoundingRadius)
{
CameraRadius = loadedModel.BoundingRadius;
}
PositionCamera();
modelNode.AttachObject(loadedModel);
AddCollisionObject(loadedModel, modelNode, 0,
Path.GetFileNameWithoutExtension(clippedFilename) + ".physics");
//modelNode.DetachObject(loadedModel);
loadedModel.ShowBoundingBox = showBoundingBox;
if (loadedModel.Mesh != null && loadedModel.Mesh.ContainsAnimation("manual")) {
AnimationState manualAnimState = loadedModel.GetAnimationState("manual");
manualAnimState.Time = 0;
manualAnimState.IsEnabled = true;
}
PopulateSubMeshListBox();
PopulateAnimationListBox();
PopulateSocketListBox();
PopulateBonesListBox();
// disable the fields
parentBoneLabel.Visible = false;
parentBoneValueLabel.Text = "";
parentBoneValueLabel.Visible = false;
}