]> git.mxchange.org Git - flightgear.git/blob - src/Time/tmp.cxx
Sync. w. JSBSim CVS
[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/SGMath.hxx>
29 #include <simgear/math/vector.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/timing/sg_time.hxx>
32
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/viewer.hxx>
36 #include <Scenery/scenery.hxx>
37
38 #include "light.hxx"
39 #include "sunsolver.hxx"
40 #include "tmp.hxx"
41
42
43 // periodic time updater wrapper
44 void fgUpdateLocalTime() {
45     static const SGPropertyNode *longitude
46         = fgGetNode("/position/longitude-deg");
47     static const SGPropertyNode *latitude
48         = fgGetNode("/position/latitude-deg");
49
50     SGPath zone( globals->get_fg_root() );
51     zone.append( "Timezone" );
52
53     SG_LOG(SG_GENERAL, SG_INFO, "updateLocal("
54            << longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
55            << ", "
56            << latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
57            << ", " << zone.str() << ")");
58     globals->get_time_params()->updateLocal( longitude->getDoubleValue()
59                                                * SGD_DEGREES_TO_RADIANS, 
60                                              latitude->getDoubleValue()
61                                                * SGD_DEGREES_TO_RADIANS,
62                                              zone.str() );
63 }
64
65
66 // update the cur_time_params structure with the current sun position
67 void fgUpdateSunPos( void ) {
68     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
69     SGTime *t = globals->get_time_params();
70     FGViewer *v = globals->get_current_view();
71
72     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
73     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t->getGst() );
74
75     double sun_l;
76     double sun_gd_lat;
77     fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat);
78     l->set_sun_lon(sun_l);
79     l->set_sun_lat(sun_gd_lat);
80     l->set_sunpos(SGVec3d::fromGeod(SGGeod::fromRad(sun_l, sun_gd_lat)));
81
82     SG_LOG( SG_EVENT, SG_DEBUG, "    t->cur_time = " << t->get_cur_time() );
83     SG_LOG( SG_EVENT, SG_DEBUG,
84             "    Sun Geodetic lat = " << sun_gd_lat
85             << " Geodetic lat = " << sun_gd_lat );
86
87     // update the sun light vector
88     l->sun_vec() = SGVec4f(toVec3f(normalize(l->get_sunpos())), 0);
89     l->sun_vec_inv() = - l->sun_vec();
90
91     // calculate the sun's relative angle to local up
92     SGVec3d viewPos = v->get_view_pos();
93     SGQuatd hlOr = SGQuatd::fromLonLat(SGGeod::fromCart(viewPos));
94     SGVec3f nup(toVec3f(hlOr.backTransform(-SGVec3d::e3())));
95
96     SGVec3f nsun(toVec3f(normalize(l->get_sunpos())));
97     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
98     //      << nup[2] << endl;
99     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
100     //      << nsun[2] << endl;
101
102     l->set_sun_angle( acos( dot ( nup, nsun ) ) );
103     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
104             << l->get_sun_angle() );
105     
106     // calculate vector to sun's position on the earth's surface
107     SGVec3d rel_sunpos = l->get_sunpos() - v->get_view_pos();
108     // vector in cartesian coordinates from current position to the
109     // postion on the earth's surface the sun is directly over
110     SGVec3f to_sun = toVec3f(rel_sunpos);
111     // printf( "Vector to sun = %.2f %.2f %.2f\n",
112     //         v->to_sun[0], v->to_sun[1], v->to_sun[2]);
113
114     // Given a vector from the view position to the point on the
115     // earth's surface the sun is directly over, map into onto the
116     // local plane representing "horizontal".
117
118     SGVec3f world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
119     SGVec3f view_pos = toVec3f(v->get_view_pos());
120     // surface direction to go to head towards sun
121     SGVec3f surface_to_sun;
122     sgmap_vec_onto_cur_surface_plane( world_up.data(), view_pos.data(),
123                                       to_sun.data(), surface_to_sun.data() );
124     surface_to_sun = normalize(surface_to_sun);
125     // cout << "(sg) Surface direction to sun is "
126     //   << surface_to_sun[0] << "," 
127     //   << surface_to_sun[1] << ","
128     //   << surface_to_sun[2] << endl;
129     // cout << "Should be close to zero = " 
130     //   << sgScalarProductVec3(nup, surface_to_sun) << endl;
131
132     // calculate the angle between surface_to_sun and
133     // v->get_surface_east().  We do this so we can sort out the
134     // acos() ambiguity.  I wish I could think of a more efficient
135     // way. :-(
136     SGVec3f surface_east(toVec3f(hlOr.backTransform(SGVec3d::e2())));
137     float east_dot = dot( surface_to_sun, surface_east );
138     // cout << "  East dot product = " << east_dot << endl;
139
140     // calculate the angle between v->surface_to_sun and
141     // v->surface_south.  this is how much we have to rotate the sky
142     // for it to align with the sun
143     SGVec3f surface_south(toVec3f(hlOr.backTransform(-SGVec3d::e1())));
144     float dot_ = dot( surface_to_sun, surface_south );
145     // cout << "  Dot product = " << dot << endl;
146
147     if (dot_ > 1.0) {
148         SG_LOG( SG_ASTRO, SG_INFO,
149                 "Dot product  = " << dot_ << " is greater than 1.0" );
150         dot_ = 1.0;
151     }
152     else if (dot_ < -1.0) {
153          SG_LOG( SG_ASTRO, SG_INFO,
154                  "Dot product  = " << dot_ << " is less than -1.0" );
155          dot_ = -1.0;
156      }
157
158     if ( east_dot >= 0 ) {
159         l->set_sun_rotation( acos(dot_) );
160     } else {
161         l->set_sun_rotation( -acos(dot_) );
162     }
163     // cout << "  Sky needs to rotate = " << angle << " rads = "
164     //      << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
165 }
166