Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9461 for code/branches/shaders


Ignore:
Timestamp:
Nov 20, 2012, 6:46:03 PM (12 years ago)
Author:
davidsa
Message:

Updated orxonox::Billboard so you can disable FrustumCulling. Also improved orxonox::LensFlare and added documentation.

Location:
code/branches/shaders/src/orxonox/graphics
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/shaders/src/orxonox/graphics/Billboard.cc

    r9448 r9461  
    180180        }
    181181    }
     182   
     183    void Billboard::disableFrustumCulling()
     184    {
     185        Ogre::BillboardSet* bSet = this->billboard_.getBillboardSet();
     186        if( bSet != NULL )
     187        {
     188            bSet->setBounds(Ogre::AxisAlignedBox(Ogre::AxisAlignedBox::EXTENT_INFINITE),0);
     189        }
     190    }
    182191}
  • code/branches/shaders/src/orxonox/graphics/Billboard.h

    r9448 r9461  
    8383           
    8484            void setRenderQueueGroup(unsigned char groupID);
     85           
     86            void disableFrustumCulling();
    8587
    8688
  • code/branches/shaders/src/orxonox/graphics/LensFlare.cc

    r9448 r9461  
    4747    CreateFactory(LensFlare);
    4848   
    49     LensFlare::LensFlare(BaseObject* creator) : StaticEntity(creator), scale_(1.0f)
     49    LensFlare::LensFlare(BaseObject* creator) : StaticEntity(creator), scale_(1.0f), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.0f)
    5050    {
    5151        RegisterObject(LensFlare);
     
    6464        SUPER(LensFlare, XMLPort, xmlelement, mode);
    6565        XMLPortParam(LensFlare, "scale", setScale, getScale, xmlelement, mode).defaultValues(1.0f);
     66        XMLPortParam(LensFlare, "fadeOnViewBorder", setFadeOnViewBorder, isFadeOnViewBorder, xmlelement, mode).defaultValues(true);
     67        XMLPortParam(LensFlare, "fadeResolution", setFadeResolution, getFadeResolution, xmlelement, mode).defaultValues(7);
     68        XMLPortParam(LensFlare, "fadeExponent", setFadeExponent, getFadeExponent, xmlelement, mode).defaultValues(2.0f);
    6669    }
    6770   
    6871    void LensFlare::registerVariables()
    6972    {
    70         registerVariable(this->scale_, VariableDirection::ToClient, new NetworkCallback<LensFlare>(this, &LensFlare::updateBillboardPositions));
    71     }
    72    
     73        registerVariable(this->scale_, VariableDirection::ToClient);
     74        registerVariable(this->fadeOnViewBorder_, VariableDirection::ToClient);
     75        registerVariable(this->fadeResolution_, VariableDirection::ToClient);
     76    }
     77
     78    /**
     79    @brief
     80        This function creates all the billboards needed for the flare effect
     81    */
    7382    void LensFlare::createBillboards()
    7483    {
     84        //TODO: add more billboards, possibly do some cleaning up, by using a loop
    7585        this->occlusionBillboard_ = new Billboard(this);
    7686        this->occlusionBillboard_->setMaterial("lensflare/hoq");
    7787        this->occlusionBillboard_->setPosition(this->getPosition());
    7888        this->occlusionBillboard_->setVisible(false);
     89        this->occlusionBillboard_->disableFrustumCulling();
    7990        this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
    8091        this->attach(this->occlusionBillboard_);
     
    8394        burst->setMaterial("lensflare/burst");
    8495        burst->setPosition(this->getPosition());
    85         burst->setVisible(false);
     96        burst->disableFrustumCulling();
     97        burst->setVisible(true);
    8698        this->attach(burst);
    8799    }
     100
     101    /**
     102    @brief
     103        This function updates the states of all the billboards, i.e. their positions, visibilty and dimensions
     104    @param dimension
     105        the current dimension of the main billboard, we're always using square billboards
     106    */
     107    void LensFlare::updateBillboardStates(unsigned int dimension, bool lightIsVisible)
     108    {
     109        //TODO: position and dimensions need to be calculated for everything but the main burst of the flare
     110        for(std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) {
     111            Billboard* billboard=static_cast<Billboard*>(*it);
     112            billboard->setPosition(this->getPosition());
     113            billboard->setVisible(lightIsVisible);
     114            billboard->setDefaultDimensions(dimension,dimension);
     115        }
     116    }
     117
     118    /**
     119    @brief
     120        This function updates the alpha values for all billboards except for the one used for Hardware Occlusion Querying
     121    @param alpha
     122        the new alpha value all visible billboards should use
     123    */
     124    void LensFlare::updateBillboardAlphas(float alpha)
     125    {
     126        ColourValue* colour = new ColourValue(1.0f,1.0f,1.0f,alpha);
     127        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
     128        it++;
     129        for(;it!=this->getAttachedObjects().end(); it++) {
     130            Billboard* billboard=static_cast<Billboard*>(*it);
     131            billboard->setColour(*colour);
     132        }
     133        delete colour;
     134    }
    88135   
    89     void LensFlare::updateBillboardPositions()
    90     {
    91         Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
    92         bool lightIsVisible=camera->isVisible(this->getPosition()); //is the light source visible from our point of view?
    93         this->cameraDistance_=camera->getPosition().distance(this->getPosition());
    94         unsigned int dimension=this->cameraDistance_*this->scale_;
    95        
    96         this->occlusionBillboard_->setPosition(this->getPosition());
    97         this->occlusionBillboard_->setVisible(lightIsVisible);
    98         this->occlusionBillboard_->setDefaultDimensions(dimension,dimension);
    99        
    100         Billboard* burst=static_cast<Billboard*>(getAttachedObject(1));
    101         burst->setPosition(this->getPosition());
    102         burst->setVisible(lightIsVisible);
    103         burst->setDefaultDimensions(dimension,dimension);
     136    /**
     137    @brief
     138        This function generates point samples of the main burst billboard according to the fadeResolution and returns how many of them are in the view port
     139    @param dimension
     140        the current dimension of the main billboard, we're always using square billboards
     141    @return
     142        the absolute amount of point samples that are currently captured by the camera of the view port
     143    */
     144    unsigned int LensFlare::getPointCount(unsigned int dimension) const
     145    {
     146        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
     147        Vector3 position = this->getPosition();
     148        Vector3 nX = camera->getOrientation().xAxis().normalisedCopy();
     149        Vector3 nY = camera->getOrientation().yAxis().normalisedCopy();
     150        int halfRes=fadeResolution_/2;
     151        int resDim=dimension/fadeResolution_;
     152        unsigned int count=0;
     153        for(int i=-halfRes;i<=halfRes;i++)
     154        {
     155            for(int j=-halfRes;j<=halfRes;j++)
     156            {
     157                Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
     158                if(camera->isVisible(point))
     159                {
     160                    count++;
     161                }
     162            }
     163        }
     164        return count;
    104165    }
    105166
     
    108169        if(this->isVisible())
    109170        {
    110             updateBillboardPositions();
    111             if(this->occlusionBillboard_->isVisible()) {
    112                 unsigned int dimension=this->cameraDistance_*this->scale_;
     171            Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
     172            this->cameraDistance_=camera->getPosition().distance(this->getPosition());
     173            unsigned int dimension=this->cameraDistance_*this->scale_;
     174            if(!this->fadeOnViewBorder_)
     175            {
     176                this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
     177            }
     178            unsigned int pointCount=this->getPointCount(dimension);
     179            updateBillboardStates(dimension,pointCount>0);
     180            if(pointCount>0) {
    113181                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25);
    114                 Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
    115182                float left, right, top, bottom;
    116183                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
     
    120187                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25;
    121188                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
    122                 float ratio=pixelCount/maxCount;
    123                 //orxout() << "maxCount: " << maxCount << " HOQ: " << pixelCount << " ratio: " << ratio << std::endl;
    124                 ColourValue* colour = new ColourValue(1.0f,1.0f,1.0f,ratio); //adjust alpha of billboard
     189                float ratio=(maxCount==0)?0:(pixelCount/maxCount);//prevent division by zero
     190                float borderRatio=1.0f;
     191                if(this->fadeOnViewBorder_)
     192                {
     193                    borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
     194                }
    125195               
    126                 Billboard* burst=static_cast<Billboard*>(getAttachedObject(1));
    127                 burst->setColour(*colour);
    128                 delete colour;
     196                //update alpha values of all billboards except the HOQ billboard
     197                this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),2.0f)));
    129198            }
    130199        }
     
    133202    void LensFlare::changedVisibility()
    134203    {
     204     
    135205    }
    136206}
  • code/branches/shaders/src/orxonox/graphics/LensFlare.h

    r9448 r9461  
    5858    // if we have multiple strong lights it'll become way more complicated to determine how much of every object is occluded individually
    5959    // there's below a 100 render queue groups, so maybe we should take turns for each object to be tested, so we only have one of the objects rendered at a time
    60     // obviously we shouldn't use too much of these effects anyways, since they use a lot of performance, so not sure whether it's worth implementing a solution that works for multiple lens flares on screen
     60    // obviously we shouldn't use too many of these effects anyways, since they use a lot of processing power, so not sure whether it's worth implementing a solution that works for multiple lens flares on screen
    6161    class _OrxonoxExport LensFlare : public StaticEntity, public Tickable
    6262    {
     
    6767            inline void setScale(float scale)
    6868                { this->scale_=scale; }
    69             inline float getScale()
     69            inline float getScale() const
    7070                { return this->scale_; }
     71               
     72            /**
     73            @brief
     74                This sets the resolution of the out-of-screen-fade-effect
     75               
     76                the higher the resolution, the smoother the transition, but it will also have a greater impact on the performance
     77                this happens with O(n^2) since it's a two dimensional operation.
     78            @param fadeResolution
     79                how many point samples should be used per axis
     80               
     81                note: this will always be an odd number, so the center point is included in the samples
     82            */
     83            inline void setFadeResolution(unsigned int fadeResolution)
     84                { this->fadeResolution_=fadeResolution>0?fadeResolution:1; }
     85            /**
     86            @brief
     87                This returns the resolution of the out-of-screen-fade-effect
     88            @return how many point samples are being used per axis
     89            */
     90            inline unsigned int getFadeResolution() const
     91                { return this->fadeResolution_; }
     92               
     93            /**
     94            @brief
     95                This sets the exponent of the fade-out function
     96            @param exponent
     97                how strong should the fade-out effect be
     98            */
     99            inline void setFadeExponent(float exponent)
     100                { this->fadeExponent_=exponent; }
     101            /**
     102            @brief
     103                This returns the exponent of the fade-out function
     104            @return the exponent of the fade-out function
     105            */
     106            inline float getFadeExponent() const
     107                { return this->fadeExponent_; }
     108               
     109            /**
     110            @brief
     111               Turn the out-of-screen-fade-effect on or off
     112            @param fadeOnViewBorder
     113                true to turn the effect on, false to turn it off
     114            */
     115            inline void setFadeOnViewBorder(bool fadeOnViewBorder)
     116                { this->fadeOnViewBorder_=fadeOnViewBorder; }
     117            /**
     118            @brief
     119               Determine whether the out-of-screen-fade-effect is on or off
     120            @return
     121                true if the effect is on, false if it is off
     122            */
     123            inline bool isFadeOnViewBorder() const
     124                { return this->fadeOnViewBorder_; }
    71125
    72126            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     
    81135            void createBillboards();
    82136           
    83             void updateBillboardPositions();
     137            void updateBillboardStates(unsigned int dimension, bool isLightVisible);
    84138           
    85             Billboard* occlusionBillboard_;
    86             unsigned int cameraDistance_;
    87             float scale_;
     139            void updateBillboardAlphas(float alpha);
     140           
     141            unsigned int getPointCount(unsigned int dimension) const;
     142           
     143            Billboard* occlusionBillboard_;//!< this is a transparent billboard used solely for the Hardware Occlusion Query
     144            unsigned int cameraDistance_;//!< current distance of the lensflare center from the camera
     145            float scale_;//!< this factor is used to scale the billboard to the desired size
     146            bool fadeOnViewBorder_;//!< should the effect fade out on the border of the view?
     147            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
     148            float fadeExponent_;//!< this determines how fast the flare fades away as it gets obstructed
    88149    };
    89150}
Note: See TracChangeset for help on using the changeset viewer.