- // calculate vector to sun's position on the earth's surface
- SGVec3d rel_sunpos = sunpos - v->get_view_pos();
- // vector in cartesian coordinates from current position to the
- // postion on the earth's surface the sun is directly over
- SGVec3f to_sun = toVec3f(rel_sunpos);
- // printf( "Vector to sun = %.2f %.2f %.2f\n",
- // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
-
- // Given a vector from the view position to the point on the
- // earth's surface the sun is directly over, map into onto the
- // local plane representing "horizontal".
-
- // surface direction to go to head towards sun
- SGVec3f surface_to_sun;
- SGVec3f view_pos = toVec3f(v->get_view_pos());
- surface_to_sun = map_vec_onto_cur_surface_plane(world_up, view_pos, to_sun);
- surface_to_sun = normalize(surface_to_sun);
- // cout << "(sg) Surface direction to sun is "
- // << surface_to_sun[0] << ","
- // << surface_to_sun[1] << ","
- // << surface_to_sun[2] << endl;
- // cout << "Should be close to zero = "
- // << sgScalarProductVec3(nup, surface_to_sun) << endl;
-
- // calculate the angle between surface_to_sun and
- // v->get_surface_east(). We do this so we can sort out the
- // acos() ambiguity. I wish I could think of a more efficient
- // way. :-(
- SGVec3f surface_east(toVec3f(hlOr.backTransform(SGVec3d::e2())));
- float east_dot = dot( surface_to_sun, surface_east );
- // cout << " East dot product = " << east_dot << endl;
-
- // calculate the angle between v->surface_to_sun and
- // v->surface_south. this is how much we have to rotate the sky
- // for it to align with the sun
- SGVec3f surface_south(toVec3f(hlOr.backTransform(-SGVec3d::e1())));
- float dot_ = dot( surface_to_sun, surface_south );
- // cout << " Dot product = " << dot << endl;
-
- if (dot_ > 1.0) {
- SG_LOG( SG_ASTRO, SG_INFO,
- "Dot product = " << dot_ << " is greater than 1.0" );
- dot_ = 1.0;
- }
- else if (dot_ < -1.0) {
- SG_LOG( SG_ASTRO, SG_INFO,
- "Dot product = " << dot_ << " is less than -1.0" );
- dot_ = -1.0;
- }
-
- if ( east_dot >= 0 ) {
- set_sun_rotation( acos(dot_) );
- } else {
- set_sun_rotation( -acos(dot_) );
- }
- // cout << " Sky needs to rotate = " << angle << " rads = "
- // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;