//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * 変換基本マテリアル実装
 * @author Junpee
 */

#include "System/stdafx.h"
#include "Translator/Material/TranslationBasicMaterial.h"
#include "Material/Utility/LampMaterialUtility.h"
#include "Graphics/Scene/Scene.h"
#include "Graphics/Material/MaterialManager.h"
#include "Graphics/Texture/TextureManager.h"

namespace LampForMaya{

//------------------------------------------------------------------------------
// コンストラクタ
TranslationBasicMaterial::TranslationBasicMaterial(
	const MObject& initializeObject, const String& initializeName) :
	TranslationMaterial(initializeObject, initializeName){
}
//------------------------------------------------------------------------------
// デストラクタ
TranslationBasicMaterial::~TranslationBasicMaterial(){
}
//------------------------------------------------------------------------------
// 分析
bool TranslationBasicMaterial::analyze(){
	// マテリアルの分析
	if(!analyzeMaterial()){ return false; }
	// ベーステクスチャ名
	baseTextureName_ = LampMaterialUtility::getTextureName(object_, "color");
	// ベースUVインデックス
	baseUVIndex_ = MayaAttributeUtility::getInt(object_, "baseUVIndex");
	// 光沢テクスチャ名
	glossTextureName_ =
		LampMaterialUtility::getTextureName(object_, "glossTexture");
	// 光沢UVインデックス
	glossUVIndex_ = MayaAttributeUtility::getInt(object_, "glossUVIndex");
	// ライトテクスチャ名
	lightTextureName_ =
		LampMaterialUtility::getTextureName(object_, "lightTexture");
	// ライトUVインデックス
	lightUVIndex_ = MayaAttributeUtility::getInt(object_, "lightUVIndex");
	// 汚れテクスチャ名
	stainTextureName_ =
		LampMaterialUtility::getTextureName(object_, "stainTexture");
	// 汚れUVインデックス
	stainUVIndex_ = MayaAttributeUtility::getInt(object_, "stainUVIndex");

	// ディフューズカラー
	diffuseColor_ = MayaAttributeUtility::getColor3f(object_, "diffuseColor");
	// スペキュラカラー
	specularColor_ = MayaAttributeUtility::getColor3f(object_, "specularColor");
	// スペキュラパワー
	specularPower_ = MayaAttributeUtility::getFloat(object_, "specularPower");
	// アンビエントカラー
	ambientColor_ = MayaAttributeUtility::getColor3f(object_, "ambientColor");
	// エミッシブカラー
	emissiveColor_ = MayaAttributeUtility::getColor3f(object_, "emissiveColor");
	return true;
}
//------------------------------------------------------------------------------
// Lampへの変換
bool TranslationBasicMaterial::convertToLamp(Scene* scene){
	MaterialManager* materialManager = scene->getMaterialManager();
	BasicMaterial* material = materialManager->createBasicMaterial(name_);
	TextureManager* textureManager = scene->getTextureManager();
	// マテリアルの変換
	if(!convertMaterial(material)){ return false; }
	// ベーステクスチャ名
	if(!baseTextureName_.isEmpty()){
		Texture* baseTexture = textureManager->search(baseTextureName_);
		if(baseTexture == NULL){
			MayaErrorOut("TranslationBasicMaterial::convertToLamp() "
				"ベーステクスチャが見つかりません " + baseTextureName_);
			return false;
		}
		material->setBaseTexture(baseTexture);
	}
	// ベースUVインデックス
	material->setBaseUVIndex(baseUVIndex_);
	// 光沢テクスチャ名
	if(!glossTextureName_.isEmpty()){
		Texture* glossTexture = textureManager->search(glossTextureName_);
		if(glossTexture == NULL){
			MayaErrorOut("TranslationBasicMaterial::convertToLamp() "
				"光沢テクスチャが見つかりません " + glossTextureName_);
			return false;
		}
		material->setGlossTexture(glossTexture);
	}
	// 光沢UVインデックス
	material->setGlossUVIndex(glossUVIndex_);
	// ライトテクスチャ名
	if(!lightTextureName_.isEmpty()){
		Texture* lightTexture = textureManager->search(lightTextureName_);
		if(lightTexture == NULL){
			MayaErrorOut("TranslationBasicMaterial::convertToLamp() "
				"ライトテクスチャが見つかりません " + lightTextureName_);
			return false;
		}
		material->setLightTexture(lightTexture);
	}
	// ライトUVインデックス
	material->setLightUVIndex(lightUVIndex_);
	// 汚れテクスチャ名
	if(!stainTextureName_.isEmpty()){
		Texture* stainTexture = textureManager->search(stainTextureName_);
		if(stainTexture == NULL){
			MayaErrorOut("TranslationBasicMaterial::convertToLamp() "
				"汚れテクスチャが見つかりません " + stainTextureName_);
			return false;
		}
		material->setStainTexture(stainTexture);
	}
	// 汚れUVインデックス
	material->setStainUVIndex(stainUVIndex_);

	// ディフューズカラー
	material->setDiffuseColor(diffuseColor_);
	// スペキュラカラー
	material->setSpecularColor(specularColor_);
	// スペキュラパワー
	material->setSpecularPower(specularPower_);
	// アンビエントカラー
	material->setAmbientColor(ambientColor_);
	// エミッシブカラー
	material->setEmissiveColor(emissiveColor_);
	return true;
}
//------------------------------------------------------------------------------
} // End of namespace LampForMaya
//------------------------------------------------------------------------------
