//------------------------------------------------------------------------------
// 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/Texture/TranslationTexture.h"
#include "Graphics/Scene/Scene.h"
#include "Graphics/Texture/TextureManager.h"
#include "Graphics/Picture/PictureManager.h"
#include "Core/InputOutput/FilePath.h"

namespace LampForMaya{

//------------------------------------------------------------------------------
// コンストラクタ
TranslationTexture::TranslationTexture(
	const MObject& initializeObject, const String& initializeName) :
	object_(initializeObject), name_(initializeName){
}
//------------------------------------------------------------------------------
// 分析
bool TranslationTexture::analyze(){
	// ピクチャパスの取得
	picturePath_ = MayaAttributeUtility::getString(object_, "fileTextureName");
	if(picturePath_.equals("")){
		MayaErrorOut(String("TranslationTexture::analyze() "
			"ピクチャファイル名が設定されていません。 ") + name_);
		return false;
	}
	// ピクチャ名の取得
	FilePath filePath(picturePath_);
	pictureName_ = filePath.getName();
	// .tgaのみ許可する
	if(filePath.getExtension() != "tga"){
		MayaErrorOut(String("TranslationTexture::analyze() "
			"Targa(.tga)以外のピクチャフォーマットは未対応です ") +
			name_ + " " + picturePath_);
		return false;
	}
	// ミラーUVフラグの取得
	mirrorU_ = MayaAttributeUtility::getBool(object_, "mirrorU");
	mirrorV_ = MayaAttributeUtility::getBool(object_, "mirrorV");
	// ラップUVフラグの取得
	wrapU_ = MayaAttributeUtility::getBool(object_, "wrapU");
	wrapV_ = MayaAttributeUtility::getBool(object_, "wrapV");
	// リピートUVの取得
	repeatUV_.u = MayaAttributeUtility::getFloat(object_, "repeatU");
	repeatUV_.v = MayaAttributeUtility::getFloat(object_, "repeatV");
	// オフセットUVの取得
	offsetUV_.u = MayaAttributeUtility::getFloat(object_, "offsetU");
	offsetUV_.v = MayaAttributeUtility::getFloat(object_, "offsetV");
	return true;
}
//------------------------------------------------------------------------------
// デストラクタ
TranslationTexture::~TranslationTexture(){
}
//------------------------------------------------------------------------------
// Lampへの変換
bool TranslationTexture::convertToLamp(Scene* scene){
	TextureManager* textureManager = scene->getTextureManager();
	SurfaceTexture* texture = textureManager->createSurfaceTexture(name_);
	if(mirrorU_){
		texture->setAddressModeU(Texture::addressModeMirror);
	}else if(wrapU_){
		texture->setAddressModeU(Texture::addressModeWrap);
	}else{
		texture->setAddressModeU(Texture::addressModeClamp);
	}
	if(mirrorV_){
		texture->setAddressModeV(Texture::addressModeMirror);
	}else if(wrapV_){
		texture->setAddressModeV(Texture::addressModeWrap);
	}else{
		texture->setAddressModeV(Texture::addressModeClamp);
	}
	texture->setRepeatUV(repeatUV_);
	texture->setOffsetUV(offsetUV_);
	// ピクチャとのリンク
	PictureManager* pictureManager = scene->getPictureManager();
	Picture* picture = pictureManager->search(pictureName_);
	if(picture == NULL){
		MayaErrorOut(String("TranslationTexture::convertToLamp() "
			"ピクチャが見つかりません ") + pictureName_);
		return false;
	}
	texture->addPicture(picture);
	return true;
}
//------------------------------------------------------------------------------
} // End of namespace LampForMaya
//------------------------------------------------------------------------------
