Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.