]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloudfield.cxx
8a67926f1fd28a117dc3ea2e0812886d78053a59
[simgear.git] / simgear / scene / sky / cloudfield.cxx
1 // a layer of 3d clouds
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 <simgear/math/sg_random.h>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/math/polar3d.hxx>
33
34 #include STL_ALGORITHM
35 #include <vector>
36
37 SG_USING_STD(vector);
38
39 #include <simgear/environment/visual_enviro.hxx>
40 #include "sky.hxx"
41 #include "newcloud.hxx"
42 #include "cloudfield.hxx"
43
44 #if defined(__MINGW32__)
45 #define isnan(x) _isnan(x)
46 #endif
47
48 #if defined (__FreeBSD__)
49 #  if __FreeBSD_version < 500000
50      extern "C" {
51        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
52      }
53 #  endif
54 #endif
55
56
57 #if defined (__CYGWIN__)
58 #include <ieeefp.h>
59 #endif
60
61 extern SGSky *thesky;
62
63 static list_of_culledCloud inViewClouds;
64
65 // visibility distance for clouds in meters
66 float SGCloudField::CloudVis = 25000.0f;
67 bool SGCloudField::enable3D = false;
68 // fieldSize must be > CloudVis or we can destroy the impostor cache
69 // a cloud must only be seen once or the impostor will be generated for each of his positions
70 double SGCloudField::fieldSize = 50000.0;
71 float SGCloudField::density = 100.0;
72 double SGCloudField::timer_dt = 0.0;
73 sgVec3 SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
74
75 static int last_cache_size = 1*1024;
76 static int cacheResolution = 64;
77 static sgVec3 last_sunlight={0.0f, 0.0f, 0.0f};
78
79 int SGCloudField::get_CacheResolution(void) {
80 #if 0
81         return cacheResolution;
82 #endif
83         return 0;
84 }
85
86 void SGCloudField::set_CacheResolution(int resolutionPixels) {
87 #if 0
88         if(cacheResolution == resolutionPixels)
89                 return;
90         cacheResolution = resolutionPixels;
91         if(enable3D) {
92                 int count = last_cache_size * 1024 / (cacheResolution * cacheResolution * 4);
93                 if(count == 0)
94                         count = 1;
95                 SGNewCloud::cldCache->setCacheSize(count, cacheResolution);
96         }
97 #endif
98 }
99
100 int SGCloudField::get_CacheSize(void) { 
101 #if 0
102         return SGNewCloud::cldCache->queryCacheSize(); 
103 #endif
104         return 0;
105 }
106
107 void SGCloudField::set_CacheSize(int sizeKb) {
108 #if 0
109         // apply in rendering option dialog
110         if(last_cache_size == sizeKb)
111                 return;
112         if(sizeKb == 0)
113                 return;
114         if(sizeKb)
115                 last_cache_size = sizeKb;
116         if(enable3D) {
117                 int count = last_cache_size * 1024 / (cacheResolution * cacheResolution * 4);
118                 if(count == 0)
119                         count = 1;
120                 SGNewCloud::cldCache->setCacheSize(count, cacheResolution);
121         }
122 #endif
123 }
124 void SGCloudField::set_CloudVis(float distance) {
125 #if 0
126         if( distance <= fieldSize )
127                 SGCloudField::CloudVis = distance;
128 #endif
129 }
130 void SGCloudField::set_density(float density) {
131 #if 0
132         SGCloudField::density = density;
133 #endif
134 }
135 void SGCloudField::set_enable3dClouds(bool enable) {
136 #if 0
137         if(enable3D == enable)
138                 return;
139         enable3D = enable;
140         if(enable) {
141                 int count = last_cache_size * 1024 / (cacheResolution * cacheResolution * 4);
142                 if(count == 0)
143                         count = 1;
144                 SGNewCloud::cldCache->setCacheSize(count, cacheResolution);
145         } else {
146                 SGNewCloud::cldCache->setCacheSize(0);
147         }
148 #endif
149 }
150
151 // reposition the cloud layer at the specified origin and orientation
152 void SGCloudField::reposition( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double dt, float direction, float speed) {
153 #if 0
154     sgMat4 T1, LON, LAT;
155     sgVec3 axis;
156
157     sgMakeTransMat4( T1, p );
158
159     sgSetVec3( axis, 0.0, 0.0, 1.0 );
160     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
161
162     sgSetVec3( axis, 0.0, 1.0, 0.0 );
163     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
164
165     sgMat4 TRANSFORM;
166
167     sgCopyMat4( TRANSFORM, T1 );
168     sgPreMultMat4( TRANSFORM, LON );
169     sgPreMultMat4( TRANSFORM, LAT );
170
171     sgCoord layerpos;
172     sgSetCoord( &layerpos, TRANSFORM );
173
174         sgMakeCoordMat4( transform, &layerpos );
175
176
177         this->alt = alt;
178
179         // simulate clouds movement from wind
180     double sp_dist = speed*dt;
181     if (sp_dist > 0) {
182         double bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
183         double by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
184                 relative_position[SG_X] += bx;
185                 relative_position[SG_Y] += by;
186     }
187
188     if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
189         Point3D start( last_lon, last_lat, 0.0 );
190         Point3D dest( lon, lat, 0.0 );
191         double course = 0.0, dist = 0.0;
192
193         calc_gc_course_dist( dest, start, &course, &dist );
194         // if start and dest are too close together,
195         // calc_gc_course_dist() can return a course of "nan".  If
196         // this happens, lets just use the last known good course.
197         // This is a hack, and it would probably be better to make
198         // calc_gc_course_dist() more robust.
199         if ( isnan(course) ) {
200             course = last_course;
201         } else {
202             last_course = course;
203         }
204
205         // calculate cloud movement due to external forces
206         double ax = 0.0, ay = 0.0;
207
208         if (dist > 0.0) {
209             ax = cos(course) * dist;
210             ay = sin(course) * dist;
211         }
212
213                 deltax += ax;
214                 deltay += ay;
215
216         last_lon = lon;
217         last_lat = lat;
218     }
219
220
221         // correct the frustum with the right far plane
222         ssgContext *context = ssgGetCurrentContext();
223         frustum = *context->getFrustum();
224
225         float w, h;
226         sgEnviro.getFOV( w, h );
227         frustum.setFOV( w, h );
228         frustum.setNearFar(1.0, CloudVis);
229         timer_dt = dt;
230 #endif
231 }
232
233 SGCloudField::SGCloudField() :
234         deltax(0.0),
235         deltay(0.0),
236         last_course(0.0),
237         last_density(0.0),
238         draw_in_3d(true)
239 {
240 #if 0
241         sgSetVec3( relative_position, 0,0,0);
242         theField.reserve(200);
243         inViewClouds.reserve(200);
244         sg_srandom_time_10();
245 #else
246         draw_in_3d = false;
247 #endif
248 }
249
250 SGCloudField::~SGCloudField() {
251 #if 0
252         list_of_Cloud::iterator iCloud;
253         for( iCloud = theField.begin() ; iCloud != theField.end() ; iCloud++ ) {
254                 delete iCloud->aCloud;
255         }
256         theField.clear();
257 #endif
258 }
259
260
261 void SGCloudField::clear(void) {
262 #if 0
263         list_of_Cloud::iterator iCloud;
264         for( iCloud = theField.begin() ; iCloud != theField.end() ; iCloud++ ) {
265                 delete iCloud->aCloud;
266         }
267         theField.clear();
268         // force a recompute of density on first redraw
269         last_density = 0.0;
270         // true to come back in set density after layer is built
271         draw_in_3d = true;
272 #endif
273 }
274
275 // use a table or else we see poping when moving the slider...
276 static int densTable[][10] = {
277         {0,0,0,0,0,0,0,0,0,0},
278         {1,0,0,0,0,0,0,0,0,0},
279         {1,0,0,0,1,0,0,0,0,0},
280         {1,0,0,0,1,0,0,1,0,0}, // 30%
281         {1,0,1,0,1,0,0,1,0,0},
282         {1,0,1,0,1,0,1,1,0,0}, // 50%
283         {1,0,1,0,1,0,1,1,0,1},
284         {1,0,1,1,1,0,1,1,0,1}, // 70%
285         {1,1,1,1,1,0,1,1,0,1},
286         {1,1,1,1,1,0,1,1,1,1}, // 90%
287         {1,1,1,1,1,1,1,1,1,1}
288 };
289
290 // set the visible flag depending on density
291 void SGCloudField::applyDensity(void) {
292 #if 0
293         int row = (int) (density / 10.0);
294         int col = 0;
295     sgBox fieldBox;
296
297         list_of_Cloud::iterator iCloud;
298         for( iCloud = theField.begin() ; iCloud != theField.end() ; iCloud++ ) {
299                 if(++col > 9)
300                         col = 0;
301                 if( densTable[row][col] ) {
302                         iCloud->visible = true;
303             fieldBox.extend( *iCloud->aCloud->getCenter() );
304                 } else
305                         iCloud->visible = false;
306         }
307         last_density = density;
308         draw_in_3d = ( theField.size() != 0);
309     sgVec3 center;
310     sgSubVec3( center, fieldBox.getMax(), fieldBox.getMin() );
311     sgScaleVec3( center, 0.5f );
312     center[1] = 0.0f;
313     field_sphere.setCenter( center );
314     field_sphere.setRadius( fieldSize * 0.5f * 1.414f );
315 #endif
316 }
317
318 // add one cloud, data is not copied, ownership given
319 void SGCloudField::addCloud( sgVec3 pos, SGNewCloud *cloud) {
320 #if 0
321         Cloud cl;
322         cl.aCloud = cloud;
323         cl.visible = true;
324         cloud->SetPos( pos );
325         sgCopyVec3( cl.pos, *cloud->getCenter() );
326         theField.push_back( cl );
327 #endif
328 }
329
330
331 static float Rnd(float n) {
332         return n * (-0.5f + sg_random());
333 }
334
335 // for debug only
336 // build a field of cloud of size 25x25 km, its a grid of 11x11 clouds
337 void SGCloudField::buildTestLayer(void) {
338 #if 0
339         const float s = 2250.0f;
340
341         for( int z = -5 ; z <= 5 ; z++) {
342                 for( int x = -5 ; x <= 5 ; x++ ) {
343                         SGNewCloud *cloud = new SGNewCloud(SGNewCloud::CLFamilly_cu);
344                         cloud->new_cu();
345                         sgVec3 pos = {(x+Rnd(0.7)) * s, 750.0f, (z+Rnd(0.7)) * s};
346             addCloud(pos, cloud);
347                 }
348         }
349         applyDensity();
350 #endif
351 }
352
353 // cull all clouds of a tiled field
354 void SGCloudField::cullClouds(sgVec3 eyePos, sgMat4 mat) {
355 #if 0
356         list_of_Cloud::iterator iCloud;
357
358     sgSphere tile_sphere;
359     tile_sphere.setRadius( field_sphere.getRadius() );
360     sgVec3 tile_center;
361     sgSubVec3( tile_center, field_sphere.getCenter(), eyePos );
362     tile_sphere.setCenter( tile_center );
363     tile_sphere.orthoXform(mat);
364     if( frustum.contains( & tile_sphere ) == SG_OUTSIDE )
365         return;
366
367         for( iCloud = theField.begin() ; iCloud != theField.end() ; iCloud++ ) {
368                 sgVec3 dist;
369                 sgSphere sphere;
370                 if( ! iCloud->visible )
371                         continue;
372                 sgSubVec3( dist, iCloud->pos, eyePos );
373                 sphere.setCenter(dist[0], dist[2], dist[1] + eyePos[1]);
374                 float radius = iCloud->aCloud->getRadius();
375                 sphere.setRadius(radius);
376                 sphere.orthoXform(mat);
377                 if( frustum.contains( & sphere ) != SG_OUTSIDE ) {
378                         float squareDist = dist[0]*dist[0] + dist[1]*dist[1] + dist[2]*dist[2];
379                         culledCloud tmp;
380                         tmp.aCloud = iCloud->aCloud;
381                         sgCopyVec3( tmp.eyePos, eyePos ); 
382                         // save distance for later sort, opposite distance because we want back to front
383                         tmp.dist   =  - squareDist;
384                         tmp.heading = -SG_PI/2.0 - atan2( dist[0], dist[2] ); // + SG_PI;
385                         tmp.alt         = iCloud->pos[1];
386                         inViewClouds.push_back(tmp);
387                         if( squareDist - radius*radius < 100*100 )
388                                 sgEnviro.set_view_in_cloud(true);
389                 }
390         }
391 #endif
392 }
393
394
395 // Render a cloud field
396 // because no field can have an infinite size (and we don't want to reach his border)
397 // we draw this field and adjacent fields.
398 // adjacent fields are not real, its the same field displaced by some offset
399 void SGCloudField::Render(void) {
400 #if 0
401     sgVec3 eyePos;
402         double relx, rely;
403
404         if( ! enable3D )
405                 return;
406
407         if( last_density != density ) {
408                 last_density = density;
409                 applyDensity();
410         }
411
412         if( ! draw_in_3d )
413                 return;
414
415         if( ! SGNewCloud::cldCache->isRttAvailable() )
416                 return;
417
418         inViewClouds.clear();
419
420  
421         glPushMatrix();
422  
423         sgMat4 modelview, tmp, invtrans;
424
425         // try to find the sun position
426         sgTransposeNegateMat4( invtrans, transform );
427     sgVec3 lightVec;
428     ssgGetLight( 0 )->getPosition( lightVec );
429     sgXformVec3( lightVec, invtrans );
430
431         sgSetVec3(  SGNewCloud::modelSunDir, lightVec[0], lightVec[2], lightVec[1]);
432         // try to find the lighting data (not accurate)
433         sgVec4 diffuse, ambient;
434         ssgGetLight( 0 )->getColour( GL_DIFFUSE, diffuse );
435         ssgGetLight( 0 )->getColour( GL_AMBIENT, ambient );
436 //      sgScaleVec3 ( SGNewCloud::sunlight, diffuse , 1.0f);
437         sgScaleVec3 ( SGNewCloud::ambLight, ambient , 1.1f);
438         // trying something else : clouds are more yellow/red at dawn/dusk
439         // and added a bit of blue ambient
440     float *sun_color = thesky->get_sun_color();
441         sgScaleVec3 ( SGNewCloud::sunlight, sun_color , 0.4f);
442         SGNewCloud::ambLight[2] += 0.1f;
443
444         sgVec3 delta_light;
445         sgSubVec3(delta_light, last_sunlight, SGNewCloud::sunlight);
446         if( (fabs(delta_light[0]) + fabs(delta_light[1]) + fabs(delta_light[2])) > 0.05f ) {
447                 sgCopyVec3( last_sunlight, SGNewCloud::sunlight );
448                 // force the redraw of all the impostors
449                 SGNewCloud::cldCache->invalidateCache();
450         }
451
452         // voodoo things on the matrix stack
453     ssgGetModelviewMatrix( modelview );
454         sgCopyMat4( tmp, transform );
455     sgPostMultMat4( tmp, modelview );
456
457         // cloud fields are tiled on the flat earth
458         // compute the position in the tile
459         relx = fmod( deltax + relative_position[SG_X], fieldSize );
460         rely = fmod( deltay + relative_position[SG_Y], fieldSize );
461
462         relx = fmod( relx + fieldSize, fieldSize );
463         rely = fmod( rely + fieldSize, fieldSize );
464         sgSetVec3( eyePos, relx, alt, rely);
465
466         sgSetVec3( view_X, tmp[0][0], tmp[1][0], tmp[2][0] );
467         sgSetVec3( view_Y, tmp[0][1], tmp[1][1], tmp[2][1] );
468         sgSetVec3( view_vec, tmp[0][2], tmp[1][2], tmp[2][2] );
469
470     ssgLoadModelviewMatrix( tmp );
471  
472 /* flat earth
473
474         ^
475         |
476         |    FFF
477         |    FoF
478         |    FFF
479         |
480         O----------->
481                 o = we are here
482                 F = adjacent fields
483 */
484
485         for(int x = -1 ; x <= 1 ; x++)
486                 for(int y = -1 ; y <= 1 ; y++ ) {
487                         sgVec3 fieldPos;
488                         // pretend we are not where we are
489                         sgSetVec3(fieldPos, eyePos[0] + x*fieldSize, eyePos[1], eyePos[2] + y*fieldSize);
490                         cullClouds(fieldPos, tmp);
491                 }
492         // sort all visible clouds back to front (because of transparency)
493         std::sort( inViewClouds.begin(), inViewClouds.end() );
494  
495         // TODO:push states
496         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
497     glEnable(GL_ALPHA_TEST);
498     glAlphaFunc(GL_GREATER, 0.0f);
499         glDisable(GL_CULL_FACE);
500         glEnable(GL_DEPTH_TEST);
501         glDepthMask( GL_FALSE );
502         glEnable(GL_SMOOTH);
503     glEnable(GL_BLEND);
504         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
505         glEnable( GL_TEXTURE_2D );
506         glDisable( GL_FOG );
507     glDisable(GL_LIGHTING);
508
509         // test data: field = 11x11 cloud, 9 fields
510         // depending on position and view direction, perhaps 40 to 60 clouds not culled
511         list_of_culledCloud::iterator iCloud;
512         for( iCloud = inViewClouds.begin() ; iCloud != inViewClouds.end() ; iCloud++ ) {
513 //              iCloud->aCloud->drawContainers();
514                 iCloud->aCloud->Render(iCloud->eyePos);
515                 sgEnviro.callback_cloud(iCloud->heading, iCloud->alt, 
516                         iCloud->aCloud->getRadius(), iCloud->aCloud->getFamilly(), - iCloud->dist, iCloud->aCloud->getId());
517         }
518
519         glBindTexture(GL_TEXTURE_2D, 0);
520     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
521         glEnable( GL_FOG );
522         glEnable(GL_CULL_FACE);
523         glEnable(GL_DEPTH_TEST);
524
525         ssgLoadModelviewMatrix( modelview );
526
527         glPopMatrix();
528 #endif
529 }