Changeset 9461 for code/branches/shaders/src/orxonox/graphics/LensFlare.cc
- Timestamp:
- Nov 20, 2012, 6:46:03 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/shaders/src/orxonox/graphics/LensFlare.cc
r9448 r9461 47 47 CreateFactory(LensFlare); 48 48 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) 50 50 { 51 51 RegisterObject(LensFlare); … … 64 64 SUPER(LensFlare, XMLPort, xmlelement, mode); 65 65 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); 66 69 } 67 70 68 71 void LensFlare::registerVariables() 69 72 { 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 */ 73 82 void LensFlare::createBillboards() 74 83 { 84 //TODO: add more billboards, possibly do some cleaning up, by using a loop 75 85 this->occlusionBillboard_ = new Billboard(this); 76 86 this->occlusionBillboard_->setMaterial("lensflare/hoq"); 77 87 this->occlusionBillboard_->setPosition(this->getPosition()); 78 88 this->occlusionBillboard_->setVisible(false); 89 this->occlusionBillboard_->disableFrustumCulling(); 79 90 this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ); 80 91 this->attach(this->occlusionBillboard_); … … 83 94 burst->setMaterial("lensflare/burst"); 84 95 burst->setPosition(this->getPosition()); 85 burst->setVisible(false); 96 burst->disableFrustumCulling(); 97 burst->setVisible(true); 86 98 this->attach(burst); 87 99 } 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 } 88 135 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; 104 165 } 105 166 … … 108 169 if(this->isVisible()) 109 170 { 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) { 113 181 Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25); 114 Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();115 182 float left, right, top, bottom; 116 183 camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere … … 120 187 float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25; 121 188 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 } 125 195 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))); 129 198 } 130 199 } … … 133 202 void LensFlare::changedVisibility() 134 203 { 204 135 205 } 136 206 }
Note: See TracChangeset
for help on using the changeset viewer.