[6741] | 1 | /* |
---|
[7810] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[6741] | 3 | |
---|
[7810] | 4 | Copyright (C) 2004 orx |
---|
[6741] | 5 | |
---|
[7810] | 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: |
---|
[7810] | 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 | |
---|
[6772] | 20 | #include "glincl.h" |
---|
[6741] | 21 | |
---|
[8255] | 22 | #include "shell_command.h" |
---|
[6772] | 23 | |
---|
[8255] | 24 | SHELL_COMMAND(activate, FogEffect, activateFog); |
---|
| 25 | SHELL_COMMAND(deactivate, FogEffect, deactivateFog); |
---|
| 26 | SHELL_COMMAND(startfogging, FogEffect, startFogging); |
---|
| 27 | |
---|
[6741] | 28 | using namespace std; |
---|
| 29 | |
---|
[6772] | 30 | CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); |
---|
[6741] | 31 | |
---|
[7810] | 32 | FogEffect::FogEffect(const TiXmlElement* root) |
---|
[6741] | 33 | { |
---|
[7810] | 34 | this->setClassID(CL_FOG_EFFECT, "FogEffect"); |
---|
[6741] | 35 | |
---|
[7810] | 36 | this->init(); |
---|
[6772] | 37 | |
---|
[7810] | 38 | if (root != NULL) |
---|
| 39 | this->loadParams(root); |
---|
[7107] | 40 | |
---|
[8255] | 41 | if (this->fogActivate) |
---|
| 42 | this->activate(); |
---|
[6741] | 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | FogEffect::~FogEffect() |
---|
[6980] | 47 | { |
---|
[7810] | 48 | this->deactivate(); |
---|
[6980] | 49 | } |
---|
[6741] | 50 | |
---|
| 51 | |
---|
| 52 | void FogEffect::loadParams(const TiXmlElement* root) |
---|
| 53 | { |
---|
[7810] | 54 | WeatherEffect::loadParams(root); |
---|
[6741] | 55 | |
---|
[7810] | 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); |
---|
[8255] | 60 | |
---|
| 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 | |
---|
[7810] | 68 | bool FogEffect::init() |
---|
| 69 | { |
---|
| 70 | //default values |
---|
[8255] | 71 | this->fogActivate = false; |
---|
| 72 | this->fogFadeDuration = 0; |
---|
| 73 | this->localTimer = 0; |
---|
| 74 | |
---|
[7810] | 75 | this->fogMode = GL_EXP2; |
---|
| 76 | this->fogDensity = 0.001; |
---|
| 77 | this->fogStart = 0; |
---|
| 78 | this->fogEnd = 5000; |
---|
[8255] | 79 | this->colorVector = Vector(0.7, 0.7, 0.7); |
---|
| 80 | |
---|
[6741] | 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | bool FogEffect::activate() |
---|
[6752] | 85 | { |
---|
[7810] | 86 | 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] | 87 | |
---|
[8255] | 88 | this->fogActivate = true; |
---|
| 89 | |
---|
| 90 | GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; |
---|
| 91 | glFogi(GL_FOG_MODE, this->fogMode); |
---|
| 92 | glFogfv(GL_FOG_COLOR, fogColor); |
---|
| 93 | glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
| 94 | glHint(GL_FOG_HINT, GL_DONT_CARE); |
---|
| 95 | glFogf(GL_FOG_START, this->fogStart); |
---|
| 96 | glFogf(GL_FOG_END, this->fogEnd); |
---|
| 97 | |
---|
[7810] | 98 | glEnable(GL_FOG); |
---|
[8255] | 99 | // glClearColor(0.5, 0.5, 0.5, 1.0); |
---|
[6752] | 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
[6741] | 103 | bool FogEffect::deactivate() |
---|
[6772] | 104 | { |
---|
[7810] | 105 | PRINTF(0)("Deactivating FogEffect\n"); |
---|
[8255] | 106 | |
---|
| 107 | this->fogActivate = false; |
---|
| 108 | |
---|
[7810] | 109 | glDisable(GL_FOG); |
---|
[6772] | 110 | } |
---|
| 111 | |
---|
[8255] | 112 | void FogEffect::draw() const { |
---|
[6772] | 113 | |
---|
[8255] | 114 | if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) |
---|
| 115 | glFogf(GL_FOG_DENSITY, this->fogFadeDensity); |
---|
| 116 | //else |
---|
| 117 | // glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | void FogEffect::tick(float dt) |
---|
| 121 | { |
---|
| 122 | if (!this->fogActivate) |
---|
| 123 | return; |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | if (this->fogFadeDuration != 0 && this->localTimer < this->fogFadeDuration) { |
---|
| 127 | this->localTimer += dt; |
---|
| 128 | float progress = this->localTimer / this->fogFadeDuration; |
---|
| 129 | this->fogFadeDensity = progress * this->fogDensity; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void FogEffect::startFogging() { |
---|
| 134 | |
---|
| 135 | if (this->fogActivate) |
---|
| 136 | this->deactivate(); |
---|
| 137 | |
---|
| 138 | this->fogFadeDuration = 10; |
---|
| 139 | this->localTimer = 0; |
---|
| 140 | this->activate(); |
---|
| 141 | |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
[7221] | 145 | GLint FogEffect::stringToFogMode(const std::string& mode) |
---|
[6772] | 146 | { |
---|
[7810] | 147 | if(mode == "GL_LINEAR") |
---|
| 148 | return GL_LINEAR; |
---|
| 149 | else if(mode == "GL_EXP") |
---|
| 150 | return GL_EXP; |
---|
| 151 | else if(mode == "GL_EXP2" ) |
---|
| 152 | return GL_EXP2; |
---|
| 153 | else |
---|
| 154 | return -1; |
---|
[6772] | 155 | } |
---|
| 156 | |
---|
[7810] | 157 | |
---|