2012-01-23 23:45:33 +00:00
|
|
|
#include "scenery.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "color.h"
|
|
|
|
#include "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-04-09 19:58:08 +00:00
|
|
|
#include "vegetation.h"
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
static AtmosphereDefinition _atmosphere;
|
|
|
|
static CameraDefinition _camera;
|
|
|
|
static CloudsDefinition _clouds;
|
|
|
|
static LightingDefinition _lighting;
|
|
|
|
static SkyDefinition _sky;
|
|
|
|
static TerrainDefinition _terrain;
|
|
|
|
static TexturesDefinition _textures;
|
|
|
|
static VegetationDefinition* _vegetation;
|
|
|
|
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
|
|
|
cloudsInit();
|
|
|
|
lightingInit();
|
|
|
|
texturesInit();
|
|
|
|
|
2012-01-24 15:47:49 +00:00
|
|
|
_atmosphere = atmosphereCreateDefinition();
|
2012-01-23 23:45:33 +00:00
|
|
|
_camera = cameraCreateDefinition();
|
|
|
|
_clouds = cloudsCreateDefinition();
|
|
|
|
_lighting = lightingCreateDefinition();
|
|
|
|
_sky = skyCreateDefinition();
|
|
|
|
_terrain = terrainCreateDefinition();
|
|
|
|
_textures = texturesCreateDefinition();
|
2012-04-09 19:58:08 +00:00
|
|
|
_vegetation = vegetationCreateDefinition();
|
2012-01-23 23:45:33 +00:00
|
|
|
_water = waterCreateDefinition();
|
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()
|
|
|
|
{
|
|
|
|
atmosphereDeleteDefinition(&_atmosphere);
|
|
|
|
cameraDeleteDefinition(&_camera);
|
|
|
|
cloudsDeleteDefinition(&_clouds);
|
|
|
|
lightingDeleteDefinition(&_lighting);
|
|
|
|
skyDeleteDefinition(&_sky);
|
|
|
|
terrainDeleteDefinition(&_terrain);
|
|
|
|
texturesDeleteDefinition(&_textures);
|
2012-04-09 19:58:08 +00:00
|
|
|
vegetationDeleteDefinition(_vegetation);
|
2012-02-12 16:57:29 +00:00
|
|
|
waterDeleteDefinition(&_water);
|
|
|
|
|
|
|
|
cloudsQuit();
|
|
|
|
lightingQuit();
|
|
|
|
texturesQuit();
|
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);
|
|
|
|
atmosphereSave(stream, &_atmosphere);
|
|
|
|
cameraSave(stream, &_camera);
|
|
|
|
cloudsSave(stream, &_clouds);
|
|
|
|
lightingSave(stream, &_lighting);
|
|
|
|
skySave(stream, &_sky);
|
|
|
|
terrainSave(stream, &_terrain);
|
|
|
|
texturesSave(stream, &_textures);
|
|
|
|
vegetationSave(stream, _vegetation);
|
|
|
|
waterSave(stream, &_water);
|
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);
|
|
|
|
atmosphereLoad(stream, &_atmosphere);
|
|
|
|
cameraLoad(stream, &_camera);
|
|
|
|
cloudsLoad(stream, &_clouds);
|
|
|
|
lightingLoad(stream, &_lighting);
|
|
|
|
skyLoad(stream, &_sky);
|
|
|
|
terrainLoad(stream, &_terrain);
|
|
|
|
texturesLoad(stream, &_textures);
|
|
|
|
vegetationLoad(stream, _vegetation);
|
|
|
|
waterLoad(stream, &_water);
|
2012-01-29 11:34:49 +00:00
|
|
|
|
|
|
|
atmosphereValidateDefinition(&_atmosphere);
|
|
|
|
cameraValidateDefinition(&_camera, 0);
|
|
|
|
cloudsValidateDefinition(&_clouds);
|
|
|
|
lightingValidateDefinition(&_lighting);
|
|
|
|
skyValidateDefinition(&_sky);
|
|
|
|
terrainValidateDefinition(&_terrain);
|
|
|
|
texturesValidateDefinition(&_textures);
|
2012-04-09 19:58:08 +00:00
|
|
|
vegetationValidateDefinition(_vegetation);
|
2012-01-23 23:45:33 +00:00
|
|
|
waterValidateDefinition(&_water);
|
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)
|
|
|
|
{
|
|
|
|
atmosphereCopyDefinition(atmosphere, &_atmosphere);
|
|
|
|
atmosphereValidateDefinition(&_atmosphere);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetAtmosphere(AtmosphereDefinition* atmosphere)
|
|
|
|
{
|
|
|
|
atmosphereCopyDefinition(&_atmosphere, atmosphere);
|
|
|
|
}
|
|
|
|
|
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 scenerySetLighting(LightingDefinition* lighting)
|
|
|
|
{
|
|
|
|
lightingCopyDefinition(lighting, &_lighting);
|
|
|
|
lightingValidateDefinition(&_lighting);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetLighting(LightingDefinition* lighting)
|
|
|
|
{
|
|
|
|
lightingCopyDefinition(&_lighting, lighting);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetSky(SkyDefinition* sky)
|
|
|
|
{
|
|
|
|
skyCopyDefinition(sky, &_sky);
|
|
|
|
skyValidateDefinition(&_sky);
|
|
|
|
|
2012-01-24 15:47:49 +00:00
|
|
|
atmosphereValidateDefinition(&_atmosphere);
|
2012-01-23 23:45:33 +00:00
|
|
|
lightingValidateDefinition(&_lighting);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetSky(SkyDefinition* sky)
|
|
|
|
{
|
|
|
|
skyCopyDefinition(&_sky, sky);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
terrainCopyDefinition(terrain, &_terrain);
|
|
|
|
terrainValidateDefinition(&_terrain);
|
2012-01-29 11:34:49 +00:00
|
|
|
|
|
|
|
cameraValidateDefinition(&_camera, 1);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTerrain(TerrainDefinition* terrain)
|
|
|
|
{
|
|
|
|
terrainCopyDefinition(&_terrain, terrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scenerySetTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
texturesCopyDefinition(textures, &_textures);
|
|
|
|
texturesValidateDefinition(&_textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTextures(TexturesDefinition* textures)
|
|
|
|
{
|
|
|
|
texturesCopyDefinition(&_textures, textures);
|
|
|
|
}
|
|
|
|
|
2012-04-09 19:58:08 +00:00
|
|
|
void scenerySetVegetation(VegetationDefinition* vegetation)
|
|
|
|
{
|
|
|
|
vegetationCopyDefinition(vegetation, _vegetation);
|
|
|
|
vegetationValidateDefinition(_vegetation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sceneryGetTVegetation(VegetationDefinition* vegetation)
|
|
|
|
{
|
|
|
|
vegetationCopyDefinition(_vegetation, vegetation);
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
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-01-29 21:45:58 +00:00
|
|
|
terrainRender(&_terrain, renderer);
|
|
|
|
waterRender(&_water, renderer);
|
2012-06-17 09:40:40 +00:00
|
|
|
skyRender(&_sky, renderer);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******* Standard renderer *********/
|
2012-06-24 16:04:01 +00:00
|
|
|
static int _getSkyDomeLights(Renderer* renderer, LightDefinition* array, int max_lights)
|
|
|
|
{
|
|
|
|
return skyGetLights(&_sky, renderer, array, max_lights);
|
|
|
|
}
|
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static void _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
Vector3 light_location;
|
|
|
|
Vector3 direction_to_light;
|
|
|
|
|
|
|
|
direction_to_light = v3Normalize(v3Scale(light->direction, -1.0));
|
|
|
|
light_location = v3Add(location, v3Scale(direction_to_light, 1000.0));
|
|
|
|
|
|
|
|
if (light->filtered)
|
|
|
|
{
|
|
|
|
// TODO atmosphere filter
|
|
|
|
light->color = waterLightFilter(&_water, renderer, light->color, location, light_location, direction_to_light);
|
|
|
|
}
|
|
|
|
if (light->masked)
|
|
|
|
{
|
|
|
|
light->color = terrainLightFilter(&_terrain, renderer, light->color, location, light_location, direction_to_light);
|
|
|
|
light->color = cloudsFilterLight(&_clouds, renderer, light->color, location, light_location, direction_to_light);
|
|
|
|
}
|
2012-01-25 17:31:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static void _getLightStatus(Renderer* renderer, LightStatus* status, Vector3 location)
|
2012-01-25 17:31:36 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
lightingGetStatus(&_lighting, renderer, location, status);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static Color _applyLightStatus(Renderer* renderer, LightStatus* status, Vector3 location, Vector3 normal, SurfaceMaterial material)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
return lightingApplyStatusToSurface(renderer, status, location, normal, material);
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!terrainProjectRay(&_terrain, renderer, location, direction, &result.hit_location, &result.hit_color))
|
|
|
|
{
|
2012-01-26 18:20:19 +00:00
|
|
|
sky_color = skyGetColor(&_sky, renderer, location, direction);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
result.hit = 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getTerrainHeight(Renderer* renderer, double x, double z)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
return terrainGetHeight(&_terrain, x, z);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
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 _applyAtmosphere(Renderer* renderer, Vector3 location, Color base)
|
|
|
|
{
|
2012-01-24 15:47:49 +00:00
|
|
|
return atmosphereApply(&_atmosphere, renderer, location, base);
|
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
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
Renderer sceneryCreateStandardRenderer()
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
|
|
|
Renderer result;
|
2012-01-29 21:45:58 +00:00
|
|
|
|
|
|
|
result = rendererCreate();
|
2012-01-24 15:47:49 +00:00
|
|
|
|
2012-01-31 11:20:52 +00:00
|
|
|
cameraCopyDefinition(&_camera, &result.render_camera);
|
2012-01-24 13:16:20 +00:00
|
|
|
result.camera_location = _camera.location;
|
|
|
|
|
2012-06-24 16:04:01 +00:00
|
|
|
result.getSkyDomeLights = _getSkyDomeLights;
|
2012-06-05 20:22:12 +00:00
|
|
|
result.alterLight = _alterLight;
|
|
|
|
result.getLightStatus = _getLightStatus;
|
|
|
|
result.applyLightStatus = _applyLightStatus;
|
2012-01-24 13:16:20 +00:00
|
|
|
result.rayWalking = _rayWalking;
|
|
|
|
result.getTerrainHeight = _getTerrainHeight;
|
2012-01-29 17:53:12 +00:00
|
|
|
result.getWaterHeightInfo = _getWaterHeightInfo;
|
2012-01-24 13:16:20 +00:00
|
|
|
result.applyTextures = _applyTextures;
|
|
|
|
result.applyAtmosphere = _applyAtmosphere;
|
|
|
|
result.applyClouds = _applyClouds;
|
|
|
|
result.projectPoint = _projectPoint;
|
|
|
|
result.unprojectPoint = _unprojectPoint;
|
|
|
|
result.getPrecision = _getPrecision;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|