paysages3d/src/definition/AtmosphereDefinition.cpp

170 lines
4.5 KiB
C++
Raw Normal View History

2013-11-15 23:27:40 +00:00
#include "AtmosphereDefinition.h"
#include <cmath>
2013-11-15 23:27:40 +00:00
#include "PackStream.h"
2013-12-26 16:30:22 +00:00
#include "RandomGenerator.h"
#include "FloatNode.h"
#include "GodRaysDefinition.h"
2013-11-15 23:27:40 +00:00
AtmosphereDefinition::AtmosphereDefinition(DefinitionNode *parent)
: DefinitionNode(parent, "atmosphere", "atmosphere") {
model = ATMOSPHERE_MODEL_DISABLED;
godrays = new GodRaysDefinition(this);
daytime = new FloatNode(this, "daytime");
2015-08-23 23:19:19 +00:00
humidity = new FloatNode(this, "humidity");
sun_radius = new FloatNode(this, "sun_radius");
2013-11-15 23:27:40 +00:00
}
AtmosphereDefinition::~AtmosphereDefinition() {
2013-11-15 23:27:40 +00:00
}
void AtmosphereDefinition::save(PackStream *stream) const {
2015-08-19 20:07:44 +00:00
DefinitionNode::save(stream);
stream->write((int *)&model);
2013-11-15 23:27:40 +00:00
sun_color.save(stream);
stream->write(&dome_lighting);
2013-12-26 15:55:37 +00:00
stream->write(&moon_radius);
stream->write(&moon_theta);
stream->write(&moon_phi);
2013-12-26 16:30:22 +00:00
int star_count = stars.size();
stream->write(&star_count);
for (const auto &star : stars) {
2013-12-26 16:30:22 +00:00
star.location.save(stream);
star.col.save(stream);
stream->write(&star.radius);
}
2013-11-15 23:27:40 +00:00
}
void AtmosphereDefinition::load(PackStream *stream) {
2015-08-19 20:07:44 +00:00
DefinitionNode::load(stream);
stream->read((int *)&model);
2013-11-15 23:27:40 +00:00
sun_color.load(stream);
stream->read(&dome_lighting);
2013-12-26 15:55:37 +00:00
stream->read(&moon_radius);
stream->read(&moon_theta);
stream->read(&moon_phi);
2013-11-15 23:27:40 +00:00
2013-12-26 16:30:22 +00:00
int star_count;
stream->read(&star_count);
for (int i = 0; i < star_count; i++) {
2013-12-26 16:30:22 +00:00
Star star;
star.location.load(stream);
star.col.load(stream);
stream->read(&star.radius);
stars.push_back(star);
}
2013-11-15 23:27:40 +00:00
validate();
}
void AtmosphereDefinition::copy(DefinitionNode *_destination) const {
2015-08-19 20:07:44 +00:00
DefinitionNode::copy(_destination);
AtmosphereDefinition *destination = (AtmosphereDefinition *)_destination;
2013-11-15 23:27:40 +00:00
destination->model = model;
destination->sun_color = sun_color;
destination->dome_lighting = dome_lighting;
2013-12-26 15:55:37 +00:00
destination->moon_radius = moon_radius;
destination->moon_theta = moon_theta;
destination->moon_phi = moon_phi;
2013-12-26 16:30:22 +00:00
destination->stars = stars;
2013-11-15 23:27:40 +00:00
destination->validate();
}
void AtmosphereDefinition::setDayTime(double value) {
daytime->setValue(value);
}
2013-11-15 23:27:40 +00:00
void AtmosphereDefinition::setDayTime(int hour, int minute, int second) {
setDayTime((double)hour / 24.0 + (double)minute / 1440.0 + (double)second / 86400.0);
2013-11-15 23:27:40 +00:00
}
void AtmosphereDefinition::getHMS(int *hour, int *minute, int *second) const {
double value = daytime->getValue();
if (value >= 0.0) {
2014-08-28 13:09:47 +00:00
value = fmod(value, 1.0);
} else {
2014-08-28 13:09:47 +00:00
value = 1.0 - fmod(-value, 1.0);
}
value *= 86400.0;
*hour = value / 3600.0;
value -= 3600.0 * *hour;
*minute = value / 60.0;
*second = value - *minute * 60.0;
2014-08-28 13:09:47 +00:00
}
void AtmosphereDefinition::applyPreset(AtmospherePreset preset, RandomGenerator &random) {
2013-11-15 23:27:40 +00:00
sun_color.r = 1.0;
sun_color.g = 0.95;
sun_color.b = 0.9;
sun_color.a = 1.0;
sun_radius->setValue(0.7);
2013-12-26 15:55:37 +00:00
moon_radius = 1.0;
moon_theta = 0.3;
moon_phi = 0.5;
2013-11-15 23:27:40 +00:00
model = ATMOSPHERE_MODEL_BRUNETON;
switch (preset) {
case ATMOSPHERE_PRESET_CLEAR_DAY:
setDayTime(15);
humidity->setValue(0.1);
dome_lighting = 0.2;
break;
case ATMOSPHERE_PRESET_CLEAR_SUNSET:
setDayTime(17, 45);
humidity->setValue(0.1);
dome_lighting = 0.3;
break;
case ATMOSPHERE_PRESET_HAZY_MORNING:
setDayTime(8, 30);
humidity->setValue(0.4);
dome_lighting = 0.25;
break;
case ATMOSPHERE_PRESET_FOGGY:
setDayTime(15);
humidity->setValue(0.7);
dome_lighting = 0.1;
break;
case ATMOSPHERE_PRESET_STORMY:
setDayTime(15);
humidity->setValue(0.9);
dome_lighting = 0.05;
break;
default:
;
2013-11-15 23:27:40 +00:00
}
generateStars(2000, random);
2013-12-26 16:30:22 +00:00
2013-11-15 23:27:40 +00:00
validate();
}
2013-12-26 16:30:22 +00:00
void AtmosphereDefinition::generateStars(int count, RandomGenerator &random) {
2013-12-26 16:30:22 +00:00
stars.clear();
for (int i = 0; i < count; ++i) {
2013-12-26 16:30:22 +00:00
Star star;
star.location = Vector3((random.genDouble() - 0.5) * 100000.0, (random.genDouble() * 0.5) * 100000.0,
(random.genDouble() - 0.5) * 100000.0);
if (star.location.getNorm() < 30000.0) {
2013-12-26 16:30:22 +00:00
i--;
continue;
}
double brillance = random.genDouble() * 0.05 + 0.1;
star.col = Color(brillance + random.genDouble() * 0.03, brillance + random.genDouble() * 0.03,
brillance + random.genDouble() * 0.03, 1.0);
star.radius = 30.0 + random.genDouble() * 20.0;
2013-12-26 16:30:22 +00:00
stars.push_back(star);
}
}