2012-01-23 23:45:33 +00:00
|
|
|
#include "scenery.h"
|
|
|
|
|
2013-06-16 21:01:22 +00:00
|
|
|
#include <time.h>
|
2013-01-19 22:42:50 +00:00
|
|
|
#include "tools/color.h"
|
|
|
|
#include "tools/euclid.h"
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "render.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "system.h"
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
static AtmosphereDefinition* _atmosphere;
|
2013-04-27 19:41:57 +00:00
|
|
|
static CameraDefinition* _camera;
|
2013-01-22 20:50:37 +00:00
|
|
|
static CloudsDefinition* _clouds;
|
2012-12-09 17:49:28 +00:00
|
|
|
static TerrainDefinition* _terrain;
|
2013-03-31 20:27:21 +00:00
|
|
|
static TexturesDefinition* _textures;
|
2013-02-27 16:38:27 +00:00
|
|
|
static WaterDefinition* _water;
|
2012-06-27 10:30:00 +00:00
|
|
|
|
|
|
|
static SceneryCustomDataCallback _custom_save = NULL;
|
|
|
|
static SceneryCustomDataCallback _custom_load = NULL;
|
|
|
|
static void* _custom_data = NULL;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
void sceneryInit()
|
|
|
|
{
|
2012-02-27 15:25:12 +00:00
|
|
|
noiseInit();
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
_atmosphere = AtmosphereDefinitionClass.create();
|
2012-01-23 23:45:33 +00:00
|
|
|
_camera = cameraCreateDefinition();
|
2013-01-22 20:50:37 +00:00
|
|
|
_clouds = CloudsDefinitionClass.create();
|
2012-12-09 17:49:28 +00:00
|
|
|
_terrain = TerrainDefinitionClass.create();
|
2013-03-31 20:27:21 +00:00
|
|
|
_textures = TexturesDefinitionClass.create();
|
2013-02-27 16:38:27 +00:00
|
|
|
_water = WaterDefinitionClass.create();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
_custom_save = NULL;
|
|
|
|
_custom_load = NULL;
|
2013-06-16 21:01:22 +00:00
|
|
|
|
|
|
|
sceneryAutoPreset(0);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-12 16:57:29 +00:00
|
|
|
void sceneryQuit()
|
|
|
|
{
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereDefinitionClass.destroy(_atmosphere);
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraDeleteDefinition(_camera);
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsDefinitionClass.destroy(_clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.destroy(_terrain);
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesDefinitionClass.destroy(_textures);
|
2013-02-27 16:38:27 +00:00
|
|
|
AtmosphereDefinitionClass.destroy(_water);
|
2012-02-12 16:57:29 +00:00
|
|
|
|
2012-02-27 15:25:12 +00:00
|
|
|
noiseQuit();
|
2012-02-12 16:57:29 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 21:01:22 +00:00
|
|
|
void sceneryAutoPreset(int seed)
|
2013-01-30 14:30:06 +00:00
|
|
|
{
|
2013-06-16 21:01:22 +00:00
|
|
|
if (!seed)
|
|
|
|
{
|
|
|
|
seed = time(NULL);
|
|
|
|
}
|
|
|
|
srand(seed);
|
|
|
|
|
2013-01-30 14:30:06 +00:00
|
|
|
terrainAutoPreset(_terrain, TERRAIN_PRESET_STANDARD);
|
2013-03-31 20:27:21 +00:00
|
|
|
texturesAutoPreset(_textures, TEXTURES_PRESET_IRELAND);
|
2013-01-30 17:00:26 +00:00
|
|
|
atmosphereAutoPreset(_atmosphere, ATMOSPHERE_PRESET_CLEAR_DAY);
|
2013-02-27 16:38:27 +00:00
|
|
|
waterAutoPreset(_water, WATER_PRESET_LAKE);
|
2013-01-30 14:30:06 +00:00
|
|
|
cloudsAutoPreset(_clouds, CLOUDS_PRESET_PARTLY_CLOUDY);
|
2013-06-16 21:01:22 +00:00
|
|
|
|
|
|
|
cameraSetLocation(_camera, VECTOR_ZERO);
|
|
|
|
cameraSetTarget(_camera, VECTOR_NORTH);
|
|
|
|
cameraValidateDefinition(_camera, 1);
|
2013-01-30 14:30:06 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
void scenerySetCustomDataCallback(SceneryCustomDataCallback callback_save, SceneryCustomDataCallback callback_load, void* data)
|
|
|
|
{
|
|
|
|
_custom_save = callback_save;
|
|
|
|
_custom_load = callback_load;
|
|
|
|
_custom_data = data;
|
|
|
|
}
|
|
|
|
|
2012-04-28 13:36:37 +00:00
|
|
|
void scenerySave(PackStream* stream)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-04-22 17:12:39 +00:00
|
|
|
noiseSave(stream);
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereDefinitionClass.save(stream, _atmosphere);
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraSave(stream, _camera);
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsDefinitionClass.save(stream, _clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.save(stream, _terrain);
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesDefinitionClass.save(stream, _textures);
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterDefinitionClass.save(stream, _water);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
if (_custom_save)
|
|
|
|
{
|
|
|
|
_custom_save(stream, _custom_data);
|
|
|
|
}
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 13:36:37 +00:00
|
|
|
void sceneryLoad(PackStream* stream)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
|
|
|
/* TODO Use intermediary definitions ? */
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
noiseLoad(stream);
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereDefinitionClass.load(stream, _atmosphere);
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraLoad(stream, _camera);
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsDefinitionClass.load(stream, _clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.load(stream, _terrain);
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesDefinitionClass.load(stream, _textures);
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterDefinitionClass.load(stream, _water);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
if (_custom_load)
|
|
|
|
{
|
|
|
|
_custom_load(stream, _custom_data);
|
|
|
|
}
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 15:47:49 +00:00
|
|
|
void scenerySetAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereDefinitionClass.copy(atmosphere, _atmosphere);
|
2012-01-24 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereDefinitionClass.copy(_atmosphere, atmosphere);
|
2012-01-24 15:47:49 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void scenerySetCamera(CameraDefinition* camera)
|
|
|
|
{
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraCopyDefinition(camera, _camera);
|
|
|
|
cameraValidateDefinition(_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetCamera(CameraDefinition* camera)
|
|
|
|
{
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraCopyDefinition(_camera, camera);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsDefinitionClass.copy(clouds, _clouds);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsDefinitionClass.copy(_clouds, clouds);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.copy(terrain, _terrain);
|
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraValidateDefinition(_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.copy(_terrain, terrain);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetTextures(TexturesDefinition* textures)
|
|
|
|
{
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesDefinitionClass.copy(textures, _textures);
|
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraValidateDefinition(_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTextures(TexturesDefinition* textures)
|
|
|
|
{
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesDefinitionClass.copy(_textures, textures);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetWater(WaterDefinition* water)
|
|
|
|
{
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterDefinitionClass.copy(water, _water);
|
2012-01-29 11:34:49 +00:00
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
cameraValidateDefinition(_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetWater(WaterDefinition* water)
|
|
|
|
{
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterDefinitionClass.copy(_water, water);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
void sceneryRenderFirstPass(Renderer* renderer)
|
|
|
|
{
|
2012-12-09 17:49:28 +00:00
|
|
|
terrainRenderSurface(renderer);
|
2013-02-27 16:38:27 +00:00
|
|
|
waterRenderSurface(renderer);
|
2012-11-25 21:53:01 +00:00
|
|
|
atmosphereRenderSkydome(renderer);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******* Standard renderer *********/
|
|
|
|
static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int terrain, int water, int sky, int clouds)
|
|
|
|
{
|
|
|
|
RayCastingResult result;
|
2012-01-26 18:20:19 +00:00
|
|
|
Color sky_color;
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-12-09 17:49:28 +00:00
|
|
|
result = renderer->terrain->castRay(renderer, location, direction);
|
|
|
|
if (!result.hit)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
sky_color = renderer->atmosphere->getSkyColor(renderer, direction).final;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
|
|
|
result.hit = 1;
|
2012-01-26 18:20:19 +00:00
|
|
|
result.hit_location = v3Add(location, v3Scale(direction, 1000.0));
|
2013-01-22 20:50:37 +00:00
|
|
|
result.hit_color = renderer->clouds->getColor(renderer, sky_color, location, result.hit_location);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getPrecision(Renderer* renderer, Vector3 location)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
|
|
|
Vector3 projected;
|
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
projected = cameraProject(renderer->render_camera, location);
|
2012-01-24 13:16:20 +00:00
|
|
|
projected.x += 1.0;
|
|
|
|
//projected.y += 1.0;
|
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
return v3Norm(v3Sub(cameraUnproject(renderer->render_camera, projected), location)); // / (double)render_quality;
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
Renderer* sceneryCreateStandardRenderer()
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
2013-01-20 15:07:45 +00:00
|
|
|
Renderer* result;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
result = rendererCreate();
|
2012-01-24 15:47:49 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->rayWalking = _rayWalking;
|
|
|
|
result->getPrecision = _getPrecision;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-03-11 14:15:30 +00:00
|
|
|
sceneryBindRenderer(result);
|
2012-01-24 13:16:20 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-11 14:15:30 +00:00
|
|
|
void sceneryBindRenderer(Renderer* renderer)
|
|
|
|
{
|
2013-06-16 21:01:22 +00:00
|
|
|
cameraCopyDefinition(_camera, renderer->render_camera);
|
2013-03-11 14:15:30 +00:00
|
|
|
AtmosphereRendererClass.bind(renderer, _atmosphere);
|
|
|
|
TerrainRendererClass.bind(renderer, _terrain);
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesRendererClass.bind(renderer, _textures);
|
2013-03-11 14:15:30 +00:00
|
|
|
CloudsRendererClass.bind(renderer, _clouds);
|
|
|
|
WaterRendererClass.bind(renderer, _water);
|
|
|
|
}
|