Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/radarDreiD/src/modules/overlays/hud/HUDRadar.cc @ 9881

Last change on this file since 9881 was 9877, checked in by wroennin, 11 years ago

Math: New function to determine the ZOrder of the radarobjects; HUDTemplates: cleenup

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