Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/map/Map.cc @ 3113

Last change on this file since 3113 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 18.4 KB
RevLine 
[2837]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Si Sun
24 *
25 */
26#include "Map.h"
[3089]27
[2837]28#include <string>
[2942]29#include "util/String.h"
[2856]30#include <OgreSceneManager.h>
31#include <OgreSceneNode.h>
32#include <OgreEntity.h>
[2913]33#include <OgreNode.h>
34
[2942]35
[2913]36#include <OgreRenderWindow.h>
37#include <OgreRenderTexture.h>
38#include <OgreTexture.h>
39#include <OgreViewport.h>
[2942]40
[2913]41#include <OgreMaterialManager.h>
42#include <OgreRoot.h>
43#include <OgreHardwarePixelBuffer.h>
44#include "objects/worldentities/ControllableEntity.h"
45#include "objects/worldentities/CameraPosition.h"
46
[2837]47#include <OgreOverlay.h>
[2977]48#include <OgreMovablePlane.h>
[2837]49#include <OgreOverlayElement.h>
50#include <OgreOverlayManager.h>
51#include <OgreOverlayContainer.h>
52#include "core/CoreIncludes.h"
53#include "core/ConfigValueIncludes.h"
54#include "core/ConsoleCommand.h"
55#include "objects/Scene.h"
[2913]56#include "objects/RadarViewable.h"
[2942]57#include "objects/controllers/HumanController.h"
[3089]58
[2837]59 namespace orxonox
60 {
61    CreateFactory(Map);
[2942]62    SetConsoleCommand(Map, openMap, true);
[2956]63    //SetConsoleCommand(Map, rotateYaw, true).setAsInputCommand();
64    //SetConsoleCommand(Map, rotatePitch, true).setAsInputCommand();
65    SetConsoleCommand(Map, Zoom, true).setAsInputCommand();
[2942]66
[3082]67
[2942]68    Map* Map::singletonMap_s = 0;
[2956]69    Ogre::SceneManager* Map::mapSceneM_s = 0;
[3101]70    Ogre::Camera* Map::Cam_ = 0;
71    Ogre::SceneNode* Map::CamNode_ = 0;
72    Ogre::MaterialPtr Map::OverlayMaterial_;// = init();
73    Ogre::Overlay* Map::overlay_ = 0;
74/*
75Ogre::MaterialPtr Map::init()
76{
77    Ogre::MaterialPtr tmp;
78    tmp.setNull();
79    return tmp;
80}
81*/
82
[2956]83    //int Map::mouseLookSpeed_ = 200;
84    //Ogre::SceneNode* Map::playerShipNode_ = 0;
85
86    const int PITCH=-30;
87    const int DISTANCE=200;
88
[2837]89    Map::Map(BaseObject* creator) : OrxonoxOverlay(creator)
90    {
91        RegisterObject(Map);
[2942]92        Map::singletonMap_s=this;
[3089]93
[2856]94        //Getting Scene Manager (Hack)
[2956]95        if( !sManager_ )
96        {
97            ObjectList<Scene>::iterator it = ObjectList<Scene>::begin();
98            this->sManager_ = it->getSceneManager();
99        }
100        if( !Map::getMapSceneManager() )
101        {
102            Map::setMapSceneManager( Ogre::Root::getSingletonPtr()->createSceneManager( Ogre::ST_GENERIC,"MapScene" ) );
103        }
[3089]104
[2956]105        this->playerShipNode_ = 0;
[2913]106        //this->sNode_ = new Ogre::SceneNode(sManager_);
107        //oManager_ = Ogre::OverlayManager::getSingletonPtr();
108        //overlay_ = oManager_->create("Map");
[2856]109        //overlay_ is member of OrxonoxOverlay
[3089]110
[2856]111        //Not Showing the map as default
[2913]112        //this->isVisible_=false;
113        //overlay_->hide();
[2956]114        this->mouseLookSpeed_ = 200;
[2913]115
[2856]116        //TestEntity
[2956]117        //Ogre::Entity * ent = mapSceneM_s->createEntity("ent", "drone.mesh");
[3089]118
[2956]119        //Map::getMapSceneManager()->getRootSceneNode()->attachObject( ent );
[2913]120        /*sNode_->setPosition(0,0,-50);
[2856]121        overlay_->add3D(sNode_);
[2913]122        */
123
[3089]124
125
126
127
[2913]128        // Alter the camera aspect ratio to match the viewport
129        //mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
[3101]130        if(!Map::Cam_)
131            Cam_ = Map::getMapSceneManager()->createCamera("ReflectCam");
[2956]132        //Cam_->setPosition(200,170, -160);
133        //Cam_->lookAt(0,0,0);
134        Cam_->setAspectRatio(1);
135        //Cam_->setRenderingDistance(0);
[3101]136        if(!Map::CamNode_)
137            CamNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode();
[2913]138
[3089]139
[2942]140        //Create overlay material
[3101]141        if(Map::OverlayMaterial_.isNull())
142            Map::OverlayMaterial_ = this->createRenderCamera(Cam_, "RttMat");
[2913]143/*
144        Ogre::TexturePtr rttTex = Ogre::TextureManager::getSingleton().createManual("RttTex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
145
146        Ogre::RenderTexture *renderTexture = rttTex->getBuffer()->getRenderTarget();
147
[2956]148        renderTexture->addViewport(Cam_);
[2913]149        renderTexture->getViewport(0)->setClearEveryFrame(true);
150        renderTexture->getViewport(0)->setBackgroundColour(ColourValue::Black);
151        renderTexture->getViewport(0)->setOverlaysEnabled(false);
152
153        Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("RttMat", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
154        Ogre::Technique *technique = material->createTechnique();
155        technique->createPass();
156        material->getTechnique(0)->getPass(0)->setLightingEnabled(false);
157        material->getTechnique(0)->getPass(0)->createTextureUnitState("RttTex");
158*/
159
160
161        // create overlay
162/*
163        Ogre::Overlay* pOverlay = Ogre::OverlayManager::getSingleton().create("Overlay1");
[3089]164
[2913]165        // Create a panel with RenderToTexture texture
166        Ogre::OverlayContainer* m_pOverlayPanel = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel","OverlayPanelName%d"));
167        m_pOverlayPanel->setMetricsMode(Ogre::GMM_PIXELS);
168        m_pOverlayPanel->setPosition(10, 10);
169        m_pOverlayPanel->setDimensions(500, 300);
170        // Give overlay a texture
[3089]171        m_pOverlayPanel->setMaterialName(camMat_id);
[2913]172        pOverlay->add2D(m_pOverlayPanel);
173        pOverlay->show();
174*/
[3101]175        if(!this->overlay_)
176        {
177            this->overlay_ = Ogre::OverlayManager::getSingletonPtr()->create("MapOverlay");
178            Ogre::OverlayContainer* m_pOverlayPanel = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel","OverlayPanelName%d"));
179            //m_pOverlayPanel->setMetricsMode(Ogre::GMM_PIXELS);
180            //m_pOverlayPanel->setPosition(10, 10);
181            //m_pOverlayPanel->setDimensions(600, 400);
182            m_pOverlayPanel->setPosition(0.01, 0.003);
183            m_pOverlayPanel->setDimensions(0.5, 0.4);
184            // Give overlay a texture
185            m_pOverlayPanel->setMaterialName("RttMat");
186            overlay_->add2D(m_pOverlayPanel);
[3082]187
[3101]188            //Add Borders
189            Ogre::BorderPanelOverlayElement* oBorder = static_cast<Ogre::BorderPanelOverlayElement*>(Ogre::OverlayManager::getSingletonPtr()->createOverlayElement("BorderPanel", "MapBorderPanel" + getUniqueNumberString()));
190            oBorder->setBorderSize( 0.003, 0.003 );
191            oBorder->setDimensions(0.5, 0.4);
192            oBorder->setBorderMaterialName("StatsBorder");
193            oBorder->setTopBorderUV(0.49, 0.0, 0.51, 0.5);
194            oBorder->setTopLeftBorderUV(0.0, 0.0, 0.5, 0.5);
195            oBorder->setTopRightBorderUV(0.5, 0.0, 1.0, 0.5);
196            oBorder->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
197            oBorder->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
198            oBorder->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
199            oBorder->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
200            oBorder->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
201            //overlay_->add2D(oBorder);
202            m_pOverlayPanel->addChild(oBorder);
203        }
[3082]204
[3101]205
[2913]206        //Not Showing the map as default
207        this->isVisible_=false;
208        overlay_->hide();
209
[3101]210        //Create plane to show gridTypeError: blimport() takes no keyword arguments
211/*        Ogre::Entity* plane_ent;
212        if(Map::getMapSceneManager()->hasEntity("MapPlane"))
213            plane_ent = Map::getMapSceneManager()->getEntity("MapPlane");
214        else
215            plane_ent = Map::getMapSceneManager()->createEntity( "MapPlane", "plane.mesh");
216*/
217        this->movablePlane_ = new Ogre::MovablePlane( Vector3::UNIT_Y, 0 );
218        this->movablePlane_->normalise();
219
220        if(!Map::getMapSceneManager()->hasEntity("MapPlane"))
221        {
222            Ogre::Entity* plane_ent = Map::getMapSceneManager()->createEntity( "MapPlane", "plane.mesh");
223            planeNode_ = Map::getMapSceneManager()->createSceneNode();
[2977]224        //Create plane for calculations
225
[3101]226
[2942]227        //Ogre::MaterialPtr plane_mat = Ogre::MaterialManager::getSingleton().create("mapgrid", "General");
228        //plane_mat->getTechnique(0)->getPass(0)->createTextureUnitState("mapgrid.tga");
229        //plane_ent->setMaterialName("mapgrid");
[3101]230            plane_ent->setMaterialName("Map/Grid");
231            planeNode_->attachObject(plane_ent);
[3089]232
[3101]233            planeNode_->scale(160,1,160);
[3082]234//        planeNode_->attachObject(movablePlane_);
[2942]235        //Ogre::Material plane_mat = Ogre::MaterialManager::getSingletonPtr()->getByName("rock");
236
[3089]237
[2942]238        //ToDo create material script
[3101]239            Ogre::MaterialPtr myManualObjectMaterial = Ogre::MaterialManager::getSingleton().create("Map/Line","General");
240            myManualObjectMaterial->setReceiveShadows(false);
241            myManualObjectMaterial->getTechnique(0)->setLightingEnabled(true);
242            myManualObjectMaterial->getTechnique(0)->getPass(0)->setDiffuse(1,1,0,0);
243            myManualObjectMaterial->getTechnique(0)->getPass(0)->setAmbient(1,1,0);
244            myManualObjectMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(1,1,0);
245        }
[2837]246    }
[2942]247
248    Map::~Map()
249    {
[2956]250        this->singletonMap_s = 0;
[3101]251        //delete this->overlay_;
[2956]252        /*if (this->isInitialized())
253        {
[2942]254        //delete sManager_;
[2956]255        //delete Map::getMapSceneManager()->getRootSceneNode();
[2942]256        //delete oManager_;
[2956]257        //delete CamNode_;
258        //delete Cam_;
259        //delete mapSceneM_s;
260        //Map::getMapSceneManager()->destroyAllEntities();
261        //Map::getMapSceneManager()->destroyAllCameras();
262        delete Map::getMapSceneManager();
263        }*/
[2942]264    }
265
[2913]266    Ogre::MaterialPtr Map::createRenderCamera(Ogre::Camera * cam, std::string matName)
267    {
268        Ogre::TexturePtr rttTex = Ogre::TextureManager::getSingleton().createManual(matName+"_tex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
269
270        Ogre::RenderTexture *renderTexture = rttTex->getBuffer()->getRenderTarget();
271
272        renderTexture->addViewport(cam);
273        renderTexture->getViewport(0)->setClearEveryFrame(true);
274        renderTexture->getViewport(0)->setBackgroundColour(ColourValue::Black);
275        renderTexture->getViewport(0)->setOverlaysEnabled(false);
276
277        Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
278        Ogre::Technique *technique = material->createTechnique();
279        technique->createPass();
280        material->getTechnique(0)->getPass(0)->setLightingEnabled(false);
281        material->getTechnique(0)->getPass(0)->createTextureUnitState(matName+"_tex");
282        return material;
283    }
284
285    void Map::updatePositions()
286    {
[2942]287
[2956]288//Ogre::Entity * ent;// = mapSceneM_s->createEntity("ent1", "drone.mesh");
[2913]289       for(ObjectList<orxonox::RadarViewable>::iterator it = ObjectList<orxonox::RadarViewable>::begin();
290            it!=ObjectList<orxonox::RadarViewable>::end();
291            it++)
292        {
293            //COUT(0) << "Radar_Position: " << it->getRVWorldPosition() << std::endl;
294            //Ogre::SceneNode node = it->getMapNode();
295            //Ogre::Entity ent = it->getMapEntity();
296            if( !(it->MapNode_) )
297            {
[2956]298                it->MapNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode( it->getRVWorldPosition() );
[2913]299                //it->MapNode_->translate( it->getRVOrientedVelocity(), Ogre::TS_WORLD );
[2942]300                /*if(it->getRadarObjectShape() == RadarViewable::Dot)
301                {
302                    //if( !(it->MapEntity_) )//check wether the entity is already attached
303                    //{
[2956]304                        //it->MapEntity_ = Map::getMapSceneManager()->createEntity( getUniqueNumberString(), "drone.mesh");
[2942]305                        //it->addEntity();
306                        //it->MapNode_->attachObject( it->MapEntity_ );
307                        //it->MapNode_->attachObject( it->line_ );
308                   // }
309                }*/
310                it->addMapEntity();
[2913]311            }
[3082]312            if(it->isHumanShip_)
313            {
314                this->movablePlane_->redefine(it->MapNode_->getLocalAxes().GetColumn(1) , it->MapNode_->getPosition());
315                if(it->isHumanShip_ && it->MapNode_ != this->playerShipNode_)
316                {
317                    this->playerShipNode_ = it->MapNode_;
[3101]318                    if(planeNode_ && this->planeNode_->getParent())
319                        this->planeNode_->getParent()->removeChild(this->planeNode_);
[3082]320                    this->playerShipNode_->addChild(this->planeNode_);
321                //Movable Plane needs to be attached direcly for calculations
322                //this->movablePlane_->detatchFromParent();
323                //this->movablePlane_->getParentSceneNode()->detachObject(this->movablePlane_);
324                //this->movablePlane_->redefine(it->MapNode_->getLocalAxes().GetColumn(1) , it->MapNode_->getPosition());
325                //it->MapNode_->attachObject(this->movablePlane_);
[3101]326                    if(planeNode_ && this->CamNode_->getParent())
327                        this->CamNode_->getParent()->removeChild(this->CamNode_);
[3082]328                    this->playerShipNode_->addChild(this->CamNode_);
329                    this->CamNode_->attachObject(this->Cam_);
[2956]330                //this->CamNodeHelper_ = this->CamNode_->createChildSceneNode();
331                //this->CamNodeHelper_->attachObject(this->Cam_);
[3082]332                    this->Cam_->setPosition(0, 0, DISTANCE);
333                    this->Cam_->pitch( (Degree)PITCH );
334                    this->Cam_->lookAt(Vector3(0,0,0));
[2977]335                //this->Cam_->setAutoTracking(true, this->playerShipNode_);
[3082]336                }
[2956]337            }
[3082]338            it->updateMapPosition();
339
340
341
[3089]342
343
344
[2913]345        }
346    }
347
[3089]348
349
[2837]350    void Map::XMLPort(Element& xmlElement, XMLPort::Mode mode)
351    {
352        SUPER(Map, XMLPort, xmlElement, mode);
[3089]353    }
[2913]354
355    void Map::changedOwner()
356    {
[3089]357        SUPER(Map, changedOwner);
[2913]358        //COUT(0) << "shipptr" << this->getOwner()->getReverseCamera() << std::endl;
[3089]359
360        ControllableEntity* entity = dynamic_cast<ControllableEntity*>(this->getOwner());
361        if(entity && entity->getReverseCamera())
[2913]362        {
363            //COUT(0) << "foo";
[3089]364            entity->getReverseCamera()->attachCamera(this->Cam_);
[2913]365        }
[2837]366    }
[2856]367
[2913]368
[2856]369    void Map::toggleVisibility()
370    {
371        if (!(this->isVisible_))
372        {
373            this->overlay_->show();
374            this->isVisible_=1;
[2942]375            //set mouselook when showing map
376            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && !HumanController::localController_s->controllableEntity_->isInMouseLook())
377            HumanController::localController_s->controllableEntity_->mouseLook();
[2856]378        }
379        else
380        {
381            this->overlay_->hide();
382            this->isVisible_=0;
[2942]383            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->isInMouseLook())
384            HumanController::localController_s->controllableEntity_->mouseLook();
[2856]385        }
386    }
[3089]387
[2856]388    //Static function to toggle visibility of the map
389    void Map::openMap()
[2837]390    {
[2856]391        for(ObjectList<orxonox::Map>::iterator it = ObjectList<orxonox::Map>::begin();
392            it!=ObjectList<orxonox::Map>::end();
393            it++)
394        {
395        //Map * m = it->getMap();
[2942]396        //COUT(0) << it->isVisible_ << std::endl;
[2856]397        it->toggleVisibility();
[2913]398        //it->updatePositions();
[2856]399        }
[2837]400    }
[3089]401
[2837]402    void Map::tick(float dt)
403    {
[3082]404        //Debug
405        //COUT(0) << "MovablePlane Position: " << this->movablePlane_->getParentSceneNode()->getName() << this->movablePlane_->getParentSceneNode()->getPosition() << std::endl;
406        //COUT(0) << "planeNode_ Position: " << this->planeNode_ ->getName() << this->planeNode_->getPosition() << std::endl;
407        //COUT(0) <<  "planeNode_ Parrent Position" << this->planeNode_->getParent()->getName() << this->planeNode_->getParent()->getPosition() << std::endl;
[2942]408        if( this->isVisible_ )
409            updatePositions();
[2956]410        //Cam_->roll(Degree(1));
[3089]411
[2837]412    }
[2942]413
414    void Map::rotateYaw(const Vector2& value)
415    {
[2956]416        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
417            return;
418
419/*
420        singletonMap_s->CamNode_->setOrientation(singletonMap_s->CamNode_->getOrientation() * Quaternion( (Degree)(-value.y * singletonMap_s->mouseLookSpeed_) , singletonMap_s->playerShipNode_->getLocalAxes().GetColumn(1) ));
421
422        Map::singletonMap_s->CamNodeHelper_->setDirection(Vector3::UNIT_Y, Ogre::Node::TS_PARENT, Vector3::UNIT_Y);
423        Map::singletonMap_s->CamNodeHelper_->lookAt(Vector3(0,0,0), Ogre::Node::TS_PARENT);
424*/
425        singletonMap_s->CamNode_->yaw( (Degree)(-value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_PARENT);
[2942]426    }
427
428    void Map::rotatePitch(const Vector2& value)
429    {
[2956]430        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
431            return;
432            //singletonMap_s->Cam_->setOrientation(singletonMap_s->Cam_->getOrientation() * Quaternion( (Degree)(-value.y * singletonMap_s->mouseLookSpeed_) , Vector3::UNIT_X));
433/*        singletonMap_s->CamNode_->setOrientation(singletonMap_s->CamNode_->getOrientation() * Quaternion( (Degree)(-value.y * singletonMap_s->mouseLookSpeed_) , singletonMap_s->playerShipNode_->getLocalAxes().GetColumn(0) ));
434
435        Map::singletonMap_s->CamNodeHelper_->setDirection(Vector3::UNIT_Y, Ogre::Node::TS_PARENT, Vector3::UNIT_Y);
436        Map::singletonMap_s->CamNodeHelper_->lookAt(Vector3(0,0,0), Ogre::Node::TS_PARENT);
437*/
[2977]438        singletonMap_s->CamNode_->pitch( (Degree)(value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
[3089]439
[2942]440    }
[3089]441
[2956]442    void Map::Zoom(const Vector2& value)
443    {
444        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
445            return;
446        //COUT(0) << value.y << std::endl;
447        Map::singletonMap_s->Cam_->setPosition(0,0, Map::singletonMap_s->Cam_->getPosition().z + value.y * Map::singletonMap_s->mouseLookSpeed_ );
448    }
[2837]449 }
Note: See TracBrowser for help on using the repository browser.