]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/newcloud.cxx
Cygwin fix.
[simgear.git] / simgear / scene / sky / newcloud.cxx
1 // 3D cloud class
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <plib/sg.h>
30 #include <plib/ssg.h>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/misc/sg_path.hxx>
33
34 #include STL_ALGORITHM
35 #include SG_GLU_H
36
37 #include "cloudfield.hxx"
38 #include "newcloud.hxx"
39
40
41 /*
42 */
43
44 static ssgTexture *cloudTextures[SGNewCloud::CLTexture_max];
45
46
47 bool SGNewCloud::useAnisotropic = true;
48 SGBbCache *SGNewCloud::cldCache = 0;
49 static bool texturesLoaded = false;
50 static float minx, maxx, miny, maxy, minz, maxz;
51
52 float SGNewCloud::nearRadius = 3500.0f;
53 bool SGNewCloud::lowQuality = false;
54 sgVec3 SGNewCloud::sunlight = {0.5f, 0.5f, 0.5f};
55 sgVec3 SGNewCloud::ambLight = {0.5f, 0.5f, 0.5f};
56 sgVec3 SGNewCloud::modelSunDir = {0,1,0};
57
58
59 void SGNewCloud::init(void) {
60         bbId = -1;
61         fadeActive = false;
62         duration = 100.0f;
63         fadetimer = 100.0f;
64         pauseLength = 0.0f;
65         last_step = -1.0f;
66         familly = CLFamilly_nn;
67         cloudId = (int) this;
68         sgSetVec3(center, 0.0f, 0.0f, 0.0f);
69         sgSetVec3(cloudpos, 0.0f, 0.0f, 0.0f);
70         radius = 0.0f;
71         delta_base = 0.0f;
72         list_spriteContainer.reserve(8);
73         list_spriteDef.reserve(40);
74
75         if( cldCache == 0 ) {
76                 cldCache = new SGBbCache;
77                 cldCache->init( 64 );
78         }
79 }
80
81 // constructor
82 SGNewCloud::SGNewCloud(CLFamilly_type classification)
83 {
84         init();
85         familly = classification;
86 }
87
88 SGNewCloud::SGNewCloud(string classification)
89 {
90         init();
91         if( classification == "cu" )
92                 familly = CLFamilly_cu;
93         else if( classification == "cb" )
94                 familly = CLFamilly_cb;
95         else if( classification == "st" )
96                 familly = CLFamilly_st;
97         else if( classification == "ns" )
98                 familly = CLFamilly_ns;
99         else if( classification == "sc" )
100                 familly = CLFamilly_sc;
101         else if( classification == "as" )
102                 familly = CLFamilly_as;
103         else if( classification == "ac" )
104                 familly = CLFamilly_ac;
105         else if( classification == "ci" )
106                 familly = CLFamilly_ci;
107         else if( classification == "cc" )
108                 familly = CLFamilly_cc;
109         else if( classification == "cs" )
110                 familly = CLFamilly_cs;
111 }
112
113 SGNewCloud::~SGNewCloud() {
114         list_spriteDef.clear();
115         list_spriteContainer.clear();
116         cldCache->free( bbId, cloudId );
117 }
118
119
120 // load all textures used to draw cloud sprites
121 void SGNewCloud::loadTextures(const string &tex_path) {
122         if( texturesLoaded )
123                 return;
124         texturesLoaded = true;
125
126         SGPath cloud_path;
127
128     cloud_path.set(tex_path);
129     cloud_path.append("cl_cumulus.rgb");
130     cloudTextures[ CLTexture_cumulus ] = new ssgTexture( cloud_path.str().c_str(), false, false, false );
131     cloudTextures[ CLTexture_cumulus ]->ref();
132
133     cloud_path.set(tex_path);
134     cloud_path.append("cl_stratus.rgb");
135     cloudTextures[ CLTexture_stratus ] = new ssgTexture( cloud_path.str().c_str(), false, false, false );
136     cloudTextures[ CLTexture_stratus ]->ref();
137
138 }
139
140 void SGNewCloud::startFade(bool direction, float duration, float pauseLength) {
141       if(duration <= 0.0) {
142               fadeActive = false;
143               return;
144       }
145       this->direction = direction;
146       fadetimer = 0.0;
147       this->duration = duration;
148       this->pauseLength = pauseLength;
149       last_step = -1.0;
150       fadeActive = true;
151 }
152 void SGNewCloud::setFade(float howMuch) {
153       duration = 100.0;
154       fadetimer = howMuch;
155       fadeActive = false;
156       last_step = -1.0;
157 }
158
159
160 static inline float rayleighCoeffAngular(float cosAngle) {
161         return 3.0f / 4.0f * (1.0f + cosAngle * cosAngle);
162 }
163
164 // cp is normalized (len==1)
165 static void CartToPolar3d(sgVec3 cp, sgVec3 polar) {
166     polar[0] = atan2(cp[1], cp[0]);
167     polar[1] = SG_PI / 2.0f - atan2(sqrt (cp[0] * cp[0] + cp[1] * cp[1]), cp[2]);
168         polar[2] = 1.0f;
169 }
170
171 static void PolarToCart3d(sgVec3 p, sgVec3 cart) {
172     float tmp = cos(p[1]);
173     cart[0] = cos(p[0]) * tmp;
174     cart[1] = sin(p[0]) * tmp;
175     cart[2] = sin(p[1]);
176 }
177
178
179 // compute the light for a cloud sprite corner
180 // from the normal and the sun, scaled by the Rayleigh factor
181 // and finaly added to the ambient light
182 static inline void lightFunction(sgVec3 normal, sgVec4 light, float pf) {
183         float cosAngle = sgScalarProductVec3( normal, SGNewCloud::modelSunDir);
184         float vl = (1.0f - 0.5f + cosAngle * 0.5f) * pf;
185         sgScaleVec3( light, SGNewCloud::sunlight, 0.25f + 0.75f * vl );
186         sgAddVec3( light, SGNewCloud::ambLight );
187         // we need to clamp or else the light will bug when adding transparency
188         if( light[0] > 1.0 )    light[0] = 1.0;
189         if( light[1] > 1.0 )    light[1] = 1.0;
190         if( light[2] > 1.0 )    light[2] = 1.0;
191         light[3] = 1.0;
192 }
193
194 // compute the light for a cloud sprite
195 // we use ambient light and orientation versus sun position
196 void SGNewCloud::computeSimpleLight(sgVec3 FakeEyePos) {
197         // constant Rayleigh factor if we are not doing Anisotropic lighting
198         float pf = 1.0f;
199
200         list_of_spriteDef::iterator iSprite;
201         for( iSprite = list_spriteDef.begin() ; iSprite != list_spriteDef.end() ; iSprite++ ) {
202                 if( useAnisotropic ) {
203                         sgVec3 eyeDir;
204             sgSubVec3(eyeDir, iSprite->pos, FakeEyePos);
205             sgNormaliseVec3(eyeDir);
206             float cosAngle = sgScalarProductVec3(eyeDir, modelSunDir);
207             pf = rayleighCoeffAngular(cosAngle);
208                 }
209                 lightFunction(iSprite->n0, iSprite->l0, pf);
210                 lightFunction(iSprite->n1, iSprite->l1, pf);
211                 lightFunction(iSprite->n2, iSprite->l2, pf);
212                 lightFunction(iSprite->n3, iSprite->l3, pf);
213
214         }
215 }
216
217
218 // add a new box to the cloud
219 void SGNewCloud::addContainer (float x, float y, float z, float r, CLbox_type type) {
220         spriteContainer cont;
221         sgSetVec3( cont.pos, x, y, z );
222         cont.r = r;
223         cont.cont_type = type;
224         sgSetVec3( cont.center, 0.0f, 0.0f, 0.0f);
225         list_spriteContainer.push_back( cont );
226         // don't place cloud below his base
227         if( y - r*0.50 < delta_base )
228                 delta_base = y - r*0.50;
229 }
230
231 // add a sprite inside a box
232 void SGNewCloud::addSprite(float x, float y, float z, float r, CLbox_type type, int box) {
233         spriteDef newSpriteDef;
234         int rank = list_spriteDef.size();
235         sgSetVec3( newSpriteDef.pos, x, y - delta_base, z);
236         newSpriteDef.box = box;
237         newSpriteDef.sprite_type = type;
238         newSpriteDef.rank = rank;
239         newSpriteDef.r = r;
240         list_spriteDef.push_back( newSpriteDef );
241         spriteContainer *thisBox = &list_spriteContainer[box];
242         sgVec3 deltaPos;
243         sgSubVec3( deltaPos, newSpriteDef.pos, thisBox->pos );
244         sgAddVec3( thisBox->center, deltaPos );
245
246         r = r * 0.70f;  // 0.5 * 1.xxx
247     if( x - r < minx )
248                 minx = x - r;
249     if( y - r < miny )
250                 miny = y - r;
251     if( z - r < minz )
252                 minz = z - r;
253     if( x + r > maxx )
254                 maxx = x + r;
255     if( y + r > maxy )
256                 maxy = y + r;
257     if( z + r > maxz )
258                 maxz = z + r;
259
260 }
261
262 // return a random number between -n/2 and n/2
263 static float Rnd(float n) {
264         return n * (-0.5f + sg_random());
265 }
266
267 // generate all sprite with defined boxes
268 void SGNewCloud::genSprites( void ) {
269         float x, y, z, r;
270     int N, sc;
271         minx = miny = minz = 99999.0;
272         maxx = maxy = maxz = -99999.0;
273
274     N = list_spriteContainer.size();
275         for(int i = 0 ; i < N ; i++ ) {
276                 spriteContainer *thisBox = & list_spriteContainer[i];
277                 // the type defines how the sprites can be positioned inside the box, their size, etc
278                 switch(thisBox->cont_type) {
279                         case CLbox_sc:
280                                 sc = 1;
281                                 r = thisBox->r + Rnd(0.2f);
282                                 x = thisBox->pos[SG_X] + Rnd(thisBox->r * 0.75f);
283                                 y = thisBox->pos[SG_Y] + Rnd(thisBox->r * 0.75f);
284                                 z = thisBox->pos[SG_Z] + Rnd(thisBox->r * 0.75f);
285                                 addSprite(x, y, z, r, thisBox->cont_type, i);
286                                 break;
287                         case CLbox_stratus:
288                                 sc = 1;
289                                 r = thisBox->r;
290                                 x = thisBox->pos[SG_X];
291                                 y = thisBox->pos[SG_Y];
292                                 z = thisBox->pos[SG_Z];
293                                 addSprite(x, y, z, r, thisBox->cont_type, i);
294                                 break;
295                         case CLbox_cumulus:
296                                 for( sc = 0 ; sc <= 4 ; sc ++ ) {
297                                         r = thisBox->r + Rnd(0.2f);
298                                         x = thisBox->pos[SG_X] + Rnd(thisBox->r * 0.75f);
299                                         y = thisBox->pos[SG_Y] + Rnd(thisBox->r * 0.5f);
300                                         z = thisBox->pos[SG_Z] + Rnd(thisBox->r * 0.75f);
301                                         if ( y < thisBox->pos[SG_Y] - thisBox->r / 10.0f )
302                                                 y = thisBox->pos[SG_Y] - thisBox->r / 10.0f;
303                                         addSprite(x, y, z, r, thisBox->cont_type, i);
304                                 }
305                                 break;
306                         default:
307                                 for( sc = 0 ; sc <= 4 ; sc ++ ) {
308                                         r = thisBox->r + Rnd(0.2f);
309                                         x = thisBox->pos[SG_X] + Rnd(thisBox->r);
310                                         y = thisBox->pos[SG_Y] + Rnd(thisBox->r);
311                                         z = thisBox->pos[SG_Z] + Rnd(thisBox->r);
312                                         addSprite(x, y, z, r, thisBox->cont_type, i);
313                                 }
314                                 break;
315                 }
316         sgScaleVec3(thisBox->center, 1.0f / sc);
317         }
318
319         radius = maxx - minx;
320     if ( (maxy - miny) > radius )
321                 radius = (maxy - miny);
322     if ( (maxz - minz) > radius )
323                 radius = (maxz - minz);
324     radius /= 2.0f;
325     sgSetVec3( center, (maxx + minx) / 2.0f, (maxy + miny) / 2.0f, (maxz + minz) / 2.0f );
326
327         const float ang = 45.0f * SG_PI / 180.0f;
328
329         // compute normals
330         list_of_spriteDef::iterator iSprite;
331         for( iSprite = list_spriteDef.begin() ; iSprite != list_spriteDef.end() ; iSprite++ ) {
332                 sgVec3 normal;
333                 spriteContainer *thisSpriteContainer = &list_spriteContainer[iSprite->box];
334                 if( familly == CLFamilly_sc || familly == CLFamilly_cu || familly == CLFamilly_cb) {
335                         sgSubVec3(normal, iSprite->pos, center);
336                 } else {
337                         sgSubVec3(normal, iSprite->pos, thisSpriteContainer->pos);
338                         sgSubVec3(normal, thisSpriteContainer->center);
339                         sgSubVec3(normal, cloudpos);
340                 }
341                 if( normal[0] == 0.0f && normal[1] == 0.0f && normal[2] == 0.0f )
342                         sgSetVec3( normal, 0.0f, 1.0f, 0.0f );
343         sgNormaliseVec3(normal);
344                 // use exotic lightning function, this will give more 'relief' to the clouds
345                 // compute a normal for each vextex this will simulate a smooth shading for a round shape
346                 sgVec3 polar, pt;
347                 // I suspect this code to be bugged...
348         CartToPolar3d(normal, polar);
349                 sgCopyVec3(iSprite->normal, normal);
350
351                 // offset the normal vector by some angle for each vertex
352         sgSetVec3(pt, polar[0] - ang, polar[1] - ang, polar[2]);
353         PolarToCart3d(pt, iSprite->n0);
354         sgSetVec3(pt, polar[0] + ang, polar[1] - ang, polar[2]);
355         PolarToCart3d(pt, iSprite->n1);
356         sgSetVec3(pt, polar[0] + ang, polar[1] + ang, polar[2]);
357         PolarToCart3d(pt, iSprite->n2);
358         sgSetVec3(pt, polar[0] - ang, polar[1] + ang, polar[2]);
359         PolarToCart3d(pt, iSprite->n3);
360         }
361
362         // experimental : clouds are dissipating with time
363         if( familly == CLFamilly_cu ) {
364                 startFade(true, 300.0f, 30.0f);
365                 fadetimer = sg_random() * 300.0f;
366         }
367 }
368
369
370 // definition of a cu cloud, only for testing
371 void SGNewCloud::new_cu(void) {
372         float s = 250.0f;
373         float r = Rnd(1.0) + 0.5;
374         if( r < 0.5f ) {
375                 addContainer(0.0f, 0.0f, 0.0f, s, CLbox_cumulus);
376                 addContainer(s, 0, 0, s, CLbox_cumulus);
377                 addContainer(0, 0, 2 * s, s, CLbox_cumulus);
378                 addContainer(s, 0, 2 * s, s, CLbox_cumulus);
379
380                 addContainer(-1.2f * s, 0.2f * s, s, s * 1.4f, CLbox_cumulus);
381                 addContainer(0.2f * s, 0.2f * s, s, s * 1.4f, CLbox_cumulus);
382                 addContainer(1.6f * s, 0.2f * s, s, s * 1.4f, CLbox_cumulus);
383         } else if ( r < 0.90f ) {
384                 addContainer(0, 0, 0, s * 1.2, CLbox_cumulus);
385                 addContainer(s, 0, 0, s, CLbox_cumulus);
386                 addContainer(0, 0, s, s, CLbox_cumulus);
387                 addContainer(s * 1.1, 0, s, s * 1.2, CLbox_cumulus);
388
389                 addContainer(-1.2 * s, 1 + 0.2 * s, s * 0.5, s * 1.4, CLbox_standard);
390                 addContainer(0.2 * s, 1 + 0.25 * s, s * 0.5, s * 1.5, CLbox_standard);
391                 addContainer(1.6 * s, 1 + 0.2 * s, s * 0.5, s * 1.4, CLbox_standard);
392
393         } else {
394                 // cb
395                 s = 675.0f;
396                 addContainer(0, 0, 0, s, CLbox_cumulus);
397                 addContainer(0, 0, s, s, CLbox_cumulus);
398                 addContainer(s, 0, s, s, CLbox_cumulus);
399                 addContainer(s, 0, 0, s, CLbox_cumulus);
400
401                 addContainer(s / 2, s, s / 2, s * 1.5, CLbox_standard);
402
403                 addContainer(0, 2 * s, 0, s, CLbox_standard);
404                 addContainer(0, 2 * s, s, s, CLbox_standard);
405                 addContainer(s, 2 * s, s, s, CLbox_standard);
406                 addContainer(s, 2 * s, 0, s, CLbox_standard);
407
408         }
409         genSprites();
410 }
411
412
413 // define the new position of the cloud (inside the cloud field, not on sphere)
414 void SGNewCloud::SetPos(sgVec3 newPos) {
415     int N = list_spriteDef.size();
416     sgVec3 deltaPos;
417         sgSubVec3( deltaPos, newPos, cloudpos );
418
419     // for each particle
420         for(int i = 0 ; i < N ; i ++) {
421                 sgAddVec3( list_spriteDef[i].pos, deltaPos );
422         }
423         sgAddVec3( center, deltaPos );
424     sgSetVec3( cloudpos, newPos[SG_X], newPos[SG_Y], newPos[SG_Z]);
425 }
426
427
428
429
430 void SGNewCloud::drawContainers() {
431
432
433 }
434
435
436
437 // sort on distance to eye because of transparency
438 void SGNewCloud::sortSprite( sgVec3 eye ) {
439         list_of_spriteDef::iterator iSprite;
440
441         // compute distance from sprite to eye
442         for( iSprite = list_spriteDef.begin() ; iSprite != list_spriteDef.end() ; iSprite++ ) {
443                 sgVec3 dist;
444                 sgSubVec3( dist, iSprite->pos, eye );
445                 iSprite->dist = -(dist[0]*dist[0] + dist[1]*dist[1] + dist[2]*dist[2]);
446         }
447         std::sort( list_spriteDef.begin(), list_spriteDef.end() );
448 }
449
450 // render the cloud on screen or on the RTT texture to build the impostor
451 void SGNewCloud::Render3Dcloud( bool drawBB, sgVec3 FakeEyePos, sgVec3 deltaPos, float dist_center ) {
452
453         float step = ( list_spriteDef.size() * (direction ? fadetimer : duration-fadetimer)) / duration;
454         int clrank = (int) step;
455         float clfadeinrank = (step - clrank);
456         last_step = step;
457
458         float CloudVisFade = 1.0 / (0.7f * SGCloudField::get_CloudVis());
459         // blend clouds with sky based on distance to limit the contrast of distant cloud
460         float t = 1.0f - dist_center * CloudVisFade;
461 //      if ( t < 0.0f ) 
462 //              return;
463
464     computeSimpleLight( FakeEyePos );
465
466     // view point sort, we sort because of transparency
467         sortSprite( FakeEyePos );
468
469         float dark = (familly == CLFamilly_cb ? 0.9f : 1.0f);
470
471         GLint previousTexture = -1, thisTexture;
472         list_of_spriteDef::iterator iSprite;
473         for( iSprite = list_spriteDef.begin() ; iSprite != list_spriteDef.end() ; iSprite++ ) {
474                 // skip this sprite if faded
475                 if(iSprite->rank > clrank)
476                         continue;
477                 // choose texture to use depending on sprite type
478                 switch(iSprite->sprite_type) {
479                         case CLbox_stratus:
480                                 thisTexture = CLTexture_stratus;
481                                 break;
482                         default:
483                                 thisTexture = CLTexture_cumulus;
484                                 break;
485                 }
486                 // in practice there is no texture switch (atm)
487                 if( previousTexture != thisTexture ) {
488                         previousTexture = thisTexture;
489                         glBindTexture(GL_TEXTURE_2D, cloudTextures[thisTexture]->getHandle());
490                 }
491
492                         sgVec3 translate;
493                         sgSubVec3( translate, iSprite->pos, deltaPos);
494
495
496                         // flipx and flipy are random texture flip flags, this gives more random clouds
497                         float flipx = (float) ( iSprite->rank & 1 );
498                         float flipy = (float) ( (iSprite->rank >> 1) & 1 );
499                         // cu texture have a flat bottom so we can't do a vertical flip
500                         if( iSprite->sprite_type == CLbox_cumulus )
501                                 flipy = 0.0f;
502 //                      if( iSprite->sprite_type == CLbox_stratus )
503 //                              flipx = 0.0f;
504                         // adjust colors depending on cloud type
505                         // TODO : rewrite that later, still experimental
506                         switch(iSprite->sprite_type) {
507                                 case CLbox_cumulus:
508                                         // dark bottom
509                     sgScaleVec3(iSprite->l0, 0.8f * dark);
510                     sgScaleVec3(iSprite->l1, 0.8f * dark);
511                                         sgScaleVec3(iSprite->l2, dark);
512                                         sgScaleVec3(iSprite->l3, dark);
513                                         break;
514                                 case CLbox_stratus:
515                                         // usually dark grey
516                                         if( familly == CLFamilly_st ) {
517                                                 sgScaleVec3(iSprite->l0, 0.8f);
518                                                 sgScaleVec3(iSprite->l1, 0.8f);
519                                                 sgScaleVec3(iSprite->l2, 0.8f);
520                                                 sgScaleVec3(iSprite->l3, 0.8f);
521                                         } else {
522                                                 sgScaleVec3(iSprite->l0, 0.7f);
523                                                 sgScaleVec3(iSprite->l1, 0.7f);
524                                                 sgScaleVec3(iSprite->l2, 0.7f);
525                                                 sgScaleVec3(iSprite->l3, 0.7f);
526                                         }
527                                         break;
528                                 default:
529                                         // darker bottom than top
530                     sgScaleVec3(iSprite->l0, 0.8f);
531                     sgScaleVec3(iSprite->l1, 0.8f);
532                                         break;
533                         }
534                         float r = iSprite->r * 0.5f;
535
536                         sgVec4 l0, l1, l2, l3;
537                         sgCopyVec4 ( l0, iSprite->l0 );
538                         sgCopyVec4 ( l1, iSprite->l1 );
539                         sgCopyVec4 ( l2, iSprite->l2 );
540                         sgCopyVec4 ( l3, iSprite->l3 );
541                         if( ! drawBB ) {
542                                 // now clouds at the far plane are half blended
543                                 sgScaleVec4( l0, t );
544                                 sgScaleVec4( l1, t );
545                                 sgScaleVec4( l2, t );
546                                 sgScaleVec4( l3, t );
547                         }
548                         if( iSprite->rank == clrank ) {
549                                 sgScaleVec4( l0, clfadeinrank );
550                                 sgScaleVec4( l1, clfadeinrank );
551                                 sgScaleVec4( l2, clfadeinrank );
552                                 sgScaleVec4( l3, clfadeinrank );
553                         }
554                         // compute the rotations so that the quad is facing the camera
555                         sgVec3 pos;
556                         sgSetVec3( pos, translate[SG_X], translate[SG_Z], translate[SG_Y] );
557                         sgCopyVec3( translate, pos );
558                         translate[2] -= FakeEyePos[1];
559 //                      sgNormaliseVec3( translate );
560                         float dist_sprite = sgLengthVec3 ( translate );
561                         sgScaleVec3 ( translate, SG_ONE / dist_sprite ) ;
562                         sgVec3 x, y, up = {0.0f, 0.0f, 1.0f};
563                         if( dist_sprite > 2*r ) {
564                                 sgVectorProductVec3(x, translate, up);
565                                 sgVectorProductVec3(y, x, translate);
566                         } else {
567                                 sgCopyVec3( x, SGCloudField::view_X );
568                                 sgCopyVec3( y, SGCloudField::view_Y );
569                         }
570                         sgScaleVec3(x, r);
571                         sgScaleVec3(y, r);
572  
573                         sgVec3 left, right;
574                         if( drawBB )
575                                 sgSetVec3( left, iSprite->pos[SG_X], iSprite->pos[SG_Z], iSprite->pos[SG_Y]);
576                         else
577                                 sgCopyVec3( left, pos );
578                         sgSubVec3 (left, y);
579                         sgAddVec3 (right, left, x);
580                         sgSubVec3 (left, x);
581
582                         glBegin(GL_QUADS);
583                                 glColor4fv(l0);
584                 glTexCoord2f(flipx, 1.0f - flipy);
585                 glVertex3fv(left);
586                 glColor4fv(l1);
587                 glTexCoord2f(1.0f - flipx, 1.0f - flipy);
588                 glVertex3fv(right);
589                                 sgScaleVec3( y, 2.0 );
590                                 sgAddVec3( left, y);
591                                 sgAddVec3( right, y);
592                 glColor4fv(l2);
593                 glTexCoord2f(1.0f - flipx, flipy);
594                 glVertex3fv(right);
595                 glColor4fv(l3);
596                 glTexCoord2f(flipx, flipy);
597                 glVertex3fv(left);
598
599                         glEnd();        
600
601         }
602 }
603
604
605 // compute rotations so that a quad is facing the camera
606 // TODO:change obsolete code because we dont use glrotate anymore
607 void SGNewCloud::CalcAngles(sgVec3 refpos, sgVec3 FakeEyePos, float *angleY, float *angleX) {
608     sgVec3 upAux, lookAt, objToCamProj, objToCam;
609         float angle, angle2;
610
611     sgSetVec3(objToCamProj, -FakeEyePos[SG_X] + refpos[SG_X], -FakeEyePos[SG_Z] + refpos[SG_Z], 0.0f);
612     sgNormaliseVec3(objToCamProj);
613
614     sgSetVec3(lookAt, 0.0f, 1.0f, 0.0f);
615     sgVectorProductVec3(upAux, lookAt, objToCamProj);
616     angle = sgScalarProductVec3(lookAt, objToCamProj);
617         if( (angle < 0.9999f) && (angle > -0.9999f) ) {
618         angle = acos(angle) * 180.0f / SG_PI;
619         if( upAux[2] < 0.0f )
620             angle = -angle;
621         } else
622         angle = 0.0f;
623
624     sgSetVec3(objToCam, -FakeEyePos[SG_X] + refpos[SG_X], -FakeEyePos[SG_Z] + refpos[SG_Z], -FakeEyePos[SG_Y] + refpos[SG_Y]);
625         sgNormaliseVec3(objToCam);
626
627     angle2 = sgScalarProductVec3(objToCamProj, objToCam);
628         if( (angle2 < 0.9999f) && (angle2 > -0.9999f) ) {
629         angle2 = -acos(angle2) * 180.0f / SG_PI;
630         if(  objToCam[2] > 0.0f )
631             angle2 = -angle2;
632         } else
633         angle2 = 0.0f;
634
635         angle2 += 90.0f;
636
637         *angleY = angle;
638     *angleX = angle2;
639 }
640
641 // draw a cloud but this time we use the impostor texture
642 void SGNewCloud::RenderBB(sgVec3 deltaPos, bool first_time, float dist_center) {
643
644                 sgVec3 translate;
645                 sgSubVec3( translate, center, deltaPos);
646
647                 // blend clouds with sky based on distance to limit the contrast of distant cloud
648                 float CloudVisFade = (1.0f * SGCloudField::get_CloudVis());
649
650                 float t = 1.0f - (dist_center - 1.0*radius) / CloudVisFade;
651                 if ( t < 0.0f ) 
652                         return;
653                 if( t > 1.0f )
654                         t = 1.0f;
655                 if( t > 0.50f )
656                         t *= 1.1f;
657         glColor4f(t, t, t, t);
658         float r = radius;
659                 // compute the rotations so that the quad is facing the camera
660                 sgVec3 pos;
661                 sgSetVec3( pos, translate[SG_X], translate[SG_Z], translate[SG_Y] );
662                 sgCopyVec3( translate, pos );
663                 pos[2] += deltaPos[1];
664
665                 sgNormaliseVec3( translate );
666                 sgVec3 x, y, up = {0.0f, 0.0f, 1.0f};
667                 sgVectorProductVec3(x, translate, up);
668                 sgVectorProductVec3(y, x, translate);
669                 if(first_time) {
670                         sgCopyVec3( rotX, x );
671                         sgCopyVec3( rotY, y );
672                 } else if(fabs(sgScalarProductVec3(rotX, x)) < 0.93f || fabs(sgScalarProductVec3(rotY, y)) < 0.93f ) {
673                         // ask for a redraw of this impostor if the view angle changed too much
674                         sgCopyVec3( rotX, x );
675                         sgCopyVec3( rotY, y );
676                         cldCache->invalidate(cloudId, bbId);
677                 }
678                 sgScaleVec3(x, r);
679                 sgScaleVec3(y, r);
680
681                 sgVec3 left, right;
682                 sgCopyVec3( left, pos );
683                 sgSubVec3 (left, y);
684                 sgAddVec3 (right, left, x);
685                 sgSubVec3 (left, x);
686
687                 glBegin(GL_QUADS);
688                         glTexCoord2f(0.0f, 0.0f);
689             glVertex3fv(left);
690                         glTexCoord2f(1.0f, 0.0f);
691             glVertex3fv(right);
692                         sgScaleVec3( y, 2.0 );
693                         sgAddVec3( left, y);
694                         sgAddVec3( right, y);
695                         glTexCoord2f(1.0f, 1.0f);
696             glVertex3fv(right);
697                         glTexCoord2f(0.0f, 1.0f);
698             glVertex3fv(left);
699                 glEnd();
700
701 #if 0   // debug only
702                 int age = cldCache->queryImpostorAge(bbId);
703                 // draw a red border for the newly generated BBs else draw a white border
704         if( age < 200 )
705             glColor3f(1, 0, 0);
706                 else
707             glColor3f(1, 1, 1);
708
709         glBindTexture(GL_TEXTURE_2D, 0);
710         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
711         glBegin(GL_QUADS);
712             glVertex2f(-r, -r);
713             glVertex2f(r, -r);
714             glVertex2f(r, r);
715             glVertex2f(-r, r);
716         glEnd();
717                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
718
719 #endif
720
721 }
722
723 // determine if it is a good idea to use an impostor to render the cloud
724 bool SGNewCloud::isBillboardable(float dist) {
725
726         if( dist <= ( 2.1f * radius ) ) {
727         // inside cloud
728         return false;
729         }
730         if( (dist-radius) <= nearRadius ) {
731         // near clouds we don't want to use BB
732         return false;
733         }
734         return true;
735 }
736
737
738
739 // render the cloud, fakepos is a relative position inside the cloud field
740 void SGNewCloud::Render(sgVec3 FakeEyePos) {
741         sgVec3 dist;
742
743         sgVec3 deltaPos;
744         sgCopyVec3( deltaPos, FakeEyePos);
745         deltaPos[1] = 0.0;
746     sgSubVec3( dist, center, FakeEyePos);
747     float dist_center = sgLengthVec3(dist);
748
749         if( fadeActive ) {
750                 fadetimer += SGCloudField::timer_dt;
751                 if( fadetimer > duration + pauseLength ) {
752                         // fade out after fade in, and vice versa
753                         direction = ! direction;
754                         fadetimer = 0.0;
755                 }
756         }
757
758         if( !isBillboardable(dist_center) ) {
759                 // not a good candidate for impostors, draw a real cloud
760                 Render3Dcloud(false, FakeEyePos, deltaPos, dist_center);
761         } else {
762                 GLuint texID = 0;
763                         bool first_time = false;
764                         // lets use our impostor
765                         if( bbId >= 0)
766                                 texID = cldCache->QueryTexID(cloudId, bbId);
767
768                         // ok someone took our impostor, so allocate a new one
769                         if( texID == 0 ) {
770                 // allocate a new Impostor
771                 bbId = cldCache->alloc(cloudId);
772                                 texID = cldCache->QueryTexID(cloudId, bbId);
773                                 first_time = true;
774                         }
775                         if( texID == 0 ) {
776                 // no more free texture in the pool
777                 Render3Dcloud(false, FakeEyePos, deltaPos, dist_center);
778                         } else {
779                 float angleX=0.0f, angleY=0.0f;
780
781                                 // force a redraw of the impostor if the cloud shape has changed enought
782                                 float step = ( list_spriteDef.size() * (direction ? fadetimer : duration-fadetimer)) / duration;
783                                 if( fabs(step - last_step) > 0.5f )
784                                         cldCache->invalidate(cloudId, bbId);
785
786                                 if( ! cldCache->isBbValid( cloudId, bbId, angleY, angleX)) {
787                     // we must build or rebuild this billboard
788                                         // start render to texture
789                     cldCache->beginCapture();
790                                         // set transformation matrices
791                     cldCache->setRadius(radius, dist_center);
792                                         gluLookAt(FakeEyePos[SG_X], FakeEyePos[SG_Z], FakeEyePos[SG_Y], center[SG_X], center[SG_Z], center[SG_Y], 0.0, 0.0, 1.0);
793                                         // draw into texture
794                     Render3Dcloud(true, FakeEyePos, deltaPos, dist_center);
795                                         // save rotation angles for later use
796                                         // TODO:this is not ok
797                                         cldCache->setReference(cloudId, bbId, angleY, angleX);
798                                         // save the rendered cloud into the cache
799                                         cldCache->setTextureData( bbId );
800                                         // finish render to texture and go back into standard context
801                     cldCache->endCapture();
802                                 }
803                 // draw the newly built BB or an old one
804                 glBindTexture(GL_TEXTURE_2D, texID);
805                 RenderBB(FakeEyePos, first_time, dist_center);
806                         }
807         }
808
809 }
810