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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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>
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/viewer.hxx>
36 #include <Scenery/scenery.hxx>
39 #include "sunsolver.hxx"
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");
50 SGPath zone( globals->get_fg_root() );
51 zone.append( "Timezone" );
53 SG_LOG(SG_GENERAL, SG_INFO, "updateLocal("
54 << longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS
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,
66 // update the cur_time_params structure with the current sun position
67 void fgUpdateSunPos( void ) {
69 // This only works at lat,lon = 0,0
70 // need to find a way to get it working at other locations
72 FGLight *light = (FGLight *)(globals->get_subsystem("lighting"));
73 FGViewer *viewer = globals->get_current_view();
74 SGTime *time_now = globals->get_time_params();
76 SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
77 SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << time_now->getGst() );
81 fgSunPositionGST(time_now->getGst(), &sun_lon, &sun_gd_lat);
82 light->set_sun_lon(sun_lon);
83 light->set_sun_lat(sun_gd_lat);
85 // update the sun light vector
86 // calculations are in the horizontal normal plane:
87 // x-north, y-east, z-down
89 SGGeod geodViewPos = SGGeod::fromCart(viewer->getViewPosition());
90 SGGeod geodSunPos = SGGeod::fromRad(sun_lon, sun_gd_lat);
92 //static SGQuatd q = SGQuatd::fromLonLat(SGGeod::fromRad(0,0));
93 SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos);
94 SGQuatd sunOr = SGQuatd::fromLonLat(geodSunPos);
96 SGVec3d sunDirection = (hlOr*sunOr).transform(SGVec3d::e3());
97 light->set_sun_rotation( acos(sunDirection[1]) - SGD_PI_2 );
98 light->set_sun_angle( acos(-sunDirection[2]) );
100 SGVec3d sunPos = SGVec3d::fromGeod(geodSunPos);
101 light->sun_vec() = SGVec4f(toVec3f(normalize(sunPos)), 0);
102 light->sun_vec_inv() = -light->sun_vec();
105 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
106 SGTime *t = globals->get_time_params();
107 FGViewer *v = globals->get_current_view();
109 SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
110 SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t->getGst() );
114 fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat);
115 l->set_sun_lon(sun_l);
116 l->set_sun_lat(sun_gd_lat);
117 SGVec3d sunpos(SGVec3d::fromGeod(SGGeod::fromRad(sun_l, sun_gd_lat)));
119 SG_LOG( SG_EVENT, SG_DEBUG, " t->cur_time = " << t->get_cur_time() );
120 SG_LOG( SG_EVENT, SG_DEBUG,
121 " Sun Geodetic lat = " << sun_gd_lat
122 << " Geodetic lat = " << sun_gd_lat );
124 // update the sun light vector
125 l->sun_vec() = SGVec4f(toVec3f(normalize(sunpos)), 0);
126 l->sun_vec_inv() = - l->sun_vec();
128 // calculate the sun's relative angle to local up
129 SGVec3d viewPos = v->get_view_pos();
130 SGQuatd hlOr = SGQuatd::fromLonLat(SGGeod::fromCart(viewPos));
131 SGVec3f nup(toVec3f(hlOr.backTransform(-SGVec3d::e3())));
133 SGVec3f nsun(toVec3f(normalize(sunpos)));
134 // cout << "nup = " << nup[0] << "," << nup[1] << ","
135 // << nup[2] << endl;
136 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
137 // << nsun[2] << endl;
139 l->set_sun_angle( acos( dot ( nup, nsun ) ) );
140 SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
141 << l->get_sun_angle() );
143 // calculate vector to sun's position on the earth's surface
144 SGVec3d rel_sunpos = sunpos - v->get_view_pos();
145 // vector in cartesian coordinates from current position to the
146 // postion on the earth's surface the sun is directly over
147 SGVec3f to_sun = toVec3f(rel_sunpos);
148 // printf( "Vector to sun = %.2f %.2f %.2f\n",
149 // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
151 // Given a vector from the view position to the point on the
152 // earth's surface the sun is directly over, map into onto the
153 // local plane representing "horizontal".
155 SGVec3f world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
156 SGVec3f view_pos = toVec3f(v->get_view_pos());
157 // surface direction to go to head towards sun
158 SGVec3f surface_to_sun;
159 sgmap_vec_onto_cur_surface_plane( world_up.data(), view_pos.data(),
160 to_sun.data(), surface_to_sun.data() );
161 surface_to_sun = normalize(surface_to_sun);
162 // cout << "(sg) Surface direction to sun is "
163 // << surface_to_sun[0] << ","
164 // << surface_to_sun[1] << ","
165 // << surface_to_sun[2] << endl;
166 // cout << "Should be close to zero = "
167 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
169 // calculate the angle between surface_to_sun and
170 // v->get_surface_east(). We do this so we can sort out the
171 // acos() ambiguity. I wish I could think of a more efficient
173 SGVec3f surface_east(toVec3f(hlOr.backTransform(SGVec3d::e2())));
174 float east_dot = dot( surface_to_sun, surface_east );
175 // cout << " East dot product = " << east_dot << endl;
177 // calculate the angle between v->surface_to_sun and
178 // v->surface_south. this is how much we have to rotate the sky
179 // for it to align with the sun
180 SGVec3f surface_south(toVec3f(hlOr.backTransform(-SGVec3d::e1())));
181 float dot_ = dot( surface_to_sun, surface_south );
182 // cout << " Dot product = " << dot << endl;
185 SG_LOG( SG_ASTRO, SG_INFO,
186 "Dot product = " << dot_ << " is greater than 1.0" );
189 else if (dot_ < -1.0) {
190 SG_LOG( SG_ASTRO, SG_INFO,
191 "Dot product = " << dot_ << " is less than -1.0" );
195 if ( east_dot >= 0 ) {
196 l->set_sun_rotation( acos(dot_) );
198 l->set_sun_rotation( -acos(dot_) );
200 // cout << " Sky needs to rotate = " << angle << " rads = "
201 // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;