Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3099 was 3089, checked in by landauf, 15 years ago

merged map branch back to trunk

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