Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed a ton of msvc warnings revealed with OGRE v1.7 (they removed the warning suppressors in OgrePrerequisites.h).
All of them are conversions from one type to another that might be lossy (mostly double to float, please always use "3.7f" instead of "3.7" as constants when using floats).

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