Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/fog_effect.cc @ 8569

Last change on this file since 8569 was 8495, checked in by bensch, 19 years ago

merged the branche atmos back. no conflicts

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;
71    this->fogDensity = 0.03;
72    this->fogStart = 0;
73    this->fogEnd = 50;
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() {
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);
[6772]90
[8495]91    this->fogActivate = true;
[8316]92
[8495]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);
[8316]100
[8495]101    glEnable(GL_FOG);
102
[6752]103}
104
105
[8495]106void FogEffect::deactivate() {
107    PRINTF(0)("Deactivating FogEffect\n");
[8255]108
[8495]109    this->fogFadeInActivate = false;
110    this->fogFadeOutActivate = false;
111    this->fogActivate = false;
[8316]112
[8495]113    glDisable(GL_FOG);
[8316]114
[6772]115}
116
[8255]117void FogEffect::draw() const {
[6772]118
[8495]119    if (!this->fogActivate)
120        return;
[8255]121
[8495]122    // If Fog Fade
123    if ( this->fogFadeInActivate || this->fogFadeOutActivate )
124        glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
125
[8255]126}
127
[8495]128void FogEffect::tick(float dt) {
129    if (!this->fogActivate)
130        return;
[8316]131
[8495]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    }
[8255]147}
148
[8495]149void FogEffect::fadeInFog() {
[8255]150
[8495]151    // If Fog is already fading out, stop it
152    this->fogFadeOutActivate = false;
[8255]153
[8495]154    // If Fog is already on, turn it off first
155    if (this->fogActivate)
156        this->deactivate();
[8255]157
[8495]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
[8255]171}
172
173
[8495]174void 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
[6772]193}
194
[7810]195
[8495]196GLint 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 TracBrowser for help on using the repository browser.