//------------------------------------------------------------------------------
// 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
 * Xマテリアルトランスレータ実装
 * @author Junpee
 */

#include <LampBasic.h>
#include "XTranslator/XMaterialTranslator.h"
#include "XTranslator/XTextureTranslator.h"
#include <Core/InputOutput/FilePath.h>
#include <Graphics/Scene/Scene.h>
#include <Graphics/Material/MaterialManager.h>

/// マテリアルサフィックス
const String XMaterialTranslator::materialSuffix = "Material";

//------------------------------------------------------------------------------
// コンストラクタ
XMaterialTranslator::XMaterialTranslator() : material_(NULL){
}
//------------------------------------------------------------------------------
// デストラクタ
XMaterialTranslator::~XMaterialTranslator(){
}
//------------------------------------------------------------------------------
// 変換
bool XMaterialTranslator::translate(const FilePath& xFilePath,
	Scene* scene, const String& name, const D3DXMATERIAL& xMaterial){
	// マテリアルを作成する
	MaterialManager* materialManager = scene->getMaterialManager();
	String materialName = materialManager->rename(name + materialSuffix);
	BasicMaterial* material =
		materialManager->createBasicMaterial(materialName);
	material_ = material;

	// マテリアルの変換
	const D3DMaterial& d3dMaterial = xMaterial.MatD3D;
	material->setDiffuseColor(convertColor3f(d3dMaterial.Diffuse));
	float alpha = d3dMaterial.Diffuse.a;
	// 何も考え無しにアルファに0が設定されているデータがあまりにも多いので
	// 0の場合は1にしておく
	if(alpha == 0.f){ alpha = 1.f; }
	material->setAlpha(alpha);
	if(Math::abs(alpha - 1.f) > Math::epsilon){
		material->setBlendMode(Material::blendModeAdd);
	}
	// Xファイルには常にアンビエント色が無いので白をセットする
	Color3f ambient = convertColor3f(d3dMaterial.Ambient);
	Assert(ambient == Color3f::black);
	material->setAmbientColor(Color3f::white);
	material->setSpecularColor(convertColor3f(d3dMaterial.Specular));
	material->setSpecularPower(d3dMaterial.Power);
	material->setEmissiveColor(convertColor3f(d3dMaterial.Emissive));

	// テクスチャを変換する
	XTextureTranslator xTextureTranslator;
	if(!xTextureTranslator.translate(
		xFilePath, scene, name, String(xMaterial.pTextureFilename))){
		return false;
	}
	material->setBaseTexture(xTextureTranslator.getTexture());

	return true;
}
//------------------------------------------------------------------------------
