Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8734 was 8734, checked in by amaechler, 18 years ago

branches/atmospheric_engine: skydome should get reflected

File size: 5.0 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;
[8734]73    this->fogEnd = 300;
74    this->colorVector = Vector(0.6, 0.0, 0.0);
[8316]75
[8495]76    // init variables
77    this->fogFadeInDuration = 0;
78    this->fogFadeOutDuration = 0;
79    this->fogFadeDensity = 0;
[8716]80    this->fogFadeEnd = 0;
81
[8495]82    this->localTimer = 0;
83    this->fogActivate = false;
84    this->fogFadeInActivate = false;
85    this->fogFadeOutActivate = false;
[8255]86
[6741]87}
88
89
[8495]90void FogEffect::activate() {
[8521]91    PRINTF(0)( "Activating FogEffect\n");
[6772]92
[8521]93    // init fogGL
[8495]94    GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
95    glFogi(GL_FOG_MODE, this->fogMode);
96    glFogfv(GL_FOG_COLOR, fogColor);
97    glHint(GL_FOG_HINT, GL_DONT_CARE);
98    glFogf(GL_FOG_DENSITY, this->fogDensity);
99    glFogf(GL_FOG_START, this->fogStart);
100    glFogf(GL_FOG_END, this->fogEnd);
[8316]101
[8521]102    this->fogActivate = true;
103
[8495]104    glEnable(GL_FOG);
105
[6752]106}
107
108
[8495]109void FogEffect::deactivate() {
110    PRINTF(0)("Deactivating FogEffect\n");
[8255]111
[8495]112    this->fogFadeInActivate = false;
113    this->fogFadeOutActivate = false;
114    this->fogActivate = false;
[8316]115
[8495]116    glDisable(GL_FOG);
[8316]117
[6772]118}
119
[8255]120void FogEffect::draw() const {
[6772]121
[8495]122    if (!this->fogActivate)
123        return;
[8255]124
[8495]125    // If Fog Fade
[8702]126    if ( this->fogFadeInActivate || this->fogFadeOutActivate ) {
[8716]127        if ( this->fogMode == GL_LINEAR)
128            glFogf(GL_FOG_END, this->fogFadeEnd);
129        else
130            glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
[8702]131    }
[8255]132}
133
[8495]134void FogEffect::tick(float dt) {
[8521]135
[8495]136    if (!this->fogActivate)
137        return;
[8316]138
[8495]139    if ( this->fogFadeInActivate ) {
140        this->localTimer += dt;
141
[8716]142        if ( this->fogMode == GL_LINEAR)
143          this->fogFadeEnd = 2000 * (1 - ( this->localTimer / this->fogFadeInDuration )) + this->fogEnd;
144        else
145            this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity;
146
147        if ( this->localTimer >= this->fogFadeInDuration )
[8495]148            this->fogFadeInActivate = false;
149    }
150
151    if ( this->fogFadeOutActivate ) {
152        this->localTimer += dt;
153
[8716]154        if ( this->fogMode == GL_LINEAR)
155          this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeInDuration ) + this->fogEnd;
156        else
157            this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity);
158
[8495]159        if ( this->localTimer >= this->fogFadeOutDuration )
160            this->deactivate();
161    }
[8255]162}
163
[8495]164void FogEffect::fadeInFog() {
[8255]165
[8495]166    // If Fog is already fading out, stop it
167    this->fogFadeOutActivate = false;
[8255]168
[8495]169    // If Fog is already on, turn it off first
170    if (this->fogActivate)
171        this->deactivate();
[8255]172
[8495]173    // If no manual FadeIn value was set, set a default value
174    if (!this->fogFadeInDuration > 0)
175        this->fogFadeInDuration = 20;
176
177    // Reset local timer
178    this->localTimer = 0;
179
[8716]180    // Activate Fog
181    this->activate();
182
[8495]183    // set FogFadeIn activate
184    this->fogFadeInActivate = true;
185
[8255]186}
187
188
[8495]189void FogEffect::fadeOutFog() {
190
191    // If Fog is already fading in, stop it
192    this->fogFadeInActivate = false;
193
194    // If Fog is off, turn it on first
195    if (!this->fogActivate)
196        this->activate();
197
198    // If no manual FadeOut value was set, set a default value
199    if (!this->fogFadeOutDuration > 0)
200        this->fogFadeOutDuration = 20;
201
202    // set FogFadeOut activate
203    this->fogFadeOutActivate = true;
204
205    // Reset local timer
206    this->localTimer = 0;
207
[6772]208}
209
Note: See TracBrowser for help on using the repository browser.