Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ogre1.9/src/modules/overlays/hud/HUDRadar.cc @ 11247

Last change on this file since 11247 was 11129, checked in by landauf, 9 years ago

orxonox now compiles with ogre 1.9 (but still needs some other changes)

  • Property svn:eol-style set to native
File size: 12.0 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 *      Yuning Chai
24 *      Felix Schulthess
25 *   Co-authors:
26 *      Reto Grieder
27 *      Wolfgang Roenninger
28 *
29 */
30
31#include "HUDRadar.h"
32
33#if OGRE_VERSION >= 0x010900
34#   include <Overlay/OgreOverlayManager.h>
35#   include <Overlay/OgrePanelOverlayElement.h>
36#else
37#   include <OgreOverlayManager.h>
38#   include <OgrePanelOverlayElement.h>
39#endif
40
41#include "util/Math.h"
42#include "util/StringUtils.h"
43#include "core/CoreIncludes.h"
44#include "core/XMLPort.h"
45#include "tools/TextureGenerator.h"
46#include "worldentities/ControllableEntity.h"
47#include "Scene.h"
48#include "Radar.h"
49#include "core/config/ConfigValueIncludes.h"
50
51namespace orxonox
52{
53    RegisterClass(HUDRadar);
54
55    HUDRadar::HUDRadar(Context* context)
56        : OrxonoxOverlay(context)
57    {
58        RegisterObject(HUDRadar);
59        this->setConfigValues();
60
61        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
62            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
63        this->marker_->setMaterialName("Orxonox/RadarMarker");
64        this->overlay_->add2D(this->marker_);
65        this->marker_->hide();
66
67        this->setRadarSensitivity(1.0f);
68        this->setHalfDotSizeDistance(3000.0f);
69        this->setMaximumDotSize(0.1f);
70        this->setMaximumDotSize3D(0.07f);
71
72        this->shapeMaterials_[RadarViewable::Shape::Dot]      = "RadarDot.png";
73        this->shapeMaterials_[RadarViewable::Shape::Triangle] = "RadarTriangle.png";
74        this->shapeMaterials_[RadarViewable::Shape::Square]   = "RadarSquare.png";
75        this->owner_ = nullptr;
76
77        this->map3DFront_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
78            .createOverlayElement("Panel", "HUDRadar_mapDreiDFront_" + getUniqueNumberString()));
79        this->map3DFront_->setMaterialName("Orxonox/Radar3DFront");
80        this->overlay_->add2D(this->map3DFront_);
81        this->map3DFront_->hide();
82
83        this->map3DBack_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
84            .createOverlayElement("Panel", "HUDRadar_mapDreiDBack_" + getUniqueNumberString()));
85        this->map3DBack_->setMaterialName("Orxonox/Radar3DBack");
86        this->overlay_->add2D(this->map3DBack_);
87        this->map3DBack_->hide();
88
89    }
90
91    HUDRadar::~HUDRadar()
92    {
93        if (this->isInitialized())
94        {
95            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
96            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->map3DFront_);
97            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->map3DBack_);
98
99            for (const auto& mapEntry : this->radarObjects_)
100            {
101                Ogre::OverlayManager::getSingleton().destroyOverlayElement(mapEntry.second);
102            }
103        }
104    }
105
106
107
108    void HUDRadar::setConfigValues()
109       {
110           SetConfigValue(RadarMode_, true);
111       }
112
113    void HUDRadar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
114    {
115        SUPER(HUDRadar, XMLPort, xmlelement, mode);
116
117        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode);
118        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode);
119        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlelement, mode);
120        XMLPortParam(HUDRadar, "maximumDotSize3D", setMaximumDotSize3D, getMaximumDotSize3D, xmlelement, mode);
121        XMLPortParam(HUDRadar, "material2D", set2DMaterial, get2DMaterial, xmlelement, mode);
122        XMLPortParam(HUDRadar, "material3DMiddle", set3DMaterial, get3DMaterial, xmlelement, mode);
123        XMLPortParam(HUDRadar, "material3DFront", set3DMaterialFront, get3DMaterialFront, xmlelement, mode);
124        XMLPortParam(HUDRadar, "material3DBack", set3DMaterialBack, get3DMaterialBack, xmlelement, mode);
125        XMLPortParam(HUDRadar, "mapAngle3D", setMapAngle, getMapAngle, xmlelement, mode);
126        XMLPortParam(HUDRadar, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode);
127    }
128
129    void HUDRadar::addObject(RadarViewable* object)
130    {
131        if (object == orxonox_cast<RadarViewable*>(this->owner_))
132            return;
133        if( showObject(object) == false ) //do not show objects that are "invisible" or "radar invisible"
134            return;
135
136        // Make sure the object hasn't been added yet
137        assert( this->radarObjects_.find(object) == this->radarObjects_.end() );
138
139        // Create everything needed to display the object on the radar and add it to the map
140        Ogre::PanelOverlayElement* panel;
141        panel = static_cast<Ogre::PanelOverlayElement*>(
142            Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
143        this->overlay_->add2D(panel);
144        // get right material
145        panel->setMaterialName(TextureGenerator::getMaterialName(
146            shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
147        panel->hide();
148        this->radarObjects_[object] = panel;
149    }
150
151    void HUDRadar::removeObject(RadarViewable* object)
152    {
153        // If object was added at all then remove it
154        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
155        it = this->radarObjects_.find( object );
156        if( it != this->radarObjects_.end() )
157        {
158            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
159            this->radarObjects_.erase(it);
160        }
161    }
162
163    void HUDRadar::objectChanged( RadarViewable* rv )
164    {// The new implementation behaves more precisely, since inactive RadarViewables are not displayed anymore.
165        this->removeObject(rv);
166        this->addObject(rv);
167    }
168
169    void HUDRadar::gatherObjects()
170    {
171        const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects();
172        for( RadarViewable* viewable : objectSet )
173            this->addObject(viewable);
174        this->radarTick(0);
175    }
176
177    void HUDRadar::radarTick(float dt)
178    {
179        // Make sure the owner of the radar was defined
180        if( !this->owner_ )
181            return;
182
183        this->marker_->hide();      // in case that no object is in focus
184        // get the focus object
185        Radar* radar = this->getOwner()->getScene()->getRadar();
186        const RadarViewable* focusObject = radar->getFocus();
187
188        // update the distances for all objects
189
190        if(RadarMode_)
191        {
192            this->setBackgroundMaterial(material3D_);
193            this->map3DFront_->_notifyZOrder(this->overlay_->getZOrder() * 100 + 250); // it seems that the ZOrder of overlayelements is 100 times the ZOrder of the overlay
194            this->map3DBack_->_notifyZOrder(this->overlay_->getZOrder() * 100 - 250); // 250 a little bit buffer so that the two shels are displayed all in the front / in the back
195            this->map3DFront_->show();
196            this->map3DBack_->show();
197        }
198        else
199        {
200            this->setBackgroundMaterial(material2D_);
201            this->map3DFront_->hide();
202            this->map3DBack_->hide();
203        }
204
205        for( const auto& mapEntry : this->radarObjects_ )
206        {
207            // Make sure the object really is a WorldEntity
208            const WorldEntity* wePointer = mapEntry.first->getWorldEntity();
209            if( !wePointer )
210            {
211                orxout(internal_error) << "Cannot display a non-WorldEntitiy on the radar" << endl;
212                assert(0);
213            }
214            bool isFocus = (mapEntry.first == focusObject);
215            // set size to fit distance...
216            float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
217            // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
218
219            float size;
220            if(RadarMode_)
221                size = maximumDotSize3D_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * mapEntry.first->getRadarObjectScale();
222            else
223                size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * mapEntry.first->getRadarObjectScale();
224            mapEntry.second->setDimensions(size, size);
225
226            // calc position on radar...
227            Vector2 coord;
228
229            if(RadarMode_)
230            {
231                coord = get3DProjection(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), 0.6435011, detectionLimit_);
232
233                // set zOrder on screen
234                bool overXZPlain = isObjectHigherThanShipOnMap(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), this->mapAngle_);
235
236                int zOrder = determineMap3DZOrder(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), detectionLimit_);
237                if(overXZPlain == false /*&& (it->second->getZOrder() >  100 * this->overlay_->getZOrder())*/) // it appears that zOrder of attached Overlayelements is 100 times the zOrder of the Overlay
238                    mapEntry.second->_notifyZOrder(this->overlay_->getZOrder() * 100 - 70 + zOrder);
239                if(overXZPlain == true /*&& (it->second->getZOrder() <= 100 * this->overlay_->getZOrder())*/)
240                    mapEntry.second->_notifyZOrder(this->overlay_->getZOrder() * 100 + 70 + zOrder);
241            }
242            else
243                coord = get2DViewCoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
244
245            coord *= math::pi / 3.5f; // small adjustment to make it fit the texture
246            mapEntry.second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f);
247
248            if( distance < detectionLimit_ || detectionLimit_ < 0 )
249                mapEntry.second->show();
250            else
251                mapEntry.second->hide();
252
253            // if this object is in focus, then set the focus marker
254            if (isFocus)
255            {
256                this->marker_->setDimensions(size * 1.5f, size * 1.5f);
257                this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f);
258                if(RadarMode_)
259                    this->marker_->_notifyZOrder(mapEntry.second->getZOrder() -1);
260                this->marker_->show();
261            }
262        }
263    }
264
265    bool HUDRadar::showObject(RadarViewable* rv)
266    {
267        if ( rv == orxonox_cast<RadarViewable*> ( this->getOwner() ) )
268            return false;
269        assert( rv->getWorldEntity() );
270        if ( rv->getWorldEntity()->isVisible()==false || rv->getRadarVisibility()==false )
271            return false;
272        return true;
273    }
274
275
276    void HUDRadar::changedOwner()
277    {
278        SUPER(HUDRadar, changedOwner);
279
280        this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner());
281        assert(this->radarObjects_.size() == 0);
282        this->gatherObjects();
283    }
284}
Note: See TracBrowser for help on using the repository browser.