//------------------------------------------------------------------------------
// 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 "LampBasic.h"
#include "Graphics/Shader/Shader.h"
#include "Graphics/Renderer/Renderer.h"
#include "Graphics/Renderer/RenderingDevice.h"
#include "Graphics/Renderer/DrawRequest.h"
#include "Graphics/Mesh/Mesh.h"
#include "Graphics/Material/Material.h"
#include "Graphics/Texture/Texture.h"

namespace Lamp{

// 最大アクティブライト数
const int Shader::maxActiveLightCount_ = Renderer::maxActiveLightCount_;

//------------------------------------------------------------------------------
// コンストラクタ
Shader::Shader(){
	device_ = RenderingDevice::getInstance();
}
//------------------------------------------------------------------------------
// デストラクタ
Shader::~Shader(){
}
//------------------------------------------------------------------------------
// マテリアル開始の構築
void Shader::buildMaterialStart(Material* material){
	// アルファ値はFixedとProgrammableで扱いが違うのでサブクラスで設定する

	// アルファブレンド
	if(material->isBlendEnabled()){
		device_->setBlendMode(material->getBlendMode(),
			material->getBlendSource(), material->getBlendDestination());
	}

	// Z書き込み
	device_->setRenderState(D3DRS_ZWRITEENABLE, material->useZWrite());
	// Zテスト
	device_->setZTest(material->useZTest());
}
//------------------------------------------------------------------------------
// マテリアル終了の構築
//void Shader::buildMaterialEnd(Material* material){
//}
//------------------------------------------------------------------------------
// 描画
//------------------------------------------------------------------------------
// 描画コール
void Shader::drawCall(DrawRequest* request){
	Mesh* mesh = request->getMesh();
	// インデックスを持つか
	if(mesh->hasVertexIndices()){
		// パイプラインに関係なく、同じメッシュデータならば設定を行わない
		if(request->isMeshDataChanged()){
			// インデックスバッファを設定
			device_->setIndexBuffer(mesh->getIndexBuffer());
		}
		// インデックス三角リスト描画
		device_->drawIndexedTriangleList(
			mesh->getVertexCount(), mesh->getPrimitiveCount());
	}else{
		// 三角リスト描画
		device_->drawTriangleList(mesh->getPrimitiveCount());
	}
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
