]> git.mxchange.org Git - flightgear.git/blob - src/Time/tmp.cxx
Modified Files:
[flightgear.git] / src / Time / tmp.cxx
1 // tmp.cxx -- stuff I don't know what to do with at the moment
2 //
3 // Written by Curtis Olson, started July 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
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 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/math/vector.hxx>
29 #include <simgear/math/polar3d.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/timing/sg_time.hxx>
34
35 #include <FDM/flight.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38 #include <Main/viewer.hxx>
39 #include <Scenery/scenery.hxx>
40
41 #include "light.hxx"
42 #include "sunsolver.hxx"
43 #include "tmp.hxx"
44
45
46 // periodic time updater wrapper
47 void fgUpdateLocalTime() {
48     static const SGPropertyNode *longitude
49         = fgGetNode("/position/longitude-deg");
50     static const SGPropertyNode *latitude
51         = fgGetNode("/position/latitude-deg");
52
53     SGPath zone( globals->get_fg_root() );
54     zone.append( "Timezone" );
55
56     SG_LOG(SG_GENERAL, SG_INFO, "updateLocal("
57            << longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
58            << ", "
59            << latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
60            << ", " << zone.str() << ")");
61     globals->get_time_params()->updateLocal( longitude->getDoubleValue()
62                                                * SGD_DEGREES_TO_RADIANS, 
63                                              latitude->getDoubleValue()
64                                                * SGD_DEGREES_TO_RADIANS,
65                                              zone.str() );
66 }
67
68
69 // update the cur_time_params structure with the current sun position
70 void fgUpdateSunPos( void ) {
71     sgVec3 nup, nsun;
72     double dot, east_dot;
73     double sun_gd_lat, sl_radius;
74
75     // vector in cartesian coordinates from current position to the
76     // postion on the earth's surface the sun is directly over
77     sgVec3 to_sun;
78
79     // surface direction to go to head towards sun
80     sgVec3 surface_to_sun;
81
82     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
83     SGTime *t = globals->get_time_params();
84     FGViewer *v = globals->get_current_view();
85
86     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
87     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t->getGst() );
88
89     double sun_l;
90     fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat);
91     l->set_sun_lon(sun_l);
92
93     sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &sun_l);
94     l->set_sun_gc_lat(sun_l);
95
96     SGGeoc geocSun = SGGeoc::fromRadM( l->get_sun_lon(), l->get_sun_gc_lat(),
97                                        sl_radius );
98     l->set_sunpos( SGVec3d::fromGeoc(geocSun) );
99
100     SG_LOG( SG_EVENT, SG_DEBUG, "    t->cur_time = " << t->get_cur_time() );
101     SG_LOG( SG_EVENT, SG_DEBUG,
102             "    Sun Geodetic lat = " << sun_gd_lat
103             << " Geocentric lat = " << l->get_sun_gc_lat() );
104
105     // update the sun light vector
106     sgSetVec4( l->sun_vec().data(), l->get_sunpos().x(),
107                l->get_sunpos().y(), l->get_sunpos().z(), 0.0 );
108     sgNormalizeVec4( l->sun_vec().data() );
109     sgCopyVec4( l->sun_vec_inv().data(), l->sun_vec().data() );
110     sgNegateVec4( l->sun_vec_inv().data() );
111
112     // make sure these are directional light sources only
113     l->sun_vec()[3] = l->sun_vec_inv()[3] = 0.0;
114     // cout << "  l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
115     //      << ","<< l->sun_vec[2] << endl;
116
117     // calculate the sun's relative angle to local up
118     sgCopyVec3( nup, v->get_world_up().data() );
119     sgSetVec3( nsun, l->get_sunpos().x(),
120                l->get_sunpos().y(), l->get_sunpos().z() );
121     sgNormalizeVec3(nup);
122     sgNormalizeVec3(nsun);
123     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
124     //      << nup[2] << endl;
125     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
126     //      << nsun[2] << endl;
127
128     l->set_sun_angle( acos( sgScalarProductVec3 ( nup, nsun ) ) );
129     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
130             << l->get_sun_angle() );
131     
132     // calculate vector to sun's position on the earth's surface
133     SGVec3d rel_sunpos = globals->get_scenery()->get_center();
134     SGVec3f vp( v->get_view_pos() );
135     rel_sunpos += l->get_sunpos() - toVec3d(vp);
136     sgSetVec3( to_sun, rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
137     // printf( "Vector to sun = %.2f %.2f %.2f\n",
138     //         v->to_sun[0], v->to_sun[1], v->to_sun[2]);
139
140     // Given a vector from the view position to the point on the
141     // earth's surface the sun is directly over, map into onto the
142     // local plane representing "horizontal".
143
144     SGVec3f world_up = v->get_world_up();
145     SGVec3f view_pos = v->get_view_pos();
146     sgmap_vec_onto_cur_surface_plane( world_up.data(), view_pos.data(),
147                                       to_sun, surface_to_sun );
148     sgNormalizeVec3(surface_to_sun);
149     // cout << "(sg) Surface direction to sun is "
150     //   << surface_to_sun[0] << "," 
151     //   << surface_to_sun[1] << ","
152     //   << surface_to_sun[2] << endl;
153     // cout << "Should be close to zero = " 
154     //   << sgScalarProductVec3(nup, surface_to_sun) << endl;
155
156     // calculate the angle between surface_to_sun and
157     // v->get_surface_east().  We do this so we can sort out the
158     // acos() ambiguity.  I wish I could think of a more efficient
159     // way. :-(
160     east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east().data() );
161     // cout << "  East dot product = " << east_dot << endl;
162
163     // calculate the angle between v->surface_to_sun and
164     // v->surface_south.  this is how much we have to rotate the sky
165     // for it to align with the sun
166     dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south().data() );
167     // cout << "  Dot product = " << dot << endl;
168
169     if (dot > 1.0) {
170         SG_LOG( SG_ASTRO, SG_INFO,
171                 "Dot product  = " << dot << " is greater than 1.0" );
172         dot = 1.0;
173     }
174     else if (dot < -1.0) {
175          SG_LOG( SG_ASTRO, SG_INFO,
176                  "Dot product  = " << dot << " is less than -1.0" );
177          dot = -1.0;
178      }
179
180     if ( east_dot >= 0 ) {
181         l->set_sun_rotation( acos(dot) );
182     } else {
183         l->set_sun_rotation( -acos(dot) );
184     }
185     // cout << "  Sky needs to rotate = " << angle << " rads = "
186     //      << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
187 }
188