| 1 | /* | 
|---|
| 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 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |         main-programmer: hdavid, amaechler | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include "fog_effect.h" | 
|---|
| 16 |  | 
|---|
| 17 | #include "util/loading/load_param.h" | 
|---|
| 18 | #include "util/loading/factory.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "glincl.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "shell_command.h" | 
|---|
| 23 |  | 
|---|
| 24 | SHELL_COMMAND(activate, FogEffect, activateFog); | 
|---|
| 25 | SHELL_COMMAND(deactivate, FogEffect, deactivateFog); | 
|---|
| 26 | SHELL_COMMAND(startfogging, FogEffect, startFogging); | 
|---|
| 27 |  | 
|---|
| 28 | using namespace std; | 
|---|
| 29 |  | 
|---|
| 30 | CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); | 
|---|
| 31 |  | 
|---|
| 32 | FogEffect::FogEffect(const TiXmlElement* root) | 
|---|
| 33 | { | 
|---|
| 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 | { | 
|---|
| 48 |         this->deactivate(); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | void FogEffect::loadParams(const TiXmlElement* root) | 
|---|
| 53 | { | 
|---|
| 54 |         WeatherEffect::loadParams(root); | 
|---|
| 55 |  | 
|---|
| 56 |         LoadParam(root, "mode", this, FogEffect, setFogMode); | 
|---|
| 57 |         LoadParam(root, "density", this, FogEffect, setFogDensity); | 
|---|
| 58 |         LoadParam(root, "range", this, FogEffect, setFogRange); | 
|---|
| 59 |         LoadParam(root, "color", this, FogEffect, setFogColor); | 
|---|
| 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 | bool FogEffect::init() | 
|---|
| 69 | { | 
|---|
| 70 |         //default values | 
|---|
| 71 |         this->fogActivate = false; | 
|---|
| 72 |         this->fogFadeDuration = 0; | 
|---|
| 73 |         this->localTimer = 0; | 
|---|
| 74 |  | 
|---|
| 75 |         this->fogMode = GL_EXP2; | 
|---|
| 76 |         this->fogDensity = 0.001; | 
|---|
| 77 |         this->fogStart = 0; | 
|---|
| 78 |         this->fogEnd = 5000; | 
|---|
| 79 |         this->colorVector = Vector(0.7, 0.7, 0.7); | 
|---|
| 80 |  | 
|---|
| 81 |         return true; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | bool FogEffect::activate() | 
|---|
| 86 | { | 
|---|
| 87 |         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); | 
|---|
| 88 |  | 
|---|
| 89 |         this->fogActivate = true; | 
|---|
| 90 |  | 
|---|
| 91 |         GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; | 
|---|
| 92 |         glFogi(GL_FOG_MODE, this->fogMode); | 
|---|
| 93 |         glFogfv(GL_FOG_COLOR, fogColor); | 
|---|
| 94 |         glFogf(GL_FOG_DENSITY, this->fogDensity); | 
|---|
| 95 |         glHint(GL_FOG_HINT, GL_DONT_CARE); | 
|---|
| 96 |         glFogf(GL_FOG_START, this->fogStart); | 
|---|
| 97 |         glFogf(GL_FOG_END, this->fogEnd); | 
|---|
| 98 |  | 
|---|
| 99 |         glEnable(GL_FOG); | 
|---|
| 100 |         // glClearColor(0.5, 0.5, 0.5, 1.0); | 
|---|
| 101 |         return true; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | bool FogEffect::deactivate() | 
|---|
| 106 | { | 
|---|
| 107 |         PRINTF(0)("Deactivating FogEffect\n"); | 
|---|
| 108 |  | 
|---|
| 109 |         this->fogActivate = false; | 
|---|
| 110 |  | 
|---|
| 111 |         glDisable(GL_FOG); | 
|---|
| 112 |  | 
|---|
| 113 |         return true; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void FogEffect::draw() const { | 
|---|
| 117 |  | 
|---|
| 118 |         if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) | 
|---|
| 119 |                 glFogf(GL_FOG_DENSITY, this->fogFadeDensity); | 
|---|
| 120 |         //else | 
|---|
| 121 |         //      glFogf(GL_FOG_DENSITY, this->fogDensity); | 
|---|
| 122 |  | 
|---|
| 123 | } | 
|---|
| 124 | void FogEffect::tick(float dt) | 
|---|
| 125 | { | 
|---|
| 126 |         if (!this->fogActivate) | 
|---|
| 127 |                 return; | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |         if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) { | 
|---|
| 131 |                 this->localTimer += dt; | 
|---|
| 132 |                 float progress = this->localTimer / this->fogFadeDuration; | 
|---|
| 133 |                 this->fogFadeDensity = progress * this->fogDensity; | 
|---|
| 134 |         } | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | void FogEffect::startFogging() { | 
|---|
| 138 |  | 
|---|
| 139 |         if (this->fogActivate) | 
|---|
| 140 |                 this->deactivate(); | 
|---|
| 141 |  | 
|---|
| 142 |         this->fogFadeDuration = 10; | 
|---|
| 143 |         this->localTimer = 0; | 
|---|
| 144 |         this->activate(); | 
|---|
| 145 |  | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | GLint FogEffect::stringToFogMode(const std::string& mode) | 
|---|
| 150 | { | 
|---|
| 151 |         if(mode == "GL_LINEAR") | 
|---|
| 152 |                 return GL_LINEAR; | 
|---|
| 153 |         else if(mode == "GL_EXP") | 
|---|
| 154 |                 return GL_EXP; | 
|---|
| 155 |         else if(mode == "GL_EXP2" ) | 
|---|
| 156 |                 return GL_EXP2; | 
|---|
| 157 |         else | 
|---|
| 158 |                 return -1; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|