Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/fog_effect.cc @ 8704

Last change on this file since 8704 was 8704, checked in by amaechler, 18 years ago
File size: 4.9 KB
RevLine 
[6741]1/*
[8495]2  orxonox - the future of 3D-vertical-scrollers
[6741]3
[8495]4  Copyright (C) 2004 orx
[6741]5
[8495]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.
[6741]10
11### File Specific:
[8495]12  main-programmer: hdavid, amaechler
[6741]13*/
14
15#include "fog_effect.h"
16
[7193]17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
[6741]19
[8255]20#include "shell_command.h"
[6772]21
[8255]22SHELL_COMMAND(activate, FogEffect, activateFog);
23SHELL_COMMAND(deactivate, FogEffect, deactivateFog);
[8495]24SHELL_COMMAND(fadein, FogEffect, fadeInFog);
25SHELL_COMMAND(fadeout, FogEffect, fadeOutFog);
[8255]26
[8495]27// TODO: Fix fades
28
[6741]29using namespace std;
30
[6772]31CREATE_FACTORY(FogEffect, CL_FOG_EFFECT);
[6741]32
[8495]33FogEffect::FogEffect(const TiXmlElement* root) {
34    this->setClassID(CL_FOG_EFFECT, "FogEffect");
[6741]35
[8495]36    this->init();
[6772]37
[8495]38    if (root != NULL)
39        this->loadParams(root);
[7107]40
[8495]41    if (this->fogActivate)
42        this->activate();
[6741]43}
44
45
[8495]46FogEffect::~FogEffect() {
47    this->deactivate();
[6980]48}
[6741]49
50
[8495]51void FogEffect::loadParams(const TiXmlElement* root) {
52    WeatherEffect::loadParams(root);
[6741]53
[8495]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);
[8255]60
[8495]61    LOAD_PARAM_START_CYCLE(root, element);
62    {
63        LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption);
64    }
65    LOAD_PARAM_END_CYCLE(element);
[7810]66}
[6772]67
[8495]68void FogEffect::init() {
69    // default values
70    this->fogMode = GL_LINEAR;
[8700]71    this->fogDensity = 0.005;
[8495]72    this->fogStart = 0;
[8700]73    this->fogEnd = 200;
[8495]74    this->colorVector = Vector(0.3, 0.3, 0.3);
[8316]75
[8495]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;
[8255]84
[6741]85}
86
87
[8495]88void FogEffect::activate() {
[8521]89    PRINTF(0)( "Activating FogEffect\n");
[6772]90
[8521]91    // init fogGL
[8495]92    GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
93    glFogi(GL_FOG_MODE, this->fogMode);
94    glFogfv(GL_FOG_COLOR, fogColor);
95    glHint(GL_FOG_HINT, GL_DONT_CARE);
96    glFogf(GL_FOG_DENSITY, this->fogDensity);
97    glFogf(GL_FOG_START, this->fogStart);
98    glFogf(GL_FOG_END, this->fogEnd);
[8316]99
[8521]100    this->fogActivate = true;
101
[8495]102    glEnable(GL_FOG);
103
[6752]104}
105
106
[8495]107void FogEffect::deactivate() {
108    PRINTF(0)("Deactivating FogEffect\n");
[8255]109
[8495]110    this->fogFadeInActivate = false;
111    this->fogFadeOutActivate = false;
112    this->fogActivate = false;
[8316]113
[8495]114    glDisable(GL_FOG);
[8316]115
[6772]116}
117
[8255]118void FogEffect::draw() const {
[6772]119
[8495]120    if (!this->fogActivate)
121        return;
[8255]122
[8495]123    // If Fog Fade
[8702]124    if ( this->fogFadeInActivate || this->fogFadeOutActivate ) {
125      glDisable(GL_FOG);
[8703]126
[8704]127      GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
[8703]128      glFogi(GL_FOG_MODE, this->fogMode);
129      glFogfv(GL_FOG_COLOR, fogColor);
130      glHint(GL_FOG_HINT, GL_DONT_CARE);
[8702]131      glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
[8703]132      glFogf(GL_FOG_START, this->fogStart);
133      glFogf(GL_FOG_END, this->fogEnd);
134
[8702]135      glEnable(GL_FOG);
136    }
[8255]137}
138
[8495]139void FogEffect::tick(float dt) {
[8521]140
[8495]141    if (!this->fogActivate)
142        return;
[8316]143
[8495]144    if ( this->fogFadeInActivate ) {
145        this->localTimer += dt;
146        this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity;
147
148        if ( this->localTimer >= this->fogFadeOutDuration )
149            this->fogFadeInActivate = false;
150    }
151
152    if ( this->fogFadeOutActivate ) {
153        this->localTimer += dt;
154        this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity);
155
156        if ( this->localTimer >= this->fogFadeOutDuration )
157            this->deactivate();
158    }
[8255]159}
160
[8495]161void FogEffect::fadeInFog() {
[8255]162
[8495]163    // If Fog is already fading out, stop it
164    this->fogFadeOutActivate = false;
[8255]165
[8495]166    // If Fog is already on, turn it off first
167    if (this->fogActivate)
168        this->deactivate();
[8255]169
[8495]170    // If no manual FadeIn value was set, set a default value
171    if (!this->fogFadeInDuration > 0)
172        this->fogFadeInDuration = 20;
173
174    // Reset local timer
175    this->localTimer = 0;
176
177    // set FogFadeIn activate
178    this->fogFadeInActivate = true;
179
180    // Activate Fog
181    this->activate();
182
[8255]183}
184
185
[8495]186void FogEffect::fadeOutFog() {
187
188    // If Fog is already fading in, stop it
189    this->fogFadeInActivate = false;
190
191    // If Fog is off, turn it on first
192    if (!this->fogActivate)
193        this->activate();
194
195    // If no manual FadeOut value was set, set a default value
196    if (!this->fogFadeOutDuration > 0)
197        this->fogFadeOutDuration = 20;
198
199    // set FogFadeOut activate
200    this->fogFadeOutActivate = true;
201
202    // Reset local timer
203    this->localTimer = 0;
204
[6772]205}
206
Note: See TracBrowser for help on using the repository browser.