From 3786b21e1574dd546c0fe15860c935603cb2fdb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Thu, 26 Dec 2013 17:30:22 +0100 Subject: [PATCH] Added stars to night sky --- src/definition/AtmosphereDefinition.cpp | 48 +++++++++++++++++++++++++ src/definition/AtmosphereDefinition.h | 12 +++++++ src/render/software/NightSky.cpp | 20 +++++++++++ 3 files changed, 80 insertions(+) diff --git a/src/definition/AtmosphereDefinition.cpp b/src/definition/AtmosphereDefinition.cpp index 50c22d9..d1fd3c5 100644 --- a/src/definition/AtmosphereDefinition.cpp +++ b/src/definition/AtmosphereDefinition.cpp @@ -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); + } +} diff --git a/src/definition/AtmosphereDefinition.h b/src/definition/AtmosphereDefinition.h index 19d1a0b..39d4838 100644 --- a/src/definition/AtmosphereDefinition.h +++ b/src/definition/AtmosphereDefinition.h @@ -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 stars; }; } diff --git a/src/render/software/NightSky.cpp b/src/render/software/NightSky.cpp index dc6cd3e..1ba1a7b 100644 --- a/src/render/software/NightSky.cpp +++ b/src/render/software/NightSky.cpp @@ -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};