Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/overlays/map/Map.cc @ 3154

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

Tried to reduce dependencies in the core. There wasn't much to achieve though…

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