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

Scene.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/Scene/Scene.h"
00027 #include "Core/Renamer/CountRenamer.h"
00028 #include "Graphics/Camera/CameraManager.h"
00029 #include "Graphics/Fog/Fog.h"
00030 #include "Graphics/SceneNode/SceneNodeManager.h"
00031 #include "Graphics/Light/LightManager.h"
00032 #include "Graphics/Model/ModelManager.h"
00033 #include "Graphics/Mesh/MeshManager.h"
00034 #include "Graphics/MeshData/MeshDataManager.h"
00035 #include "Graphics/Material/MaterialManager.h"
00036 #include "Graphics/Texture/TextureManager.h"
00037 #include "Graphics/Picture/PictureManager.h"
00038 #include "Graphics/Renderer/DrawRequest.h"
00039 
00040 namespace Lamp{
00041 
00042 //------------------------------------------------------------------------------
00043 // コンストラクタ
00044 Scene::Scene(const String& name) :
00045     name_(name), currentCamera_(NULL), tick_(0){
00046     // リネーマの初期化
00047     renamer_ = new CountRenamer();
00048     // 各マネージャ初期化
00049     cameraManager_ = new CameraManager(this);
00050     sceneNodeManager_ = new SceneNodeManager(this);
00051     lightManager_ = new LightManager(this);
00052     modelManager_ = new ModelManager(this);
00053     meshManager_ = new MeshManager(this);
00054     meshDataManager_ = new MeshDataManager(this);
00055     materialManager_ = new MaterialManager(this);
00056     textureManager_ = new TextureManager(this);
00057     pictureManager_ = new PictureManager(this);
00058     // ルートノード初期化
00059     rootNode_ = sceneNodeManager_->createSceneNode("RootNode");
00060     // フォグ初期化
00061     fog_ = new Fog();
00062 }
00063 //------------------------------------------------------------------------------
00064 // デストラクタ
00065 Scene::~Scene(){
00066     // フォグの後始末
00067     SafeDelete(fog_);
00068     // ルートノードの後始末
00069     sceneNodeManager_->destroy(rootNode_);
00070     // 各マネージャの後始末
00071     SafeDelete(pictureManager_);
00072     SafeDelete(textureManager_);
00073     SafeDelete(materialManager_);
00074     SafeDelete(meshDataManager_);
00075     SafeDelete(meshManager_);
00076     SafeDelete(modelManager_);
00077     SafeDelete(lightManager_);
00078     SafeDelete(sceneNodeManager_);
00079     SafeDelete(cameraManager_);
00080     // リネーマの後始末
00081     SafeDelete(renamer_);
00082 }
00083 //------------------------------------------------------------------------------
00084 // クリア
00085 int Scene::clear(){
00086     int result = 0;
00087     result += pictureManager_->clear();
00088     result += textureManager_->clear();
00089     result += materialManager_->clear();
00090     result += meshDataManager_->clear();
00091     result += meshManager_->clear();
00092     // RootNodeを残すため、SceneLeafよりも先にSceneNodeをクリアする必要がある
00093     result += sceneNodeManager_->clear();
00094     result += modelManager_->clear();
00095     result += lightManager_->clear();
00096     result += cameraManager_->clear();
00097     return result;
00098 }
00099 //------------------------------------------------------------------------------
00100 // 走査
00101 void Scene::traverse(){
00102     // チックのインクリメント
00103     tick_++;
00104     rootNode_->traverse();
00105 }
00106 //------------------------------------------------------------------------------
00107 // メッシュリストの取得
00108 void Scene::getMeshList(ArrayList<Mesh*>* meshList, Camera* camera){
00109     // シーン管理システムによる高速な刈り込みに動的に切り替えれるようにする
00110     int meshCount = meshManager_->getCount();
00111     for(int i = 0; i < meshCount; i++){
00112         Mesh* mesh = meshManager_->get(i);
00113         if(!mesh->isGlobalEnabled()){ continue; }
00114         // マテリアル、メッシュデータを持っていないメッシュは禁止
00115         Assert(mesh->getMaterial() != NULL);
00116         Assert(mesh->getMeshData() != NULL);
00117         // スフィアチェックにより見えないメッシュをはじく
00118         Clipping::State clippingState =
00119             camera->clipping(mesh->getWorldBoundingSphere());
00120         if(clippingState == Clipping::invisible){ continue; }
00121         // 交差している場合はボックスチェックも行う
00122         if(clippingState == Clipping::intersect){
00123             clippingState = camera->clipping(mesh->getWorldBoundingBox());
00124             if(clippingState == Clipping::invisible){ continue; }
00125         }
00126         // リストに追加
00127         meshList->add(mesh);
00128     }
00129 }
00130 //------------------------------------------------------------------------------
00131 // ローカルライトリストの取得
00132 void Scene::getLocalLightList(Mesh* mesh, DrawRequest* drawRequest){
00133     // シーン管理システムによる高速な刈り込みに動的に切り替えれるようにする
00134     const Sphere& meshSphere = mesh->getWorldBoundingSphere();
00135     const AxisAlignedBox& meshBox = mesh->getWorldBoundingBox();
00136     u_int lightMask = drawRequest->getMaterial()->getLightMask();
00137     int lightCount = lightManager_->getCount();
00138     for(int i = 0; i < lightCount; i++){
00139         Light* light = lightManager_->get(i);
00140         if(!light->isLocalLight()){ continue; }
00141         if(!light->isGlobalEnabled()){ continue; }
00142         // ライトマスク判定
00143         if((light->getLightMask() & lightMask) == 0){ continue; }
00144         if(light->isPointLight()){
00145             // ポイントライト
00146             PointLight* pointLight = light->castPointLight();
00147             // スフィアチェック
00148             float limitSquaredDistance =
00149                 pointLight->getRange() + meshSphere.getRadius();
00150             limitSquaredDistance *= limitSquaredDistance;
00151             float checkValue = limitSquaredDistance -
00152                 (pointLight->getWorldPosition() -
00153                 meshSphere.getCenter()).getSquaredLength();
00154             if(checkValue <= 0.f){ continue; }
00155             // ボックスチェック
00156             Sphere lightSphere(
00157                 pointLight->getWorldPosition(), pointLight->getRange());
00158             if(!meshBox.intersect(lightSphere)){ continue; }
00159             // 描画リクエストに追加
00160             drawRequest->addLocalLight(pointLight);
00161         }else{
00162             ErrorOut("Scene::getLocalLightList() 非サポートのライトです");
00163         }
00164     }
00165 }
00166 //------------------------------------------------------------------------------
00167 // リネーマの設定
00168 void Scene::setRenamer(Renamer* renamer){
00169     Assert(renamer != NULL);
00170     SafeDelete(renamer_);
00171     renamer_ = renamer;
00172 }
00173 //------------------------------------------------------------------------------
00174 // デバイスオブジェクトの初期化
00175 bool Scene::initializeGraphicsDeviceObjects(){
00176     if(!meshManager_->initializeGraphicsDeviceObjects()){ return false; }
00177     if(!meshDataManager_->initializeGraphicsDeviceObjects()){ return false; }
00178     if(!materialManager_->initializeGraphicsDeviceObjects()){ return false; }
00179     if(!pictureManager_->initializeGraphicsDeviceObjects()){ return false; }
00180     return true;
00181 }
00182 //------------------------------------------------------------------------------
00183 // デバイスオブジェクトの削除
00184 void Scene::deleteGraphicsDeviceObjects(){
00185     pictureManager_->deleteGraphicsDeviceObjects();
00186     materialManager_->deleteGraphicsDeviceObjects();
00187     meshDataManager_->deleteGraphicsDeviceObjects();
00188     meshManager_->deleteGraphicsDeviceObjects();
00189 }
00190 //------------------------------------------------------------------------------
00191 // デバイスオブジェクトのリストア
00192 bool Scene::restoreGraphicsDeviceObjects(){
00193     if(!meshManager_->restoreGraphicsDeviceObjects()){ return false; }
00194     if(!meshDataManager_->restoreGraphicsDeviceObjects()){ return false; }
00195     if(!materialManager_->restoreGraphicsDeviceObjects()){ return false; }
00196     if(!pictureManager_->restoreGraphicsDeviceObjects()){ return false; }
00197     return true;
00198 }
00199 //------------------------------------------------------------------------------
00200 // デバイスオブジェクトの無効化
00201 void Scene::invalidateGraphicsDeviceObjects(){
00202     pictureManager_->invalidateGraphicsDeviceObjects();
00203     materialManager_->invalidateGraphicsDeviceObjects();
00204     meshDataManager_->invalidateGraphicsDeviceObjects();
00205     meshManager_->invalidateGraphicsDeviceObjects();
00206 }
00207 //------------------------------------------------------------------------------
00208 } // End of namespace Lamp
00209 //------------------------------------------------------------------------------

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