Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3370 was 3334, checked in by Naaduun, 15 years ago

=Fixed a camera bug in map

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