Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9006 in orxonox.OLD for trunk/src/lib/graphics


Ignore:
Timestamp:
Jul 2, 2006, 2:11:59 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the mountain_lake branche back to the trunk
merged with command:
svn merge -r8799:HEAD https://svn.orxonox.net/orxonox/branches/mountain_lake .

conflicts in script taken from the branche, since they are indentation-problems.

also fixed the delete-bug for the lightning-effect

Location:
trunk/src/lib/graphics
Files:
2 deleted
9 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/Makefile.am

    r8751 r9006  
    2929                effects/snow_effect.cc \
    3030                effects/cloud_effect.cc \
    31                 effects/lightening_effect.cc \
     31                effects/lightning_effect.cc \
    3232                effects/lense_flare.cc
    3333
     
    5858                effects/snow_effect.h \
    5959                effects/cloud_effect.h \
    60                 effects/lightening_effect.h \
     60                effects/lightning_effect.h \
    6161                effects/lense_flare.h
    6262
  • trunk/src/lib/graphics/effects/cloud_effect.cc

    r8793 r9006  
    11/*
    22  orxonox - the future of 3D-vertical-scrollers
    3  
     3
    44  Copyright (C) 2004 orx
    5  
     5
    66  This program is free software; you can redistribute it and/or modify
    77  it under the terms of the GNU General Public License as published by
    88  the Free Software Foundation; either version 2, or (at your option)
    99  any later version.
    10  
     10
    1111### File Specific:
    1212  main-programmer: hdavid, amaechler
    13  
     13
    1414  INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
    1515*/
     
    2626#include "shader.h"
    2727#include "shell_command.h"
     28#include "t_animation.h"
     29#include "script_class.h"
    2830
    2931#include "parser/tinyxml/tinyxml.h"
    3032
    31 Vector  CloudEffect::cloudColor;
    32 Vector  CloudEffect::skyColor;
    33 Vector  CloudEffect::newCloudColor;
    34 Vector  CloudEffect::newSkyColor;
     33Vector    CloudEffect::cloudColor;
     34Vector    CloudEffect::skyColor;
     35Vector    CloudEffect::newCloudColor;
     36Vector    CloudEffect::newSkyColor;
     37
     38bool      CloudEffect::fadeSky;
     39bool      CloudEffect::fadeCloud;
     40float     CloudEffect::fadeTime;
     41bool      CloudEffect::flashSkyActivate;
     42float     CloudEffect::localTimer;
     43float     CloudEffect::flashTime;
    3544
    3645using namespace std;
     
    3847SHELL_COMMAND(activate, CloudEffect, activateCloud);
    3948SHELL_COMMAND(deactivate, CloudEffect, deactivateCloud);
     49SHELL_COMMAND(skyColor, CloudEffect, shellSkyColor);
     50SHELL_COMMAND(cloudColor, CloudEffect, shellCloudColor);
     51
     52
     53CREATE_SCRIPTABLE_CLASS(CloudEffect, CL_CLOUD_EFFECT,
     54                        addMethod("skyColor", ExecutorLua4<CloudEffect,float,float,float,float>(&CloudEffect::shellSkyColor))
     55                      ->addMethod("cloudColor", ExecutorLua4<CloudEffect,float,float,float,float>(&CloudEffect::shellCloudColor))
     56                       );
     57
    4058
    4159CREATE_FACTORY(CloudEffect, CL_CLOUD_EFFECT);
    4260
    43 CloudEffect::CloudEffect(const TiXmlElement* root)
    44 {
    45   this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
    46 
    47   this->init();
    48 
    49   if (root != NULL)
    50     this->loadParams(root);
    51 
    52   if(cloudActivate)
    53     this->activate();
    54 }
    55 
    56 CloudEffect::~CloudEffect()
    57 {
    58   this->deactivate();
    59 
    60   if (glIsTexture(noise3DTexName))
    61     glDeleteTextures(1, &noise3DTexName);
    62 
    63   delete shader;
    64 }
    65 
    66 
    67 void CloudEffect::init()
    68 {
    69   PRINTF(0)("Initializing CloudEffect\n");
    70 
    71   this->offsetZ = 0;
    72   this->animationSpeed = 2;
    73   this->scale = 0.0004f;
    74   this->atmosphericRadius = 4000;
    75   this->planetRadius = 1500;
    76   this->divs = 50;
    77 
    78   skyColor = Vector(0.0f, 0.0f, 0.8f);
    79   cloudColor = Vector(0.8f, 0.8f, 0.8f);
    80   newSkyColor = skyColor;
    81   newCloudColor = cloudColor;
    82   this->fadeTime = 3;
    83 
    84   this->noise3DTexSize = 128;
    85   this->noise3DTexName = 0;
    86 
    87   this->make3DNoiseTexture();
    88 
    89   glGenTextures(1, &noise3DTexName);
    90   glBindTexture(GL_TEXTURE_3D, noise3DTexName);
    91 
    92   glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    93   glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    94   glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
    95   glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    96   glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    97 
    98   glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
    99                noise3DTexSize, noise3DTexSize, noise3DTexSize,
    100                0, GL_RGBA, GL_UNSIGNED_BYTE, noise3DTexPtr);
    101 
    102   this->skydome = new Skydome();
    103   this->skydome->setTexture(noise3DTexName);
    104 
    105   this->shader = new Shader(ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.vert",
    106                             ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.frag");
    107 
    108   this->shader->activateShader();
    109 
    110   Shader::Uniform(shader, "Noise").set(0);
    111 
    112   this->offset = new Shader::Uniform(shader, "Offset");
    113   this->skycolor = new Shader::Uniform(shader, "SkyColor");
    114   this->cloudcolor = new Shader::Uniform(shader, "CloudColor");
    115 
    116   this->shader->deactivateShader();
    117 
    118   this->skydome->setShader(shader);
    119 }
    120 
    121 
    122 void CloudEffect::loadParams(const TiXmlElement* root)
    123 {
    124   WeatherEffect::loadParams(root);
    125 
    126   LoadParam(root, "speed", this, CloudEffect, setAnimationSpeed);
    127   LoadParam(root, "scale", this, CloudEffect, setCloudScale);
    128   LoadParam(root, "cloudcolor", this, CloudEffect, setCloudColor);
    129   LoadParam(root, "skycolor", this, CloudEffect, setSkyColor);
    130 
    131   LoadParam(root, "planetRadius", this, CloudEffect, setPlanetRadius);
    132   LoadParam(root, "atmosphericRadius", this, CloudEffect, setAtmosphericRadius);
    133   LoadParam(root, "divisions", this, CloudEffect, setDivisions);
    134 
    135   LOAD_PARAM_START_CYCLE(root, element);
    136   {
    137     LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
    138   }
    139   LOAD_PARAM_END_CYCLE(element);
    140 }
    141 
    142 
    143 void CloudEffect::activate()
    144 {
    145   PRINTF(0)( "Activating\n");
    146 
    147   // Can only be set after the loadParams call
    148   this->shader->activateShader();
    149   Shader::Uniform(shader, "Scale").set(this->scale);
    150   this->skycolor->set(skyColor.x, skyColor.y, skyColor.z);
    151   this->cloudcolor->set(cloudColor.x, cloudColor.y, cloudColor.z);
    152   this->shader->deactivateShader();
    153 
    154   this->skydome->generateSkyPlane(this->divs, this->planetRadius, this->atmosphericRadius, 1, 1);
    155 
    156   this->cloudActivate = true;
    157 }
    158 
    159 void CloudEffect::deactivate()
    160 {
    161   PRINTF(0)("Deactivating CloudEffect\n");
    162 
    163   this->cloudActivate = false;
    164 }
    165 
    166 void CloudEffect::draw() const
    167   {}
    168 
    169 void CloudEffect::tick (float dt)
    170 {
    171   if (this->cloudActivate)
    172   {
    173     this->offsetZ += 0.05 * dt * this->animationSpeed;
     61CloudEffect::CloudEffect(const TiXmlElement* root) {
     62    this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
     63
     64    this->init();
     65
     66    if (root != NULL)
     67        this->loadParams(root);
     68
     69    if(cloudActivate)
     70        this->activate();
     71}
     72
     73CloudEffect::~CloudEffect() {
     74    this->deactivate();
     75
     76    if (glIsTexture(noise3DTexName))
     77        glDeleteTextures(1, &noise3DTexName);
     78
     79    delete shader;
     80}
     81
     82
     83void CloudEffect::init() {
     84    PRINTF(0)("Initializing CloudEffect\n");
     85
     86    // default values
     87    this->offsetZ = 0;
     88    this->animationSpeed = 2;
     89    this->scale = 0.0004f;
     90    this->atmosphericRadius = 4000;
     91    this->planetRadius = 1500;
     92    this->divs = 15;
     93    fadeSky = false;
     94    fadeCloud = false;
     95
     96    flashSkyActivate = false;
     97    localTimer = 0;
     98    flashTime = 0;
     99
     100    skyColor = Vector(0.0f, 0.0f, 0.8f);
     101    cloudColor = Vector(0.8f, 0.8f, 0.8f);
     102    newSkyColor = skyColor;
     103    newCloudColor = cloudColor;
     104
     105    this->noise3DTexSize = 128;
     106    this->noise3DTexName = 0;
     107
     108    this->make3DNoiseTexture();
     109
     110    glGenTextures(1, &noise3DTexName);
     111    glBindTexture(GL_TEXTURE_3D, noise3DTexName);
     112
     113    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     114    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     115    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
     116    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     117    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     118
     119    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
     120                 noise3DTexSize, noise3DTexSize, noise3DTexSize,
     121                 0, GL_RGBA, GL_UNSIGNED_BYTE, noise3DTexPtr);
     122
     123    this->skydome = new Skydome();
     124    this->skydome->setTexture(noise3DTexName);
     125
     126    this->shader = new Shader(ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.vert",
     127                              ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.frag");
    174128
    175129    this->shader->activateShader();
    176     this->offset->set(0.0f, 0.0f, offsetZ);
    177    
    178     //if(cloudColor != newCloudColor)
    179     //{
    180       // TODO: fade from cloudColor to newCloudColor
    181       cloudColor = newCloudColor;
    182       this->cloudcolor->set(this->cloudColor.x, this->cloudColor.y, this->cloudColor.z);
    183     //}
    184     //if(cloudColor != newCloudColor)
    185     //{
    186       // TODO: fade from skyColor to newSkyColor
    187       skyColor = newSkyColor;
    188       this->skycolor->set(this->skyColor.x, this->skyColor.y, this->skyColor.z);
    189     //}
    190    
     130
     131    Shader::Uniform(shader, "Noise").set(0);
     132
     133    this->offset = new Shader::Uniform(shader, "Offset");
     134    this->skycolor = new Shader::Uniform(shader, "SkyColor");
     135    this->cloudcolor = new Shader::Uniform(shader, "CloudColor");
     136
    191137    this->shader->deactivateShader();
    192   }
    193 }
    194 
    195 
    196 void CloudEffect::changeSkyColor(Vector color)
    197 {
    198   newSkyColor = color;
    199 }
    200 
    201 
    202 void CloudEffect::changeCloudColor(Vector color)
    203 {
    204   newCloudColor = color;
    205 }
    206 
    207 
    208 void CloudEffect::make3DNoiseTexture()
    209 {
    210   int f, i, j, k, inc;
    211   int startFrequency = 4;
    212   int numOctaves = 4;
    213   double ni[3];
    214   double inci, incj, inck;
    215   int frequency = startFrequency;
    216   GLubyte *ptr;
    217   double amp = 0.5;
    218 
    219   if ((noise3DTexPtr = (GLubyte *) malloc(noise3DTexSize *
    220                                           noise3DTexSize *
    221                                           noise3DTexSize * 4)) == NULL)
    222     PRINTF(0)("ERROR: Could not allocate 3D noise texture\n");
    223 
    224   for (f=0, inc=0; f < numOctaves;
    225        ++f, frequency *= 2, ++inc, amp *= 0.5)
    226   {
    227     SetNoiseFrequency(frequency);
    228     ptr = noise3DTexPtr;
    229     ni[0] = ni[1] = ni[2] = 0;
    230 
    231     inci = 1.0 / (noise3DTexSize / frequency);
    232     for (i=0; i<noise3DTexSize; ++i, ni[0] += inci)
     138
     139    this->skydome->setShader(shader);
     140}
     141
     142
     143void CloudEffect::loadParams(const TiXmlElement* root) {
     144    WeatherEffect::loadParams(root);
     145
     146    LoadParam(root, "speed", this, CloudEffect, setAnimationSpeed);
     147    LoadParam(root, "scale", this, CloudEffect, setCloudScale);
     148    LoadParam(root, "cloudcolor", this, CloudEffect, setCloudColor);
     149    LoadParam(root, "skycolor", this, CloudEffect, setSkyColor);
     150
     151    LoadParam(root, "planetRadius", this, CloudEffect, setPlanetRadius);
     152    LoadParam(root, "atmosphericRadius", this, CloudEffect, setAtmosphericRadius);
     153    LoadParam(root, "divisions", this, CloudEffect, setDivisions);
     154
     155    LOAD_PARAM_START_CYCLE(root, element);
    233156    {
    234       incj = 1.0 / (noise3DTexSize / frequency);
    235       for (j=0; j<noise3DTexSize; ++j, ni[1] += incj)
    236       {
    237         inck = 1.0 / (noise3DTexSize / frequency);
    238         for (k=0; k<noise3DTexSize; ++k, ni[2] += inck, ptr+= 4)
    239         {
    240           *(ptr+inc) = (GLubyte) (((noise3(ni)+1.0) * amp)*128.0);
     157        LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
     158    }
     159    LOAD_PARAM_END_CYCLE(element);
     160}
     161
     162
     163void CloudEffect::activate() {
     164    PRINTF(0)( "Activating\n");
     165
     166    // Can only be set after the loadParams call
     167    this->shader->activateShader();
     168    Shader::Uniform(shader, "Scale").set(this->scale);
     169    this->skycolor->set
     170    (skyColor.x, skyColor.y, skyColor.z);
     171    this->cloudcolor->set
     172    (cloudColor.x, cloudColor.y, cloudColor.z);
     173    this->shader->deactivateShader();
     174
     175    this->skydome->generateSkyPlane(this->divs, this->planetRadius, this->atmosphericRadius, 1, 1);
     176    this->skydome->activate();
     177
     178    this->cloudActivate = true;
     179}
     180
     181void CloudEffect::deactivate() {
     182    PRINTF(0)("Deactivating CloudEffect\n");
     183
     184    this->skydome->deactivate();
     185    this->cloudActivate = false;
     186}
     187
     188void CloudEffect::draw() const {}
     189
     190void CloudEffect::tick (float dt) {
     191
     192    if (this->cloudActivate) {
     193
     194        localTimer += dt;
     195
     196        this->offsetZ += 0.05 * dt * this->animationSpeed;
     197
     198        this->shader->activateShader();
     199        this->offset->set(0.0f, 0.0f, offsetZ);
     200
     201        if (flashSkyActivate) {
     202
     203            this->skycolor->set(1, 1, 1);
     204            PRINTF(0)("SkyColor set to white\n");
     205
     206            if (localTimer > flashTime) {
     207                flashSkyActivate = false;
     208                this->skycolor->set(this->skyColor.x, this->skyColor.y, this->skyColor.z);
     209                PRINTF(0)("SkyColor reset\n");
     210            }
     211
     212        } else {
     213
     214            if(cloudColor != newCloudColor) {
     215
     216                if(fadeCloud) {
     217
     218                    this->cloudColorFadeX = new tAnimation<CloudEffect>(this, &CloudEffect::setColorCloudX);
     219                    this->cloudColorFadeX->setInfinity(ANIM_INF_CONSTANT);
     220                    this->cloudColorFadeY = new tAnimation<CloudEffect>(this, &CloudEffect::setColorCloudY);
     221                    this->cloudColorFadeY->setInfinity(ANIM_INF_CONSTANT);
     222                    this->cloudColorFadeZ = new tAnimation<CloudEffect>(this, &CloudEffect::setColorCloudZ);
     223                    this->cloudColorFadeZ->setInfinity(ANIM_INF_CONSTANT);
     224
     225                    this->cloudColorFadeX->addKeyFrame(cloudColor.x, fadeTime, ANIM_LINEAR);
     226                    this->cloudColorFadeX->addKeyFrame(newCloudColor.x, 0, ANIM_LINEAR);
     227
     228                    this->cloudColorFadeY->addKeyFrame(cloudColor.y, fadeTime, ANIM_LINEAR);
     229                    this->cloudColorFadeY->addKeyFrame(newCloudColor.y, 0, ANIM_LINEAR);
     230
     231                    this->cloudColorFadeZ->addKeyFrame(cloudColor.z, fadeTime, ANIM_LINEAR);
     232                    this->cloudColorFadeZ->addKeyFrame(newCloudColor.z, 0, ANIM_LINEAR);
     233
     234                    fadeCloud = false;
     235
     236                    this->cloudColorFadeX->replay();
     237                    this->cloudColorFadeY->replay();
     238                    this->cloudColorFadeZ->replay();
     239                }
     240
     241                this->cloudcolor->set(this->cloudColor.x, this->cloudColor.y, this->cloudColor.z);
     242            }
     243
     244            if(skyColor != newSkyColor) {
     245
     246                if(fadeSky) {
     247
     248                    this->skyColorFadeX = new tAnimation<CloudEffect>(this, &CloudEffect::setColorSkyX);
     249                    this->skyColorFadeX->setInfinity(ANIM_INF_CONSTANT);
     250                    this->skyColorFadeY = new tAnimation<CloudEffect>(this, &CloudEffect::setColorSkyY);
     251                    this->skyColorFadeY->setInfinity(ANIM_INF_CONSTANT);
     252                    this->skyColorFadeZ = new tAnimation<CloudEffect>(this, &CloudEffect::setColorSkyZ);
     253                    this->skyColorFadeZ->setInfinity(ANIM_INF_CONSTANT);
     254
     255                    this->skyColorFadeX->addKeyFrame(skyColor.x, fadeTime, ANIM_LINEAR);
     256                    this->skyColorFadeX->addKeyFrame(newSkyColor.x, 0, ANIM_LINEAR);
     257
     258                    this->skyColorFadeY->addKeyFrame(skyColor.y, fadeTime, ANIM_LINEAR);
     259                    this->skyColorFadeY->addKeyFrame(newSkyColor.y, 0, ANIM_LINEAR);
     260
     261                    this->skyColorFadeZ->addKeyFrame(skyColor.z, fadeTime, ANIM_LINEAR);
     262                    this->skyColorFadeZ->addKeyFrame(newSkyColor.z, 0, ANIM_LINEAR);
     263
     264                    fadeSky = false;
     265
     266                    this->skyColorFadeX->replay();
     267                    this->skyColorFadeY->replay();
     268                    this->skyColorFadeZ->replay();
     269                }
     270
     271                this->skycolor->set(this->skyColor.x, this->skyColor.y, this->skyColor.z);
     272            }
    241273        }
    242       }
    243     }
    244   }
    245 }
    246 
    247 void CloudEffect::initNoise()
    248 {
    249   int i, j, k;
    250 
    251   srand(30757);
    252   for (i = 0 ; i < B ; i++)
    253   {
    254     p[i] = i;
    255     g1[i] = (double)((rand() % (B + B)) - B) / B;
    256 
    257     for (j = 0 ; j < 2 ; j++)
    258       g2[i][j] = (double)((rand() % (B + B)) - B) / B;
    259     normalize2(g2[i]);
    260 
    261     for (j = 0 ; j < 3 ; j++)
    262       g3[i][j] = (double)((rand() % (B + B)) - B) / B;
    263     normalize3(g3[i]);
    264   }
    265 
    266   while (--i)
    267   {
    268     k = p[i];
    269     p[i] = p[j = rand() % B];
    270     p[j] = k;
    271   }
    272 
    273   for (i = 0 ; i < B + 2 ; i++)
    274   {
    275     p[B + i] = p[i];
    276     g1[B + i] = g1[i];
    277     for (j = 0 ; j < 2 ; j++)
    278       g2[B + i][j] = g2[i][j];
    279     for (j = 0 ; j < 3 ; j++)
    280       g3[B + i][j] = g3[i][j];
    281   }
    282 }
    283 
    284 void CloudEffect::SetNoiseFrequency( int frequency)
    285 {
    286   start = 1;
    287   B = frequency;
    288   BM = B-1;
    289 }
    290 
    291 double CloudEffect::noise3( double vec[3])
    292 {
    293   int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
    294   double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
    295   int i, j;
    296 
    297   if (start)
    298   {
    299     start = 0;
    300     initNoise();
    301   }
    302 
    303   setup(0, bx0,bx1, rx0,rx1);
    304   setup(1, by0,by1, ry0,ry1);
    305   setup(2, bz0,bz1, rz0,rz1);
    306 
    307   i = p[ bx0 ];
    308   j = p[ bx1 ];
    309 
    310   b00 = p[ i + by0 ];
    311   b10 = p[ j + by0 ];
    312   b01 = p[ i + by1 ];
    313   b11 = p[ j + by1 ];
    314 
    315   t  = s_curve(rx0);
    316   sy = s_curve(ry0);
    317   sz = s_curve(rz0);
    318 
    319   q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
    320   q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
    321   a = lerp(t, u, v);
    322 
    323   q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
    324   q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
    325   b = lerp(t, u, v);
    326 
    327   c = lerp(sy, a, b);
    328 
    329   q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
    330   q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
    331   a = lerp(t, u, v);
    332 
    333   q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
    334   q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
    335   b = lerp(t, u, v);
    336 
    337   d = lerp(sy, a, b);
    338 
    339   return lerp(sz, c, d);
    340 }
    341 
    342 void CloudEffect::normalize2( double v[2])
    343 {
    344   double s;
    345 
    346   s = sqrt(v[0] * v[0] + v[1] * v[1]);
    347   v[0] = v[0] / s;
    348   v[1] = v[1] / s;
    349 }
    350 
    351 void CloudEffect::normalize3( double v[3])
    352 {
    353   double s;
    354 
    355   s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
    356   v[0] = v[0] / s;
    357   v[1] = v[1] / s;
    358   v[2] = v[2] / s;
    359 }
    360 
     274
     275        this->shader->deactivateShader();
     276    }
     277}
     278
     279
     280void CloudEffect::setColorSkyX(float color) {
     281    skyColor.x = color;
     282}
     283void CloudEffect::setColorSkyY(float color) {
     284    skyColor.y = color;
     285}
     286void CloudEffect::setColorSkyZ(float color) {
     287    skyColor.z = color;
     288}
     289void CloudEffect::setColorCloudX(float color) {
     290    cloudColor.x = color;
     291}
     292void CloudEffect::setColorCloudY(float color) {
     293    cloudColor.y = color;
     294}
     295void CloudEffect::setColorCloudZ(float color) {
     296    cloudColor.z = color;
     297}
     298
     299void CloudEffect::changeSkyColor(Vector color, float time) {
     300    newSkyColor = color;
     301    fadeSky = true;
     302    fadeTime = time;
     303}
     304
     305
     306void CloudEffect::changeCloudColor(Vector color, float time) {
     307    newCloudColor = color;
     308    fadeCloud = true;
     309    fadeTime = time;
     310}
     311
     312void CloudEffect::shellSkyColor(float colorX, float colorY, float colorZ, float time) {
     313    changeSkyColor( Vector(colorX, colorY, colorZ), time);
     314}
     315
     316void CloudEffect::shellCloudColor(float colorX, float colorY, float colorZ, float time) {
     317    changeCloudColor( Vector(colorX, colorY, colorZ), time);
     318}
     319
     320void CloudEffect::flashSky( float time ) {
     321
     322  flashSkyActivate = true;
     323  localTimer = 0;
     324  flashTime = time;
     325
     326}
     327
     328
     329
     330void CloudEffect::make3DNoiseTexture() {
     331    int f, i, j, k, inc;
     332    int startFrequency = 4;
     333    int numOctaves = 4;
     334    double ni[3];
     335    double inci, incj, inck;
     336    int frequency = startFrequency;
     337    GLubyte *ptr;
     338    double amp = 0.5;
     339
     340    if ((noise3DTexPtr = (GLubyte *) malloc(noise3DTexSize *
     341                                            noise3DTexSize *
     342                                            noise3DTexSize * 4)) == NULL)
     343        PRINTF(0)("ERROR: Could not allocate 3D noise texture\n");
     344
     345    for (f=0, inc=0; f < numOctaves;
     346            ++f, frequency *= 2, ++inc, amp *= 0.5) {
     347        SetNoiseFrequency(frequency);
     348        ptr = noise3DTexPtr;
     349        ni[0] = ni[1] = ni[2] = 0;
     350
     351        inci = 1.0 / (noise3DTexSize / frequency);
     352        for (i=0; i<noise3DTexSize; ++i, ni[0] += inci) {
     353            incj = 1.0 / (noise3DTexSize / frequency);
     354            for (j=0; j<noise3DTexSize; ++j, ni[1] += incj) {
     355                inck = 1.0 / (noise3DTexSize / frequency);
     356                for (k=0; k<noise3DTexSize; ++k, ni[2] += inck, ptr+= 4) {
     357                    *(ptr+inc) = (GLubyte) (((noise3(ni)+1.0) * amp)*128.0);
     358                }
     359            }
     360        }
     361    }
     362}
     363
     364void CloudEffect::initNoise() {
     365    int i, j, k;
     366
     367    srand(30757);
     368    for (i = 0 ; i < B ; i++) {
     369        p[i] = i;
     370        g1[i] = (double)((rand() % (B + B)) - B) / B;
     371
     372        for (j = 0 ; j < 2 ; j++)
     373            g2[i][j] = (double)((rand() % (B + B)) - B) / B;
     374        normalize2(g2[i]);
     375
     376        for (j = 0 ; j < 3 ; j++)
     377            g3[i][j] = (double)((rand() % (B + B)) - B) / B;
     378        normalize3(g3[i]);
     379    }
     380
     381    while (--i) {
     382        k = p[i];
     383        p[i] = p[j = rand() % B];
     384        p[j] = k;
     385    }
     386
     387    for (i = 0 ; i < B + 2 ; i++) {
     388        p[B + i] = p[i];
     389        g1[B + i] = g1[i];
     390        for (j = 0 ; j < 2 ; j++)
     391            g2[B + i][j] = g2[i][j];
     392        for (j = 0 ; j < 3 ; j++)
     393            g3[B + i][j] = g3[i][j];
     394    }
     395}
     396
     397void CloudEffect::SetNoiseFrequency( int frequency) {
     398    start = 1;
     399    B = frequency;
     400    BM = B-1;
     401}
     402
     403double CloudEffect::noise3( double vec[3]) {
     404    int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
     405    double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
     406    int i, j;
     407
     408    if (start) {
     409        start = 0;
     410        initNoise();
     411    }
     412
     413    setup(0, bx0,bx1, rx0,rx1);
     414    setup(1, by0,by1, ry0,ry1);
     415    setup(2, bz0,bz1, rz0,rz1);
     416
     417    i = p[ bx0 ];
     418    j = p[ bx1 ];
     419
     420    b00 = p[ i + by0 ];
     421    b10 = p[ j + by0 ];
     422    b01 = p[ i + by1 ];
     423    b11 = p[ j + by1 ];
     424
     425    t  = s_curve(rx0);
     426    sy = s_curve(ry0);
     427    sz = s_curve(rz0);
     428
     429    q = g3[ b00 + bz0 ] ;
     430    u = at3(rx0,ry0,rz0);
     431    q = g3[ b10 + bz0 ] ;
     432    v = at3(rx1,ry0,rz0);
     433    a = lerp(t, u, v);
     434
     435    q = g3[ b01 + bz0 ] ;
     436    u = at3(rx0,ry1,rz0);
     437    q = g3[ b11 + bz0 ] ;
     438    v = at3(rx1,ry1,rz0);
     439    b = lerp(t, u, v);
     440
     441    c = lerp(sy, a, b);
     442
     443    q = g3[ b00 + bz1 ] ;
     444    u = at3(rx0,ry0,rz1);
     445    q = g3[ b10 + bz1 ] ;
     446    v = at3(rx1,ry0,rz1);
     447    a = lerp(t, u, v);
     448
     449    q = g3[ b01 + bz1 ] ;
     450    u = at3(rx0,ry1,rz1);
     451    q = g3[ b11 + bz1 ] ;
     452    v = at3(rx1,ry1,rz1);
     453    b = lerp(t, u, v);
     454
     455    d = lerp(sy, a, b);
     456
     457    return lerp(sz, c, d);
     458}
     459
     460void CloudEffect::normalize2( double v[2]) {
     461    double s;
     462
     463    s = sqrt(v[0] * v[0] + v[1] * v[1]);
     464    v[0] = v[0] / s;
     465    v[1] = v[1] / s;
     466}
     467
     468void CloudEffect::normalize3( double v[3]) {
     469    double s;
     470
     471    s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
     472    v[0] = v[0] / s;
     473    v[1] = v[1] / s;
     474    v[2] = v[2] / s;
     475}
     476
  • trunk/src/lib/graphics/effects/cloud_effect.h

    r8793 r9006  
    11/**
    22 * @file cloud_effect.h
    3  * Creates clouds
     3 * Create clouds
    44*/
    55
     
    3232#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
    3333
     34// FORWARD DECLARATION
     35template <class T>
     36class tAnimation;
    3437
    35 class CloudEffect : public WeatherEffect
    36 {
     38class CloudEffect : public WeatherEffect {
     39
    3740public:
    38   CloudEffect(const TiXmlElement* root = NULL);
    39   virtual ~CloudEffect();
    4041
    41   virtual void loadParams(const TiXmlElement* root);
     42    CloudEffect(const TiXmlElement* root = NULL);
     43    virtual ~CloudEffect();
    4244
    43   virtual void init();
     45    virtual void loadParams(const TiXmlElement* root);
    4446
    45   virtual void activate();
    46   virtual void deactivate();
     47    virtual void init();
    4748
    48   inline void activateCloud()
    49   { this->activate(); }
     49    virtual void activate();
     50    virtual void deactivate();
    5051
    51   inline void deactivateCloud()
    52   { this->deactivate(); }
     52    void activateCloud() {
     53        this->activate();
     54    }
     55    void deactivateCloud() {
     56        this->deactivate();
     57    }
    5358
    54   inline void setCloudOption(const std::string& option)
    55   {
    56     if (option == "activate")
    57       this->cloudActivate = true;
    58   }
     59    void setCloudOption(const std::string& option) {
     60        if (option == "activate")
     61            this->cloudActivate = true;
     62    }
    5963
    60   inline void setAnimationSpeed(float speed)
    61 { this->animationSpeed = speed; }
     64    void setAnimationSpeed(float speed) {
     65        this->animationSpeed = speed;
     66    }
    6267
    63   inline void setCloudScale(float scale)
    64   { this->scale = scale; }
     68    void setCloudScale(float scale) {
     69        this->scale = scale;
     70    }
    6571
    66   inline void setCloudColor(float colorX, float colorY, float colorZ)
    67   { this->cloudColor = Vector(colorX, colorY, colorZ); }
     72    void setCloudColor(float colorX, float colorY, float colorZ) {
     73        this->cloudColor = Vector(colorX, colorY, colorZ);
     74    }
    6875
    69   inline void setSkyColor(float colorX, float colorY, float colorZ)
    70   { this->skyColor = Vector(colorX, colorY, colorZ); }
     76    void setSkyColor(float colorX, float colorY, float colorZ) {
     77        this->skyColor = Vector(colorX, colorY, colorZ);
     78    }
    7179
    72   inline void setPlanetRadius(float planetRadius)
    73   { this->planetRadius = planetRadius; }
     80    void setPlanetRadius(float planetRadius) {
     81        this->planetRadius = planetRadius;
     82    }
    7483
    75   inline void setAtmosphericRadius(float atmosphericRadius)
    76   { this->atmosphericRadius = atmosphericRadius; }
     84    void setAtmosphericRadius(float atmosphericRadius) {
     85        this->atmosphericRadius = atmosphericRadius;
     86    }
    7787
    78   inline void setDivisions(int divs)
    79   { this->divs = divs; }
     88    void setDivisions(int divs) {
     89        this->divs = divs;
     90    }
    8091
    81   virtual void draw() const;
    82   virtual void tick(float dt);
     92    virtual void draw() const;
     93    virtual void tick(float dt);
    8394
    84   static void changeSkyColor(Vector color);
    85   static void changeCloudColor(Vector color);
    86  
    87   static Vector    cloudColor;
    88   static Vector    skyColor;
     95    static void changeSkyColor(Vector color, float time);
     96    static void changeCloudColor(Vector color, float time);
    8997
    90   void make3DNoiseTexture();
    91   void initNoise();
    92   void SetNoiseFrequency(int frequency);
    93   double noise3(double vec[3]);
    94   void normalize2(double v[2]);
    95   void normalize3(double v[3]);
     98    void setColorSkyX(float color);
     99    void setColorSkyY(float color);
     100    void setColorSkyZ(float color);
     101    void setColorCloudX(float color);
     102    void setColorCloudY(float color);
     103    void setColorCloudZ(float color);
    96104
    97   void generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius,
    98                         float hTile, float vTile);
     105    static Vector cloudColor;
     106    static Vector skyColor;
     107
     108    void make3DNoiseTexture();
     109    void initNoise();
     110    void SetNoiseFrequency(int frequency);
     111    double noise3(double vec[3]);
     112    void normalize2(double v[2]);
     113    void normalize3(double v[3]);
     114
     115    void generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius,
     116                          float hTile, float vTile);
     117
     118    void shellSkyColor(float colorX, float colorY, float colorZ, float time);
     119    void shellCloudColor(float colorX, float colorY, float colorZ, float time);
     120
     121    static void flashSky(float time);
     122
    99123private:
    100124
    101   bool             cloudActivate;
    102   float            animationSpeed;
    103   float            fadeTime;
    104  
    105   static Vector           newSkyColor;
    106   static Vector           newCloudColor;
     125    bool                    cloudActivate;
     126    float                   animationSpeed;
    107127
    108   // Material                 cloudMaterial;
    109   Skydome*         skydome;
     128    static Vector           newSkyColor;
     129    static Vector           newCloudColor;
    110130
    111   // SHADER STUFF
    112   Shader*          shader;
    113   Shader::Uniform* offset;
    114   Shader::Uniform* skycolor;
    115   Shader::Uniform* cloudcolor;
    116   float            offsetZ;
    117   float            scale;
    118   float            planetRadius;
    119   float            atmosphericRadius;
    120   int              divs;
     131    // Material             cloudMaterial;
     132    Skydome*                 skydome;
    121133
    122   // NOISE STUFF
    123   int              noise3DTexSize;
    124   GLuint           noise3DTexName;
    125   GLubyte          *noise3DTexPtr;
     134    tAnimation<CloudEffect>*  skyColorFadeX;
     135    tAnimation<CloudEffect>*  skyColorFadeY;
     136    tAnimation<CloudEffect>*  skyColorFadeZ;
     137    tAnimation<CloudEffect>*  cloudColorFadeX;
     138    tAnimation<CloudEffect>*  cloudColorFadeY;
     139    tAnimation<CloudEffect>*  cloudColorFadeZ;
     140    static bool fadeSky;
     141    static bool fadeCloud;
     142    static float fadeTime;
    126143
    127   int              p[MAXB + MAXB + 2];
    128   double           g3[MAXB + MAXB + 2][3];
    129   double           g2[MAXB + MAXB + 2][2];
    130   double           g1[MAXB + MAXB + 2];
     144    // SHADER STUFF
     145    Shader*          shader;
     146    Shader::Uniform* offset;
     147    Shader::Uniform* skycolor;
     148    Shader::Uniform* cloudcolor;
     149    float            offsetZ;
     150    float            scale;
     151    float            planetRadius;
     152    float            atmosphericRadius;
     153    int              divs;
    131154
    132   int              start;
    133   int              B;
    134   int              BM;
     155    // NOISE STUFF
     156    int              noise3DTexSize;
     157    GLuint           noise3DTexName;
     158    GLubyte          *noise3DTexPtr;
     159
     160    int              p[MAXB + MAXB + 2];
     161    double           g3[MAXB + MAXB + 2][3];
     162    double           g2[MAXB + MAXB + 2][2];
     163    double           g1[MAXB + MAXB + 2];
     164
     165    int              start;
     166    int              B;
     167    int              BM;
     168
     169    static bool      flashSkyActivate;
     170    static float     localTimer;
     171    static float     flashTime;
     172
    135173};
    136174
  • trunk/src/lib/graphics/effects/fog_effect.cc

    r8793 r9006  
    103103 */
    104104void FogEffect::activate() {
    105     PRINTF(0)( "Activating FogEffect\n");
     105    PRINTF(3)( "Activating FogEffect\n");
    106106
    107107    // init fogGL
     
    123123 */
    124124void FogEffect::deactivate() {
    125     PRINTF(0)("Deactivating FogEffect\n");
     125    PRINTF(3)("Deactivating FogEffect\n");
    126126
    127127    this->fogFadeInActivate = false;
  • trunk/src/lib/graphics/effects/lense_flare.cc

    r8619 r9006  
    6464
    6565    this->lightSource = (LightManager::getInstance())->getLight(0);
    66     PRINTF(0)("light is: %p\n", this->lightSource);
     66    PRINTF(4)("light is: %p\n", this->lightSource);
    6767
    6868    if (root != NULL) {
  • trunk/src/lib/graphics/effects/rain_effect.cc

    r8793 r9006  
    11/*
    22  orxonox - the future of 3D-vertical-scrollers
    3  
     3
    44  Copyright (C) 2004 orx
    5  
     5
    66  This program is free software; you can redistribute it and/or modify
    77  it under the terms of the GNU General Public License as published by
    88  the Free Software Foundation; either version 2, or (at your option)
    99  any later version.
    10  
     10
    1111### File Specific:
    1212  main-programmer: hdavid, amaechler
    1313*/
     14
     15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
    1416
    1517#include "rain_effect.h"
     
    4345 * @brief standard constructor
    4446 */
    45 RainEffect::RainEffect(const TiXmlElement* root)
    46 {
    47   this->setClassID(CL_RAIN_EFFECT, "RainEffect");
    48 
    49   this->init();
    50 
    51   if (root != NULL)
    52     this->loadParams(root);
    53 
    54   //load rain sound
    55   if (this->rainBuffer != NULL)
    56     ResourceManager::getInstance()->unload(this->rainBuffer);
    57   this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
    58 
    59   //load wind sound
    60   if (this->rainWindForce != 0)
    61   {
     47RainEffect::RainEffect(const TiXmlElement* root) {
     48    this->setClassID(CL_RAIN_EFFECT, "RainEffect");
     49
     50    this->init();
     51
     52    if (root != NULL)
     53        this->loadParams(root);
     54
     55    //load rain sound
     56    if (this->rainBuffer != NULL)
     57        ResourceManager::getInstance()->unload(this->rainBuffer);
     58    this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
     59
     60    //load wind sound
     61    if (this->rainWindForce != 0) {
     62        if (this->windBuffer != NULL)
     63            ResourceManager::getInstance()->unload(this->windBuffer);
     64        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
     65    }
     66
     67    if(rainActivate) {
     68        this->activate();
     69        RainEffect::rainParticles->precache((int)this->rainLife * 2);
     70    }
     71}
     72
     73/**
     74 * @brief standard deconstructor
     75 */
     76RainEffect::~RainEffect() {
     77    this->deactivate();
     78
     79    if (this->rainBuffer != NULL)
     80        ResourceManager::getInstance()->unload(this->rainBuffer);
     81
    6282    if (this->windBuffer != NULL)
    63       ResourceManager::getInstance()->unload(this->windBuffer);
    64     this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
    65   }
    66 
    67   if(rainActivate)
    68   {
    69     this->activate();
    70     RainEffect::rainParticles->precache((int)this->rainLife * 2);
    71   }
    72 }
    73 
    74 /**
    75  * @brief standard deconstructor
    76  */
    77 RainEffect::~RainEffect()
    78 {
    79   this->deactivate();
    80 
    81   if (this->rainBuffer != NULL)
    82     ResourceManager::getInstance()->unload(this->rainBuffer);
    83 
    84   if (this->windBuffer != NULL)
    85     ResourceManager::getInstance()->unload(this->windBuffer);
     83        ResourceManager::getInstance()->unload(this->windBuffer);
    8684}
    8785
     
    8987 * @brief initalizes the rain effect with default values
    9088 */
    91 void RainEffect::init()
    92 {
    93   //Default values
    94   this->rainActivate = false;
    95   this->rainMove = false;
    96   this->rainCoord = Vector(500, 500, 500);
    97   this->rainSize = Vector2D(1000, 1000);
    98   this->rainRate = 4000;
    99   this->rainVelocity = -300;
    100   this->rainLife = 4;
    101   this->rainWindForce  = 0;
    102   this->rainFadeInDuration = 0;
    103   this->rainFadeOutDuration = 0;
    104  
    105   this->cloudColor = Vector(0.8f, 0.8f, 0.8f);
    106   this->skyColor = Vector(0.0f, 0.0f, 0.0f);
    107 
    108   this->rainMaxParticles = this->rainRate * this->rainLife;
    109   this->localTimer = 0;
    110   this->soundRainVolume = 0.3f;
    111   this->emitter = new PlaneEmitter(this->rainSize);
    112 
    113   lightMan = LightManager::getInstance();
    114   this->rainAmbient = lightMan->getAmbientColor();
     89void RainEffect::init() {
     90    //Default values
     91    this->rainActivate = false;
     92    this->rainFadeInActivate = false;
     93    this->rainFadeOutActivate = false;
     94
     95    this->rainMove = false;
     96    this->rainCoord = Vector(500, 500, 500);
     97    this->rainSize = Vector2D(1000, 1000);
     98    this->rainRate = 4000;
     99    this->rainVelocity = -300;
     100    this->rainLife = 4;
     101    this->rainWindForce = 0;
     102    this->rainFadeInDuration = 10;
     103    this->rainFadeOutDuration = 10;
     104
     105    this->cloudColor = Vector(0.6f, 0.6f, 0.6f);
     106    this->skyColor = Vector(0.0f, 0.0f, 0.0f);
     107
     108    this->rainMaxParticles = this->rainRate * this->rainLife;
     109    this->localTimer = 0;
     110    this->soundRainVolume = 0.3f;
     111    this->emitter = new PlaneEmitter(this->rainSize);
     112
    115113}
    116114
     
    119117 * @param root: the XML-Element to load the data from
    120118 */
    121 void RainEffect::loadParams(const TiXmlElement* root)
    122 {
    123   WeatherEffect::loadParams(root);
    124 
    125   LoadParam(root, "coord", this, RainEffect, setRainCoord);
    126   LoadParam(root, "size", this, RainEffect, setRainSize);
    127   LoadParam(root, "rate", this, RainEffect, setRainRate);
    128   LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
    129   LoadParam(root, "life", this, RainEffect, setRainLife);
    130   LoadParam(root, "wind", this, RainEffect, setRainWind);
    131   LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
    132   LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
    133   LoadParam(root, "cloudcolor", this, RainEffect, setCloudColor);
    134   LoadParam(root, "skycolor", this, RainEffect, setSkyColor);
    135 
    136   LOAD_PARAM_START_CYCLE(root, element);
    137   {
    138     LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
    139   }
    140   LOAD_PARAM_END_CYCLE(element);
     119void RainEffect::loadParams(const TiXmlElement* root) {
     120    WeatherEffect::loadParams(root);
     121
     122    LoadParam(root, "coord", this, RainEffect, setRainCoord);
     123    LoadParam(root, "size", this, RainEffect, setRainSize);
     124    LoadParam(root, "rate", this, RainEffect, setRainRate);
     125    LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
     126    LoadParam(root, "life", this, RainEffect, setRainLife);
     127    LoadParam(root, "wind", this, RainEffect, setRainWind);
     128    LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
     129    LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
     130    LoadParam(root, "cloudcolor", this, RainEffect, setCloudColor);
     131    LoadParam(root, "skycolor", this, RainEffect, setSkyColor);
     132
     133    LOAD_PARAM_START_CYCLE(root, element);
     134    {
     135        LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
     136    }
     137    LOAD_PARAM_END_CYCLE(element);
    141138}
    142139
     
    146143 * @brief activates the rain effect
    147144 */
    148 void RainEffect::activate()
    149 {
    150   PRINTF(0)( "Activating RainEffect, coord: %f, %f, %f, size: %f, %f, rate: %f, velocity: %f, moveRain: %s\n", this->rainCoord.x, this->rainCoord.y, this->rainCoord.z, this->rainSize.x, this-> rainSize.y, this->rainRate, this->rainVelocity, this->rainMove ? "true" : "false" );
    151 
    152   this->rainActivate = true;
    153 
    154   if (unlikely(RainEffect::rainParticles == NULL))
    155   {
    156     RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
    157     RainEffect::rainParticles->setName("RainParticles");
    158     RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
    159     RainEffect::rainParticles->setRadius(0, 0.03);
    160     RainEffect::rainParticles->setRadius(0.2, 0.02);
    161     RainEffect::rainParticles->setRadius(1, 0.01);
    162     RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
    163     RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
    164     RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
    165   }
    166 
    167   this->emitter->setSystem(RainEffect::rainParticles);
    168 
    169   this->emitter->setRelCoor(this->rainCoord);
    170 
    171   this->emitter->setEmissionRate(this->rainRate);
    172   this->emitter->setEmissionVelocity(this->rainVelocity);
    173 
    174   this->emitter->setSpread(this->rainWindForce / 50, 0.2);
    175 
    176   // plays the rain sound and loops it
    177   this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
    178 
    179   // if there is wind, play the wind sound
    180   if (this->rainWindForce != 0)
    181     this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true);
    182 
    183   if (this->rainFadeInDuration == 0)
    184     lightMan->setAmbientColor(.1,.1,.1);
    185  
    186   // Change the cloudcolor,skycolor
    187   this->oldCloudColor = CloudEffect::cloudColor;
    188   this->oldSkyColor = CloudEffect::skyColor;
    189   CloudEffect::changeCloudColor(this->cloudColor);
    190   CloudEffect::changeSkyColor(this->skyColor);
     145void RainEffect::activate() {
     146    PRINTF(3)( "Activating RainEffect, coord: %f, %f, %f, size: %f, %f, rate: %f, velocity: %f, moveRain: %s\n", this->rainCoord.x, this->rainCoord.y, this->rainCoord.z, this->rainSize.x, this-> rainSize.y, this->rainRate, this->rainVelocity, this->rainMove ? "true" : "false" );
     147
     148    this->rainActivate = true;
     149
     150    if (unlikely(RainEffect::rainParticles == NULL)) {
     151        RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
     152        RainEffect::rainParticles->setName("RainParticles");
     153        RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
     154        RainEffect::rainParticles->setRadius(0, 0.03);
     155        RainEffect::rainParticles->setRadius(0.2, 0.02);
     156        RainEffect::rainParticles->setRadius(1, 0.01);
     157        RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2);   // grey blue 1
     158        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
     159        RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2);   // light grey
     160    }
     161
     162    this->emitter->setSystem(RainEffect::rainParticles);
     163    this->emitter->setRelCoor(this->rainCoord);
     164    this->emitter->setEmissionRate(this->rainRate);
     165    this->emitter->setEmissionVelocity(this->rainVelocity);
     166    this->emitter->setSpread(this->rainWindForce / 50, 0.2);
     167
     168    // play rain sound and loop it
     169    this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     170
     171    // if there's wind, play wind sound
     172    if (this->rainWindForce > 0)
     173        this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true);
     174
     175    // Store cloud- and sky color before the rain;
     176    this->oldCloudColor = CloudEffect::cloudColor;
     177    this->oldSkyColor   = CloudEffect::skyColor;
     178
     179    // If we're not fading, change color immediately
     180    if (!this->rainFadeInActivate || !this->rainFadeOutActivate) {
     181        CloudEffect::changeCloudColor(this->cloudColor, 0);
     182        CloudEffect::changeSkyColor(this->skyColor, 0);
     183    }
    191184}
    192185
     
    194187 * @brief deactivates the rain effect
    195188 */
    196 void RainEffect::deactivate()
    197 {
    198   PRINTF(0)("Deactivating RainEffect\n");
    199 
    200   this->rainActivate = false;
    201   this->emitter->setSystem(NULL);
    202 
    203   // Stop Sound
    204   this->soundSource.stop();
    205 
    206   // Restore Light Ambient
    207   lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
    208  
    209   CloudEffect::changeCloudColor(this->oldCloudColor);
    210   CloudEffect::changeSkyColor(this->oldSkyColor);
     189void RainEffect::deactivate() {
     190    PRINTF(3)("Deactivating RainEffect\n");
     191
     192    this->rainActivate = false;
     193    this->rainFadeInActivate = false;
     194    this->rainFadeOutActivate = false;
     195
     196    this->emitter->setSystem(NULL);
     197
     198    // Stop Sound
     199    this->soundSource.stop();
     200
     201    // Restore the old cloud- and sky color
     202    CloudEffect::changeCloudColor(this->oldCloudColor, 0);
     203    CloudEffect::changeSkyColor(this->oldSkyColor, 0);
    211204}
    212205
     
    215208 * @param dt: tick float
    216209 */
    217 void RainEffect::tick (float dt)
    218 {
    219   if (!this->rainActivate)
    220     return;
    221 
    222   if (this->rainMove)
    223   {
    224     this->rainCoord = State::getCameraNode()->getAbsCoor();
    225     this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
    226   }
    227 
    228   if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration)
    229   {
    230     this->localTimer += dt;
    231     float progress = this->localTimer / this->rainFadeInDuration;
    232 
    233     // Dim Light
    234     lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
    235 
    236     // use alpha in color to fade in
    237     RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
    238     RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
    239     RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
    240 
    241     // increase radius for more "heavy" rain
    242     RainEffect::rainParticles->setRadius(0, 0.03 * progress);
    243     RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
    244     RainEffect::rainParticles->setRadius(1, 0.01 * progress);
    245 
    246     // increase sound volume
    247     if (!this->soundSource.isPlaying())
    248       this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
    249 
    250     this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
    251   }
    252   else if ( this->rainFadeOutDuration != 0 )
    253   {
    254     if ( this->localTimer < this->rainFadeOutDuration )
    255     {
    256       this->localTimer += dt;
    257       float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
    258 
    259       // Fade In Light
    260       lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
    261 
    262       // use alpha in color to fade out
    263       RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
    264       RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
    265       RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
    266 
    267       // decrease radius
    268       RainEffect::rainParticles->setRadius(0, 0.03 * progress);
    269       RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
    270       RainEffect::rainParticles->setRadius(1, 0.01 * progress);
    271 
    272       // decrease sound volume
    273       if (!this->soundSource.isPlaying())
    274         this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
    275 
    276       this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
    277     }
    278     else
    279       this->deactivate();
    280   }
     210void RainEffect::tick (float dt) {
     211    if (!this->rainActivate)
     212        return;
     213
     214    if (this->rainMove) {
     215        this->rainCoord = State::getCameraNode()->getAbsCoor();
     216        this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
     217    }
     218
     219    if (this->rainFadeInActivate) {
     220        PRINTF(4)("tick fading IN RainEffect\n");
     221
     222        this->localTimer += dt;
     223        float progress = this->localTimer / this->rainFadeInDuration;
     224
     225        // use alpha in color to fade in rain
     226        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
     227        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
     228        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
     229
     230        // increase radius for more "heavy" rain
     231        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
     232        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
     233        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
     234
     235        // increase sound volume
     236        if (!this->soundSource.isPlaying())
     237            this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     238        this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
     239
     240        if (progress >= 1)
     241            this->rainFadeInActivate = false;
     242    }
     243
     244    if (this->rainFadeOutActivate) {
     245        PRINTF(4)("tick fading OUT RainEffect\n");
     246
     247        this->localTimer += dt;
     248        float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
     249
     250        // use alpha in color to fade out
     251        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
     252        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
     253        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
     254
     255        // decrease radius
     256        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
     257        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
     258        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
     259
     260        // decrease sound volume
     261        if (!this->soundSource.isPlaying())
     262            this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     263        this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
     264
     265        if (progress <= 0) {
     266            PRINTF(4)("tick fading OUT RainEffect COMPLETED! Deactivating...\n");
     267            this->rainFadeOutActivate = false;
     268            this->deactivate();
     269        }
     270    }
    281271}
    282272
     
    284274 * @brief starts raining slowly
    285275*/
    286 void RainEffect::startRaining()
    287 {
    288 
    289   if (this->rainActivate)
    290     this->deactivate();
    291 
    292   if (!this->rainFadeInDuration > 0)
    293     this->rainFadeInDuration = 20;
    294 
    295   this->localTimer = 0;
    296 
    297   this->activate();
     276void RainEffect::startRaining() {
     277    PRINTF(4)("startRaining function;\n");
     278
     279    // If it is already raining, do nothing
     280    if (this->rainActivate)
     281        return;
     282
     283    this->localTimer = 0;
     284    this->rainFadeInActivate = true;
     285
     286    PRINTF(4)("startRaining function complete; fadedur: %f\n", this->rainFadeInDuration);
     287    this->activate();
     288
     289    CloudEffect::changeCloudColor(this->cloudColor, this->rainFadeInDuration);
     290    CloudEffect::changeSkyColor(this->skyColor, this->rainFadeInDuration);
    298291}
    299292
     
    301294 * @brief stops raining slowly
    302295 */
    303 void RainEffect::stopRaining()
    304 {
    305 
    306   if (!this->rainActivate)
    307     this->activate();
    308 
    309   if (!this->rainFadeOutDuration > 0)
    310     this->rainFadeOutDuration = 20;
    311 
    312   this->localTimer = 0;
     296void RainEffect::stopRaining() {
     297    PRINTF(4)("stopRaining function;\n");
     298
     299    // If it is not raining, do nothing
     300    if (!this->rainActivate)
     301        return;
     302
     303    this->localTimer = 0;
     304    this->rainFadeOutActivate = true;
     305
     306    PRINTF(4)("stopRaining function completed; fadedur: %f\n", this->rainFadeOutDuration);
     307
     308    CloudEffect::changeCloudColor(this->oldCloudColor, this->rainFadeOutDuration);
     309    CloudEffect::changeSkyColor(this->oldSkyColor, this->rainFadeOutDuration);
    313310}
    314311
     
    316313 * @brief hides the rain
    317314 */
    318 void RainEffect::hideRain()
    319 {
    320   RainEffect::rainParticles->setColor(0, 0,0,0, 0);
    321   RainEffect::rainParticles->setColor(0, 0,0,0, 0);
    322 }
     315void RainEffect::hideRain() {
     316    RainEffect::rainParticles->setColor(0, 0,0,0, 0);
     317    RainEffect::rainParticles->setColor(0, 0,0,0, 0);
     318}
  • trunk/src/lib/graphics/effects/rain_effect.h

    r8793 r9006  
    8686    this->rainFadeOutDuration = fadeout;
    8787  }
    88  
     88
    8989  inline void setCloudColor(float colorX, float colorY, float colorZ)
    9090  {
     
    113113  GLfloat                     rainFadeInDuration;
    114114  GLfloat                     rainFadeOutDuration;
     115  bool                        rainFadeInActivate;
     116  bool                        rainFadeOutActivate;
    115117
    116118  Vector                      rainCoord;
     
    130132  float                       soundRainVolume;
    131133
    132   LightManager*               lightMan;
    133   GLfloat                     rainAmbient;
    134  
    135134  Vector oldSkyColor;
    136135  Vector oldCloudColor;
  • trunk/src/lib/graphics/effects/snow_effect.cc

    r8793 r9006  
    117117void SnowEffect::activate()
    118118{
    119         PRINTF(0)("Activating SnowEffect\n");
     119        PRINTF(3)("Activating SnowEffect\n");
    120120
    121121        this->snowActivate = true;
     
    149149void SnowEffect::deactivate()
    150150{
    151         PRINTF(0)("Deactivating SnowEffect\n");
     151        PRINTF(3)("Deactivating SnowEffect\n");
    152152
    153153        this->snowActivate = false;
  • trunk/src/lib/graphics/effects/volfog_effect.cc

    r8523 r9006  
    7474
    7575void VolFogEffect::init() {
    76     PRINTF(0)("Initalize VolFogEffect\n");
     76    PRINTF(3)("Initalize VolFogEffect\n");
    7777
    7878    // set fog mode
     
    108108
    109109    if (glewInit() == GLEW_OK)
    110         PRINTF(0)("glewInit OK\n");
     110        PRINTF(4)("glewInit OK\n");
    111111    else
    112         PRINTF(0)("glewInit failed\n");
     112        PRINTF(4)("glewInit failed\n");
    113113
    114114    if (glewGetExtension("GL_EXT_fog_coord"))
    115         PRINTF(0)("GL_EXT_fog_coord extension found\n");
     115        PRINTF(4)("GL_EXT_fog_coord extension found\n");
    116116}
    117117
    118118
    119119void VolFogEffect::activate() {
    120     PRINTF(0)("Activating VolFogEffect\n");
     120    PRINTF(3)("Activating VolFogEffect\n");
    121121}
    122122
    123123void VolFogEffect::deactivate() {
    124     PRINTF(0)("Deactivating VolFogEffect\n");
     124    PRINTF(3)("Deactivating VolFogEffect\n");
    125125
    126126    glDisable(GL_FOG);
Note: See TracChangeset for help on using the changeset viewer.