]> git.mxchange.org Git - flightgear.git/blob - src/Time/tmp.cxx
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[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     Point3D rel_sunpos;
73     double dot, east_dot;
74     double sun_gd_lat, sl_radius;
75
76     // vector in cartesian coordinates from current position to the
77     // postion on the earth's surface the sun is directly over
78     sgVec3 to_sun;
79
80     // surface direction to go to head towards sun
81     sgVec3 surface_to_sun;
82
83     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
84     SGTime *t = globals->get_time_params();
85     FGViewer *v = globals->get_current_view();
86
87     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
88     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t->getGst() );
89
90     double sun_l;
91     fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat);
92     l->set_sun_lon(sun_l);
93
94     sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &sun_l);
95     l->set_sun_gc_lat(sun_l);
96
97     Point3D p = Point3D( l->get_sun_lon(), l->get_sun_gc_lat(), sl_radius );
98     l->set_sunpos( sgPolarToCart3d(p) );
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(), l->get_sunpos().x(),
107                l->get_sunpos().y(), l->get_sunpos().z(), 0.0 );
108     sgNormalizeVec4( l->sun_vec() );
109     sgCopyVec4( l->sun_vec_inv(), l->sun_vec() );
110     sgNegateVec4( l->sun_vec_inv() );
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() );
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     Point3D vp( v->get_view_pos()[0],
134                 v->get_view_pos()[1],
135                 v->get_view_pos()[2] );
136     rel_sunpos = l->get_sunpos() - (vp + globals->get_scenery()->get_center());
137     sgSetVec3( to_sun, rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
138     // printf( "Vector to sun = %.2f %.2f %.2f\n",
139     //         v->to_sun[0], v->to_sun[1], v->to_sun[2]);
140
141     // Given a vector from the view position to the point on the
142     // earth's surface the sun is directly over, map into onto the
143     // local plane representing "horizontal".
144
145     sgmap_vec_onto_cur_surface_plane( v->get_world_up(), v->get_view_pos(),
146                                       to_sun, surface_to_sun );
147     sgNormalizeVec3(surface_to_sun);
148     // cout << "(sg) Surface direction to sun is "
149     //   << surface_to_sun[0] << "," 
150     //   << surface_to_sun[1] << ","
151     //   << surface_to_sun[2] << endl;
152     // cout << "Should be close to zero = " 
153     //   << sgScalarProductVec3(nup, surface_to_sun) << endl;
154
155     // calculate the angle between surface_to_sun and
156     // v->get_surface_east().  We do this so we can sort out the
157     // acos() ambiguity.  I wish I could think of a more efficient
158     // way. :-(
159     east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
160     // cout << "  East dot product = " << east_dot << endl;
161
162     // calculate the angle between v->surface_to_sun and
163     // v->surface_south.  this is how much we have to rotate the sky
164     // for it to align with the sun
165     dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
166     // cout << "  Dot product = " << dot << endl;
167
168     if (dot > 1.0) {
169         SG_LOG( SG_ASTRO, SG_INFO,
170                 "Dot product  = " << dot << " is greater than 1.0" );
171         dot = 1.0;
172     }
173     else if (dot < -1.0) {
174          SG_LOG( SG_ASTRO, SG_INFO,
175                  "Dot product  = " << dot << " is less than -1.0" );
176          dot = -1.0;
177      }
178
179     if ( east_dot >= 0 ) {
180         l->set_sun_rotation( acos(dot) );
181     } else {
182         l->set_sun_rotation( -acos(dot) );
183     }
184     // cout << "  Sky needs to rotate = " << angle << " rads = "
185     //      << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
186 }
187