Changeset 8495 in orxonox.OLD for trunk/src/lib/graphics/effects/fog_effect.cc
- Timestamp:
- Jun 15, 2006, 9:50:56 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/effects/fog_effect.cc
r8362 r8495 1 1 /* 2 3 4 5 6 7 8 9 2 orxonox - the future of 3D-vertical-scrollers 3 4 Copyright (C) 2004 orx 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 10 11 11 ### File Specific: 12 12 main-programmer: hdavid, amaechler 13 13 */ 14 14 … … 22 22 SHELL_COMMAND(activate, FogEffect, activateFog); 23 23 SHELL_COMMAND(deactivate, FogEffect, deactivateFog); 24 SHELL_COMMAND(startfogging, FogEffect, startFogging); 24 SHELL_COMMAND(fadein, FogEffect, fadeInFog); 25 SHELL_COMMAND(fadeout, FogEffect, fadeOutFog); 26 27 // TODO: Fix fades 25 28 26 29 using namespace std; … … 28 31 CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); 29 32 30 FogEffect::FogEffect(const TiXmlElement* root) 31 { 32 this->setClassID(CL_FOG_EFFECT, "FogEffect"); 33 34 this->init(); 35 36 if (root != NULL) 37 this->loadParams(root); 38 39 if (this->fogActivate) 40 this->activate(); 41 } 42 43 44 FogEffect::~FogEffect() 45 { 46 this->deactivate(); 47 } 48 49 50 void FogEffect::loadParams(const TiXmlElement* root) 51 { 52 WeatherEffect::loadParams(root);53 54 LoadParam(root, "mode", this, FogEffect, setFogMode);55 LoadParam(root, "density", this, FogEffect, setFogDensity);56 LoadParam(root, "range", this, FogEffect, setFogRange);57 LoadParam(root, "color", this, FogEffect, setFogColor); 58 59 LOAD_PARAM_START_CYCLE(root, element); 60 { 61 LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption); 62 } 63 LOAD_PARAM_END_CYCLE(element); 64 } 65 66 bool FogEffect::init() 67 { 68 //default values 69 this->fogActivate = false;70 this->fogFadeDuration =0;71 this->localTimer = 0;72 73 this->fogMode = GL_EXP2; 74 this->fogDensity = 0.001;75 this->fogStart= 0;76 this->fogEnd = 5000;77 this->colorVector = Vector(0.7, 0.7, 0.7);78 79 return true;80 } 81 82 83 bool FogEffect::activate() 84 { 85 PRINTF(0)( "Enabling FogEffect, mode: %i, density: %f, start: %f, end: %f, color %f, %f, %f\n", this->fogMode, this->fogDensity, this->fogStart, this->fogEnd, this->colorVector.x, this->colorVector.y, this->colorVector.z); 86 87 this->fogActivate = true; 88 89 GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; 90 glFogi(GL_FOG_MODE, this->fogMode);91 glFogfv(GL_FOG_COLOR, fogColor);92 glFogf(GL_FOG_DENSITY, this->fogDensity);93 94 glFogf(GL_FOG_START, this->fogStart);95 glFogf(GL_FOG_END, this->fogEnd);96 97 glEnable(GL_FOG); 98 // glClearColor(0.5, 0.5, 0.5, 1.0);99 return true; 100 } 101 102 103 bool FogEffect::deactivate() 104 { 105 PRINTF(0)("Deactivating FogEffect\n"); 106 107 this->fogActivate = false;108 109 glDisable(GL_FOG); 110 111 return true; 33 FogEffect::FogEffect(const TiXmlElement* root) { 34 this->setClassID(CL_FOG_EFFECT, "FogEffect"); 35 36 this->init(); 37 38 if (root != NULL) 39 this->loadParams(root); 40 41 if (this->fogActivate) 42 this->activate(); 43 } 44 45 46 FogEffect::~FogEffect() { 47 this->deactivate(); 48 } 49 50 51 void FogEffect::loadParams(const TiXmlElement* root) { 52 WeatherEffect::loadParams(root); 53 54 LoadParam(root, "mode", this, FogEffect, setFogMode); 55 LoadParam(root, "density", this, FogEffect, setFogDensity); 56 LoadParam(root, "range", this, FogEffect, setFogRange); 57 LoadParam(root, "color", this, FogEffect, setFogColor); 58 LoadParam(root, "fadeinduration", this, FogEffect, setFogFadeIn); 59 LoadParam(root, "fadeoutduration", this, FogEffect, setFogFadeOut); 60 61 LOAD_PARAM_START_CYCLE(root, element); 62 { 63 LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption); 64 } 65 LOAD_PARAM_END_CYCLE(element); 66 } 67 68 void FogEffect::init() { 69 // default values 70 this->fogMode = GL_LINEAR; 71 this->fogDensity = 0.03; 72 this->fogStart = 0; 73 this->fogEnd = 50; 74 this->colorVector = Vector(0.3, 0.3, 0.3); 75 76 // init variables 77 this->fogFadeInDuration = 0; 78 this->fogFadeOutDuration = 0; 79 this->fogFadeDensity = 0; 80 this->localTimer = 0; 81 this->fogActivate = false; 82 this->fogFadeInActivate = false; 83 this->fogFadeOutActivate = false; 84 85 } 86 87 88 void FogEffect::activate() { 89 PRINTF(0)( "Enabling FogEffect, mode: %i, density: %f, start: %f, end: %f, color %f, %f, %f\n", this->fogMode, this->fogDensity, this->fogStart, this->fogEnd, this->colorVector.x, this->colorVector.y, this->colorVector.z); 90 91 this->fogActivate = true; 92 93 GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; 94 glFogi(GL_FOG_MODE, this->fogMode); 95 glFogfv(GL_FOG_COLOR, fogColor); 96 glHint(GL_FOG_HINT, GL_DONT_CARE); 97 glFogf(GL_FOG_DENSITY, this->fogDensity); 98 glFogf(GL_FOG_START, this->fogStart); 99 glFogf(GL_FOG_END, this->fogEnd); 100 101 glEnable(GL_FOG); 102 103 } 104 105 106 void FogEffect::deactivate() { 107 PRINTF(0)("Deactivating FogEffect\n"); 108 109 this->fogFadeInActivate = false; 110 this->fogFadeOutActivate = false; 111 this->fogActivate = false; 112 113 glDisable(GL_FOG); 114 112 115 } 113 116 114 117 void FogEffect::draw() const { 115 118 116 if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) 117 glFogf(GL_FOG_DENSITY, this->fogFadeDensity); 118 //else 119 // glFogf(GL_FOG_DENSITY, this->fogDensity); 120 121 } 122 void FogEffect::tick(float dt) 123 { 124 if (!this->fogActivate) 125 return; 126 127 128 if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) { 129 this->localTimer += dt; 130 float progress = this->localTimer / this->fogFadeDuration; 131 this->fogFadeDensity = progress * this->fogDensity; 132 } 133 } 134 135 void FogEffect::startFogging() { 136 137 if (this->fogActivate) 138 this->deactivate(); 139 140 this->fogFadeDuration = 10; 141 this->localTimer = 0; 142 this->activate(); 143 144 } 145 146 147 GLint FogEffect::stringToFogMode(const std::string& mode) 148 { 149 if(mode == "GL_LINEAR") 150 return GL_LINEAR; 151 else if(mode == "GL_EXP") 152 return GL_EXP; 153 else if(mode == "GL_EXP2" ) 154 return GL_EXP2; 155 else 156 return -1; 157 } 158 159 119 if (!this->fogActivate) 120 return; 121 122 // If Fog Fade 123 if ( this->fogFadeInActivate || this->fogFadeOutActivate ) 124 glFogf(GL_FOG_DENSITY, this->fogFadeDensity); 125 126 } 127 128 void FogEffect::tick(float dt) { 129 if (!this->fogActivate) 130 return; 131 132 if ( this->fogFadeInActivate ) { 133 this->localTimer += dt; 134 this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity; 135 136 if ( this->localTimer >= this->fogFadeOutDuration ) 137 this->fogFadeInActivate = false; 138 } 139 140 if ( this->fogFadeOutActivate ) { 141 this->localTimer += dt; 142 this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity); 143 144 if ( this->localTimer >= this->fogFadeOutDuration ) 145 this->deactivate(); 146 } 147 } 148 149 void FogEffect::fadeInFog() { 150 151 // If Fog is already fading out, stop it 152 this->fogFadeOutActivate = false; 153 154 // If Fog is already on, turn it off first 155 if (this->fogActivate) 156 this->deactivate(); 157 158 // If no manual FadeIn value was set, set a default value 159 if (!this->fogFadeInDuration > 0) 160 this->fogFadeInDuration = 20; 161 162 // Reset local timer 163 this->localTimer = 0; 164 165 // set FogFadeIn activate 166 this->fogFadeInActivate = true; 167 168 // Activate Fog 169 this->activate(); 170 171 } 172 173 174 void FogEffect::fadeOutFog() { 175 176 // If Fog is already fading in, stop it 177 this->fogFadeInActivate = false; 178 179 // If Fog is off, turn it on first 180 if (!this->fogActivate) 181 this->activate(); 182 183 // If no manual FadeOut value was set, set a default value 184 if (!this->fogFadeOutDuration > 0) 185 this->fogFadeOutDuration = 20; 186 187 // set FogFadeOut activate 188 this->fogFadeOutActivate = true; 189 190 // Reset local timer 191 this->localTimer = 0; 192 193 } 194 195 196 GLint FogEffect::stringToFogMode(const std::string& mode) { 197 if(mode == "GL_LINEAR") 198 return GL_LINEAR; 199 else if(mode == "GL_EXP") 200 return GL_EXP; 201 else if(mode == "GL_EXP2" ) 202 return GL_EXP2; 203 else 204 return -1; 205 } 206 207
Note: See TracChangeset
for help on using the changeset viewer.