Texmap* M2Importer::createTexture(LPCTSTR fileName)
{
	BitmapManager* bmpMgr = TheManager;

	size_t pathSlashPos = m_modelName.find_last_of('\\');
	string pathName = m_modelName.substr(0, pathSlashPos + 1);

	// 将贴图文件名改为当前目录下的tga文件

	string origFileName = fileName;

	size_t dotPos = origFileName.find_last_of('.');
	if (dotPos != string::npos)
		origFileName = origFileName.substr(0, dotPos);

	size_t slashPos = origFileName.find_last_of('\\');
	if (slashPos != string::npos)
	{
		++slashPos;
		origFileName = origFileName.substr(slashPos, origFileName.length() - slashPos);
	}

	pathName += string("texture\\");
	pathName += origFileName;
	pathName.append(".tga");

	m_logStream << "Model Texture Name: " << pathName << endl;
	TSTR newFileName = pathName.c_str();

	if (origFileName.length())
	{
		// 改成用系统的检查文件是否存在的API
		ifstream testStream(newFileName, ios::binary | ios::in);
		if (testStream.fail())
		{
			string errstr = string("Load texture error. filename: ") + string(newFileName);
			errstr += string("\n\nOriginal file: ");
			errstr += string(fileName);
			MessageBox(NULL, errstr.c_str(), "TBD: Error.", MB_OK);
			m_logStream << errstr << endl;
		}
		else
			testStream.close();
	}

	if (bmpMgr->CanImport(newFileName))
	{
		BitmapTex* bmpTex = NewDefaultBitmapTex();
		bmpTex->SetName(newFileName);
		bmpTex->SetMapName(newFileName);
		bmpTex->SetAlphaAsMono(TRUE);
		bmpTex->SetAlphaSource(ALPHA_FILE);

		return bmpTex;
	}

	return 0;
}
Texmap* NifImporter::CreateTexture(const string& filename)
{
	if (filename.empty())
		return NULL;

	BitmapManager *bmpMgr = TheManager;
	if (bmpMgr->CanImport(filename.c_str())){
		BitmapTex *bmpTex = NewDefaultBitmapTex();
		string name = filename;
		if (name.empty()) {
			TCHAR buffer[MAX_PATH];
			_tcscpy(buffer, PathFindFileName(filename.c_str()));
			PathRemoveExtension(buffer);
			name = buffer;
		}         
		bmpTex->SetName(name.c_str());
		bmpTex->SetMapName(const_cast<TCHAR*>(FindImage(filename).c_str()));
		bmpTex->SetAlphaAsMono(TRUE);
		bmpTex->SetAlphaSource(ALPHA_DEFAULT);

		bmpTex->SetFilterType(FILTER_PYR); 

		if (showTextures) {
			bmpTex->SetMtlFlag(MTL_TEX_DISPLAY_ENABLED, TRUE);
			bmpTex->ActivateTexDisplay(TRUE);
			bmpTex->NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);
		}

		if (UVGen *uvGen = bmpTex->GetTheUVGen()){
			uvGen->SetTextureTiling(0);
		}

		return bmpTex;
	}
	return NULL;
}
void Import::LoadMaterials (dScene& scene, MaterialCache& materialCache)
{
    dScene::Iterator iter (scene);
    for (iter.Begin(); iter; iter ++) {
        dScene::dTreeNode* const materialNode = iter.GetNode();
        dNodeInfo* const info = scene.GetInfoFromNode(materialNode);
        if (info->IsType(dMaterialNodeInfo::GetRttiType())) {
            MaterialProxi material;
            material.m_mtl = NewDefaultStdMat();
            StdMat* const stdMtl = (StdMat*)material.m_mtl;

            dMaterialNodeInfo* const materialInfo = (dMaterialNodeInfo*) info;
            stdMtl->SetName(materialInfo->GetName());

            dVector ambient (materialInfo->GetAmbientColor());
            dVector difusse (materialInfo->GetDiffuseColor());
            dVector specular (materialInfo->GetSpecularColor());
            float shininess (materialInfo->GetShininess());
            //float shininessStr (materialInfo->GetShinStr());
            float transparency (materialInfo->GetOpacity());

            stdMtl->SetAmbient(*((Point3*)&ambient), 0);
            stdMtl->SetDiffuse(*((Point3*)&difusse), 0);
            stdMtl->SetSpecular(*((Point3*)&specular), 0);
            stdMtl->SetShinStr(shininess / 100.0f, 0);
            stdMtl->SetOpacity(transparency, 0);


            if (materialInfo->GetDiffuseTextId() != -1) {
                dScene::dTreeNode* textNode = scene.FindTextureByTextId(materialNode, materialInfo->GetDiffuseTextId());
                if (textNode) {
                    _ASSERTE (textNode);
                    //				BitmapTex* bmtex;
                    //				const TCHAR* txtName;

                    dTextureNodeInfo* textureInfo = (dTextureNodeInfo*) scene.GetInfoFromNode(textNode);
                    TCHAR txtNameBuffer[256];
                    sprintf (txtNameBuffer, "%s/%s", m_path, textureInfo->GetPathName());

                    const TCHAR* txtName = txtNameBuffer;
                    BitmapTex* bmtex = (BitmapTex*)NewDefaultBitmapTex();
                    bmtex->SetMapName((TCHAR*)txtName);

                    txtName = textureInfo->GetPathName();
                    bmtex->SetName (txtName);
                    bmtex->GetUVGen()->SetMapChannel(1);

                    stdMtl->SetSubTexmap(ID_DI, bmtex);
                    stdMtl->SetTexmapAmt(ID_DI, 1.0f, 0);
                    stdMtl->EnableMap(ID_DI, TRUE);

                    //					const char* materialOpanacity = segment.m_opacityTextureName;
                    //					if (materialOpanacity[0]) {
                    //						BitmapTex* bmtex;
                    //						const TCHAR* txtName;
                    //
                    //						txtName = segment.m_opacityPathName;
                    //						bmtex = (BitmapTex*)NewDefaultBitmapTex();
                    //						bmtex->SetMapName((TCHAR*)txtName);
                    //
                    //						txtName = materialName;
                    //						bmtex->SetName (txtName);
                    //						bmtex->GetUVGen()->SetMapChannel(2);
                    //
                    //						stdMtl->SetSubTexmap(ID_OP, bmtex);
                    //						stdMtl->SetTexmapAmt(ID_OP, 1.0f, 0);
                    //						stdMtl->EnableMap(ID_OP, TRUE);
                    //					}
                    //				materialCache.AddMaterial(material, segment.m_textureName);
                }
            }
            materialCache.AddMaterial(material, materialInfo->GetId());
        }
    }
}
Texmap* NifImporter::CreateTexture(TexDesc& desc)
{
   BitmapManager *bmpMgr = TheManager;
   if (NiSourceTextureRef texSrc = desc.source){
      string filename = texSrc->GetTextureFileName();
      if (bmpMgr->CanImport(filename.c_str())){
         BitmapTex *bmpTex = NewDefaultBitmapTex();
         string name = texSrc->GetName();
         if (name.empty()) {
            TCHAR buffer[MAX_PATH];
            _tcscpy(buffer, PathFindFileName(filename.c_str()));
            PathRemoveExtension(buffer);
            name = buffer;
         }         
         bmpTex->SetName(name.c_str());
         bmpTex->SetMapName(const_cast<TCHAR*>(FindImage(filename).c_str()));
         bmpTex->SetAlphaAsMono(TRUE);
         bmpTex->SetAlphaSource(ALPHA_DEFAULT);

         switch (desc.filterMode)
         {
         case FILTER_TRILERP: bmpTex->SetFilterType(FILTER_PYR); break;
         case FILTER_BILERP:  bmpTex->SetFilterType(FILTER_SAT); break;
         case FILTER_NEAREST: bmpTex->SetFilterType(FILTER_NADA); break;
         }

         if (UVGen *uvGen = bmpTex->GetTheUVGen()){
            if (uvGen && uvGen->IsStdUVGen()) {
               StdUVGen *uvg = (StdUVGen*)uvGen;
               uvg->SetMapChannel(desc.uvSet + 1);
            }

            switch (desc.clampMode)
            {
            case WRAP_S_WRAP_T : uvGen->SetTextureTiling(3); break;
            case WRAP_S_CLAMP_T: uvGen->SetTextureTiling(1); break;
            case CLAMP_S_WRAP_T: uvGen->SetTextureTiling(2); break;
            case CLAMP_S_CLAMP_T:uvGen->SetTextureTiling(0); break;
            }

            if (desc.hasTextureTransform) {
               if (RefTargetHandle ref = uvGen->GetReference(0)){
                  TexCoord trans = desc.translation;
                  TexCoord tiling = desc.tiling;
                  float wangle = TODEG(desc.wRotation);

                  setMAXScriptValue(ref, "U_Offset", 0, trans.u);
                  setMAXScriptValue(ref, "V_Offset", 0, trans.v);
                  setMAXScriptValue(ref, "U_Tiling", 0, tiling.u);
                  setMAXScriptValue(ref, "V_Tiling", 0, tiling.v);
                  setMAXScriptValue(ref, "W_Angle", 0, wangle);
               }
            }
         }
		 if (showTextures) {
			 bmpTex->SetMtlFlag(MTL_TEX_DISPLAY_ENABLED, TRUE);
			 bmpTex->ActivateTexDisplay(TRUE);
			 bmpTex->NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);
		 }

         return bmpTex;
      }
   }
   return NULL;
}