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