2013-10-29 20:28:59 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
2013-11-03 14:46:39 +00:00
|
|
|
#include "NoiseGenerator.h"
|
2013-11-07 08:37:11 +00:00
|
|
|
#include "PackStream.h"
|
2013-11-12 20:34:35 +00:00
|
|
|
#include "atmosphere/public.h"
|
2013-11-14 17:47:03 +00:00
|
|
|
#include "CameraDefinition.h"
|
2013-11-12 20:34:35 +00:00
|
|
|
#include "clouds/public.h"
|
|
|
|
#include "terrain/public.h"
|
|
|
|
#include "textures/public.h"
|
|
|
|
#include "water/public.h"
|
|
|
|
#include "renderer.h"
|
|
|
|
#include "terrain/ter_raster.h"
|
2013-11-13 19:07:35 +00:00
|
|
|
#include "WaterDefinition.h"
|
2013-10-29 20:28:59 +00:00
|
|
|
|
2013-11-13 22:03:02 +00:00
|
|
|
static Scenery _main_scenery;
|
2013-10-29 20:28:59 +00:00
|
|
|
|
2013-10-30 14:39:56 +00:00
|
|
|
Scenery::Scenery():
|
|
|
|
BaseDefinition(NULL)
|
2013-10-29 20:28:59 +00:00
|
|
|
{
|
|
|
|
atmosphere = (AtmosphereDefinition*)AtmosphereDefinitionClass.create();
|
2013-11-14 17:47:03 +00:00
|
|
|
camera = new CameraDefinition;
|
2013-10-29 20:28:59 +00:00
|
|
|
clouds = (CloudsDefinition*)CloudsDefinitionClass.create();
|
|
|
|
terrain = (TerrainDefinition*)TerrainDefinitionClass.create();
|
|
|
|
textures = (TexturesDefinition*)TexturesDefinitionClass.create();
|
2013-11-13 19:07:35 +00:00
|
|
|
water = new WaterDefinition(this);
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
addChild(camera);
|
2013-11-13 19:07:35 +00:00
|
|
|
addChild(water);
|
2013-11-13 22:03:02 +00:00
|
|
|
|
|
|
|
_custom_load = NULL;
|
|
|
|
_custom_save = NULL;
|
|
|
|
_custom_data = NULL;
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Scenery::~Scenery()
|
|
|
|
{
|
|
|
|
AtmosphereDefinitionClass.destroy(atmosphere);
|
|
|
|
CloudsDefinitionClass.destroy(clouds);
|
|
|
|
TerrainDefinitionClass.destroy(terrain);
|
|
|
|
TexturesDefinitionClass.destroy(textures);
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:04:38 +00:00
|
|
|
Scenery* Scenery::getCurrent()
|
|
|
|
{
|
2013-11-13 22:03:02 +00:00
|
|
|
return &_main_scenery;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setCustomSaveCallbacks(SceneryCustomDataCallback callback_save, SceneryCustomDataCallback callback_load, void* data)
|
|
|
|
{
|
|
|
|
_custom_save = callback_save;
|
|
|
|
_custom_load = callback_load;
|
|
|
|
_custom_data = data;
|
2013-11-10 16:04:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 19:07:35 +00:00
|
|
|
void Scenery::save(PackStream* stream) const
|
2013-10-29 20:28:59 +00:00
|
|
|
{
|
2013-10-30 14:39:56 +00:00
|
|
|
BaseDefinition::save(stream);
|
|
|
|
|
2013-11-13 22:03:02 +00:00
|
|
|
noiseSave(stream);
|
|
|
|
|
2013-10-29 20:28:59 +00:00
|
|
|
AtmosphereDefinitionClass.save(stream, atmosphere);
|
|
|
|
CloudsDefinitionClass.save(stream, clouds);
|
|
|
|
TerrainDefinitionClass.save(stream, terrain);
|
|
|
|
TexturesDefinitionClass.save(stream, textures);
|
2013-11-13 22:03:02 +00:00
|
|
|
|
|
|
|
if (_custom_save)
|
|
|
|
{
|
|
|
|
_custom_save(stream, _custom_data);
|
|
|
|
}
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::load(PackStream* stream)
|
|
|
|
{
|
2013-10-30 14:39:56 +00:00
|
|
|
BaseDefinition::load(stream);
|
|
|
|
|
2013-11-13 22:03:02 +00:00
|
|
|
noiseLoad(stream);
|
|
|
|
|
2013-10-29 20:28:59 +00:00
|
|
|
AtmosphereDefinitionClass.load(stream, atmosphere);
|
|
|
|
CloudsDefinitionClass.load(stream, clouds);
|
|
|
|
TerrainDefinitionClass.load(stream, terrain);
|
|
|
|
TexturesDefinitionClass.load(stream, textures);
|
2013-11-13 22:03:02 +00:00
|
|
|
|
|
|
|
if (_custom_load)
|
|
|
|
{
|
|
|
|
_custom_load(stream, _custom_data);
|
|
|
|
}
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::validate()
|
|
|
|
{
|
|
|
|
// TODO Ensure camera is above ground and water
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::autoPreset(int seed)
|
|
|
|
{
|
|
|
|
if (!seed)
|
|
|
|
{
|
|
|
|
seed = time(NULL);
|
|
|
|
}
|
|
|
|
srand(seed);
|
|
|
|
|
|
|
|
terrainAutoPreset(terrain, TERRAIN_PRESET_STANDARD);
|
|
|
|
texturesAutoPreset(textures, TEXTURES_PRESET_FULL);
|
|
|
|
atmosphereAutoPreset(atmosphere, ATMOSPHERE_PRESET_CLEAR_DAY);
|
2013-11-13 22:03:02 +00:00
|
|
|
water->applyPreset(WATER_PRESET_LAKE);
|
2013-10-29 20:28:59 +00:00
|
|
|
cloudsAutoPreset(clouds, CLOUDS_PRESET_PARTLY_CLOUDY);
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
camera->setLocation(VECTOR_ZERO);
|
|
|
|
camera->setTarget(VECTOR_NORTH);
|
|
|
|
|
|
|
|
validate();
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
|
|
|
AtmosphereDefinitionClass.copy(atmosphere, this->atmosphere);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
|
|
|
AtmosphereDefinitionClass.copy(this->atmosphere, atmosphere);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setCamera(CameraDefinition* camera)
|
|
|
|
{
|
2013-11-14 17:47:03 +00:00
|
|
|
camera->copy(this->camera);
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getCamera(CameraDefinition* camera)
|
|
|
|
{
|
2013-11-14 17:47:03 +00:00
|
|
|
this->camera->copy(camera);
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
|
|
|
CloudsDefinitionClass.copy(clouds, this->clouds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
|
|
|
CloudsDefinitionClass.copy(this->clouds, clouds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
TerrainDefinitionClass.copy(terrain, this->terrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
TerrainDefinitionClass.copy(this->terrain, terrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
TexturesDefinitionClass.copy(textures, this->textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
TexturesDefinitionClass.copy(this->textures, textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::setWater(WaterDefinition* water)
|
|
|
|
{
|
2013-11-13 19:07:35 +00:00
|
|
|
water->copy(this->water);
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scenery::getWater(WaterDefinition* water)
|
|
|
|
{
|
2013-11-13 19:07:35 +00:00
|
|
|
this->water->copy(water);
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int, int, int, int)
|
|
|
|
{
|
|
|
|
RayCastingResult result;
|
|
|
|
Color sky_color;
|
|
|
|
|
|
|
|
result = renderer->terrain->castRay(renderer, location, direction);
|
|
|
|
if (!result.hit)
|
|
|
|
{
|
|
|
|
sky_color = renderer->atmosphere->getSkyColor(renderer, direction).final;
|
|
|
|
|
|
|
|
result.hit = 1;
|
|
|
|
result.hit_location = v3Add(location, v3Scale(direction, 1000.0));
|
|
|
|
result.hit_color = renderer->clouds->getColor(renderer, sky_color, location, result.hit_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double _getPrecision(Renderer* renderer, Vector3 location)
|
|
|
|
{
|
|
|
|
Vector3 projected;
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
projected = renderer->render_camera->project(location);
|
2013-10-29 20:28:59 +00:00
|
|
|
projected.x += 1.0;
|
|
|
|
//projected.y += 1.0;
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
return v3Norm(v3Sub(renderer->render_camera->unproject(projected), location)); // / (double)render_quality;
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:03:02 +00:00
|
|
|
void Scenery::bindToRenderer(Renderer* renderer)
|
2013-10-29 20:28:59 +00:00
|
|
|
{
|
2013-11-13 22:03:02 +00:00
|
|
|
// TODO Get rid of this !
|
|
|
|
renderer->rayWalking = _rayWalking;
|
|
|
|
renderer->getPrecision = _getPrecision;
|
2013-10-29 20:28:59 +00:00
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
camera->copy(renderer->render_camera);
|
2013-11-13 22:03:02 +00:00
|
|
|
AtmosphereRendererClass.bind(renderer, atmosphere);
|
|
|
|
TerrainRendererClass.bind(renderer, terrain);
|
|
|
|
TexturesRendererClass.bind(renderer, textures);
|
|
|
|
CloudsRendererClass.bind(renderer, clouds);
|
|
|
|
WaterRendererClass.bind(renderer, water);
|
2013-10-29 20:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:03:02 +00:00
|
|
|
// Transitional C-API
|
2013-10-29 20:28:59 +00:00
|
|
|
|
|
|
|
void sceneryRenderFirstPass(Renderer* renderer)
|
|
|
|
{
|
|
|
|
terrainRenderSurface(renderer);
|
|
|
|
waterRenderSurface(renderer);
|
|
|
|
atmosphereRenderSkydome(renderer);
|
|
|
|
}
|