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

FixedShader.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/Shader/FixedShader.h"
00027 #include "Graphics/Renderer/RenderingDevice.h"
00028 #include "Graphics/Renderer/DrawRequest.h"
00029 #include "Graphics/Fog/Fog.h"
00030 #include "Graphics/SceneNode/SceneNode.h"
00031 #include "Graphics/Light/DirectionalLight.h"
00032 #include "Graphics/Light/PointLight.h"
00033 #include "Graphics/MeshData/MeshData.h"
00034 #include "Graphics/Material/Material.h"
00035 #include "Graphics/Texture/SurfaceTexture.h"
00036 
00037 
00038 #include "Graphics/Model/Model.h"
00039 
00040 namespace Lamp{
00041 
00042 //------------------------------------------------------------------------------
00043 // コンストラクタ
00044 FixedShader::FixedShader(){
00045 }
00046 //------------------------------------------------------------------------------
00047 // デストラクタ
00048 FixedShader::~FixedShader(){
00049 }
00050 //------------------------------------------------------------------------------
00051 // ステートブロック
00052 //------------------------------------------------------------------------------
00053 // マテリアル開始の構築
00054 void FixedShader::buildMaterialStart(Material* material){
00055     Shader::buildMaterialStart(material);
00056 }
00057 //------------------------------------------------------------------------------
00058 // マテリアル終了の構築
00059 //void FixedShader::buildMaterialEnd(Material* material){
00060 //  Shader::buildMaterialEnd(material);
00061 //}
00062 //------------------------------------------------------------------------------
00063 // テクスチャ
00064 //------------------------------------------------------------------------------
00065 // 固定機能テクスチャ設定
00066 void FixedShader::setFixedTexture(int textureStage, Texture* texture){
00067     // テクスチャ設定
00068     device_->setTexture(textureStage, texture);
00069     // サーフェーステクスチャ設定
00070     SurfaceTexture* surfaceTexture = texture->castSurfaceTexture();
00071     if(surfaceTexture != NULL){
00072         // UVのリピートとオフセット
00073         device_->setTextureTransform2(textureStage,
00074             surfaceTexture->getRepeatUV(), surfaceTexture->getOffsetUV());
00075         // アドレスモード設定
00076         device_->setTextureAddressMode2(textureStage,
00077             surfaceTexture->getAddressModeU(),
00078             surfaceTexture->getAddressModeV());
00079     }
00080 }
00081 //------------------------------------------------------------------------------
00082 // 固定機能ベーステクスチャの設定
00083 int FixedShader::setFixedBaseTexture(
00084     int colorStage, Texture* baseTexture, int baseUVIndex){
00085     if(baseTexture == NULL){ return colorStage; }
00086     // テクスチャ設定
00087     setFixedTexture(colorStage, baseTexture);
00088     // Current * baseTexture
00089     device_->setColorTextureStage(colorStage,
00090         D3DTOP_MODULATE, D3DTA_CURRENT, D3DTA_TEXTURE, baseUVIndex);
00091     colorStage++;
00092     return colorStage;
00093 }
00094 //------------------------------------------------------------------------------
00095 // 固定機能ライトテクスチャの設定
00096 int FixedShader::setFixedLightTexture(
00097     int colorStage, Texture* lightTexture, int lightUVIndex){
00098     if(lightTexture == NULL){ return colorStage; }
00099     // テクスチャ設定
00100     setFixedTexture(colorStage, lightTexture);
00101     // Current + lightTexture
00102     device_->setColorTextureStage(colorStage,
00103         D3DTOP_ADD, D3DTA_CURRENT, D3DTA_TEXTURE, lightUVIndex);
00104     colorStage++;
00105     return colorStage;
00106 }
00107 //------------------------------------------------------------------------------
00108 // 固定機能汚れテクスチャの設定
00109 int FixedShader::setFixedStainTexture(
00110     int colorStage, Texture* stainTexture, int stainUVIndex){
00111     if(stainTexture == NULL){ return colorStage; }
00112     // テクスチャ設定
00113     setFixedTexture(colorStage, stainTexture);
00114     // Current * stainTexture
00115     device_->setColorTextureStage(colorStage,
00116         D3DTOP_MODULATE, D3DTA_CURRENT, D3DTA_TEXTURE, stainUVIndex);
00117     colorStage++;
00118     return colorStage;
00119 }
00120 //------------------------------------------------------------------------------
00121 // 描画の初期化
00122 //------------------------------------------------------------------------------
00123 // 固定機能描画のセットアップ
00124 void FixedShader::setupFixedDraw(DrawRequest* request){
00125     // 法線の正規化設定
00126     device_->setRenderState(D3DRS_NORMALIZENORMALS,
00127         request->requireNormalize());
00128     // 行列の設定
00129     setMatrixFixed(request);
00130     // フォグオプションの設定
00131     setFogOptionFixed(request);
00132     // ライトのセットアップ
00133     setupFixedLight(request);
00134 }
00135 //------------------------------------------------------------------------------
00136 // 固定機能行列の設定
00137 void FixedShader::setMatrixFixed(DrawRequest* request){
00138     if(request->isSceneNodeChanged()){
00139         device_->setWorldMatrix(request->getSceneNode()->getWorldMatrix());
00140     }
00141 }
00142 //------------------------------------------------------------------------------
00143 // 固定機能フォグオプションの設定
00144 void FixedShader::setFogOptionFixed(DrawRequest* request){
00145     Material::FogOption fogOption = request->getMaterial()->getFogOption();
00146     if(fogOption == Material::fogOptionDisable){
00147         device_->setRenderState(D3DRS_FOGENABLE, false);
00148     }else if(fogOption == Material::fogOptionBlack){
00149         device_->setRenderState(D3DRS_FOGCOLOR, 0);
00150     }
00151 }
00152 //------------------------------------------------------------------------------
00153 // 固定機能ライトのセットアップ
00154 void FixedShader::setupFixedLight(DrawRequest* request){
00155     Material* material = request->getMaterial();
00156     // ライトを使用しないのならば何もしない
00157     if(!material->useLight()){ return; }
00158 
00159     // アンビエントライトの設定
00160     // Requestの中でlightMask判定が行われている
00161     device_->setAmbientColor(request->getAmbientColor());
00162 
00163     // ディレクショナルライトの設定
00164     u_int lightMask = material->getLightMask();
00165     int lightCount = 0;
00166     int directionalLightCount = request->getDirectionalLightCount();
00167     for(int i = 0; i < directionalLightCount; i++){
00168         DirectionalLight* directionalLight = request->getDirectionalLight(i);
00169         // lightMask判定
00170         if((directionalLight->getLightMask() & lightMask) == 0){ continue; }
00171         device_->enableDirectionalLight(lightCount, directionalLight);
00172         lightCount++;
00173         // 最大ライト数分設定したら終了する
00174         if(lightCount == maxActiveLightCount_){ return; }
00175     }
00176 
00177     // ローカルライトの設定
00178     // シーンからローカルライトリストを取得する時にlightMask判定が行われている
00179     int localLightCount = request->getLocalLightCount();
00180     // ローカルライトが最大数を超えている場合ソートする
00181     if(localLightCount + lightCount > maxActiveLightCount_){
00182         request->sortLocalLights();
00183     }
00184     for(int i = 0; i < localLightCount; i++){
00185         LocalLight* localLight = request->getLocalLight(i);
00186         if(localLight->isPointLight()){
00187             device_->enablePointLight(lightCount, localLight->castPointLight());
00188         }else{
00189             ErrorOut("FixedShader::setupFixedLight() 非サポートのライトです");
00190         }
00191         lightCount++;
00192         // 最大ライト数分設定したら終了する
00193         if(lightCount == maxActiveLightCount_){ return; }
00194     }
00195 
00196     // ライトを閉じる
00197     device_->closeLight(lightCount);
00198 }
00199 //------------------------------------------------------------------------------
00200 // 描画
00201 //------------------------------------------------------------------------------
00202 // 固定機能描画
00203 void FixedShader::drawFixed(DrawRequest* request){
00204     Mesh* mesh = request->getMesh();
00205     // キャラクタ変形
00206     bool deformed = false;
00207     deformed |= mesh->characterDeform();
00208     if(deformed){
00209         // メッシュは必ず一回しか描画されないのでストリームキャッシュを行えない
00210         // 変形頂点記述を設定
00211         device_->setVertexDeclaration(mesh->getDeformedVertexDeclaration());
00212         // 変形頂点バッファを設定
00213         device_->setVertexBuffer(mesh->getDeformedVertexBuffer(),
00214             mesh->getDeformedVertexSize());
00215     // 同じメッシュデータでパイプラインに変更が無ければストリーム設定を行わない
00216     }else if(request->isMeshDataChanged() || request->isPipelineModeChanged()){
00217         // 頂点記述を設定
00218         device_->setVertexDeclaration(mesh->getVertexDeclaration());
00219         // 頂点バッファを設定
00220         device_->setVertexBuffer(mesh->getVertexBuffer(),
00221             mesh->getVertexSize());
00222     }
00223     // 描画コール
00224     drawCall(request);
00225 }
00226 //------------------------------------------------------------------------------
00227 // 描画の後始末
00228 //------------------------------------------------------------------------------
00229 // 固定機能描画のリセット
00230 void FixedShader::resetFixedDraw(DrawRequest* request){
00231     // フォグオプションのリセット
00232     resetFogOptionFixed(request);
00233 }
00234 //------------------------------------------------------------------------------
00235 // 固定機能フォグオプションのリセット
00236 void FixedShader::resetFogOptionFixed(DrawRequest* request){
00237     Material::FogOption fogOption = request->getMaterial()->getFogOption();
00238     if(fogOption == Material::fogOptionDisable){
00239         device_->setRenderState(D3DRS_FOGENABLE, true);
00240     }else if(fogOption == Material::fogOptionBlack){
00241         device_->setRenderState(D3DRS_FOGCOLOR,
00242             request->getFog()->getColor().getARGB());
00243     }
00244 }
00245 //------------------------------------------------------------------------------
00246 } // End of namespace Lamp
00247 //------------------------------------------------------------------------------

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