2013-12-26 14:36:15 +00:00
|
|
|
#include "NightSky.h"
|
|
|
|
|
|
|
|
#include "Color.h"
|
|
|
|
#include "Vector3.h"
|
|
|
|
#include "Geometry.h"
|
2013-12-26 15:35:29 +00:00
|
|
|
#include "SoftwareRenderer.h"
|
2013-12-26 15:55:37 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "AtmosphereDefinition.h"
|
2013-12-26 15:35:29 +00:00
|
|
|
#include "SurfaceMaterial.h"
|
|
|
|
|
|
|
|
#define WORLD_SCALING 0.05
|
|
|
|
#define MOON_DISTANCE 384403.0
|
|
|
|
#define MOON_DISTANCE_SCALED (MOON_DISTANCE / WORLD_SCALING)
|
|
|
|
#define MOON_RADIUS 1737.4
|
|
|
|
#define MOON_RADIUS_SCALED (MOON_RADIUS / WORLD_SCALING)
|
2013-12-26 14:36:15 +00:00
|
|
|
|
|
|
|
NightSky::NightSky(SoftwareRenderer* renderer):
|
|
|
|
renderer(renderer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NightSky::~NightSky()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void NightSky::update()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color NightSky::getColor(double altitude, const Vector3 &direction)
|
|
|
|
{
|
2013-12-26 15:55:37 +00:00
|
|
|
AtmosphereDefinition* atmosphere = renderer->getScenery()->getAtmosphere();
|
2013-12-26 14:36:15 +00:00
|
|
|
Color result(0.01, 0.012, 0.03);
|
|
|
|
|
|
|
|
Vector3 location(0.0, altitude, 0.0);
|
|
|
|
|
|
|
|
// Get stars
|
|
|
|
|
|
|
|
// Get moon
|
2013-12-26 15:55:37 +00:00
|
|
|
VectorSpherical moon_location_s = {MOON_DISTANCE_SCALED, atmosphere->moon_theta, -atmosphere->moon_phi};
|
|
|
|
Vector3 moon_position(moon_location_s);
|
|
|
|
Vector3 moon_direction = moon_position.normalize();
|
2013-12-26 14:36:15 +00:00
|
|
|
if (moon_direction.dotProduct(direction) >= 0)
|
|
|
|
{
|
2013-12-26 15:55:37 +00:00
|
|
|
double moon_radius = MOON_RADIUS_SCALED * 5.0 * atmosphere->moon_radius;
|
2013-12-26 14:36:15 +00:00
|
|
|
Vector3 hit1, hit2;
|
|
|
|
int hits = Geometry::rayIntersectSphere(location, direction, moon_position, moon_radius, &hit1, &hit2);
|
|
|
|
if (hits > 1)
|
|
|
|
{
|
|
|
|
double dist = hit2.sub(hit1).getNorm() / moon_radius; // distance between intersection points (relative to radius)
|
|
|
|
|
2013-12-26 15:35:29 +00:00
|
|
|
Vector3 nearest = (hit1.sub(location).getNorm() > hit2.sub(location).getNorm()) ? hit2 : hit1;
|
|
|
|
SurfaceMaterial moon_material(Color(3.0, 3.0, 3.0, 1.0));
|
|
|
|
moon_material.validate();
|
|
|
|
|
|
|
|
Color moon_color = renderer->applyLightingToSurface(nearest, nearest.sub(moon_position).normalize(), moon_material);
|
2013-12-26 14:36:15 +00:00
|
|
|
if (dist <= 0.05)
|
|
|
|
{
|
|
|
|
moon_color.a *= 1.0 - dist / 0.05;
|
|
|
|
}
|
|
|
|
result.mask(moon_color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|