Added stars to night sky

This commit is contained in:
Michaël Lemaire 2013-12-26 17:30:22 +01:00 committed by Michael Lemaire
parent c651b436ab
commit 3786b21e15
3 changed files with 80 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#include "AtmosphereDefinition.h"
#include "PackStream.h"
#include "RandomGenerator.h"
AtmosphereDefinition::AtmosphereDefinition(BaseDefinition* parent):
BaseDefinition(parent)
@ -23,6 +24,15 @@ void AtmosphereDefinition::save(PackStream* stream) const
stream->write(&moon_radius);
stream->write(&moon_theta);
stream->write(&moon_phi);
int star_count = stars.size();
stream->write(&star_count);
for (const auto &star : stars)
{
star.location.save(stream);
star.col.save(stream);
stream->write(&star.radius);
}
}
void AtmosphereDefinition::load(PackStream* stream)
@ -38,6 +48,19 @@ void AtmosphereDefinition::load(PackStream* stream)
stream->read(&moon_theta);
stream->read(&moon_phi);
int star_count;
stream->read(&star_count);
for (int i = 0; i < star_count; i++)
{
Star star;
star.location.load(stream);
star.col.load(stream);
stream->read(&star.radius);
stars.push_back(star);
}
validate();
}
@ -55,6 +78,7 @@ void AtmosphereDefinition::copy(BaseDefinition* _destination) const
destination->moon_radius = moon_radius;
destination->moon_theta = moon_theta;
destination->moon_phi = moon_phi;
destination->stars = stars;
destination->validate();
}
@ -130,5 +154,29 @@ void AtmosphereDefinition::applyPreset(AtmospherePreset preset)
;
}
generateStars(5000);
validate();
}
void AtmosphereDefinition::generateStars(int count)
{
stars.clear();
for (int i = 0; i < count; ++i)
{
Star star;
star.location = Vector3((RandomGenerator::random() - 0.5) * 100000.0, (RandomGenerator::random() - 0.5) * 100000.0, (RandomGenerator::random() - 0.5) * 100000.0);
if (star.location.getNorm() < 30000.0)
{
i--;
continue;
}
double brillance = RandomGenerator::random() * 0.05 + 0.1;
star.col = Color(brillance + RandomGenerator::random() * 0.03, brillance + RandomGenerator::random() * 0.03, brillance + RandomGenerator::random() * 0.03, 1.0);
star.radius = 30.0 + RandomGenerator::random() * 20.0;
stars.push_back(star);
}
}

View file

@ -5,6 +5,7 @@
#include "BaseDefinition.h"
#include "Vector3.h"
#include "Color.h"
namespace paysages {
@ -12,6 +13,14 @@ namespace definition {
class AtmosphereDefinition : public BaseDefinition
{
public:
typedef struct
{
Vector3 location;
double radius;
Color col;
} Star;
public:
typedef enum
{
@ -38,6 +47,7 @@ public:
virtual void validate() override;
void applyPreset(AtmospherePreset preset);
void generateStars(int count);
public:
AtmosphereModel model;
@ -53,6 +63,8 @@ public:
double moon_phi;
double _daytime;
std::vector<Star> stars;
};
}

View file

@ -35,6 +35,26 @@ const Color NightSky::getColor(double altitude, const Vector3 &direction)
Vector3 location(0.0, altitude, 0.0);
// Get stars
for (const auto &star: atmosphere->stars)
{
if (star.location.dotProduct(direction) >= 0)
{
double radius = star.radius;
Vector3 hit1, hit2;
int hits = Geometry::rayIntersectSphere(location, direction, star.location, radius, &hit1, &hit2);
if (hits > 1)
{
double dist = hit2.sub(hit1).getNorm() / radius; // distance between intersection points (relative to radius)
Color color = star.col;
if (dist <= 0.5)
{
color.a *= 1.0 - dist / 0.5;
}
result.mask(color);
}
}
}
// Get moon
VectorSpherical moon_location_s = {MOON_DISTANCE_SCALED, atmosphere->moon_theta, -atmosphere->moon_phi};