Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11078


Ignore:
Timestamp:
Jan 19, 2016, 10:49:57 PM (8 years ago)
Author:
landauf
Message:

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

Location:
code/branches/shaders_merge/src/orxonox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/shaders_merge/src/orxonox/Scene.cc

    r11073 r11078  
    9898            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
    9999
     100            this->renderQueueListener_ = nullptr;
    100101            this->radar_ = nullptr;
    101102        }
     
    125126
    126127            if (GameMode::showsGraphics())
     128            {
     129                this->sceneManager_->removeRenderQueueListener(this->renderQueueListener_);
     130                delete this->renderQueueListener_;
    127131                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
     132            }
    128133            else
    129134                delete this->sceneManager_;
  • code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc

    r11075 r11078  
    4646    RegisterClass(LensFlare);
    4747   
    48     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))
     48    LensFlare::LensFlare(Context* context) : StaticEntity(context), scale_(1.0f), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.0f)
    4949    {
    5050        RegisterObject(LensFlare);
    5151       
    52         this->lensConfiguration_=new std::vector<LensFlare::Lens*>();
    53         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),1.0f,1.0f,1.0f)); //main burst
    54         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),0.7f,1.2f,1.05f)); //secondary burst
    55         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/bursthalo"),0.7f,0.9f,1.0f)); //halo around main burst
    56         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/ring"),0.1f,2.5f,0.9f)); //all the different distanced lenses
    57         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.2f,0.5f));
    58         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.1f,0.3f,0.45f));
    59         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.4f,0.2f,0.35f));
    60         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.4f,0.25f));
    61         this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo4"),0.05f,0.2f,0.2f));
     52        this->lensConfiguration_.emplace_back("lensflare/burst",     1.0f,  1.0f, 1.0f); //main burst
     53        this->lensConfiguration_.emplace_back("lensflare/burst",     0.7f,  1.2f, 1.05f); //secondary burst
     54        this->lensConfiguration_.emplace_back("lensflare/bursthalo", 0.7f,  0.9f, 1.0f); //halo around main burst
     55        this->lensConfiguration_.emplace_back("lensflare/ring",      0.1f,  2.5f, 0.9f); //all the different distanced lenses
     56        this->lensConfiguration_.emplace_back("lensflare/iris",      0.1f,  0.2f, 0.5f);
     57        this->lensConfiguration_.emplace_back("lensflare/halo5",     0.1f,  0.3f, 0.45f);
     58        this->lensConfiguration_.emplace_back("lensflare/halo5",     0.4f,  0.2f, 0.35f);
     59        this->lensConfiguration_.emplace_back("lensflare/iris",      0.1f,  0.4f, 0.25f);
     60        this->lensConfiguration_.emplace_back("lensflare/halo4",     0.05f, 0.2f, 0.2f);
    6261       
    6362        this->createBillboards();
     
    10099        this->attach(this->occlusionBillboard_);
    101100       
    102         for(std::vector<LensFlare::Lens*>::iterator it = lensConfiguration_->begin(); it != lensConfiguration_->end(); ++it) {
    103             Billboard* lensPart=new Billboard(this->getContext());
    104             lensPart->setMaterial(*(*it)->material_);
     101        for (const LensFlare::Lens& lens : lensConfiguration_)
     102        {
     103            Billboard* lensPart = new Billboard(this->getContext());
     104            lensPart->setMaterial(lens.material_);
    105105            lensPart->disableFrustumCulling();
    106106            lensPart->setVisible(true);
     
    119119        is the (point-)light source currently visible
    120120    */
    121     void LensFlare::updateBillboardStates(Vector3& viewDirection, float dimension, bool lightIsVisible)
     121    void LensFlare::updateBillboardStates(const Vector3& viewDirection, float dimension, bool lightIsVisible)
    122122    {
    123         this->occlusionBillboard_->setDefaultDimensions(dimension*0.5f,dimension*0.5f);
     123        this->occlusionBillboard_->setDefaultDimensions(dimension * 0.5f, dimension * 0.5f);
    124124        this->occlusionBillboard_->setVisible(lightIsVisible);
    125         std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
    126         it++;
    127         for(int i=0; it != this->getAttachedObjects().end(); it++) {
    128             Billboard* billboard=static_cast<Billboard*>(*it);
    129             LensFlare::Lens* lens=lensConfiguration_->at(i);
    130             billboard->setPosition(-viewDirection*(1.0f-lens->position_));
     125
     126        int i = 0;
     127        for (WorldEntity* attachedObject : this->getAttachedObjects())
     128        {
     129            Billboard* billboard = orxonox_cast<Billboard*>(attachedObject);
     130            if (billboard == this->occlusionBillboard_)
     131                continue;
     132            const LensFlare::Lens& lens = lensConfiguration_.at(i);
     133            billboard->setPosition(-viewDirection * (1.0f - lens.position_));
    131134            billboard->setVisible(lightIsVisible);
    132             billboard->setDefaultDimensions(dimension*lens->scale_,dimension*lens->scale_);
     135            billboard->setDefaultDimensions(dimension * lens.scale_, dimension * lens.scale_);
    133136            i++;
    134137        }
     
    143146    void LensFlare::updateBillboardAlphas(float alpha)
    144147    {
    145         this->colour_->a=alpha;
    146         std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
    147         it++;
    148         for(int i=0;it!=this->getAttachedObjects().end(); it++) {
    149             ColourValue* cur=new ColourValue(0,0,0,0);
    150             (*cur)+= *(this->colour_);
    151             cur->a*=lensConfiguration_->at(i)->alpha_;
    152             Billboard* billboard=static_cast<Billboard*>(*it);
    153             billboard->setColour(*cur);
     148        ColourValue cur = this->colour_;
     149
     150        int i = 0;
     151        for (WorldEntity* attachedObject : this->getAttachedObjects())
     152        {
     153            Billboard* billboard = orxonox_cast<Billboard*>(attachedObject);
     154            if (billboard == this->occlusionBillboard_)
     155                continue;
     156            cur.a = alpha * lensConfiguration_.at(i).alpha_;
     157            billboard->setColour(cur);
    154158            i++;
    155159        }
     
    166170    unsigned int LensFlare::getPointCount(float dimension) const
    167171    {
    168         Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
     172        Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
    169173        Vector3 position = this->getWorldPosition();
    170174        Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy();
    171175        Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy();
    172         int halfRes=fadeResolution_/2;
    173         float resDim=dimension/fadeResolution_;
    174         unsigned int count=0;
    175         for(int i=-halfRes;i<=halfRes;i++)
    176         {
    177             for(int j=-halfRes;j<=halfRes;j++)
     176        int halfRes = fadeResolution_ / 2;
     177        float resDim = dimension / fadeResolution_;
     178        unsigned int count = 0;
     179        for (int i = -halfRes; i <= halfRes; i++)
     180        {
     181            for (int j = -halfRes; j <= halfRes; j++)
    178182            {
    179                 Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
    180                 if(camera->isVisible(point))
     183                Vector3 point = position + (i*resDim) * nX + (j*resDim) * nY;//generate point samples
     184                if (camera->isVisible(point))
    181185                {
    182186                    count++;
     
    191195        if(this->isVisible())
    192196        {
    193             Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
    194             this->cameraDistance_=camera->getDerivedPosition().distance(this->getPosition());
    195             float dimension=this->cameraDistance_*this->scale_;
    196             if(!this->fadeOnViewBorder_)
     197            // get the current distance of the lensflare center from the camera
     198            Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
     199            float cameraDistance = camera->getDerivedPosition().distance(this->getPosition());
     200            float dimension = cameraDistance * this->scale_;
     201            if (!this->fadeOnViewBorder_)
    197202            {
    198                 this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
     203                this->fadeResolution_ = 3;//this is so we can still determine when the billboard has left the screen
    199204            }
    200             unsigned int pointCount=this->getPointCount(dimension*0.25f*this->scale_);
    201             Vector3 viewDirection=this->getWorldPosition()-camera->getDerivedPosition()-camera->getDerivedDirection()*this->cameraDistance_;
    202             updateBillboardStates(viewDirection,dimension,pointCount>0);
    203             if(pointCount>0) {
    204                 Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25f*this->scale_);
     205            unsigned int pointCount = this->getPointCount(dimension * 0.25f * this->scale_);
     206            Vector3 viewDirection = this->getWorldPosition() - camera->getDerivedPosition() - camera->getDerivedDirection() * cameraDistance;
     207            this->updateBillboardStates(viewDirection, dimension, pointCount > 0);
     208            if (pointCount > 0)
     209            {
     210                Ogre::Sphere sphere(this->getPosition(), dimension * 0.25f * this->scale_);
    205211                float left, right, top, bottom;
    206                 camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
    207                 delete sphere;
     212                camera->projectSphere(sphere, &left, &top, &right, &bottom);//approximate maximum pixel count of billboard with a sphere
    208213               
    209214                Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
    210                 float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25f;
    211                 float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
    212                 float ratio=(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero
    213                 float borderRatio=1.0f;
    214                 if(this->fadeOnViewBorder_)
     215                float maxCount = (right - left) * (top - bottom) * window->getWidth() * window->getHeight() * 0.25f;
     216                unsigned int pixelCount = this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
     217                float ratio = (maxCount < 0.0f) ? 0.0f : (1.0f * pixelCount / maxCount);//prevent underflow and division by zero
     218                float borderRatio = 1.0f;
     219                if (this->fadeOnViewBorder_)
    215220                {
    216                     borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
     221                    borderRatio = ((float) pointCount) / (((float) fadeResolution_) * ((float) fadeResolution_));//ratio for the border fade
    217222                }
    218223                //update alpha values of all billboards except the HOQ billboard
    219                 this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),this->fadeExponent_)));
     224                this->updateBillboardAlphas(std::min(1.0f, std::pow(std::min(ratio, borderRatio), this->fadeExponent_)));
    220225            }
    221226        }
  • code/branches/shaders_merge/src/orxonox/graphics/LensFlare.h

    r11075 r11078  
    6767          {
    6868              public:
    69                   std::string* material_;//!< Which material should the Lens use, current choices include burst, bursthalo, halo1, halo2, halo3
     69                  std::string material_;//!< Which material should the Lens use, current choices include burst, bursthalo, halo1, halo2, halo3
    7070                  float alpha_;//!< Which base alpha value should the Lens use
    7171                  float scale_;//!< Which base scale should the Lens Flare have
    7272                  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
    73                   Lens(std::string* material, float alpha, float scale, float position)
     73                  Lens (const std::string& material, float alpha, float scale, float position)
    7474                  {
    75                       this->material_=material;
    76                       this->alpha_=alpha;
    77                       this->scale_=scale;
    78                       this->position_=position;
     75                      this->material_ = material;
     76                      this->alpha_ = alpha;
     77                      this->scale_ = scale;
     78                      this->position_ = position;
    7979                  }
    8080          };
     
    8585           
    8686            inline void setScale(float scale)
    87                 { this->scale_=scale; }
     87                { this->scale_ = scale; }
    8888            inline float getScale() const
    8989                { return this->scale_; }
     
    9696            */
    9797            inline void setColour(const ColourValue& colour)
    98             {
    99                 this->colour_->r=colour.r;
    100                 this->colour_->g=colour.g;
    101                 this->colour_->b=colour.b;
    102             }
     98                { this->colour_ = colour; }
    10399            /**
    104100            @brief
     
    107103            */
    108104            inline const ColourValue& getColour() const
    109                 { return *(new ColourValue(this->colour_->r,this->colour_->g,this->colour_->b)); }
     105                { return this->colour_; }
    110106               
    111107            /**
     
    121117            */
    122118            inline void setFadeResolution(unsigned int fadeResolution)
    123                 { this->fadeResolution_=fadeResolution>0?fadeResolution:1; }
     119                { this->fadeResolution_ = fadeResolution > 0 ? fadeResolution : 1; }
    124120            /**
    125121            @brief
     
    137133            */
    138134            inline void setFadeExponent(float exponent)
    139                 { this->fadeExponent_=exponent; }
     135                { this->fadeExponent_ = exponent; }
    140136            /**
    141137            @brief
     
    153149            */
    154150            inline void setFadeOnViewBorder(bool fadeOnViewBorder)
    155                 { this->fadeOnViewBorder_=fadeOnViewBorder; }
     151                { this->fadeOnViewBorder_ = fadeOnViewBorder; }
    156152            /**
    157153            @brief
     
    174170            void createBillboards();
    175171           
    176             void updateBillboardStates(Vector3& viewDirection, float dimension, bool isLightVisible);
     172            void updateBillboardStates(const Vector3& viewDirection, float dimension, bool isLightVisible);
    177173           
    178174            void updateBillboardAlphas(float alpha);
     
    180176            unsigned int getPointCount(float dimension) const;
    181177           
    182             std::vector<Lens*>* lensConfiguration_;//!< this stores the lensConfiguration
     178            std::vector<Lens> lensConfiguration_;//!< this stores the lensConfiguration
    183179            Billboard* occlusionBillboard_;//!< this is a transparent billboard used solely for the Hardware Occlusion Query
    184             float cameraDistance_;//!< current distance of the lensflare center from the camera
    185180            float scale_;//!< this factor is used to scale the billboard to the desired size
    186181            bool fadeOnViewBorder_;//!< should the effect fade out on the border of the view?
    187182            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
    188183            float fadeExponent_;//!< this determines how fast the flare fades away as it gets obstructed
    189             ColourValue* colour_;//!< this stores the base colour of the light
     184            ColourValue colour_;//!< this stores the base colour of the light
    190185    };
    191186}
Note: See TracChangeset for help on using the changeset viewer.