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 ) {
68 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
69 SGTime *t = globals->get_time_params();
70 FGViewer *v = globals->get_current_view();
72 SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" );
73 SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t->getGst() );
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)));
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 );
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();
91 // calculate the sun's relative angle to local up
92 SGVec3f nup(normalize(v->get_world_up()));
93 SGVec3f nsun(toVec3f(normalize(l->get_sunpos())));
94 // cout << "nup = " << nup[0] << "," << nup[1] << ","
96 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
97 // << nsun[2] << endl;
99 l->set_sun_angle( acos( dot ( nup, nsun ) ) );
100 SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
101 << l->get_sun_angle() );
103 // calculate vector to sun's position on the earth's surface
104 SGVec3d rel_sunpos = l->get_sunpos() - v->get_view_pos();
105 // vector in cartesian coordinates from current position to the
106 // postion on the earth's surface the sun is directly over
107 SGVec3f to_sun = toVec3f(rel_sunpos);
108 // printf( "Vector to sun = %.2f %.2f %.2f\n",
109 // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
111 // Given a vector from the view position to the point on the
112 // earth's surface the sun is directly over, map into onto the
113 // local plane representing "horizontal".
115 SGVec3f world_up = v->get_world_up();
116 SGVec3f view_pos = toVec3f(v->get_view_pos());
117 // surface direction to go to head towards sun
118 SGVec3f surface_to_sun;
119 sgmap_vec_onto_cur_surface_plane( world_up.data(), view_pos.data(),
120 to_sun.data(), surface_to_sun.data() );
121 surface_to_sun = normalize(surface_to_sun);
122 // cout << "(sg) Surface direction to sun is "
123 // << surface_to_sun[0] << ","
124 // << surface_to_sun[1] << ","
125 // << surface_to_sun[2] << endl;
126 // cout << "Should be close to zero = "
127 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
129 // calculate the angle between surface_to_sun and
130 // v->get_surface_east(). We do this so we can sort out the
131 // acos() ambiguity. I wish I could think of a more efficient
133 float east_dot = dot( surface_to_sun, v->get_surface_east() );
134 // cout << " East dot product = " << east_dot << endl;
136 // calculate the angle between v->surface_to_sun and
137 // v->surface_south. this is how much we have to rotate the sky
138 // for it to align with the sun
139 float dot_ = dot( surface_to_sun, v->get_surface_south() );
140 // cout << " Dot product = " << dot << endl;
143 SG_LOG( SG_ASTRO, SG_INFO,
144 "Dot product = " << dot_ << " is greater than 1.0" );
147 else if (dot_ < -1.0) {
148 SG_LOG( SG_ASTRO, SG_INFO,
149 "Dot product = " << dot_ << " is less than -1.0" );
153 if ( east_dot >= 0 ) {
154 l->set_sun_rotation( acos(dot_) );
156 l->set_sun_rotation( -acos(dot_) );
158 // cout << " Sky needs to rotate = " << angle << " rads = "
159 // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;