2013-11-09 17:46:34 +00:00
|
|
|
#include "SoftwareRenderer.h"
|
|
|
|
|
2013-11-15 23:27:40 +00:00
|
|
|
#include "AtmosphereDefinition.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "AtmosphereRenderer.h"
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "AtmosphereResult.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "CameraDefinition.h"
|
2013-12-15 12:59:21 +00:00
|
|
|
#include "CloudsDefinition.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "CloudsRenderer.h"
|
|
|
|
#include "FluidMediumManager.h"
|
|
|
|
#include "GodRaysResult.h"
|
|
|
|
#include "GodRaysSampler.h"
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "LightStatus.h"
|
|
|
|
#include "LightingManager.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "MoonRenderer.h"
|
|
|
|
#include "NightSky.h"
|
|
|
|
#include "RayCastingResult.h"
|
|
|
|
#include "Scenery.h"
|
|
|
|
#include "SkyRasterizer.h"
|
2013-12-09 10:59:57 +00:00
|
|
|
#include "System.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "TerrainRasterizer.h"
|
|
|
|
#include "TerrainRenderer.h"
|
|
|
|
#include "TexturesRenderer.h"
|
2013-12-09 10:59:57 +00:00
|
|
|
#include "Thread.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "VegetationRenderer.h"
|
|
|
|
#include "WaterRasterizer.h"
|
|
|
|
#include "WaterRenderer.h"
|
2016-07-25 16:27:09 +00:00
|
|
|
#include <algorithm>
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
SoftwareRenderer::SoftwareRenderer(Scenery *scenery) : scenery(scenery) {
|
2013-12-09 10:59:57 +00:00
|
|
|
render_camera = new CameraDefinition;
|
2013-11-09 17:46:34 +00:00
|
|
|
|
2015-08-18 20:47:18 +00:00
|
|
|
scenery->getCamera()->copy(render_camera);
|
|
|
|
|
2013-11-12 20:34:35 +00:00
|
|
|
atmosphere_renderer = new BaseAtmosphereRenderer(this);
|
2013-12-01 18:24:53 +00:00
|
|
|
clouds_renderer = new CloudsRenderer(this);
|
2013-12-08 19:54:34 +00:00
|
|
|
terrain_renderer = new TerrainRenderer(this);
|
2016-01-10 13:27:32 +00:00
|
|
|
textures_renderer = new TexturesRenderer();
|
2015-10-18 15:26:19 +00:00
|
|
|
vegetation_renderer = new VegetationRenderer(this);
|
2013-12-08 19:54:34 +00:00
|
|
|
water_renderer = new WaterRenderer(this);
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2013-12-26 14:36:15 +00:00
|
|
|
nightsky_renderer = new NightSky(this);
|
2016-01-16 14:05:01 +00:00
|
|
|
moon_renderer = new MoonRenderer(scenery->getAtmosphere()->childMoon());
|
2013-12-26 14:36:15 +00:00
|
|
|
|
2016-01-26 01:26:43 +00:00
|
|
|
fluid_medium = new FluidMediumManager(this); // TODO Not used yet
|
2013-12-09 10:59:57 +00:00
|
|
|
lighting = new LightingManager();
|
2015-09-29 20:31:25 +00:00
|
|
|
godrays = new GodRaysSampler();
|
2013-11-10 16:04:38 +00:00
|
|
|
|
2016-01-26 01:26:43 +00:00
|
|
|
aerial_perspective = true;
|
|
|
|
|
2013-12-17 22:45:09 +00:00
|
|
|
lighting->registerFilter(water_renderer);
|
2014-08-22 15:34:07 +00:00
|
|
|
lighting->registerFilter(terrain_renderer);
|
2015-10-18 15:26:19 +00:00
|
|
|
lighting->registerFilter(vegetation_renderer);
|
2013-12-17 22:45:09 +00:00
|
|
|
lighting->registerFilter(clouds_renderer);
|
2015-09-24 22:12:31 +00:00
|
|
|
lighting->registerSource(atmosphere_renderer);
|
2016-01-16 14:05:01 +00:00
|
|
|
lighting->registerSource(moon_renderer);
|
2015-09-10 17:33:52 +00:00
|
|
|
|
|
|
|
setQuality(0.5);
|
2013-11-09 17:46:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
SoftwareRenderer::~SoftwareRenderer() {
|
2016-01-21 22:02:22 +00:00
|
|
|
moon_renderer->unregister();
|
|
|
|
|
2013-12-09 10:59:57 +00:00
|
|
|
delete render_camera;
|
|
|
|
|
2013-12-17 22:45:09 +00:00
|
|
|
delete fluid_medium;
|
|
|
|
delete lighting;
|
2015-09-29 20:31:25 +00:00
|
|
|
delete godrays;
|
2013-12-17 22:45:09 +00:00
|
|
|
|
2013-12-26 14:36:15 +00:00
|
|
|
delete nightsky_renderer;
|
2016-01-16 14:05:01 +00:00
|
|
|
delete moon_renderer;
|
2013-12-26 14:36:15 +00:00
|
|
|
|
2013-11-12 20:34:35 +00:00
|
|
|
delete atmosphere_renderer;
|
2013-12-01 18:24:53 +00:00
|
|
|
delete clouds_renderer;
|
2013-12-08 19:54:34 +00:00
|
|
|
delete terrain_renderer;
|
|
|
|
delete textures_renderer;
|
2015-11-25 22:15:58 +00:00
|
|
|
delete vegetation_renderer;
|
2013-12-08 19:54:34 +00:00
|
|
|
delete water_renderer;
|
2013-11-17 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void SoftwareRenderer::prepare() {
|
2015-09-20 23:36:03 +00:00
|
|
|
scenery->validate();
|
2015-10-15 15:52:03 +00:00
|
|
|
scenery->getCamera()->copy(render_camera);
|
2015-09-20 23:36:03 +00:00
|
|
|
|
2013-11-12 20:34:35 +00:00
|
|
|
// Prepare sub renderers
|
2015-09-24 22:12:31 +00:00
|
|
|
// TODO Don't recreate the renderer each time, only when it changes
|
|
|
|
lighting->unregisterSource(atmosphere_renderer);
|
2013-11-12 20:34:35 +00:00
|
|
|
delete atmosphere_renderer;
|
2015-11-09 21:30:46 +00:00
|
|
|
if (getScenery()->getAtmosphere()->model == AtmosphereDefinition::ATMOSPHERE_MODEL_BRUNETON) {
|
2013-12-15 13:28:46 +00:00
|
|
|
atmosphere_renderer = new SoftwareBrunetonAtmosphereRenderer(this);
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2013-12-15 13:28:46 +00:00
|
|
|
atmosphere_renderer = new BaseAtmosphereRenderer(this);
|
|
|
|
}
|
2015-09-24 22:12:31 +00:00
|
|
|
lighting->registerSource(atmosphere_renderer);
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2013-12-01 18:24:53 +00:00
|
|
|
clouds_renderer->update();
|
2013-12-17 22:45:09 +00:00
|
|
|
terrain_renderer->update();
|
|
|
|
water_renderer->update();
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2013-12-26 14:36:15 +00:00
|
|
|
nightsky_renderer->update();
|
|
|
|
|
2013-11-12 20:34:35 +00:00
|
|
|
// Prepare global tools
|
2015-09-29 20:31:25 +00:00
|
|
|
godrays->prepare(this);
|
2013-11-12 20:34:35 +00:00
|
|
|
fluid_medium->clearMedia();
|
2015-11-09 21:30:46 +00:00
|
|
|
// fluid_medium->registerMedium(water_renderer);
|
2013-11-10 16:04:38 +00:00
|
|
|
}
|
2013-11-11 14:42:11 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void SoftwareRenderer::setQuality(double quality) {
|
2015-09-10 16:16:57 +00:00
|
|
|
terrain_renderer->setQuality(quality);
|
2016-01-10 16:14:54 +00:00
|
|
|
textures_renderer->setQuality(quality);
|
2015-09-13 16:58:11 +00:00
|
|
|
clouds_renderer->setQuality(quality);
|
2015-09-29 20:31:25 +00:00
|
|
|
godrays->setQuality(quality);
|
2015-09-10 16:16:57 +00:00
|
|
|
|
|
|
|
// TEMP compat with old code
|
2015-12-17 00:13:20 +00:00
|
|
|
render_quality = trunc_to_int(quality * 9.0) + 1;
|
2015-09-10 16:16:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:26:43 +00:00
|
|
|
void SoftwareRenderer::setAerialPerspectiveEnabled(bool enabled) {
|
|
|
|
aerial_perspective = enabled;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Color SoftwareRenderer::applyLightingToSurface(const Vector3 &location, const Vector3 &normal,
|
|
|
|
const SurfaceMaterial &material) {
|
2016-01-14 23:07:02 +00:00
|
|
|
return lighting->apply(getCameraLocation(), location, normal, material);
|
2013-12-08 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Color SoftwareRenderer::applyMediumTraversal(const Vector3 &location, const Color &color) {
|
2016-01-26 01:26:43 +00:00
|
|
|
if (aerial_perspective) {
|
|
|
|
Color result = atmosphere_renderer->applyAerialPerspective(location, color).final;
|
|
|
|
result = clouds_renderer->getColor(getCameraLocation(), location, result);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return color;
|
|
|
|
}
|
2013-12-01 18:24:53 +00:00
|
|
|
}
|
2013-12-08 16:56:59 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
RayCastingResult SoftwareRenderer::rayWalking(const Vector3 &location, const Vector3 &direction, int, int, int, int) {
|
2013-12-08 19:54:34 +00:00
|
|
|
RayCastingResult result;
|
|
|
|
Color sky_color;
|
|
|
|
|
|
|
|
result = terrain_renderer->castRay(location, direction);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (!result.hit) {
|
2013-12-08 19:54:34 +00:00
|
|
|
sky_color = atmosphere_renderer->getSkyColor(direction).final;
|
|
|
|
|
|
|
|
result.hit = 1;
|
2013-12-11 10:32:10 +00:00
|
|
|
result.hit_location = location.add(direction.scale(1000.0));
|
2013-12-08 19:54:34 +00:00
|
|
|
result.hit_color = clouds_renderer->getColor(location, result.hit_location, sky_color);
|
|
|
|
}
|
|
|
|
|
2013-12-08 16:56:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2016-01-14 23:07:02 +00:00
|
|
|
Vector3 SoftwareRenderer::getCameraLocation() {
|
2013-12-09 10:59:57 +00:00
|
|
|
return render_camera->getLocation();
|
|
|
|
}
|
|
|
|
|
2016-01-14 23:07:02 +00:00
|
|
|
Vector3 SoftwareRenderer::getCameraDirection() {
|
2013-12-09 10:59:57 +00:00
|
|
|
return render_camera->getDirectionNormalized();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
double SoftwareRenderer::getPrecision(const Vector3 &location) {
|
2013-12-09 10:59:57 +00:00
|
|
|
Vector3 projected;
|
|
|
|
|
|
|
|
projected = render_camera->project(location);
|
|
|
|
projected.x += 1.0;
|
2015-11-09 21:30:46 +00:00
|
|
|
// projected.y += 1.0;
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2016-07-25 16:27:09 +00:00
|
|
|
double result = render_camera->unproject(projected).sub(location).getNorm() * 0.1 / to_double(render_quality);
|
|
|
|
return max(result, 0.000001);
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Vector3 SoftwareRenderer::projectPoint(const Vector3 &point) {
|
2013-12-09 10:59:57 +00:00
|
|
|
return render_camera->project(point);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Vector3 SoftwareRenderer::unprojectPoint(const Vector3 &point) {
|
2013-12-09 10:59:57 +00:00
|
|
|
return render_camera->unproject(point);
|
|
|
|
}
|