1 // tmp.cxx -- stuff I don't know what to do with at the moment
3 // Written by Curtis Olson, started July 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
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>
42 #include "sunsolver.hxx"
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");
53 SGPath zone( globals->get_fg_root() );
54 zone.append( "Timezone" );
56 SG_LOG(SG_GENERAL, SG_INFO, "updateLocal("
57 << longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
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,
69 // update the cur_time_params structure with the current sun position
70 void fgUpdateSunPos( void ) {
74 double sun_gd_lat, sl_radius;
76 // vector in cartesian coordinates from current position to the
77 // postion on the earth's surface the sun is directly over
80 // surface direction to go to head towards sun
81 sgVec3 surface_to_sun;
83 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
84 SGTime *t = globals->get_time_params();
85 FGViewer *v = globals->get_current_view();
87 SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
88 SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t->getGst() );
91 fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat);
92 l->set_sun_lon(sun_l);
94 sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &sun_l);
95 l->set_sun_gc_lat(sun_l);
97 Point3D p = Point3D( l->get_sun_lon(), l->get_sun_gc_lat(), sl_radius );
98 l->set_sunpos( sgPolarToCart3d(p) );
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() );
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() );
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;
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;
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() );
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]);
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".
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;
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
159 east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
160 // cout << " East dot product = " << east_dot << endl;
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;
169 SG_LOG( SG_ASTRO, SG_INFO,
170 "Dot product = " << dot << " is greater than 1.0" );
173 else if (dot < -1.0) {
174 SG_LOG( SG_ASTRO, SG_INFO,
175 "Dot product = " << dot << " is less than -1.0" );
179 if ( east_dot >= 0 ) {
180 l->set_sun_rotation( acos(dot) );
182 l->set_sun_rotation( -acos(dot) );
184 // cout << " Sky needs to rotate = " << angle << " rads = "
185 // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;