2013-11-17 21:36:18 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
|
|
|
|
#include <ctime>
|
2013-12-10 21:32:58 +00:00
|
|
|
#include <map>
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
#include "PackStream.h"
|
|
|
|
#include "AtmosphereDefinition.h"
|
|
|
|
#include "CameraDefinition.h"
|
|
|
|
#include "CloudsDefinition.h"
|
|
|
|
#include "TerrainDefinition.h"
|
|
|
|
#include "TexturesDefinition.h"
|
|
|
|
#include "WaterDefinition.h"
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
static const double APP_HEADER = 19866544632.125;
|
|
|
|
static const int DATA_VERSION = 1;
|
|
|
|
|
2013-11-17 21:36:18 +00:00
|
|
|
Scenery::Scenery():
|
|
|
|
BaseDefinition(NULL)
|
|
|
|
{
|
|
|
|
addChild(atmosphere = new AtmosphereDefinition(this));
|
|
|
|
addChild(camera = new CameraDefinition);
|
|
|
|
addChild(clouds = new CloudsDefinition(this));
|
|
|
|
addChild(terrain = new TerrainDefinition(this));
|
|
|
|
addChild(textures = new TexturesDefinition(this));
|
|
|
|
addChild(water = new WaterDefinition(this));
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:33:46 +00:00
|
|
|
void Scenery::copy(BaseDefinition *destination_) const
|
|
|
|
{
|
|
|
|
Scenery* destination = (Scenery*)destination_;
|
|
|
|
|
|
|
|
atmosphere->copy(destination->atmosphere);
|
|
|
|
camera->copy(destination->camera);
|
|
|
|
clouds->copy(destination->clouds);
|
|
|
|
terrain->copy(destination->terrain);
|
|
|
|
textures->copy(destination->textures);
|
|
|
|
water->copy(destination->water);
|
|
|
|
|
|
|
|
destination->validate();
|
|
|
|
}
|
|
|
|
|
2013-11-17 21:36:18 +00:00
|
|
|
void Scenery::validate()
|
|
|
|
{
|
|
|
|
BaseDefinition::validate();
|
|
|
|
|
2015-07-22 16:39:46 +00:00
|
|
|
keepCameraAboveGround(camera);
|
2013-11-17 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
Scenery::FileOperationResult Scenery::saveGlobal(const std::string &filepath) const
|
|
|
|
{
|
|
|
|
PackStream stream;
|
2015-08-03 22:00:56 +00:00
|
|
|
double app_header = (double)APP_HEADER;
|
|
|
|
double version_header = (double)DATA_VERSION;
|
2013-12-10 21:32:58 +00:00
|
|
|
|
|
|
|
if (not stream.bindToFile(filepath, true))
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_IOERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.write(&app_header);
|
|
|
|
stream.write(&version_header);
|
|
|
|
|
|
|
|
save(&stream);
|
|
|
|
|
2015-08-03 22:00:56 +00:00
|
|
|
stream.write(&version_header);
|
|
|
|
stream.write(&app_header);
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
return FILE_OPERATION_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scenery::FileOperationResult Scenery::loadGlobal(const std::string &filepath)
|
|
|
|
{
|
|
|
|
PackStream stream;
|
|
|
|
double app_header, version_header;
|
|
|
|
|
|
|
|
if (not stream.bindToFile(filepath, false))
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_IOERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.read(&app_header);
|
|
|
|
if (app_header != APP_HEADER)
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_APP_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.read(&version_header);
|
|
|
|
if ((int)version_header != DATA_VERSION)
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_VERSION_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
load(&stream);
|
|
|
|
|
2015-08-03 22:00:56 +00:00
|
|
|
stream.read(&version_header);
|
|
|
|
if ((int)version_header != DATA_VERSION)
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_VERSION_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.read(&app_header);
|
|
|
|
if (app_header != APP_HEADER)
|
|
|
|
{
|
|
|
|
return FILE_OPERATION_APP_MISMATCH;
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
return FILE_OPERATION_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-17 21:36:18 +00:00
|
|
|
Scenery* Scenery::getScenery()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::autoPreset(int seed)
|
|
|
|
{
|
|
|
|
if (!seed)
|
|
|
|
{
|
|
|
|
seed = time(NULL);
|
|
|
|
}
|
|
|
|
srand(seed);
|
|
|
|
|
|
|
|
terrain->applyPreset(TerrainDefinition::TERRAIN_PRESET_STANDARD);
|
|
|
|
textures->applyPreset(TexturesDefinition::TEXTURES_PRESET_FULL);
|
|
|
|
atmosphere->applyPreset(AtmosphereDefinition::ATMOSPHERE_PRESET_CLEAR_DAY);
|
|
|
|
water->applyPreset(WaterDefinition::WATER_PRESET_LAKE);
|
|
|
|
clouds->applyPreset(CloudsDefinition::CLOUDS_PRESET_PARTLY_CLOUDY);
|
|
|
|
|
|
|
|
camera->setLocation(VECTOR_ZERO);
|
|
|
|
camera->setTarget(VECTOR_NORTH);
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
|
|
|
atmosphere->copy(this->atmosphere);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
|
|
|
this->atmosphere->copy(atmosphere);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setCamera(CameraDefinition* camera)
|
|
|
|
{
|
|
|
|
camera->copy(this->camera);
|
2015-07-22 16:39:46 +00:00
|
|
|
keepCameraAboveGround(this->camera);
|
2013-11-17 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getCamera(CameraDefinition* camera)
|
|
|
|
{
|
|
|
|
this->camera->copy(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
|
|
|
clouds->copy(this->clouds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
|
|
|
this->clouds->copy(clouds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
terrain->copy(this->terrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
this->terrain->copy(terrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
textures->copy(this->textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
this->textures->copy(textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setWater(WaterDefinition* water)
|
|
|
|
{
|
|
|
|
water->copy(this->water);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getWater(WaterDefinition* water)
|
|
|
|
{
|
|
|
|
this->water->copy(water);
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:39:46 +00:00
|
|
|
void Scenery::keepCameraAboveGround(CameraDefinition* camera)
|
2013-11-17 21:36:18 +00:00
|
|
|
{
|
|
|
|
Vector3 camera_location = camera->getLocation();
|
2014-11-21 08:45:19 +00:00
|
|
|
double terrain_height = terrain->getInterpolatedHeight(camera_location.x, camera_location.z, true, true) + 2.0;
|
2014-01-06 19:22:00 +00:00
|
|
|
double water_height = 1.5;
|
2013-11-17 21:36:18 +00:00
|
|
|
if (camera_location.y < water_height || camera_location.y < terrain_height)
|
|
|
|
{
|
|
|
|
double diff = ((water_height > terrain_height) ? water_height : terrain_height) - camera_location.y;
|
|
|
|
camera->setLocation(camera_location.add(Vector3(0.0, diff, 0.0)));
|
|
|
|
}
|
|
|
|
}
|