2012-01-23 23:45:33 +00:00
|
|
|
#include "scenery.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;
|
2012-06-27 10:30:00 +00:00
|
|
|
static CameraDefinition _camera;
|
|
|
|
static CloudsDefinition _clouds;
|
2012-12-09 17:49:28 +00:00
|
|
|
static TerrainDefinition* _terrain;
|
2012-06-27 10:30:00 +00:00
|
|
|
static TexturesDefinition _textures;
|
|
|
|
static WaterDefinition _water;
|
|
|
|
|
|
|
|
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();
|
|
|
|
_clouds = cloudsCreateDefinition();
|
2012-12-09 17:49:28 +00:00
|
|
|
_terrain = TerrainDefinitionClass.create();
|
2012-01-23 23:45:33 +00:00
|
|
|
_textures = texturesCreateDefinition();
|
|
|
|
_water = waterCreateDefinition();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
_custom_save = NULL;
|
|
|
|
_custom_load = NULL;
|
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);
|
2012-02-12 16:57:29 +00:00
|
|
|
cameraDeleteDefinition(&_camera);
|
|
|
|
cloudsDeleteDefinition(&_clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.destroy(_terrain);
|
2012-02-12 16:57:29 +00:00
|
|
|
texturesDeleteDefinition(&_textures);
|
|
|
|
waterDeleteDefinition(&_water);
|
|
|
|
|
2012-02-27 15:25:12 +00:00
|
|
|
noiseQuit();
|
2012-02-12 16:57:29 +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);
|
2012-04-22 17:12:39 +00:00
|
|
|
cameraSave(stream, &_camera);
|
|
|
|
cloudsSave(stream, &_clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.save(stream, _terrain);
|
2012-04-22 17:12:39 +00:00
|
|
|
texturesSave(stream, &_textures);
|
|
|
|
waterSave(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);
|
2012-04-22 17:12:39 +00:00
|
|
|
cameraLoad(stream, &_camera);
|
|
|
|
cloudsLoad(stream, &_clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainDefinitionClass.load(stream, _terrain);
|
2012-04-22 17:12:39 +00:00
|
|
|
texturesLoad(stream, &_textures);
|
|
|
|
waterLoad(stream, &_water);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(&_camera, 0);
|
|
|
|
cloudsValidateDefinition(&_clouds);
|
|
|
|
texturesValidateDefinition(&_textures);
|
2012-01-23 23:45:33 +00:00
|
|
|
waterValidateDefinition(&_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)
|
|
|
|
{
|
|
|
|
cameraCopyDefinition(camera, &_camera);
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(&_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetCamera(CameraDefinition* camera)
|
|
|
|
{
|
|
|
|
cameraCopyDefinition(&_camera, camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
|
|
|
cloudsCopyDefinition(clouds, &_clouds);
|
|
|
|
cloudsValidateDefinition(&_clouds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetClouds(CloudsDefinition* clouds)
|
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
cloudsCopyDefinition(&_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);
|
|
|
|
|
2012-01-29 11:34:49 +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)
|
|
|
|
{
|
|
|
|
texturesCopyDefinition(textures, &_textures);
|
|
|
|
texturesValidateDefinition(&_textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
texturesCopyDefinition(&_textures, textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetWater(WaterDefinition* water)
|
|
|
|
{
|
|
|
|
waterCopyDefinition(water, &_water);
|
|
|
|
waterValidateDefinition(&_water);
|
2012-01-29 11:34:49 +00:00
|
|
|
|
|
|
|
cameraValidateDefinition(&_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetWater(WaterDefinition* water)
|
|
|
|
{
|
|
|
|
waterCopyDefinition(&_water, water);
|
|
|
|
}
|
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
void sceneryRenderFirstPass(Renderer* renderer)
|
|
|
|
{
|
2012-12-09 17:49:28 +00:00
|
|
|
terrainRenderSurface(renderer);
|
2012-01-29 21:45:58 +00:00
|
|
|
waterRender(&_water, 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
|
|
|
{
|
2012-11-25 21:53:01 +00:00
|
|
|
sky_color = renderer->atmosphere->getSkyColor(renderer, direction);
|
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));
|
|
|
|
result.hit_color = renderer->applyClouds(renderer, sky_color, location, result.hit_location);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:53:12 +00:00
|
|
|
static HeightInfo _getWaterHeightInfo(Renderer* renderer)
|
|
|
|
{
|
|
|
|
return waterGetHeightInfo(&_water);
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static Color _applyTextures(Renderer* renderer, Vector3 location, double precision)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
return texturesGetColor(&_textures, renderer, location.x, location.z, precision);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
2012-01-24 13:16:20 +00:00
|
|
|
|
|
|
|
static Color _applyClouds(Renderer* renderer, Color base, Vector3 start, Vector3 end)
|
|
|
|
{
|
2012-07-01 13:27:57 +00:00
|
|
|
return cloudsApply(&_clouds, base, renderer, start, end);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _projectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
2012-01-31 11:20:52 +00:00
|
|
|
return cameraProject(&renderer->render_camera, renderer, point);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _unprojectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
2012-01-31 11:20:52 +00:00
|
|
|
return cameraUnproject(&renderer->render_camera, renderer, point);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2012-01-31 11:20:52 +00:00
|
|
|
projected = cameraProject(&renderer->render_camera, renderer, location);
|
2012-01-24 13:16:20 +00:00
|
|
|
projected.x += 1.0;
|
|
|
|
//projected.y += 1.0;
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
return v3Norm(v3Sub(cameraUnproject(&renderer->render_camera, renderer, 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
|
|
|
cameraCopyDefinition(&_camera, &result->render_camera);
|
|
|
|
result->camera_location = _camera.location;
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->rayWalking = _rayWalking;
|
|
|
|
result->getWaterHeightInfo = _getWaterHeightInfo;
|
|
|
|
result->applyTextures = _applyTextures;
|
|
|
|
result->applyClouds = _applyClouds;
|
|
|
|
result->projectPoint = _projectPoint;
|
|
|
|
result->unprojectPoint = _unprojectPoint;
|
|
|
|
result->getPrecision = _getPrecision;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
AtmosphereRendererClass.bind(result, _atmosphere);
|
|
|
|
TerrainRendererClass.bind(result, _terrain);
|
2012-01-24 13:16:20 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|