- Timestamp:
- Jan 19, 2016, 10:49:57 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/shaders_merge/src/orxonox/graphics/LensFlare.cc
r11075 r11078 46 46 RegisterClass(LensFlare); 47 47 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) 49 49 { 50 50 RegisterObject(LensFlare); 51 51 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); 62 61 63 62 this->createBillboards(); … … 100 99 this->attach(this->occlusionBillboard_); 101 100 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_); 105 105 lensPart->disableFrustumCulling(); 106 106 lensPart->setVisible(true); … … 119 119 is the (point-)light source currently visible 120 120 */ 121 void LensFlare::updateBillboardStates( Vector3& viewDirection, float dimension, bool lightIsVisible)121 void LensFlare::updateBillboardStates(const Vector3& viewDirection, float dimension, bool lightIsVisible) 122 122 { 123 this->occlusionBillboard_->setDefaultDimensions(dimension *0.5f,dimension*0.5f);123 this->occlusionBillboard_->setDefaultDimensions(dimension * 0.5f, dimension * 0.5f); 124 124 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_)); 131 134 billboard->setVisible(lightIsVisible); 132 billboard->setDefaultDimensions(dimension *lens->scale_,dimension*lens->scale_);135 billboard->setDefaultDimensions(dimension * lens.scale_, dimension * lens.scale_); 133 136 i++; 134 137 } … … 143 146 void LensFlare::updateBillboardAlphas(float alpha) 144 147 { 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); 154 158 i++; 155 159 } … … 166 170 unsigned int LensFlare::getPointCount(float dimension) const 167 171 { 168 Ogre::Camera* camera =CameraManager::getInstance().getActiveCamera()->getOgreCamera();172 Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); 169 173 Vector3 position = this->getWorldPosition(); 170 174 Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy(); 171 175 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++) 178 182 { 179 Vector3 point =position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples180 if (camera->isVisible(point))183 Vector3 point = position + (i*resDim) * nX + (j*resDim) * nY;//generate point samples 184 if (camera->isVisible(point)) 181 185 { 182 186 count++; … … 191 195 if(this->isVisible()) 192 196 { 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_) 197 202 { 198 this->fadeResolution_ =3;//this is so we can still determine when the billboard has left the screen203 this->fadeResolution_ = 3;//this is so we can still determine when the billboard has left the screen 199 204 } 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_); 205 211 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 208 213 209 214 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 count212 float ratio =(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero213 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_) 215 220 { 216 borderRatio =((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade221 borderRatio = ((float) pointCount) / (((float) fadeResolution_) * ((float) fadeResolution_));//ratio for the border fade 217 222 } 218 223 //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_))); 220 225 } 221 226 }
Note: See TracChangeset
for help on using the changeset viewer.