Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed at least 8 unitialised value problems though not all of them are problematic. Still, the values are being used and some condition depends on it.

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