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