Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/overlays/Map.cc @ 6122

Last change on this file since 6122 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

  • Property svn:eol-style set to native
File size: 18.6 KB
Line 
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
27#include "Map.h"
28
29#include <string>
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>
44#include <OgreSceneManager.h>
45#include <OgreSceneNode.h>
46#include <OgreTechnique.h>
47#include <OgreTexture.h>
48#include <OgreTextureManager.h>
49#include <OgreViewport.h>
50
51#include "core/ConsoleCommand.h"
52#include "core/CoreIncludes.h"
53#include "core/XMLPort.h"
54#include "interfaces/RadarViewable.h"
55#include "Scene.h"
56#include "controllers/HumanController.h"
57#include "worldentities/CameraPosition.h"
58#include "worldentities/ControllableEntity.h"
59
60 namespace orxonox
61 {
62    CreateFactory(Map);
63    SetConsoleCommand(Map, openMap, true);
64    //SetConsoleCommand(Map, rotateYaw, true).setAsInputCommand();
65    //SetConsoleCommand(Map, rotatePitch, true).setAsInputCommand();
66    SetConsoleCommand(Map, Zoom, true).setAsInputCommand();
67
68
69    Map* Map::singletonMap_s = 0;
70    Ogre::SceneManager* Map::mapSceneM_s = 0;
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
84    //int Map::mouseLookSpeed_ = 200;
85    //Ogre::SceneNode* Map::playerShipNode_ = 0;
86
87    const int PITCH=-30;
88    const int DISTANCE=200;
89
90    Map::Map(BaseObject* creator) : OrxonoxOverlay(creator)
91    {
92        RegisterObject(Map);
93        Map::singletonMap_s=this;
94
95        //Getting Scene Manager (Hack)
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        }
105
106        this->playerShipNode_ = 0;
107        //this->sNode_ = new Ogre::SceneNode(sManager_);
108        //oManager_ = Ogre::OverlayManager::getSingletonPtr();
109        //overlay_ = oManager_->create("Map");
110        //overlay_ is member of OrxonoxOverlay
111
112        //Not Showing the map as default
113        //this->isVisible_=false;
114        //overlay_->hide();
115        this->mouseLookSpeed_ = 200;
116
117        //TestEntity
118        //Ogre::Entity * ent = mapSceneM_s->createEntity("ent", "drone.mesh");
119
120        //Map::getMapSceneManager()->getRootSceneNode()->attachObject( ent );
121        /*sNode_->setPosition(0,0,-50);
122        overlay_->add3D(sNode_);
123        */
124
125
126
127
128
129        // Alter the camera aspect ratio to match the viewport
130        //mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
131        if(!Map::Cam_)
132            Cam_ = Map::getMapSceneManager()->createCamera("ReflectCam");
133        //Cam_->setPosition(200,170, -160);
134        //Cam_->lookAt(0,0,0);
135        Cam_->setAspectRatio(1);
136        //Cam_->setRenderingDistance(0);
137        if(!Map::CamNode_)
138            CamNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode();
139
140
141        //Create overlay material
142        if(Map::OverlayMaterial_.isNull())
143            Map::OverlayMaterial_ = this->createRenderCamera(Cam_, "RttMat");
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
149        renderTexture->addViewport(Cam_);
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");
165
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
172        m_pOverlayPanel->setMaterialName(camMat_id);
173        pOverlay->add2D(m_pOverlayPanel);
174        pOverlay->show();
175*/
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);
188
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        }
205
206
207        //Not Showing the map as default
208        this->isVisible_=false;
209        overlay_->hide();
210
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();
225        //Create plane for calculations
226
227
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");
231            plane_ent->setMaterialName("Map/Grid");
232            planeNode_->attachObject(plane_ent);
233
234            planeNode_->scale(160,1,160);
235//        planeNode_->attachObject(movablePlane_);
236        //Ogre::Material plane_mat = Ogre::MaterialManager::getSingletonPtr()->getByName("rock");
237
238
239        //ToDo create material script
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        }
247    }
248
249    Map::~Map()
250    {
251        this->singletonMap_s = 0;
252        //delete this->overlay_;
253        /*if (this->isInitialized())
254        {
255        //delete sManager_;
256        //delete Map::getMapSceneManager()->getRootSceneNode();
257        //delete oManager_;
258        //delete CamNode_;
259        //delete Cam_;
260        //delete mapSceneM_s;
261        //Map::getMapSceneManager()->destroyAllEntities();
262        //Map::getMapSceneManager()->destroyAllCameras();
263        delete Map::getMapSceneManager();
264        }*/
265    }
266
267    Ogre::MaterialPtr Map::createRenderCamera(Ogre::Camera * cam, const std::string& matName)
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    {
288
289//Ogre::Entity * ent;// = mapSceneM_s->createEntity("ent1", "drone.mesh");
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            {
299                it->MapNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode( it->getRVWorldPosition() );
300                //it->MapNode_->translate( it->getRVOrientedVelocity(), Ogre::TS_WORLD );
301                /*if(it->getRadarObjectShape() == RadarViewable::Dot)
302                {
303                    //if( !(it->MapEntity_) )//check wether the entity is already attached
304                    //{
305                        //it->MapEntity_ = Map::getMapSceneManager()->createEntity( getUniqueNumberString(), "drone.mesh");
306                        //it->addEntity();
307                        //it->MapNode_->attachObject( it->MapEntity_ );
308                        //it->MapNode_->attachObject( it->line_ );
309                   // }
310                }*/
311                it->addMapEntity();
312            }
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_;
319                    if(planeNode_ && this->planeNode_->getParent())
320                        this->planeNode_->getParent()->removeChild(this->planeNode_);
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_);
327                    if(planeNode_ && this->CamNode_->getParent())
328                        this->CamNode_->getParent()->removeChild(this->CamNode_);
329                    this->playerShipNode_->addChild(this->CamNode_);
330                    this->CamNode_->attachObject(this->Cam_);
331                //this->CamNodeHelper_ = this->CamNode_->createChildSceneNode();
332                //this->CamNodeHelper_->attachObject(this->Cam_);
333                    this->Cam_->setPosition(0, 0, DISTANCE);
334                    this->Cam_->pitch( static_cast<Degree>(PITCH) );
335                    this->Cam_->lookAt(this->playerShipNode_->getPosition());
336                //this->Cam_->setAutoTracking(true, this->playerShipNode_);
337                }
338            }
339            it->updateMapPosition();
340
341
342
343
344
345
346        }
347    }
348
349
350
351    void Map::XMLPort(Element& xmlElement, XMLPort::Mode mode)
352    {
353        SUPER(Map, XMLPort, xmlElement, mode);
354    }
355
356    void Map::changedOwner()
357    {
358        SUPER(Map, changedOwner);
359        //COUT(0) << "shipptr" << this->getOwner()->getReverseCamera() << std::endl;
360
361        ControllableEntity* entity = orxonox_cast<ControllableEntity*>(this->getOwner());
362        if(entity && entity->getReverseCamera())
363        {
364            //COUT(0) << "foo";
365            entity->getReverseCamera()->attachCamera(this->Cam_);
366        }
367    }
368
369
370    void Map::toggleVisibility()
371    {
372        if (!(this->isVisible_))
373        {
374            this->overlay_->show();
375            this->isVisible_=1;
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();
379        }
380        else
381        {
382            this->overlay_->hide();
383            this->isVisible_=0;
384            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->isInMouseLook())
385            HumanController::localController_s->controllableEntity_->mouseLook();
386        }
387    }
388
389    //Static function to toggle visibility of the map
390    void Map::openMap()
391    {
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();
397        //COUT(0) << it->isVisible_ << std::endl;
398        it->toggleVisibility();
399        //it->updatePositions();
400        }
401    }
402
403    // HACK!
404    void Map::hackDestroyMap()
405    {
406        Map::OverlayMaterial_.setNull();
407    }
408
409    void Map::tick(float dt)
410    {
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;
415        if( this->isVisible_ )
416            updatePositions();
417        //Cam_->roll(Degree(1));
418
419    }
420
421    void Map::rotateYaw(const Vector2& value)
422    {
423        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
424            return;
425
426/*
427        singletonMap_s->CamNode_->setOrientation(singletonMap_s->CamNode_->getOrientation() * Quaternion( static_cast<Degree>(-value.y * singletonMap_s->mouseLookSpeed_) , singletonMap_s->playerShipNode_->getLocalAxes().GetColumn(1) ));
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*/
432        singletonMap_s->CamNode_->yaw( static_cast<Degree>(-value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_PARENT);
433    }
434
435    void Map::rotatePitch(const Vector2& value)
436    {
437        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
438            return;
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) ));
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*/
445        singletonMap_s->CamNode_->pitch( static_cast<Degree>(value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
446
447    }
448
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    }
456 }
Note: See TracBrowser for help on using the repository browser.