Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6218 was 6218, checked in by rgrieder, 14 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
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        ObjectList<Scene>::iterator it = ObjectList<Scene>::begin();
97        this->sManager_ = it->getSceneManager();
98        if( !Map::getMapSceneManager() )
99        {
100            Map::setMapSceneManager( Ogre::Root::getSingletonPtr()->createSceneManager( Ogre::ST_GENERIC,"MapScene" ) );
101        }
102
103        this->playerShipNode_ = 0;
104        //this->sNode_ = new Ogre::SceneNode(sManager_);
105        //oManager_ = Ogre::OverlayManager::getSingletonPtr();
106        //overlay_ = oManager_->create("Map");
107        //overlay_ is member of OrxonoxOverlay
108
109        //Not Showing the map as default
110        //this->isVisible_=false;
111        //overlay_->hide();
112        this->mouseLookSpeed_ = 200;
113
114        //TestEntity
115        //Ogre::Entity * ent = mapSceneM_s->createEntity("ent", "drone.mesh");
116
117        //Map::getMapSceneManager()->getRootSceneNode()->attachObject( ent );
118        /*sNode_->setPosition(0,0,-50);
119        overlay_->add3D(sNode_);
120        */
121
122
123
124
125
126        // Alter the camera aspect ratio to match the viewport
127        //mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
128        if(!Map::Cam_)
129            Cam_ = Map::getMapSceneManager()->createCamera("ReflectCam");
130        //Cam_->setPosition(200,170, -160);
131        //Cam_->lookAt(0,0,0);
132        Cam_->setAspectRatio(1);
133        //Cam_->setRenderingDistance(0);
134        if(!Map::CamNode_)
135            CamNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode();
136
137
138        //Create overlay material
139        if(Map::OverlayMaterial_.isNull())
140            Map::OverlayMaterial_ = this->createRenderCamera(Cam_, "RttMat");
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
146        renderTexture->addViewport(Cam_);
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");
162
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
169        m_pOverlayPanel->setMaterialName(camMat_id);
170        pOverlay->add2D(m_pOverlayPanel);
171        pOverlay->show();
172*/
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);
185
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        }
202
203
204        //Not Showing the map as default
205        this->isVisible_=false;
206        overlay_->hide();
207
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();
222        //Create plane for calculations
223
224
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");
228            plane_ent->setMaterialName("Map/Grid");
229            planeNode_->attachObject(plane_ent);
230
231            planeNode_->scale(160,1,160);
232//        planeNode_->attachObject(movablePlane_);
233        //Ogre::Material plane_mat = Ogre::MaterialManager::getSingletonPtr()->getByName("rock");
234
235
236        //ToDo create material script
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        }
244    }
245
246    Map::~Map()
247    {
248        this->singletonMap_s = 0;
249        //delete this->overlay_;
250        /*if (this->isInitialized())
251        {
252        //delete sManager_;
253        //delete Map::getMapSceneManager()->getRootSceneNode();
254        //delete oManager_;
255        //delete CamNode_;
256        //delete Cam_;
257        //delete mapSceneM_s;
258        //Map::getMapSceneManager()->destroyAllEntities();
259        //Map::getMapSceneManager()->destroyAllCameras();
260        delete Map::getMapSceneManager();
261        }*/
262    }
263
264    Ogre::MaterialPtr Map::createRenderCamera(Ogre::Camera * cam, const std::string& matName)
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    {
285
286//Ogre::Entity * ent;// = mapSceneM_s->createEntity("ent1", "drone.mesh");
287       for(ObjectList<orxonox::RadarViewable>::iterator it = ObjectList<orxonox::RadarViewable>::begin();
288            it!=ObjectList<orxonox::RadarViewable>::end();
289            ++it)
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            {
296                it->MapNode_ = Map::getMapSceneManager()->getRootSceneNode()->createChildSceneNode( it->getRVWorldPosition() );
297                //it->MapNode_->translate( it->getRVOrientedVelocity(), Ogre::TS_WORLD );
298                /*if(it->getRadarObjectShape() == RadarViewable::Dot)
299                {
300                    //if( !(it->MapEntity_) )//check wether the entity is already attached
301                    //{
302                        //it->MapEntity_ = Map::getMapSceneManager()->createEntity( getUniqueNumberString(), "drone.mesh");
303                        //it->addEntity();
304                        //it->MapNode_->attachObject( it->MapEntity_ );
305                        //it->MapNode_->attachObject( it->line_ );
306                   // }
307                }*/
308                it->addMapEntity();
309            }
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_;
316                    if(planeNode_ && this->planeNode_->getParent())
317                        this->planeNode_->getParent()->removeChild(this->planeNode_);
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_);
324                    if(planeNode_ && this->CamNode_->getParent())
325                        this->CamNode_->getParent()->removeChild(this->CamNode_);
326                    this->playerShipNode_->addChild(this->CamNode_);
327                    this->CamNode_->attachObject(this->Cam_);
328                //this->CamNodeHelper_ = this->CamNode_->createChildSceneNode();
329                //this->CamNodeHelper_->attachObject(this->Cam_);
330                    this->Cam_->setPosition(0, 0, DISTANCE);
331                    this->Cam_->pitch( static_cast<Degree>(PITCH) );
332                    this->Cam_->lookAt(this->playerShipNode_->getPosition());
333                //this->Cam_->setAutoTracking(true, this->playerShipNode_);
334                }
335            }
336            it->updateMapPosition();
337
338
339
340
341
342
343        }
344    }
345
346
347
348    void Map::XMLPort(Element& xmlElement, XMLPort::Mode mode)
349    {
350        SUPER(Map, XMLPort, xmlElement, mode);
351    }
352
353    void Map::changedOwner()
354    {
355        SUPER(Map, changedOwner);
356        //COUT(0) << "shipptr" << this->getOwner()->getReverseCamera() << std::endl;
357
358        ControllableEntity* entity = orxonox_cast<ControllableEntity*>(this->getOwner());
359        if(entity && entity->getReverseCamera())
360        {
361            //COUT(0) << "foo";
362            entity->getReverseCamera()->attachCamera(this->Cam_);
363        }
364    }
365
366
367    void Map::toggleVisibility()
368    {
369        if (!(this->isVisible_))
370        {
371            this->overlay_->show();
372            this->isVisible_=1;
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();
376        }
377        else
378        {
379            this->overlay_->hide();
380            this->isVisible_=0;
381            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->isInMouseLook())
382            HumanController::localController_s->controllableEntity_->mouseLook();
383        }
384    }
385
386    //Static function to toggle visibility of the map
387    void Map::openMap()
388    {
389        for(ObjectList<orxonox::Map>::iterator it = ObjectList<orxonox::Map>::begin();
390            it!=ObjectList<orxonox::Map>::end();
391            ++it)
392        {
393        //Map * m = it->getMap();
394        //COUT(0) << it->isVisible_ << std::endl;
395        it->toggleVisibility();
396        //it->updatePositions();
397        }
398    }
399
400    // HACK!
401    void Map::hackDestroyMap()
402    {
403        Map::OverlayMaterial_.setNull();
404    }
405
406    void Map::tick(float dt)
407    {
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;
412        if( this->isVisible_ )
413            updatePositions();
414        //Cam_->roll(Degree(1));
415
416    }
417
418    void Map::rotateYaw(const Vector2& value)
419    {
420        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
421            return;
422
423/*
424        singletonMap_s->CamNode_->setOrientation(singletonMap_s->CamNode_->getOrientation() * Quaternion( static_cast<Degree>(-value.y * singletonMap_s->mouseLookSpeed_) , singletonMap_s->playerShipNode_->getLocalAxes().GetColumn(1) ));
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*/
429        singletonMap_s->CamNode_->yaw( static_cast<Degree>(-value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_PARENT);
430    }
431
432    void Map::rotatePitch(const Vector2& value)
433    {
434        if(!( Map::singletonMap_s && Map::singletonMap_s->CamNode_ ))
435            return;
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) ));
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*/
442        singletonMap_s->CamNode_->pitch( static_cast<Degree>(value.y * singletonMap_s->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
443
444    }
445
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    }
453 }
Note: See TracBrowser for help on using the repository browser.