]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/oursun.cxx
Modified Files:
[simgear.git] / simgear / scene / sky / oursun.cxx
1 // oursun.hxx -- model earth's sun
2 //
3 // Written by Durk Talsma. Originally started October 1997, for distribution  
4 // with the FlightGear project. Version 2 was written in August and 
5 // September 1998. This code is based upon algorithms and data kindly 
6 // provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <simgear_config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <osg/AlphaFunc>
34 #include <osg/BlendFunc>
35 #include <osg/Fog>
36 #include <osg/Geode>
37 #include <osg/Geometry>
38 #include <osg/Material>
39 #include <osg/ShadeModel>
40 #include <osg/TexEnv>
41 #include <osg/Texture2D>
42 #include <osgDB/ReadFile>
43
44 #include <simgear/screen/colors.hxx>
45 #include <simgear/scene/model/model.hxx>
46 #include "oursun.hxx"
47
48 // Constructor
49 SGSun::SGSun( void ) {
50     prev_sun_angle = -9999.0;
51     visibility = -9999.0;
52 }
53
54
55 // Destructor
56 SGSun::~SGSun( void ) {
57 }
58
59
60 // initialize the sun object and connect it into our scene graph root
61 osg::Node*
62 SGSun::build( SGPath path, double sun_size, SGPropertyNode *property_tree_Node ) {
63
64     env_node = property_tree_Node;
65
66     SGPath ihalopath = path, ohalopath = path;
67
68     // build the ssg scene graph sub tree for the sky and connected
69     // into the provide scene graph branch
70     sun_transform = new osg::MatrixTransform;
71     osg::StateSet* stateSet = sun_transform->getOrCreateStateSet();
72
73     osg::TexEnv* texEnv = new osg::TexEnv;
74     texEnv->setMode(osg::TexEnv::MODULATE);
75     stateSet->setTextureAttribute(0, texEnv, osg::StateAttribute::ON);
76  
77     osg::Material* material = new osg::Material;
78     material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
79     material->setEmission(osg::Material::FRONT_AND_BACK,
80                           osg::Vec4(0, 0, 0, 1));
81     material->setSpecular(osg::Material::FRONT_AND_BACK,
82                           osg::Vec4(0, 0, 0, 1));
83     stateSet->setAttribute(material);
84
85     osg::ShadeModel* shadeModel = new osg::ShadeModel;
86     shadeModel->setMode(osg::ShadeModel::SMOOTH);
87     stateSet->setAttributeAndModes(shadeModel);
88
89     osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
90     alphaFunc->setFunction(osg::AlphaFunc::ALWAYS);
91     stateSet->setAttributeAndModes(alphaFunc);
92
93     osg::BlendFunc* blendFunc = new osg::BlendFunc;
94     blendFunc->setSource(osg::BlendFunc::SRC_ALPHA);
95     blendFunc->setDestination(osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
96     stateSet->setAttributeAndModes(blendFunc);
97
98     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
99     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
100     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
101     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
102
103
104     osg::Geode* geode = new osg::Geode;
105     stateSet = geode->getOrCreateStateSet();
106
107     stateSet->setRenderBinDetails(-6, "RenderBin");
108
109     // set up the sun-state
110     path.append( "sun.rgba" );
111     osg::Texture2D* texture = SGLoadTexture2D(path);
112     stateSet->setTextureAttributeAndModes(0, texture);
113
114     // Build scenegraph
115     sun_cl = new osg::Vec4Array;
116     sun_cl->push_back(osg::Vec4(1, 1, 1, 1));
117
118     osg::Vec3Array* sun_vl = new osg::Vec3Array;
119     sun_vl->push_back(osg::Vec3(-sun_size, 0, -sun_size));
120     sun_vl->push_back(osg::Vec3(sun_size, 0, -sun_size));
121     sun_vl->push_back(osg::Vec3(-sun_size, 0, sun_size));
122     sun_vl->push_back(osg::Vec3(sun_size, 0, sun_size));
123
124     osg::Vec2Array* sun_tl = new osg::Vec2Array;
125     sun_tl->push_back(osg::Vec2(0, 0));
126     sun_tl->push_back(osg::Vec2(1, 0));
127     sun_tl->push_back(osg::Vec2(0, 1));
128     sun_tl->push_back(osg::Vec2(1, 1));
129
130     osg::Geometry* geometry = new osg::Geometry;
131     geometry->setUseDisplayList(false);
132     geometry->setVertexArray(sun_vl);
133     geometry->setColorArray(sun_cl.get());
134     geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
135     geometry->setNormalBinding(osg::Geometry::BIND_OFF);
136     geometry->setTexCoordArray(0, sun_tl);
137     geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4));
138     geode->addDrawable(geometry);
139
140     sun_transform->addChild( geode );
141
142
143     // set up the inner-halo state
144     geode = new osg::Geode;
145     stateSet = geode->getOrCreateStateSet();
146     stateSet->setRenderBinDetails(-7, "RenderBin");
147     
148     ihalopath.append( "inner_halo.rgba" );
149     texture = SGLoadTexture2D(ihalopath);
150     stateSet->setTextureAttributeAndModes(0, texture);
151
152     // Build ssg structure
153     ihalo_cl = new osg::Vec4Array;
154     ihalo_cl->push_back(osg::Vec4(1, 1, 1, 1));
155
156     float ihalo_size = sun_size * 2.0;
157     osg::Vec3Array* ihalo_vl = new osg::Vec3Array;
158     ihalo_vl->push_back(osg::Vec3(-ihalo_size, 0, -ihalo_size));
159     ihalo_vl->push_back(osg::Vec3(ihalo_size, 0, -ihalo_size));
160     ihalo_vl->push_back(osg::Vec3(-ihalo_size, 0, ihalo_size));
161     ihalo_vl->push_back(osg::Vec3(ihalo_size, 0, ihalo_size));
162
163     osg::Vec2Array* ihalo_tl = new osg::Vec2Array;
164     ihalo_tl->push_back(osg::Vec2(0, 0));
165     ihalo_tl->push_back(osg::Vec2(1, 0));
166     ihalo_tl->push_back(osg::Vec2(0, 1));
167     ihalo_tl->push_back(osg::Vec2(1, 1));
168
169     geometry = new osg::Geometry;
170     geometry->setUseDisplayList(false);
171     geometry->setVertexArray(ihalo_vl);
172     geometry->setColorArray(ihalo_cl.get());
173     geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
174     geometry->setNormalBinding(osg::Geometry::BIND_OFF);
175     geometry->setTexCoordArray(0, ihalo_tl);
176     geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4));
177     geode->addDrawable(geometry);
178
179     sun_transform->addChild( geode );
180
181     
182     // set up the outer halo state
183     
184     geode = new osg::Geode;
185     stateSet = geode->getOrCreateStateSet();
186     stateSet->setRenderBinDetails(-8, "RenderBin");
187
188     ohalopath.append( "outer_halo.rgba" );
189     texture = SGLoadTexture2D(ohalopath);
190     stateSet->setTextureAttributeAndModes(0, texture);
191
192     // Build ssg structure
193     ohalo_cl = new osg::Vec4Array;
194     ohalo_cl->push_back(osg::Vec4(1, 1, 1, 1));
195
196     double ohalo_size = sun_size * 8.0;
197     osg::Vec3Array* ohalo_vl = new osg::Vec3Array;
198     ohalo_vl->push_back(osg::Vec3(-ohalo_size, 0, -ohalo_size));
199     ohalo_vl->push_back(osg::Vec3(ohalo_size, 0, -ohalo_size));
200     ohalo_vl->push_back(osg::Vec3(-ohalo_size, 0, ohalo_size));
201     ohalo_vl->push_back(osg::Vec3(ohalo_size, 0, ohalo_size));
202
203     osg::Vec2Array* ohalo_tl = new osg::Vec2Array;
204     ohalo_tl->push_back(osg::Vec2(0, 0));
205     ohalo_tl->push_back(osg::Vec2(1, 0));
206     ohalo_tl->push_back(osg::Vec2(0, 1));
207     ohalo_tl->push_back(osg::Vec2(1, 1));
208
209     geometry = new osg::Geometry;
210     geometry->setUseDisplayList(false);
211     geometry->setVertexArray(ohalo_vl);
212     geometry->setColorArray(ohalo_cl.get());
213     geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
214     geometry->setNormalBinding(osg::Geometry::BIND_OFF);
215     geometry->setTexCoordArray(0, ohalo_tl);
216     geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4));
217     geode->addDrawable(geometry);
218
219     sun_transform->addChild( geode );
220
221
222     // force a repaint of the sun colors with arbitrary defaults
223     repaint( 0.0, 1.0 );
224
225     return sun_transform.get();
226 }
227
228
229 // repaint the sun colors based on current value of sun_angle in
230 // degrees relative to verticle
231 // 0 degrees = high noon
232 // 90 degrees = sun rise/set
233 // 180 degrees = darkest midnight
234 bool SGSun::repaint( double sun_angle, double new_visibility ) {
235     
236         if ( visibility != new_visibility ) {
237                 visibility = new_visibility;
238
239                 static const double sqrt_m_log01 = sqrt( -log( 0.01 ) );
240                 sun_exp2_punch_through = sqrt_m_log01 / ( visibility * 15 );
241         }
242
243         if ( prev_sun_angle != sun_angle ) {
244                 prev_sun_angle = sun_angle;
245
246                 // determine how much aerosols are in the air (rough guess)
247                 double aerosol_factor;
248                 if ( visibility < 100 ){
249                         aerosol_factor = 8000;
250                 }
251                 else {
252                         aerosol_factor = 80.5 / log( visibility / 100 );
253                 }
254
255                 // get environmental data from property tree or use defaults
256                 double rel_humidity, density_avg;
257
258                 if ( !env_node )
259                 {
260                         rel_humidity = 0.5;
261                         density_avg = 0.7;
262                 }
263                 else
264                 {
265                         rel_humidity = env_node->getFloatValue( "relative-humidity" ); 
266                         density_avg =  env_node->getFloatValue( "atmosphere/density-tropo-avg" );
267                 }
268
269                 // ok, now let's go and generate the sun color
270                 osg::Vec4 i_halo_color, o_halo_color, sun_color;
271
272                 // Some comments: 
273                 // When the sunangle changes, light has to travel a longer distance through the atmosphere.
274                 // So it's scattered more due to raleigh scattering, which affects blue more than green light.
275                 // Red is almost not scattered and effectively only get's touched when the sun is near the horizon.
276                 // Visability also affects suncolor inasmuch as more particles are in the air that cause more scattering.
277                 // We base our calculation on the halo's color, which is most scattered. 
278  
279                 // Red - is almost not scattered        
280                 // Lambda is 700 nm
281                 
282                 double red_scat_f = ( aerosol_factor * path_distance * density_avg ) / 5E+07;
283                 sun_color[0] = 1 - red_scat_f;
284                 i_halo_color[0] = 1 - ( 1.1 * red_scat_f );
285                 o_halo_color[0] = 1 - ( 1.4 * red_scat_f );
286
287                 // Green - 546.1 nm
288                 double green_scat_f = ( aerosol_factor * path_distance * density_avg ) / 8.8938E+06;
289                 sun_color[1] = 1 - green_scat_f;
290                 i_halo_color[1] = 1 - ( 1.1 * green_scat_f );
291                 o_halo_color[1] = 1 - ( 1.4 * green_scat_f );
292  
293                 // Blue - 435.8 nm
294                 double blue_scat_f = ( aerosol_factor * path_distance * density_avg ) / 3.607E+06;
295                 sun_color[2] = 1 - blue_scat_f;
296                 i_halo_color[2] = 1 - ( 1.1 * blue_scat_f );
297                 o_halo_color[2] = 1 - ( 1.4 * blue_scat_f );
298
299                 // Alpha
300                 sun_color[3] = 1;
301                 i_halo_color[3] = 1;
302
303                 o_halo_color[3] = blue_scat_f; 
304                 if ( ( new_visibility < 10000 ) &&  ( blue_scat_f > 1 )){
305                         o_halo_color[3] = 2 - blue_scat_f; 
306                 }
307
308
309                 // Now that we have the color calculated 
310                 // let's consider the saturation which is produced by mie scattering
311                 double saturation = 1 - ( rel_humidity / 200 );
312                 sun_color[1] += (( 1 - saturation ) * ( 1 - sun_color[1] ));
313                 sun_color[2] += (( 1 - saturation ) * ( 1 - sun_color[2] ));
314
315                 i_halo_color[1] += (( 1 - saturation ) * ( 1 - i_halo_color[1] ));
316                 i_halo_color[2] += (( 1 - saturation ) * ( 1 - i_halo_color[2] )); 
317
318                 o_halo_color[1] += (( 1 - saturation ) * ( 1 - o_halo_color[1] )); 
319                 o_halo_color[2] += (( 1 - saturation ) * ( 1 - o_halo_color[2] )); 
320
321                 // just to make sure we're in the limits
322                 if ( sun_color[0] < 0 ) sun_color[0] = 0;
323                 else if ( sun_color[0] > 1) sun_color[0] = 1;
324                 if ( i_halo_color[0] < 0 ) i_halo_color[0] = 0;
325                 else if ( i_halo_color[0] > 1) i_halo_color[0] = 1;
326                 if ( o_halo_color[0] < 0 ) o_halo_color[0] = 0;
327                 else if ( o_halo_color[0] > 1) o_halo_color[0] = 1;
328
329                 if ( sun_color[1] < 0 ) sun_color[1] = 0;
330                 else if ( sun_color[1] > 1) sun_color[1] = 1;
331                 if ( i_halo_color[1] < 0 ) i_halo_color[1] = 0;
332                 else if ( i_halo_color[1] > 1) i_halo_color[1] = 1;
333                 if ( o_halo_color[1] < 0 ) o_halo_color[1] = 0;
334                 else if ( o_halo_color[1] > 1) o_halo_color[1] = 1;
335
336                 if ( sun_color[2] < 0 ) sun_color[2] = 0;
337                 else if ( sun_color[2] > 1) sun_color[2] = 1;
338                 if ( i_halo_color[2] < 0 ) i_halo_color[2] = 0;
339                 else if ( i_halo_color[2] > 1) i_halo_color[2] = 1;
340                 if ( o_halo_color[2] < 0 ) o_halo_color[2] = 0;
341                 else if ( o_halo_color[2] > 1) o_halo_color[2] = 1;
342                 if ( o_halo_color[3] < 0 ) o_halo_color[2] = 0;
343                 else if ( o_halo_color[3] > 1) o_halo_color[3] = 1;
344
345         
346                 gamma_correct_rgb( i_halo_color._v );
347                 gamma_correct_rgb( o_halo_color._v );
348                 gamma_correct_rgb( sun_color._v );      
349
350                 (*sun_cl)[0] = sun_color;
351                 sun_cl->dirty();
352                 (*ihalo_cl)[0] = i_halo_color;
353                 ihalo_cl->dirty();
354                 (*ohalo_cl)[0] = o_halo_color;
355                 ohalo_cl->dirty();
356     }
357
358     return true;
359 }
360
361
362 // reposition the sun at the specified right ascension and
363 // declination, offset by our current position (p) so that it appears
364 // fixed at a great distance from the viewer.  Also add in an optional
365 // rotation (i.e. for the current time of day.)
366 // Then calculate stuff needed for the sun-coloring
367 bool SGSun::reposition( const SGVec3f& p, double angle,
368                         double rightAscension, double declination, 
369                         double sun_dist, double lat, double alt_asl, double sun_angle)
370 {
371     // GST - GMT sidereal time 
372     osg::Matrix T1, T2, GST, RA, DEC;
373
374     T1.makeTranslate(p.osg());
375     GST.makeRotate(SGD_DEGREES_TO_RADIANS*angle, osg::Vec3(0, 0, -1));
376
377     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
378     //             0.0, 0.0, 1.0);
379     RA.makeRotate(rightAscension - 90*SGD_DEGREES_TO_RADIANS, osg::Vec3(0, 0, 1));
380
381     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
382     DEC.makeRotate(declination, osg::Vec3(1, 0, 0));
383
384     // xglTranslatef(0,sun_dist);
385     T2.makeTranslate(osg::Vec3(0, sun_dist, 0));
386
387     sun_transform->setMatrix(T2*DEC*RA*GST*T1);
388
389     // Suncolor related things:
390     if ( prev_sun_angle != sun_angle ) {
391       if ( sun_angle == 0 ) sun_angle = 0.1;
392          const double r_earth_pole = 6356752.314;
393          const double r_tropo_pole = 6356752.314 + 8000;
394          const double epsilon_earth2 = 6.694380066E-3;
395          const double epsilon_tropo2 = 9.170014946E-3;
396
397          double r_tropo = r_tropo_pole / sqrt ( 1 - ( epsilon_tropo2 * pow ( cos( lat ), 2 )));
398          double r_earth = r_earth_pole / sqrt ( 1 - ( epsilon_earth2 * pow ( cos( lat ), 2 )));
399  
400          double position_radius = r_earth + alt_asl;
401
402          double gamma =  SG_PI - sun_angle;
403          double sin_beta =  ( position_radius * sin ( gamma )  ) / r_tropo;
404          double alpha =  SG_PI - gamma - asin( sin_beta );
405
406          // OK, now let's calculate the distance the light travels
407          path_distance = sqrt( pow( position_radius, 2 ) + pow( r_tropo, 2 )
408                         - ( 2 * position_radius * r_tropo * cos( alpha ) ));
409
410          double alt_half = sqrt( pow ( r_tropo, 2 ) + pow( path_distance / 2, 2 ) - r_tropo * path_distance * cos( asin( sin_beta )) ) - r_earth;
411
412          if ( alt_half < 0.0 ) alt_half = 0.0;
413
414          // Push the data to the property tree, so it can be used in the enviromental code
415          if ( env_node ){
416             env_node->setDoubleValue( "atmosphere/altitude-troposphere-top", r_tropo - r_earth );
417             env_node->setDoubleValue( "atmosphere/altitude-half-to-sun", alt_half );
418       }
419     }
420
421     return true;
422 }
423
424 SGVec4f
425 SGSun::get_color()
426 {
427     return SGVec4f((*sun_cl)[0][0], (*sun_cl)[0][1], (*sun_cl)[0][2], (*sun_cl)[0][3]);
428 }