[Orxonox-commit 6421] r11078 - in code/branches/shaders_merge/src/orxonox: . graphics

landauf at orxonox.net landauf at orxonox.net
Tue Jan 19 22:49:58 CET 2016


Author: landauf
Date: 2016-01-19 22:49:57 +0100 (Tue, 19 Jan 2016)
New Revision: 11078

Modified:
   code/branches/shaders_merge/src/orxonox/Scene.cc
   code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc
   code/branches/shaders_merge/src/orxonox/graphics/LensFlare.h
Log:
fixed numerous issues:
 - fixed memory leaks (delete allocated heap space)
 - use 'new' only when necessary
 - use orxonox_cast instead of static_cast
 - fixed warning in MSVC
also fixed formatting and added certain c++11 features

Modified: code/branches/shaders_merge/src/orxonox/Scene.cc
===================================================================
--- code/branches/shaders_merge/src/orxonox/Scene.cc	2016-01-19 13:22:10 UTC (rev 11077)
+++ code/branches/shaders_merge/src/orxonox/Scene.cc	2016-01-19 21:49:57 UTC (rev 11078)
@@ -97,6 +97,7 @@
             this->sceneManager_ = new Ogre::DefaultSceneManager("");
             this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
 
+            this->renderQueueListener_ = nullptr;
             this->radar_ = nullptr;
         }
 
@@ -124,7 +125,11 @@
                 this->radar_->destroy();
 
             if (GameMode::showsGraphics())
+            {
+                this->sceneManager_->removeRenderQueueListener(this->renderQueueListener_);
+                delete this->renderQueueListener_;
                 Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
+            }
             else
                 delete this->sceneManager_;
         }

Modified: code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc
===================================================================
--- code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc	2016-01-19 13:22:10 UTC (rev 11077)
+++ code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc	2016-01-19 21:49:57 UTC (rev 11078)
@@ -45,20 +45,19 @@
 {
     RegisterClass(LensFlare);
     
-    LensFlare::LensFlare(Context* context) : StaticEntity(context), scale_(1.0f), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.0f), colour_(new ColourValue(1.0f,0.9f,0.9f))
+    LensFlare::LensFlare(Context* context) : StaticEntity(context), scale_(1.0f), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.0f)
     {
         RegisterObject(LensFlare);
         
-        this->lensConfiguration_=new std::vector<LensFlare::Lens*>();
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),1.0f,1.0f,1.0f)); //main burst
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),0.7f,1.2f,1.05f)); //secondary burst
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/bursthalo"),0.7f,0.9f,1.0f)); //halo around main burst
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/ring"),0.1f,2.5f,0.9f)); //all the different distanced lenses
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.2f,0.5f));
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.1f,0.3f,0.45f));
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.4f,0.2f,0.35f));
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.4f,0.25f));
-        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo4"),0.05f,0.2f,0.2f));
+        this->lensConfiguration_.emplace_back("lensflare/burst",     1.0f,  1.0f, 1.0f); //main burst
+        this->lensConfiguration_.emplace_back("lensflare/burst",     0.7f,  1.2f, 1.05f); //secondary burst
+        this->lensConfiguration_.emplace_back("lensflare/bursthalo", 0.7f,  0.9f, 1.0f); //halo around main burst
+        this->lensConfiguration_.emplace_back("lensflare/ring",      0.1f,  2.5f, 0.9f); //all the different distanced lenses
+        this->lensConfiguration_.emplace_back("lensflare/iris",      0.1f,  0.2f, 0.5f);
+        this->lensConfiguration_.emplace_back("lensflare/halo5",     0.1f,  0.3f, 0.45f);
+        this->lensConfiguration_.emplace_back("lensflare/halo5",     0.4f,  0.2f, 0.35f);
+        this->lensConfiguration_.emplace_back("lensflare/iris",      0.1f,  0.4f, 0.25f);
+        this->lensConfiguration_.emplace_back("lensflare/halo4",     0.05f, 0.2f, 0.2f);
         
         this->createBillboards();
         
@@ -99,9 +98,10 @@
         this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
         this->attach(this->occlusionBillboard_);
         
-        for(std::vector<LensFlare::Lens*>::iterator it = lensConfiguration_->begin(); it != lensConfiguration_->end(); ++it) {
-            Billboard* lensPart=new Billboard(this->getContext());
-            lensPart->setMaterial(*(*it)->material_);
+        for (const LensFlare::Lens& lens : lensConfiguration_)
+        {
+            Billboard* lensPart = new Billboard(this->getContext());
+            lensPart->setMaterial(lens.material_);
             lensPart->disableFrustumCulling();
             lensPart->setVisible(true);
             this->attach(lensPart);
@@ -118,18 +118,21 @@
     @param lightIsVisible
         is the (point-)light source currently visible
     */
-    void LensFlare::updateBillboardStates(Vector3& viewDirection, float dimension, bool lightIsVisible)
+    void LensFlare::updateBillboardStates(const Vector3& viewDirection, float dimension, bool lightIsVisible)
     { 
-        this->occlusionBillboard_->setDefaultDimensions(dimension*0.5f,dimension*0.5f);
+        this->occlusionBillboard_->setDefaultDimensions(dimension * 0.5f, dimension * 0.5f);
         this->occlusionBillboard_->setVisible(lightIsVisible);
-        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
-        it++;
-        for(int i=0; it != this->getAttachedObjects().end(); it++) {
-            Billboard* billboard=static_cast<Billboard*>(*it);
-            LensFlare::Lens* lens=lensConfiguration_->at(i);
-            billboard->setPosition(-viewDirection*(1.0f-lens->position_));
+
+        int i = 0;
+        for (WorldEntity* attachedObject : this->getAttachedObjects())
+        {
+            Billboard* billboard = orxonox_cast<Billboard*>(attachedObject);
+            if (billboard == this->occlusionBillboard_)
+                continue;
+            const LensFlare::Lens& lens = lensConfiguration_.at(i);
+            billboard->setPosition(-viewDirection * (1.0f - lens.position_));
             billboard->setVisible(lightIsVisible);
-            billboard->setDefaultDimensions(dimension*lens->scale_,dimension*lens->scale_);
+            billboard->setDefaultDimensions(dimension * lens.scale_, dimension * lens.scale_);
             i++;
         }
     }
@@ -142,15 +145,16 @@
     */
     void LensFlare::updateBillboardAlphas(float alpha)
     {
-        this->colour_->a=alpha;
-        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
-        it++;
-        for(int i=0;it!=this->getAttachedObjects().end(); it++) {
-            ColourValue* cur=new ColourValue(0,0,0,0);
-            (*cur)+= *(this->colour_);
-            cur->a*=lensConfiguration_->at(i)->alpha_;
-            Billboard* billboard=static_cast<Billboard*>(*it);
-            billboard->setColour(*cur);
+        ColourValue cur = this->colour_;
+
+        int i = 0;
+        for (WorldEntity* attachedObject : this->getAttachedObjects())
+        {
+            Billboard* billboard = orxonox_cast<Billboard*>(attachedObject);
+            if (billboard == this->occlusionBillboard_)
+                continue;
+            cur.a = alpha * lensConfiguration_.at(i).alpha_;
+            billboard->setColour(cur);
             i++;
         }
     }
@@ -165,19 +169,19 @@
     */
     unsigned int LensFlare::getPointCount(float dimension) const
     {
-        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
+        Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
         Vector3 position = this->getWorldPosition();
         Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy();
         Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy();
-        int halfRes=fadeResolution_/2;
-        float resDim=dimension/fadeResolution_;
-        unsigned int count=0;
-        for(int i=-halfRes;i<=halfRes;i++)
+        int halfRes = fadeResolution_ / 2;
+        float resDim = dimension / fadeResolution_;
+        unsigned int count = 0;
+        for (int i = -halfRes; i <= halfRes; i++)
         {
-            for(int j=-halfRes;j<=halfRes;j++)
+            for (int j = -halfRes; j <= halfRes; j++)
             {
-                Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
-                if(camera->isVisible(point))
+                Vector3 point = position + (i*resDim) * nX + (j*resDim) * nY;//generate point samples
+                if (camera->isVisible(point))
                 {
                     count++;
                 }
@@ -190,33 +194,34 @@
     {
         if(this->isVisible())
         {
-            Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
-            this->cameraDistance_=camera->getDerivedPosition().distance(this->getPosition());
-            float dimension=this->cameraDistance_*this->scale_;
-            if(!this->fadeOnViewBorder_)
+            // get the current distance of the lensflare center from the camera
+            Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
+            float cameraDistance = camera->getDerivedPosition().distance(this->getPosition());
+            float dimension = cameraDistance * this->scale_;
+            if (!this->fadeOnViewBorder_)
             {
-                this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
+                this->fadeResolution_ = 3;//this is so we can still determine when the billboard has left the screen
             }
-            unsigned int pointCount=this->getPointCount(dimension*0.25f*this->scale_);
-            Vector3 viewDirection=this->getWorldPosition()-camera->getDerivedPosition()-camera->getDerivedDirection()*this->cameraDistance_;
-            updateBillboardStates(viewDirection,dimension,pointCount>0);
-            if(pointCount>0) {
-                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25f*this->scale_);
+            unsigned int pointCount = this->getPointCount(dimension * 0.25f * this->scale_);
+            Vector3 viewDirection = this->getWorldPosition() - camera->getDerivedPosition() - camera->getDerivedDirection() * cameraDistance;
+            this->updateBillboardStates(viewDirection, dimension, pointCount > 0);
+            if (pointCount > 0)
+            {
+                Ogre::Sphere sphere(this->getPosition(), dimension * 0.25f * this->scale_);
                 float left, right, top, bottom;
-                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
-                delete sphere;
+                camera->projectSphere(sphere, &left, &top, &right, &bottom);//approximate maximum pixel count of billboard with a sphere
                 
                 Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
-                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25f;
-                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
-                float ratio=(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero
-                float borderRatio=1.0f;
-                if(this->fadeOnViewBorder_)
+                float maxCount = (right - left) * (top - bottom) * window->getWidth() * window->getHeight() * 0.25f;
+                unsigned int pixelCount = this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
+                float ratio = (maxCount < 0.0f) ? 0.0f : (1.0f * pixelCount / maxCount);//prevent underflow and division by zero
+                float borderRatio = 1.0f;
+                if (this->fadeOnViewBorder_)
                 {
-                    borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
+                    borderRatio = ((float) pointCount) / (((float) fadeResolution_) * ((float) fadeResolution_));//ratio for the border fade
                 }
                 //update alpha values of all billboards except the HOQ billboard
-                this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),this->fadeExponent_)));
+                this->updateBillboardAlphas(std::min(1.0f, std::pow(std::min(ratio, borderRatio), this->fadeExponent_)));
             }
         }
     }
@@ -225,4 +230,4 @@
     {
       
     }
-}
\ No newline at end of file
+}

Modified: code/branches/shaders_merge/src/orxonox/graphics/LensFlare.h
===================================================================
--- code/branches/shaders_merge/src/orxonox/graphics/LensFlare.h	2016-01-19 13:22:10 UTC (rev 11077)
+++ code/branches/shaders_merge/src/orxonox/graphics/LensFlare.h	2016-01-19 21:49:57 UTC (rev 11078)
@@ -66,16 +66,16 @@
           class Lens
           {
               public:
-                  std::string* material_;//!< Which material should the Lens use, current choices include burst, bursthalo, halo1, halo2, halo3
+                  std::string material_;//!< Which material should the Lens use, current choices include burst, bursthalo, halo1, halo2, halo3
                   float alpha_;//!< Which base alpha value should the Lens use
                   float scale_;//!< Which base scale should the Lens Flare have
                   float position_;//!< This defines how far along the view direction the flare should be positioned, e.g. 0.5 would position the flare halfway between the viewer and the base burst, 0 at the camera, 1 at the burst
-                  Lens(std::string* material, float alpha, float scale, float position)
+                  Lens (const std::string& material, float alpha, float scale, float position)
                   {
-                      this->material_=material;
-                      this->alpha_=alpha;
-                      this->scale_=scale;
-                      this->position_=position;
+                      this->material_ = material;
+                      this->alpha_ = alpha;
+                      this->scale_ = scale;
+                      this->position_ = position;
                   }
           };
           
@@ -84,7 +84,7 @@
             virtual ~LensFlare();
             
             inline void setScale(float scale)
-                { this->scale_=scale; }
+                { this->scale_ = scale; }
             inline float getScale() const
                 { return this->scale_; }
                 
@@ -95,18 +95,14 @@
                 Vector3 containing r,g,b values
             */
             inline void setColour(const ColourValue& colour)
-            {
-                this->colour_->r=colour.r;
-                this->colour_->g=colour.g;
-                this->colour_->b=colour.b;
-            }
+                { this->colour_ = colour; }
             /**
             @brief
                 This returns the current base colour of the billboards
             @return a Vector3 containing r,g,b values
             */
             inline const ColourValue& getColour() const
-                { return *(new ColourValue(this->colour_->r,this->colour_->g,this->colour_->b)); }
+                { return this->colour_; }
                 
             /**
             @brief
@@ -120,7 +116,7 @@
                 note: this will always be an odd number, so the center point is included in the samples
             */
             inline void setFadeResolution(unsigned int fadeResolution)
-                { this->fadeResolution_=fadeResolution>0?fadeResolution:1; }
+                { this->fadeResolution_ = fadeResolution > 0 ? fadeResolution : 1; }
             /**
             @brief
                 This returns the resolution of the out-of-screen-fade-effect
@@ -136,7 +132,7 @@
                 how strong should the fade-out effect be
             */
             inline void setFadeExponent(float exponent)
-                { this->fadeExponent_=exponent; }
+                { this->fadeExponent_ = exponent; }
             /**
             @brief
                 This returns the exponent of the fade-out function
@@ -152,7 +148,7 @@
                 true to turn the effect on, false to turn it off
             */
             inline void setFadeOnViewBorder(bool fadeOnViewBorder)
-                { this->fadeOnViewBorder_=fadeOnViewBorder; }
+                { this->fadeOnViewBorder_ = fadeOnViewBorder; }
             /**
             @brief
                Determine whether the out-of-screen-fade-effect is on or off
@@ -173,21 +169,20 @@
             
             void createBillboards();
             
-            void updateBillboardStates(Vector3& viewDirection, float dimension, bool isLightVisible);
+            void updateBillboardStates(const Vector3& viewDirection, float dimension, bool isLightVisible);
             
             void updateBillboardAlphas(float alpha);
             
             unsigned int getPointCount(float dimension) const;
             
-            std::vector<Lens*>* lensConfiguration_;//!< this stores the lensConfiguration
+            std::vector<Lens> lensConfiguration_;//!< this stores the lensConfiguration
             Billboard* occlusionBillboard_;//!< this is a transparent billboard used solely for the Hardware Occlusion Query
-            float cameraDistance_;//!< current distance of the lensflare center from the camera
             float scale_;//!< this factor is used to scale the billboard to the desired size
             bool fadeOnViewBorder_;//!< should the effect fade out on the border of the view?
             unsigned int fadeResolution_;//!< how many points should be sampled per axis for the screen border fade. High number => smooth fade, but uses more processing power
             float fadeExponent_;//!< this determines how fast the flare fades away as it gets obstructed
-            ColourValue* colour_;//!< this stores the base colour of the light
+            ColourValue colour_;//!< this stores the base colour of the light
     };
 }
 
-#endif /* _LensFlare_H__ */
\ No newline at end of file
+#endif /* _LensFlare_H__ */




More information about the Orxonox-commit mailing list