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

Material.h

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 #ifndef MATERIAL_H_
00026 #define MATERIAL_H_
00027 
00028 #include <Graphics/Scene/SceneObject.h>
00029 #include <Graphics/System/GraphicsDeviceObjectHolder.h>
00030 #include <Core/Container/ArrayList.h>
00031 
00032 namespace Lamp{
00033 
00034 class Mesh;
00035 class Texture;
00036 class DrawRequest;
00037 class BasicMaterial;
00038 
00039 //------------------------------------------------------------------------------
00040 /**
00041  * マテリアル
00042  */
00043 class Material : public SceneObject , public GraphicsDeviceObjectHolder{
00044 friend class SceneObjectManagerTemplate<Material>;
00045 friend class MaterialManager;
00046 friend class Mesh;
00047 friend class Renderer;
00048 public:
00049     //--------------------------------------------------------------------------
00050     /**
00051      * リファレンスカウントの取得
00052      * @return リファレンスカウント
00053      */
00054     virtual int getReferenceCount() const{ return parents_.getCount(); }
00055 
00056     //--------------------------------------------------------------------------
00057     /**
00058      * コピー
00059      * @param copyMask コピーマスク
00060      * @return コピーされたマテリアル
00061      */
00062     virtual Material* copy(u_int copyMask = 0) const = 0;
00063 
00064     /**
00065      * 再帰的破棄
00066      * @param material 破棄するマテリアル
00067      * @return 破棄したオブジェクト数
00068      */
00069     static int recursiveDestroy(Material* material);
00070 
00071     //--------------------------------------------------------------------------
00072     /**
00073      * ステート変更フラグを立てる
00074      */
00075     virtual void stateChanged(){ hasStateChanged_ = true; }
00076 
00077     /**
00078      * ステート変更フラグの取得
00079      * @return ステート変更フラグ
00080      */
00081     virtual bool hasStateChanged() const{ return hasStateChanged_; }
00082 
00083     //--------------------------------------------------------------------------
00084     /**
00085      * 親の数取得
00086      * @return 親の数
00087      */
00088     virtual int getParentCount() const{ return parents_.getCount(); }
00089 
00090     /**
00091      * 親の取得
00092      * @param index 親のインデックス
00093      * @return 親
00094      */
00095     virtual Mesh* getParent(int index) const{
00096         Assert(index >= 0);
00097         Assert(index < getParentCount());
00098         return parents_.get(index);
00099     }
00100 
00101     //--------------------------------------------------------------------------
00102     /**
00103      * ライトを使用するか
00104      * @return ライトを使用するならtrue
00105      */
00106     virtual bool useLight() const{ return true; }
00107 
00108     //--------------------------------------------------------------------------
00109     /**
00110      * 優先度の設定
00111      * @param priority 優先度
00112      */
00113     virtual void setPriority(int priority){
00114         priority_ = priority;
00115         stateChanged();
00116     }
00117 
00118     /**
00119      * 優先度の取得
00120      * @return 優先度
00121      */
00122     virtual int getPriority() const{ return priority_; }
00123 
00124     //--------------------------------------------------------------------------
00125     // ブレンディング
00126     //--------------------------------------------------------------------------
00127     /// ブレンドモード
00128     enum BlendMode{
00129         blendModeDisable = 0,
00130         blendModeAdd,
00131         blendModeSubtract,
00132         blendModeInverseSubtract,
00133         blendModeMinimum,
00134         blendModeMaximum,
00135         blendModeMax,
00136     };
00137 
00138     /**
00139      * ブレンドモードから文字列への変換
00140      * @param blendMode ブレンドモード
00141      * @return ブレンドモード文字列
00142      */
00143     static const String& blendModeToString(BlendMode blendMode);
00144 
00145     /**
00146      * 文字列からブレンドモードへの変換
00147      * @param blendModeString ブレンドモード文字列
00148      * @return ブレンドモード
00149      */
00150     static BlendMode blendModeFromString(const String& blendModeString);
00151 
00152     //--------------------------------------------------------------------------
00153     /**
00154      * ブレンドモードの設定
00155      * @param blendMode ブレンドモード
00156      */
00157     virtual void setBlendMode(BlendMode blendMode){
00158         blendMode_ = blendMode;
00159         stateChanged();
00160     }
00161 
00162     /**
00163      * ブレンドモードの取得
00164      * @return ブレンドモード
00165      */
00166     virtual BlendMode getBlendMode() const{ return blendMode_; }
00167 
00168     /**
00169      * ブレンドが有効か
00170      * @return ブレンドが有効ならtrue
00171      */
00172     virtual bool isBlendEnabled() const{
00173         return (blendMode_ != blendModeDisable);
00174     }
00175 
00176     //--------------------------------------------------------------------------
00177     /**
00178      * アルファの設定
00179      * @param alpha アルファ
00180      */
00181     virtual void setAlpha(float alpha){
00182         alpha_ = alpha;
00183         stateChanged();
00184     }
00185 
00186     /**
00187      * アルファの取得
00188      * @return アルファ
00189      */
00190     virtual float getAlpha() const{ return alpha_; }
00191 
00192     //--------------------------------------------------------------------------
00193     /// ブレンドステート
00194     enum BlendState{
00195         blendStateZero = 0,
00196         blendStateOne,
00197         blendStateSourceColor,
00198         blendStateInverseSourceColor,
00199         blendStateSourceAlpha,
00200         blendStateInverseSourceAlpha,
00201         blendStateSourceAlphaSaturate,
00202         blendStateDestinationColor,
00203         blendStateInverseDestinationColor,
00204         blendStateDestinationAlpha,
00205         blendStateInverseDestinationAlpha,
00206         blendStateMax,
00207     };
00208 
00209     /**
00210      * ブレンドステートから文字列への変換
00211      * @param blendState ブレンドステート
00212      * @return ブレンドステート文字列
00213      */
00214     static const String& blendStateToString(BlendState blendState);
00215 
00216     /**
00217      * 文字列からブレンドステートへの変換
00218      * @param blendStateString ブレンドステート文字列
00219      * @return ブレンドステート
00220      */
00221     static BlendState blendStateFromString(const String& blendStateString);
00222 
00223     //--------------------------------------------------------------------------
00224     /**
00225      * ブレンドソースの設定
00226      * @param blendSource ブレンドソース
00227      */
00228     virtual void setBlendSource(BlendState blendSource){
00229         Assert(blendSource >= 0);
00230         Assert(blendSource < blendStateMax);
00231         blendSource_ = blendSource;
00232         stateChanged();
00233     }
00234 
00235     /**
00236      * ブレンドソースの取得
00237      * @return ブレンドソース
00238      */
00239     virtual BlendState getBlendSource() const{ return blendSource_; }
00240 
00241     //--------------------------------------------------------------------------
00242     /**
00243      * ブレンドデスティネーションの設定
00244      * @param blendDestination ブレンドデスティネーション
00245      */
00246     virtual void setBlendDestination(BlendState blendDestination){
00247         Assert(blendDestination_ >= 0);
00248         Assert(blendDestination_ < blendStateMax);
00249         blendDestination_ = blendDestination;
00250         stateChanged();
00251     }
00252 
00253     /**
00254      * ブレンドデスティネーションの取得
00255      * @return ブレンドデスティネーション
00256      */
00257     virtual BlendState getBlendDestination() const{
00258         return blendDestination_;
00259     }
00260 
00261     //--------------------------------------------------------------------------
00262     // Zテスト
00263     //--------------------------------------------------------------------------
00264     /**
00265      * Z書き込みの設定
00266      * @param zWrite Z書き込みを行うならtrue
00267      */
00268     virtual void setZWrite(bool zWrite){
00269         zWrite_ = zWrite;
00270         stateChanged();
00271     }
00272 
00273     /**
00274      * Z書き込みの取得
00275      * @return Z書き込みを行うならtrue
00276      */
00277     virtual bool useZWrite() const{ return zWrite_; }
00278 
00279     //--------------------------------------------------------------------------
00280     /**
00281      * Zテストの設定
00282      * @param zTest Zテストを行うならtrue
00283      */
00284     virtual void setZTest(bool zTest){
00285         zTest_ = zTest;
00286         stateChanged();
00287     }
00288 
00289     /**
00290      * Zテストの取得
00291      * @return Zテストを行うならtrue
00292      */
00293     virtual bool useZTest() const{ return zTest_; }
00294 
00295     //--------------------------------------------------------------------------
00296     // フォグオプション
00297     //--------------------------------------------------------------------------
00298     /// フォグオプション
00299     enum FogOption{
00300         fogOptionNone = 0,
00301         fogOptionDisable,
00302         fogOptionBlack,
00303         fogOptionMax,
00304     };
00305 
00306     /**
00307      * フォグオプションから文字列への変換
00308      * @param fogOption フォグオプション
00309      * @return フォグオプション文字列
00310      */
00311     static const String& fogOptionToString(FogOption fogOption);
00312 
00313     /**
00314      * 文字列からフォグオプションへの変換
00315      * @param fogOptionString フォグオプション文字列
00316      * @return フォグオプション
00317      */
00318     static FogOption fogOptionFromString(const String& fogOptionString);
00319 
00320     //--------------------------------------------------------------------------
00321     /**
00322      * フォグオプションの設定
00323      * @param fogOption フォグオプション
00324      */
00325     virtual void setFogOption(FogOption fogOption){
00326         Assert(fogOption >= 0);
00327         Assert(fogOption < fogOptionMax);
00328         fogOption_ = fogOption;
00329         stateChanged();
00330     }
00331 
00332     /**
00333      * フォグオプションの取得
00334      * @return フォグオプション
00335      */
00336     virtual FogOption getFogOption() const{ return fogOption_; }
00337 
00338     //--------------------------------------------------------------------------
00339     // ライトマスク
00340     //--------------------------------------------------------------------------
00341     /**
00342      * ライトマスクの設定
00343      * @param lightMask ライトマスク
00344      */
00345     virtual void setLightMask(u_int lightMask){ lightMask_ = lightMask; }
00346 
00347     /**
00348      * ライトマスクの取得
00349      * @return ライトマスク
00350      */
00351     virtual u_int getLightMask() const{ return lightMask_; }
00352 
00353     //--------------------------------------------------------------------------
00354     // パイプラインモード
00355     //--------------------------------------------------------------------------
00356     /// パイプラインモード
00357     enum PipelineMode{
00358         pipelineModeNone = 0,
00359         pipelineModeFixed,
00360         pipelineModeProgrammableShader2,
00361     };
00362 
00363     /**
00364      * パイプラインモードの取得
00365      * @return パイプラインモード
00366      */
00367     virtual PipelineMode getPipelineMode() const{
00368         Assert(pipelineMode_ != pipelineModeNone);
00369         return pipelineMode_;
00370     }
00371 
00372     //--------------------------------------------------------------------------
00373     // デバイスオブジェクト管理
00374     //--------------------------------------------------------------------------
00375     /**
00376      * デバイスオブジェクトの初期化
00377      * @return 成功したらtrueを返す
00378      */
00379     virtual bool initializeGraphicsDeviceObjects(){ return true; }
00380 
00381     /**
00382      * デバイスオブジェクトの削除
00383      */
00384     virtual void deleteGraphicsDeviceObjects(){}
00385 
00386     /**
00387      * デバイスオブジェクトのリストア
00388      * @return 成功したらtrueを返す
00389      */
00390     virtual bool restoreGraphicsDeviceObjects(){
00391         buildStateBlock(&startStateBlock_, &endStateBlock_);
00392         return true;
00393     }
00394 
00395     /**
00396      * デバイスオブジェクトの無効化
00397      */
00398     virtual void invalidateGraphicsDeviceObjects(){ releaseStateBlock(); }
00399 
00400     //--------------------------------------------------------------------------
00401     // RTTI
00402     //--------------------------------------------------------------------------
00403     /**
00404      * マテリアルかどうか
00405      * @return マテリアルならtrue
00406      */
00407     virtual bool isMaterial() const{ return true; }
00408 
00409     //--------------------------------------------------------------------------
00410     /**
00411      * 基本マテリアルかどうか
00412      * @return 基本マテリアルならtrue
00413      */
00414     virtual bool isBasicMaterial() const{ return false; }
00415 
00416     /**
00417      * 基本マテリアルへのキャスト
00418      * @return 基本マテリアル。型が違えばNULLを返す。
00419      */
00420     virtual BasicMaterial* castBasicMaterial() const{
00421         if(isBasicMaterial()){ return (BasicMaterial*)this; }
00422         return NULL;
00423     }
00424 
00425     //--------------------------------------------------------------------------
00426 protected:
00427     /**
00428      * コンストラクタ
00429      * @param name 名前
00430      * @param scene シーン
00431      */
00432     Material(const String& name, Scene* scene);
00433 
00434     /**
00435      * デストラクタ
00436      */
00437     virtual ~Material();
00438 
00439     /**
00440      * マテリアルの値コピー
00441      * @param destination コピー先マテリアル
00442      */
00443     virtual void copyMaterialValue(Material* destination) const;
00444 
00445     /**
00446      * 子の破棄
00447      * @return 破棄したオブジェクト数
00448      */
00449     virtual int destroyChildren() = 0;
00450 
00451     //--------------------------------------------------------------------------
00452     // 描画
00453     //--------------------------------------------------------------------------
00454     /**
00455      * 描画のセットアップ
00456      * @param request 描画リクエスト
00457      */
00458     virtual void drawSetup(DrawRequest* request);
00459 
00460     /**
00461      * 描画
00462      * @param request 描画リクエスト
00463      */
00464     virtual void draw(DrawRequest* request) = 0;
00465 
00466     //--------------------------------------------------------------------------
00467     /**
00468      * ステートブロックの構築
00469      * @param startBlock [out]開始ステートブロック
00470      * @param endBlock [out]終了ステートブロック
00471      */
00472     virtual void buildStateBlock(
00473         Direct3DStateBlock** startBlock, Direct3DStateBlock** endBlock) = 0;
00474 
00475     /**
00476      * ステートブロックの解放
00477      */
00478     virtual void releaseStateBlock(){
00479         SafeRelease(endStateBlock_);
00480         SafeRelease(startStateBlock_);
00481     }
00482 
00483     /**
00484      * パイプラインモードの設定
00485      * @param pipelineMode パイプラインモード
00486      */
00487     virtual void setPipelineMode(PipelineMode pipelineMode){
00488         Assert(pipelineMode != pipelineModeNone);
00489         pipelineMode_ = pipelineMode;
00490     }
00491 
00492     //--------------------------------------------------------------------------
00493     /**
00494      * 参照の追加
00495      * @param parent 親
00496      * @return 参照カウント
00497      */
00498     virtual int addReference(Mesh* parent){
00499         parents_.add(parent);
00500         return getParentCount();
00501     }
00502 
00503     /**
00504      * 参照の削除
00505      * @param parent 親
00506      * @return 参照カウント
00507      */
00508     virtual int removeReference(Mesh* parent){
00509         parents_.removeByValue(parent);
00510         return getParentCount();
00511     }
00512 
00513     //--------------------------------------------------------------------------
00514     // テクスチャリファレンス
00515     //--------------------------------------------------------------------------
00516     /**
00517      * テクスチャリファレンスの設定
00518      * @param nowTexture 現在のテクスチャ
00519      * @param newTexture 新しいテクスチャ
00520      * @return 新しいテクスチャ
00521      */
00522     virtual Texture* setTextureReferense(
00523         Texture* nowTexture, Texture* newTexture);
00524 
00525 private:
00526     //--------------------------------------------------------------------------
00527     // 描画
00528     //--------------------------------------------------------------------------
00529     /**
00530      * 描画の開始
00531      */
00532     virtual void drawStart();
00533 
00534     /**
00535      * 描画の終了
00536      */
00537     virtual void drawEnd();
00538 
00539     //--------------------------------------------------------------------------
00540     // 親配列
00541     ArrayList<Mesh*> parents_;
00542     // 開始ステートブロック
00543     Direct3DStateBlock* startStateBlock_;
00544     // 終了ステートブロック
00545     Direct3DStateBlock* endStateBlock_;
00546     // 優先度
00547     int priority_;
00548     // ブレンドモード
00549     BlendMode blendMode_;
00550     // ブレンドソース
00551     BlendState blendSource_;
00552     // ブレンドデスティネーション
00553     BlendState blendDestination_;
00554     // アルファ
00555     float alpha_;
00556     // フォグオプション
00557     FogOption fogOption_;
00558     // ライトマスク
00559     u_int lightMask_;
00560     // パイプラインモード
00561     PipelineMode pipelineMode_;
00562     // アルファブレンド
00563     bool alphaBlend_;
00564     // Z書き込み
00565     bool zWrite_;
00566     // Zテスト
00567     bool zTest_;
00568     // ステートステート変更フラグ
00569     bool hasStateChanged_;
00570 
00571     // ブレンドモード文字列テーブル
00572     static const String blendModeStringTable[];
00573     // ブレンドステート文字列テーブル
00574     static const String blendStateStringTable[];
00575     // フォグオプション文字列テーブル
00576     static const String fogOptionStringTable[];
00577 
00578 };
00579 
00580 //------------------------------------------------------------------------------
00581 } // End of namespace Lamp
00582 #endif // End of MATERIAL_H_
00583 //------------------------------------------------------------------------------

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