paysages3d/src/render/software/SoftwareRenderer.cpp

225 lines
5.7 KiB
C++
Raw Normal View History

2013-11-09 17:46:34 +00:00
#include "SoftwareRenderer.h"
#include "CameraDefinition.h"
2013-11-10 16:04:38 +00:00
#include "Scenery.h"
#include "FluidMediumManager.h"
#include "AtmosphereRenderer.h"
2013-11-15 23:27:40 +00:00
#include "AtmosphereDefinition.h"
#include "AtmosphereResult.h"
#include "CloudsRenderer.h"
2013-12-15 12:59:21 +00:00
#include "CloudsDefinition.h"
#include "TerrainRenderer.h"
#include "TexturesRenderer.h"
#include "WaterRenderer.h"
#include "SkyRasterizer.h"
2013-12-05 15:44:18 +00:00
#include "TerrainRasterizer.h"
2013-12-08 17:05:18 +00:00
#include "WaterRasterizer.h"
#include "NightSky.h"
#include "LightStatus.h"
#include "LightingManager.h"
#include "System.h"
#include "Thread.h"
SoftwareRenderer::SoftwareRenderer(Scenery* scenery)
{
render_quality = 5;
render_width = 1;
render_height = 1;
render_interrupt = 0;
render_progress = 0.0;
is_rendering = 0;
render_camera = new CameraDefinition;
2013-11-09 17:46:34 +00:00
atmosphere_renderer = new BaseAtmosphereRenderer(this);
clouds_renderer = new CloudsRenderer(this);
terrain_renderer = new TerrainRenderer(this);
textures_renderer = new TexturesRenderer(this);
water_renderer = new WaterRenderer(this);
nightsky_renderer = new NightSky(this);
fluid_medium = new FluidMediumManager(this);
lighting = new LightingManager();
2013-11-10 16:04:38 +00:00
2013-12-17 22:45:09 +00:00
lighting->registerFilter(terrain_renderer);
lighting->registerFilter(water_renderer);
lighting->registerFilter(clouds_renderer);
this->scenery = new Scenery;
2013-11-10 16:04:38 +00:00
if (scenery)
{
scenery->copy(this->scenery);
2013-11-10 16:04:38 +00:00
}
2013-11-09 17:46:34 +00:00
}
SoftwareRenderer::~SoftwareRenderer()
{
delete render_camera;
2013-12-17 22:45:09 +00:00
delete fluid_medium;
delete lighting;
delete nightsky_renderer;
delete atmosphere_renderer;
delete clouds_renderer;
delete terrain_renderer;
delete textures_renderer;
delete water_renderer;
delete scenery;
2013-11-09 17:46:34 +00:00
}
2013-11-10 16:04:38 +00:00
void SoftwareRenderer::setScenery(Scenery* scenery)
{
scenery->copy(this->scenery);
}
void SoftwareRenderer::prepare()
2013-11-10 16:04:38 +00:00
{
scenery->getCamera()->copy(render_camera);
// Prepare sub renderers
2013-12-17 22:45:09 +00:00
// TODO Don't recreate the renderer each time
delete atmosphere_renderer;
2013-12-15 13:28:46 +00:00
if (getScenery()->getAtmosphere()->model == AtmosphereDefinition::ATMOSPHERE_MODEL_BRUNETON)
{
atmosphere_renderer = new SoftwareBrunetonAtmosphereRenderer(this);
}
else
{
atmosphere_renderer = new BaseAtmosphereRenderer(this);
}
clouds_renderer->update();
2013-12-17 22:45:09 +00:00
terrain_renderer->update();
water_renderer->update();
textures_renderer->update();
nightsky_renderer->update();
// Prepare global tools
fluid_medium->clearMedia();
//fluid_medium->registerMedium(water_renderer);
2013-11-10 16:04:38 +00:00
}
2013-12-15 12:59:21 +00:00
void SoftwareRenderer::disableClouds()
{
scenery->getClouds()->clear();
}
2013-12-20 16:30:27 +00:00
void SoftwareRenderer::disableAtmosphere()
{
LightComponent light;
std::vector<LightComponent> lights;
light.color.r = 1.5;
light.color.g = 1.5;
light.color.b = 1.5;
light.direction.x = -1.0;
light.direction.y = -0.3;
2013-12-20 16:30:27 +00:00
light.direction.z = 1.0;
light.direction = light.direction.normalize();
light.altered = 1;
light.reflection = 0.0;
lights.push_back(light);
2013-12-29 17:44:12 +00:00
light.color.r = 0.25;
light.color.g = 0.25;
light.color.b = 0.265;
2013-12-20 16:30:27 +00:00
light.direction.x = 1.0;
light.direction.y = -0.5;
light.direction.z = -1.0;
light.direction = light.direction.normalize();
light.altered = 0;
light.reflection = 0.0;
lights.push_back(light);
disableAtmosphere(lights);
}
2013-12-15 13:28:46 +00:00
void SoftwareRenderer::disableAtmosphere(const std::vector<LightComponent> &lights)
{
scenery->getAtmosphere()->model = AtmosphereDefinition::ATMOSPHERE_MODEL_DISABLED;
delete atmosphere_renderer;
atmosphere_renderer = new BaseAtmosphereRenderer(this);
atmosphere_renderer->setStaticLights(lights);
}
void SoftwareRenderer::interrupt()
{
render_interrupt = 1;
}
Color SoftwareRenderer::applyLightingToSurface(const Vector3 &location, const Vector3 &normal, const SurfaceMaterial &material)
{
LightStatus status(lighting, location, getCameraLocation(location));
atmosphere_renderer->getLightingStatus(&status, normal, 0);
return status.apply(normal, material);
}
Color SoftwareRenderer::applyMediumTraversal(Vector3 location, Color color)
{
color = atmosphere_renderer->applyAerialPerspective(location, color).final;
color = clouds_renderer->getColor(getCameraLocation(location), location, color);
return color;
/*Vector3 eye = cameraGetLocation(scenery->getCamera());
return fluid_medium->applyTraversal(eye, location, color);*/
}
2013-12-08 16:56:59 +00:00
RayCastingResult SoftwareRenderer::rayWalking(const Vector3 &location, const Vector3 &direction, int, int, int, int)
2013-12-08 16:56:59 +00:00
{
RayCastingResult result;
Color sky_color;
result = terrain_renderer->castRay(location, direction);
if (!result.hit)
{
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));
result.hit_color = clouds_renderer->getColor(location, result.hit_location, sky_color);
}
2013-12-08 16:56:59 +00:00
return result;
}
int SoftwareRenderer::addRenderProgress(double)
{
return not render_interrupt;
}
2013-12-11 11:46:39 +00:00
Vector3 SoftwareRenderer::getCameraLocation(const Vector3 &)
{
return render_camera->getLocation();
}
2013-12-11 11:46:39 +00:00
Vector3 SoftwareRenderer::getCameraDirection(const Vector3 &)
{
return render_camera->getDirectionNormalized();
}
2013-12-11 11:46:39 +00:00
double SoftwareRenderer::getPrecision(const Vector3 &location)
{
Vector3 projected;
projected = render_camera->project(location);
projected.x += 1.0;
//projected.y += 1.0;
2013-12-11 10:32:10 +00:00
return render_camera->unproject(projected).sub(location).getNorm(); // / (double)render_quality;
}
2013-12-11 11:46:39 +00:00
Vector3 SoftwareRenderer::projectPoint(const Vector3 &point)
{
return render_camera->project(point);
}
2013-12-11 11:46:39 +00:00
Vector3 SoftwareRenderer::unprojectPoint(const Vector3 &point)
{
return render_camera->unproject(point);
}