Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Material.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * マテリアル実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Graphics/Material/Material.h"
00027 #include "Graphics/Material/MaterialManager.h"
00028 #include "Graphics/Texture/Texture.h"
00029 #include "Graphics/Renderer/RenderingDevice.h"
00030 #include "Graphics/Renderer/DrawRequest.h"
00031 
00032 namespace Lamp{
00033 
00034 // ブレンドモード文字列テーブル
00035 const String Material::blendModeStringTable[] = {
00036     "Disable",
00037     "Add",
00038     "Subtract",
00039     "InverseSubtract",
00040     "Minimum",
00041     "Maximum",
00042 };
00043 
00044 // ブレンドステート文字列テーブル
00045 const String Material::blendStateStringTable[] = {
00046     "Zero",
00047     "One",
00048     "SourceColor",
00049     "InverseSourceColor",
00050     "SourceAlpha",
00051     "InverseSourceAlpha",
00052     "SourceAlphaSaturate",
00053     "DestinationColor",
00054     "InverseDestinationColor",
00055     "DestinationAlpha",
00056     "InverseDestinationAlpha",
00057 };
00058 
00059 // フォグオプション文字列テーブル
00060 const String Material::fogOptionStringTable[] = {
00061     "None",
00062     "Disable",
00063     "Black",
00064 };
00065 
00066 //------------------------------------------------------------------------------
00067 // コンストラクタ
00068 Material::Material(const String& name, Scene* scene) :
00069     SceneObject(name, scene), startStateBlock_(NULL), endStateBlock_(NULL),
00070     priority_(0), blendMode_(blendModeDisable),
00071     blendSource_(blendStateSourceAlpha),
00072     blendDestination_(blendStateInverseSourceAlpha), alpha_(1.f),
00073     fogOption_(fogOptionNone), lightMask_(1), pipelineMode_(pipelineModeNone),
00074     alphaBlend_(false), zWrite_(true), zTest_(true), hasStateChanged_(true){
00075 }
00076 //------------------------------------------------------------------------------
00077 // デストラクタ
00078 Material::~Material(){
00079     releaseStateBlock();
00080 }
00081 //------------------------------------------------------------------------------
00082 // マテリアルの値コピー
00083 void Material::copyMaterialValue(Material* destination) const{
00084     destination->setPriority(priority_);
00085     destination->setBlendMode(blendMode_);
00086     destination->setBlendSource(blendSource_);
00087     destination->setBlendDestination(blendDestination_);
00088     destination->setAlpha(alpha_);
00089     destination->setFogOption(fogOption_);
00090     destination->setLightMask(lightMask_);
00091     destination->setZWrite(zWrite_);
00092     destination->setZTest(zTest_);
00093 }
00094 //------------------------------------------------------------------------------
00095 // 再帰的破棄
00096 int Material::recursiveDestroy(Material* material){
00097     Assert(material != NULL);
00098     int result = 0;
00099     // 子の破棄
00100     if(material->getReferenceCount() == 0){
00101         result += material->destroyChildren();
00102     }
00103     // 引数の破棄
00104     MaterialManager* manager = material->getScene()->getMaterialManager();
00105     if(manager->destroy(material) == 0){
00106         result++;
00107     }
00108     return result;
00109 }
00110 //------------------------------------------------------------------------------
00111 // 描画の開始
00112 void Material::drawStart(){
00113     // ステートが変更されていればステートブロックを再構築する
00114     if(hasStateChanged_){
00115         releaseStateBlock();
00116         buildStateBlock(&startStateBlock_, &endStateBlock_);
00117         hasStateChanged_ = false;
00118     }
00119     // 開始ステートブロックを適用する
00120     if(startStateBlock_ != NULL){
00121         RenderingDevice::getInstance()->applyStateBlock(startStateBlock_);
00122     }
00123 }
00124 //------------------------------------------------------------------------------
00125 // 描画の終了
00126 void Material::drawEnd(){
00127     // ステートが変更されていればステートブロックを再構築する
00128     if(hasStateChanged_){
00129         releaseStateBlock();
00130         buildStateBlock(&startStateBlock_, &endStateBlock_);
00131         hasStateChanged_ = false;
00132     }
00133     // 終了ステートブロックを適用する
00134     if(endStateBlock_ != NULL){
00135         RenderingDevice::getInstance()->applyStateBlock(endStateBlock_);
00136     }
00137 }
00138 //------------------------------------------------------------------------------
00139 // 描画のセットアップ
00140 void Material::drawSetup(DrawRequest* request){
00141     // 同じマテリアルなら変更しない
00142     if(request->isMaterialChanged()){
00143         // 前回の描画を終了する
00144         Material* preMaterial = request->getPreMaterial();
00145         if(preMaterial != NULL){ preMaterial->drawEnd(); }
00146         // 描画を開始する
00147         drawStart();
00148     }
00149 }
00150 //------------------------------------------------------------------------------
00151 // ブレンディング
00152 //------------------------------------------------------------------------------
00153 // ブレンドモードから文字列への変換
00154 const String& Material::blendModeToString(BlendMode blendMode){
00155     Assert(blendMode >= 0);
00156     Assert(blendMode < blendModeMax);
00157     return blendModeStringTable[blendMode];
00158 }
00159 //------------------------------------------------------------------------------
00160 // 文字列からブレンドモードへの変換
00161 Material::BlendMode Material::blendModeFromString(
00162     const String& blendModeString){
00163     for(int i = 0; i < blendModeMax; i++){
00164         if(blendModeStringTable[i].equals(blendModeString)){
00165             return BlendMode(i);
00166         }
00167     }
00168     ErrorOut("Material::blendModeFromString() " + blendModeString);
00169     return blendModeMax;
00170 }
00171 //------------------------------------------------------------------------------
00172 // ブレンドステートから文字列への変換
00173 const String& Material::blendStateToString(BlendState blendState){
00174     Assert(blendState >= 0);
00175     Assert(blendState < blendStateMax);
00176     return blendStateStringTable[blendState];
00177 }
00178 //------------------------------------------------------------------------------
00179 // 文字列からブレンドステートへの変換
00180 Material::BlendState Material::blendStateFromString(
00181     const String& blendStateString){
00182     for(int i = 0; i < blendStateMax; i++){
00183         if(blendStateStringTable[i].equals(blendStateString)){
00184             return BlendState(i);
00185         }
00186     }
00187     ErrorOut("Material::blendStateFromString() " + blendStateString);
00188     return blendStateMax;
00189 }
00190 //------------------------------------------------------------------------------
00191 // フォグオプションから文字列への変換
00192 const String& Material::fogOptionToString(FogOption fogOption){
00193     Assert(fogOption >= 0);
00194     Assert(fogOption < fogOptionMax);
00195     return fogOptionStringTable[fogOption];
00196 }
00197 //------------------------------------------------------------------------------
00198 // 文字列からフォグオプションへの変換
00199 Material::FogOption Material::fogOptionFromString(
00200     const String& fogOptionString){
00201     for(int i = 0; i < fogOptionMax; i++){
00202         if(fogOptionStringTable[i].equals(fogOptionString)){
00203             return FogOption(i);
00204         }
00205     }
00206     ErrorOut("Material::fogOptionFromString() " + fogOptionString);
00207     return fogOptionMax;
00208 }
00209 //------------------------------------------------------------------------------
00210 // テクスチャリファレンス
00211 //------------------------------------------------------------------------------
00212 // テクスチャリファレンスの設定
00213 Texture* Material::setTextureReferense(
00214     Texture* nowTexture, Texture* newTexture){
00215     if(nowTexture != NULL){ nowTexture->removeReference(this); }
00216     if(newTexture != NULL){ newTexture->addReference(this); }
00217     stateChanged();
00218     return newTexture;
00219 }
00220 //------------------------------------------------------------------------------
00221 } // End of namespace Lamp
00222 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:32 2005 for Lamp by doxygen 1.3.2