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/Renderer/DrawRequest.h" 00027 #include "Graphics/Renderer/Renderer.h" 00028 #include "Graphics/SceneNode/SceneNode.h" 00029 #include "Graphics/Light/AmbientLight.h" 00030 #include "Graphics/Light/DirectionalLight.h" 00031 #include "Graphics/Light/PointLight.h" 00032 #include "Graphics/Model/Model.h" 00033 #include "Graphics/Mesh/Mesh.h" 00034 #include "Graphics/Material/Material.h" 00035 00036 namespace Lamp{ 00037 00038 // インスタンス 00039 DrawRequest* DrawRequest::instance_ = NULL; 00040 00041 //------------------------------------------------------------------------------ 00042 // コンストラクタ 00043 DrawRequest::DrawRequest(){ 00044 clear(); 00045 Assert(instance_ == NULL); 00046 instance_ = this; 00047 } 00048 //------------------------------------------------------------------------------ 00049 // デストラクタ 00050 DrawRequest::~DrawRequest(){ 00051 Assert(instance_ != NULL); 00052 instance_ = NULL; 00053 } 00054 //------------------------------------------------------------------------------ 00055 // クリア 00056 void DrawRequest::clear(){ 00057 directionalLights_.clear(); 00058 ambientLights_.clear(); 00059 localLights_.clear(); 00060 fog_ = NULL; 00061 camera_ = NULL; 00062 mesh_ = preMesh_ = NULL; 00063 sceneNode_ = preSceneNode_ = NULL; 00064 model_ = preModel_ = NULL; 00065 meshData_ = preMeshData_ = NULL; 00066 material_ = preMaterial_ = NULL; 00067 } 00068 //------------------------------------------------------------------------------ 00069 // メッシュの設定 00070 void DrawRequest::setMesh(Mesh* mesh){ 00071 Assert(mesh != NULL); 00072 preMesh_ = mesh_; 00073 preSceneNode_ = sceneNode_; 00074 preModel_ = model_; 00075 preMeshData_ = meshData_; 00076 preMaterial_ = material_; 00077 mesh_ = mesh; 00078 model_ = mesh_->getParent(); 00079 sceneNode_ = model_->getParent(); 00080 meshData_ = mesh_->getMeshData(); 00081 material_ = mesh_->getMaterial(); 00082 Assert(sceneNode_ != NULL); 00083 Assert(model_ != NULL); 00084 Assert(meshData_ != NULL); 00085 Assert(material_ != NULL); 00086 } 00087 //------------------------------------------------------------------------------ 00088 // 正規化を必要とするか 00089 bool DrawRequest::requireNormalize() const{ 00090 // シーンノードにスケールが含まれるなら正規化を行う 00091 bool result = sceneNode_->isGlobalScaled(); 00092 // メッシュが正規化を必要とするなら正規化を行う 00093 result |= mesh_->requireNormalize(); 00094 return result; 00095 } 00096 //------------------------------------------------------------------------------ 00097 // パイプラインモードに変更があったか 00098 bool DrawRequest::isPipelineModeChanged() const{ 00099 if(preMaterial_ == NULL){ return true; } 00100 return (material_->getPipelineMode() != 00101 preMaterial_->getPipelineMode()); 00102 } 00103 //------------------------------------------------------------------------------ 00104 // ブレンドが有効になった 00105 bool DrawRequest::isBlendEnabled() const{ 00106 Assert(material_ != NULL); 00107 bool alphaBlend = material_->isBlendEnabled(); 00108 if(preMaterial_ == NULL){ 00109 return alphaBlend; 00110 }else{ 00111 bool preAlphaBlend = preMaterial_->isBlendEnabled(); 00112 // アルファ状態からアルファでない状態にはならない 00113 Assert(!(preAlphaBlend && (!alphaBlend))); 00114 if((!preAlphaBlend) && alphaBlend){ return true; } 00115 } 00116 return false; 00117 } 00118 //------------------------------------------------------------------------------ 00119 // アンビエントライトの追加 00120 void DrawRequest::addAmbientLight(AmbientLight* ambientLight){ 00121 Assert(ambientLight->isAmbientLight()); 00122 ambientLights_.add(ambientLight); 00123 } 00124 //------------------------------------------------------------------------------ 00125 // アンビエント色の取得 00126 Color3f DrawRequest::getAmbientColor() const{ 00127 Assert(material_ != NULL); 00128 u_int lightMask = material_->getLightMask(); 00129 Color3f ambientColor(Color3f::black); 00130 int ambientLightCount = ambientLights_.getCount(); 00131 for(int i = 0; i < ambientLightCount; i++){ 00132 AmbientLight* ambientLight = ambientLights_.get(i); 00133 // ライトマスク判定 00134 if((ambientLight->getLightMask() & lightMask) == 0){ continue; } 00135 ambientColor += ambientLight->getColor(); 00136 } 00137 return ambientColor; 00138 } 00139 //------------------------------------------------------------------------------ 00140 // ディレクショナルライトの追加 00141 void DrawRequest::addDirectionalLight(DirectionalLight* directionalLight){ 00142 Assert(directionalLight->isDirectionalLight()); 00143 directionalLights_.add(directionalLight); 00144 } 00145 //------------------------------------------------------------------------------ 00146 // ローカルライトの追加 00147 void DrawRequest::addLocalLight(LocalLight* localLight){ 00148 Assert(localLight->isLocalLight()); 00149 localLights_.add(localLight); 00150 } 00151 //------------------------------------------------------------------------------ 00152 // ローカルライトのソート用コールバック 00153 int DrawRequest::sortLocalLightsCallback( 00154 LocalLight* const* left, LocalLight* const* right){ 00155 return instance_->sortLocalLightsImprement((*left), (*right)); 00156 } 00157 //------------------------------------------------------------------------------ 00158 // ローカルライトのソート実装 00159 int DrawRequest::sortLocalLightsImprement(LocalLight* left, LocalLight* right){ 00160 Assert(mesh_ != NULL); 00161 // メッシュに近いものから遠いものへソート 00162 // メッシュ中心に対して最も明るいものでソートしたほうがいい 00163 // 輝度と減衰(distance - near)/(far - near)をかけたもので 00164 // LocalLightにgetWorldLuminance()やgetWorldSquaredDistance() 00165 const Vector3& meshCenter = mesh_->getWorldCenter(); 00166 PointLight* pointLeft = left->castPointLight(); 00167 Assert(pointLeft != NULL); 00168 PointLight* pointRight = right->castPointLight(); 00169 Assert(pointRight != NULL); 00170 float leftDistance = 00171 (pointLeft->getWorldPosition() - meshCenter).getSquaredLength(); 00172 float rightDistance = 00173 (pointRight->getWorldPosition() - meshCenter).getSquaredLength(); 00174 if((rightDistance - leftDistance) > 0.f){ return -1; } 00175 else{ return 1; } 00176 } 00177 //------------------------------------------------------------------------------ 00178 } // End of namespace Lamp 00179 //------------------------------------------------------------------------------