Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/World.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "World.h"
00004 
00005 #include "BloomEffect.h"
00006 #include "Camera.h"
00007 #include "CoreEngine.h"
00008 #include "DayNightCycle.h"
00009 #include "FontRenderer.h"
00010 #include "Frustum.h"
00011 #include "GPUProgram.h"
00012 #include "Light.h"
00013 #include "Renderer.h"
00014 #include "SceneAnalyseGUI.h"
00015 #include "Sky.h"
00016 
00017 
00018 
00019 namespace pge {
00020 
00021         //****************************************************************************
00022         //
00023         //
00024         //
00025         //****************************************************************************
00026         World::World(void) {
00027                 m_camera = new Camera();
00028                 m_frustum = new Frustum();
00029 
00030                 m_globalLightProgram = new GPUProgram("../data/shader/light_vs.txt", "../data/shader/light_fs.txt");
00031 
00032                 m_bloomEffect = new BloomEffect();
00033 
00034                 m_sky = NULL;
00035         }
00036 
00037 
00038         //****************************************************************************
00039         //
00040         //
00041         //
00042         //****************************************************************************
00043         World::~World(void) {
00044                 std::vector<IRenderableObject*>::iterator it;
00045                 std::vector<Light*>::iterator lightIt;
00046 
00047 
00048                 // Delete objects.
00049                 for(it = m_objects.begin(); it != m_objects.end(); it++) {
00050                         if(*it != NULL) {
00051                                 delete *it;
00052                         }
00053                 }
00054                 // Delete objects without bloom.
00055                 for(it = m_objectsWithoutBloom.begin(); it != m_objectsWithoutBloom.end(); it++) {
00056                         if(*it != NULL) {
00057                                 delete *it;
00058                         }
00059                 }
00060 
00061                 // Delete lights.
00062                 for(lightIt = m_lights.begin(); lightIt != m_lights.end(); lightIt++) {
00063                         if(*lightIt != NULL) {
00064                                 delete *lightIt;
00065                         }
00066                 }
00067 
00068                 // Delete frustum.
00069                 if(m_frustum != NULL) {
00070                         delete m_frustum;
00071                         m_frustum = NULL;
00072                 }
00073 
00074                 // Delete bloom effect.
00075                 if(m_bloomEffect != NULL) {
00076                         delete m_bloomEffect;
00077                         m_bloomEffect = NULL;
00078                 }
00079 
00080                 // Delete global light program.
00081                 if(m_globalLightProgram != NULL) {
00082                         delete m_globalLightProgram;
00083                         m_globalLightProgram = NULL;
00084                 }
00085 
00086                 // Delete sky.
00087                 if(m_sky != NULL) {
00088                         delete m_sky;
00089                         m_sky = NULL;
00090                 }
00091         }
00092 
00093 
00094         //****************************************************************************
00095         //
00096         //
00097         //
00098         //****************************************************************************
00099         bool World::init(void) {
00100                 return true;
00101         }
00102 
00103 
00104         //****************************************************************************
00105         //
00106         //
00107         //
00108         //****************************************************************************
00109         void World::render(void) {
00110                 //
00111                 // Variables
00112                 //
00113                 std::vector<IRenderableObject*>::iterator it;
00114 
00115 
00117                 // 3D Mode for world and object rendering!
00119                 renderer::enter3DMode();
00120 
00121                 m_camera->applyCameraView();
00122 
00123                 // After setting the camera, apply view frustum.
00124                 m_frustum->update();
00125 
00126                 // Prepare bloom effect here. Therefore render all objects that want bloom rendering.
00127                 glViewport(0, 0, m_bloomEffect->getTextureSize(), m_bloomEffect->getTextureSize());
00128                 for(it = m_objects.begin(); it != m_objects.end(); it++) {
00129                         IRenderableObject *object = *it;
00130                         object->render();
00131                 }
00132                 m_bloomEffect->prepareRendering();
00133                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00134                 glViewport(0, 0, CoreEngine::getInstance()->getResolutionWidth(), CoreEngine::getInstance()->getResolutionHeight());
00135 
00136                 // Now render all objects with bloom and all objects without bloom for final rendered image.
00137                 renderer::pushMatrix();
00138                 // Apply the camera view
00139                 m_camera->applyCameraRotation();
00140                 // If a sky is set ...
00141                 if(m_sky != NULL) {
00142                         // ... render sky at first with deactivated z-buffer, so its always seen
00143                         // but all other objects will be in front of the sky.
00144                         glDisable(GL_DEPTH_TEST);
00145                         m_sky->render();
00146                         glEnable(GL_DEPTH_TEST);
00147                 }
00148                 renderer::popMatrix();
00149 
00150                 m_camera->applyCameraView();
00151                 for(it = m_objectsWithoutBloom.begin(); it != m_objectsWithoutBloom.end(); it++) {
00152                         IRenderableObject *object = *it;
00153                         object->render();
00154                 }
00155                 for(it = m_objects.begin(); it != m_objects.end(); it++) {
00156                         IRenderableObject *object = *it;
00157                         object->render();
00158                 }
00159 
00161                 // 2D Mode for GUI rendering!
00163                 renderer::enter2DMode(0.0f, (float)CoreEngine::getInstance()->getResolutionWidth(), 0.0f, (float)CoreEngine::getInstance()->getResolutionHeight(), -1.0f, 1.0f);
00164 
00165                 // Apply the bloom image to the scene.
00166                 m_bloomEffect->render();
00167 
00168                 // Finally render the GUI stuff.
00169                 gui::SceneAnalyseGUI::getInstance()->render();
00170         }
00171 
00172 
00173         //****************************************************************************
00174         //
00175         //
00176         //
00177         //****************************************************************************
00178         void World::timer(unsigned int delay) {
00179                 //
00180                 // Variables
00181                 //
00182                 std::vector<IRenderableObject*>::iterator it;
00183 
00184 
00185                 for(it = m_objects.begin(); it != m_objects.end(); it++) {
00186                         IRenderableObject *object = *it;
00187                         object->timer(delay);
00188                 }
00189                 for(it = m_objectsWithoutBloom.begin(); it != m_objectsWithoutBloom.end(); it++) {
00190                         IRenderableObject *object = *it;
00191                         object->timer(delay);
00192                 }
00193 
00194                 if(m_camera != NULL) {
00195                         m_camera->timer(delay);
00196                 }
00197 
00198                 DayNightCycle::getInstance()->update(delay);
00199 
00200                 if(m_sky != NULL) {
00201                         m_sky->timer(delay);
00202                 }
00203 
00204                 gui::SceneAnalyseGUI::getInstance()->timer(delay);
00205         }
00206 
00207 
00208         //****************************************************************************
00209         //
00210         //
00211         //
00212         //****************************************************************************
00213         void World::action(InputSystem::InputAction type, int param0, int param1) {
00214                 switch(type) {
00215                         case InputSystem::Forward:
00216                                 m_camera->forward();
00217                                 break;
00218                         case InputSystem::Backward:
00219                                 m_camera->backward();
00220                                 break;
00221                         case InputSystem::Up:
00222                                 m_camera->up();
00223                                 break;
00224                         case InputSystem::Down:
00225                                 m_camera->down();
00226                                 break;
00227                         case InputSystem::SlideLeft:
00228                                 m_camera->slideLeft();
00229                                 break;
00230                         case InputSystem::SlideRight:
00231                                 m_camera->slideRight();
00232                                 break;
00233                         case InputSystem::TurnLeft:
00234                                 m_camera->turnLeft(param0);
00235                                 break;
00236                         case InputSystem::TurnRight:
00237                                 m_camera->turnRight(param0);
00238                                 break;
00239                         case InputSystem::TurnUp:
00240                                 m_camera->turnUp(param0);
00241                                 break;
00242                         case InputSystem::TurnDown:
00243                                 m_camera->turnDown(param0);
00244                                 break;
00245                         default:
00246                                 break;
00247                 }
00248         }
00249 
00250 
00251         //****************************************************************************
00252         //
00253         //
00254         //
00255         //****************************************************************************
00256         Camera* World::getCamera(void) {
00257                 return m_camera;
00258         }
00259 
00260 
00261         //****************************************************************************
00262         //
00263         //
00264         //
00265         //****************************************************************************
00266         Frustum* World::getFrustum(void) {
00267                 return m_frustum;
00268         }
00269 
00270 
00271         //****************************************************************************
00272         //
00273         //
00274         //
00275         //****************************************************************************
00276         GPUProgram* World::getLightProgram(void) {
00277                 return m_globalLightProgram;
00278         }
00279 
00280 
00281         //****************************************************************************
00282         //
00283         //
00284         //
00285         //****************************************************************************
00286         void World::setSky(ISky *sky) {
00287                 if(m_sky != NULL) {
00288                         delete m_sky;
00289                         m_sky = NULL;
00290                 }
00291                 m_sky = sky;
00292         }
00293 
00294 
00295         //****************************************************************************
00296         //
00297         //
00298         //
00299         //****************************************************************************
00300         void World::addRenderableObject(IRenderableObject *object, bool disableBloom) {
00301 
00302                 object->init();
00303 
00304                 if(disableBloom) {
00305                         m_objectsWithoutBloom.push_back(object);
00306                 } else {
00307                         m_objects.push_back(object);
00308                 }
00309         }
00310 
00311 
00312         //****************************************************************************
00313         //
00314         //
00315         //
00316         //****************************************************************************
00317         void World::clearRenderableObjects() {
00318                 m_objects.clear();
00319         }
00320 
00321 
00322         //****************************************************************************
00323         //
00324         //
00325         //
00326         //****************************************************************************
00327         void World::addLight(Light *light) {
00328                 m_lights.push_back(light);
00329         }
00330 
00331 
00332         //****************************************************************************
00333         //
00334         //
00335         //
00336         //****************************************************************************
00337         int World::getLightNum(void) {
00338                 return (int)m_lights.size();
00339         }
00340 
00341 
00342         //****************************************************************************
00343         //
00344         //
00345         //
00346         //****************************************************************************
00347         Light* World::getLightAt(int index) {
00348                 return m_lights.at(index);
00349         }
00350 };

Generated on Mon Oct 16 12:08:11 2006 for Phobosengine by doxygen 1.3.4