void MaterialEditor::OnSaveMaterial(wxCommandEvent &e)
{
	if(mMaterial.isNull())
		return;
	if(mSaveFileName == "")
	{
		wxFileDialog dialog(this, wxT("保存材质文件"), EFFECT_PATH, wxT(""),
			"All texture files (*.material)|*.material", wxSAVE); 

		if (dialog.ShowModal() == wxID_OK)
		{		
			wxArrayString SavePaths;
			dialog.GetPaths(SavePaths);

			mSaveFileName = SavePaths[0].c_str();
		}
		else
		{
			return;
		}

	}

	Ogre::MaterialSerializer ser;
	ser.queueForExport(mMaterial);
	ser.exportQueued(mSaveFileName);

}
void MaterialEditorDialog::OnSaveButtonDown(wxCommandEvent &e)
{
	if (mCurrentMaterial.isNull()==false)
	{
		Ogre::String matName;
		switch (gEffectSettingGlobalData.mCurrentEditEffectType)
		{
		case CEFT_EFFECT_PROJECTOR:
			{
				matName = gEffectSettingGlobalData.mOriginProjectorMaterial;

				Ogre::MaterialPtr newMat = Ogre::MaterialManager::getSingleton().getByName(matName);

				mCurrentMaterial->copyDetailsTo(newMat);
				newMat->setSelfIllumination(0.0f, 0.0f, 0.0f);

				Ogre::String pathFileName = EFFECT_PATH + matName + ".material";

				Ogre::MaterialSerializer ser;
				ser.queueForExport(newMat);
				ser.exportQueued(pathFileName);

				break;
			}

		default:
			{
				matName = mCurrentMaterial->getName();

				Ogre::String pathFileName = EFFECT_PATH + matName + ".material";

				Ogre::MaterialSerializer ser;
				ser.queueForExport(mCurrentMaterial);
				ser.exportQueued(pathFileName);

				break;
			}
		}		

		wxMessageBox("Success Save!");

		Hide();
	}
}
void MilkshapePlugin::doExportMaterials(msModel* pModel)
{
	Ogre::LogManager& logMgr = Ogre::LogManager::getSingleton();
	Ogre::MaterialManager matMgrSgl;
	Ogre::String msg;

    matMgrSgl.initialise();

	int numMaterials = msModel_GetMaterialCount(pModel);
	msg = "Number of materials: " + Ogre::StringConverter::toString(numMaterials);
	logMgr.logMessage(msg);

	OPENFILENAME ofn;
	memset (&ofn, 0, sizeof (OPENFILENAME));

	char szFile[MS_MAX_PATH];
	char szFileTitle[MS_MAX_PATH];
	char szDefExt[32] = "material";
	char szFilter[128] = "OGRE .material Files (*.material)\0*.material\0All Files (*.*)\0*.*\0\0";
	szFile[0] = '\0';
	szFileTitle[0] = '\0';

	ofn.lStructSize = sizeof (OPENFILENAME);
	ofn.lpstrDefExt = szDefExt;
	ofn.lpstrFilter = szFilter;
	ofn.lpstrFile = szFile;
	ofn.nMaxFile = MS_MAX_PATH;
	ofn.lpstrFileTitle = szFileTitle;
	ofn.nMaxFileTitle = MS_MAX_PATH;
	ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
	ofn.lpstrTitle = "Export to OGRE Material";

	if (!::GetSaveFileName (&ofn))
		return;

	// Strip off the path
	Ogre::String matName = szFile;
	size_t lastSlash = matName.find_last_of("\\");
	matName = matName.substr(lastSlash+1);

	// Set up
	logMgr.logMessage("Trying to create Material object");

	Ogre::MaterialSerializer matSer;

	for (int i = 0; i < numMaterials; ++i)
	{
		msMaterial *mat = msModel_GetMaterialAt(pModel, i);

		msg = "Creating material " + Ogre::String(mat->szName);
		logMgr.logMessage(msg);
        Ogre::MaterialPtr ogremat = matMgrSgl.create(mat->szName, 
            Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
		logMgr.logMessage("Created.");

		ogremat->setAmbient(msVec4ToColourValue(mat->Ambient));
		ogremat->setDiffuse(msVec4ToColourValue(mat->Diffuse));
		ogremat->setSpecular(msVec4ToColourValue(mat->Specular));
		ogremat->setShininess(mat->fShininess);

		if (0 < strlen(mat->szDiffuseTexture))
			ogremat->getTechnique(0)->getPass(0)->createTextureUnitState(mat->szDiffuseTexture);

        if (0 < strlen(mat->szAlphaTexture))
			ogremat->getTechnique(0)->getPass(0)->createTextureUnitState(mat->szAlphaTexture);


		matSer.queueForExport(ogremat);
	}

	msg = "Exporting materials to " + matName;
	logMgr.logMessage(msg);
	matSer.exportQueued(matName);
}