2015-10-18 15:26:19 +00:00
|
|
|
#include "VegetationRenderer.h"
|
|
|
|
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "LightComponent.h"
|
2015-10-18 15:26:19 +00:00
|
|
|
#include "RayCastingResult.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "SoftwareRenderer.h"
|
2015-10-18 15:26:19 +00:00
|
|
|
#include "SpaceGridIterator.h"
|
|
|
|
#include "SpaceSegment.h"
|
|
|
|
#include "TerrainRenderer.h"
|
|
|
|
#include "VegetationDefinition.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "VegetationInstance.h"
|
|
|
|
#include "VegetationInstance.h"
|
2015-10-18 20:15:19 +00:00
|
|
|
#include "VegetationLayerDefinition.h"
|
2016-07-23 20:58:32 +00:00
|
|
|
#include "VegetationModelRenderer.h"
|
2015-10-18 20:15:19 +00:00
|
|
|
#include "VegetationPresenceDefinition.h"
|
2015-10-18 15:26:19 +00:00
|
|
|
#include "VegetationResult.h"
|
|
|
|
|
2015-10-18 20:15:19 +00:00
|
|
|
/**
|
|
|
|
* Grid iterator to collect instances of a layer, in small squares.
|
|
|
|
*/
|
2015-11-09 21:38:00 +00:00
|
|
|
class VegetationGridIterator : public SpaceGridIterator {
|
|
|
|
public:
|
|
|
|
VegetationGridIterator(const SpaceSegment &segment, VegetationRenderer *renderer, bool only_hit)
|
|
|
|
: segment(segment), renderer(renderer), only_hit(only_hit) {
|
2015-10-18 15:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
inline const RayCastingResult &getResult() const {
|
|
|
|
return result;
|
|
|
|
}
|
2015-10-18 15:26:19 +00:00
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
virtual bool onCell(int x, int, int z) override {
|
2015-12-17 00:13:20 +00:00
|
|
|
result = renderer->getBoundResult(segment, to_double(x), to_double(z), only_hit);
|
2015-11-02 19:14:35 +00:00
|
|
|
return not result.hit;
|
2015-10-18 15:26:19 +00:00
|
|
|
}
|
2015-11-09 21:38:00 +00:00
|
|
|
|
|
|
|
private:
|
2015-10-18 15:26:19 +00:00
|
|
|
const SpaceSegment &segment;
|
|
|
|
VegetationRenderer *renderer;
|
|
|
|
RayCastingResult result;
|
|
|
|
bool only_hit;
|
|
|
|
};
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
VegetationRenderer::VegetationRenderer(SoftwareRenderer *parent) : parent(parent) {
|
2015-10-19 17:14:45 +00:00
|
|
|
enabled = true;
|
2015-10-18 15:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:15:58 +00:00
|
|
|
VegetationRenderer::~VegetationRenderer() {
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
void VegetationRenderer::setEnabled(bool enabled) {
|
2015-10-18 15:35:42 +00:00
|
|
|
this->enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
RayCastingResult VegetationRenderer::renderInstance(const SpaceSegment &segment, const VegetationInstance &instance,
|
|
|
|
bool only_hit, bool displaced) {
|
|
|
|
if (!displaced) {
|
2015-10-18 20:15:19 +00:00
|
|
|
// Recursive call on displaced instance
|
2016-01-10 13:27:32 +00:00
|
|
|
auto base = instance.getBase();
|
|
|
|
auto terrain = parent->getTerrainRenderer()->getResult(base.x, base.z, true);
|
|
|
|
auto displaced = parent->getTerrainRenderer()->getDisplaced(base.x, base.z, true);
|
|
|
|
auto displaced_instance = instance.displace(displaced, terrain.normal);
|
2015-10-18 20:15:19 +00:00
|
|
|
return renderInstance(segment, displaced_instance, only_hit, true);
|
|
|
|
}
|
2015-10-18 15:26:19 +00:00
|
|
|
|
2015-10-18 20:15:19 +00:00
|
|
|
RayCastingResult final;
|
2015-10-18 15:26:19 +00:00
|
|
|
VegetationModelRenderer model_renderer(parent, &instance.getModel());
|
|
|
|
SpaceSegment scaled_segment(segment.getStart().sub(instance.getBase()).scale(1.0 / instance.getSize()),
|
|
|
|
segment.getEnd().sub(instance.getBase()).scale(1.0 / instance.getSize()));
|
|
|
|
VegetationResult result = model_renderer.getResult(scaled_segment, only_hit);
|
|
|
|
|
|
|
|
final.hit = result.isHit();
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
if (final.hit and not only_hit) {
|
2015-10-18 15:26:19 +00:00
|
|
|
Vector3 location = result.getLocation().scale(instance.getSize()).add(instance.getBase());
|
|
|
|
final.hit_color = parent->applyLightingToSurface(location, result.getNormal(), result.getMaterial());
|
|
|
|
final.hit_color = parent->applyMediumTraversal(location, final.hit_color);
|
|
|
|
final.hit_location = result.getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
RayCastingResult VegetationRenderer::getResult(const SpaceSegment &segment, bool only_hit) {
|
|
|
|
if (enabled) {
|
2015-11-02 19:14:35 +00:00
|
|
|
// Find instances potentially crossing the segment
|
|
|
|
VegetationGridIterator it(segment, this, only_hit);
|
2015-11-09 21:38:00 +00:00
|
|
|
if (not segment.projectedOnYPlane().iterateOnGrid(it)) {
|
2015-11-02 19:14:35 +00:00
|
|
|
return it.getResult();
|
2015-10-18 15:35:42 +00:00
|
|
|
}
|
2015-10-18 20:15:19 +00:00
|
|
|
return RayCastingResult();
|
2015-11-09 21:38:00 +00:00
|
|
|
} else {
|
2015-10-18 15:26:19 +00:00
|
|
|
return RayCastingResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
RayCastingResult VegetationRenderer::getBoundResult(const SpaceSegment &segment, double x, double z, bool only_hit,
|
|
|
|
double xsize, double zsize) {
|
2015-11-02 22:00:02 +00:00
|
|
|
VegetationDefinition *vegetation = parent->getScenery()->getVegetation();
|
|
|
|
|
2015-11-02 19:14:35 +00:00
|
|
|
// Early check if we may cross any vegetation
|
|
|
|
double ymin, ymax;
|
|
|
|
parent->getTerrainRenderer()->estimateMinMaxHeight(x, z, x + xsize, z + zsize, &ymin, &ymax);
|
2015-11-02 22:00:02 +00:00
|
|
|
ymax += vegetation->getMaxHeight();
|
2015-11-02 19:14:35 +00:00
|
|
|
SpaceSegment bbox(Vector3(x, ymin, z), Vector3(x + xsize, ymax, z + zsize));
|
|
|
|
if (not segment.intersectBoundingBox(bbox)) {
|
|
|
|
return RayCastingResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate all layers and instances
|
2015-11-20 00:16:48 +00:00
|
|
|
int n = vegetation->getLayerCount();
|
2015-11-09 21:38:00 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
2015-11-02 19:14:35 +00:00
|
|
|
VegetationLayerDefinition *layer = vegetation->getVegetationLayer(i);
|
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
vector<VegetationInstance> instances;
|
2015-11-02 19:14:35 +00:00
|
|
|
layer->getPresence()->collectInstances(&instances, *layer->getModel(), x, z, x + xsize, z + zsize);
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
for (auto &instance : instances) {
|
2015-11-02 19:14:35 +00:00
|
|
|
RayCastingResult result = renderInstance(segment, instance, only_hit);
|
2015-11-09 21:38:00 +00:00
|
|
|
if (result.hit) {
|
2015-11-02 19:14:35 +00:00
|
|
|
// TODO Don't stop at first hit, find the nearest one
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RayCastingResult();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
bool VegetationRenderer::applyLightFilter(LightComponent &light, const Vector3 &at) {
|
|
|
|
if (enabled) {
|
2015-10-18 15:35:42 +00:00
|
|
|
// Get segment to iterate
|
|
|
|
SpaceSegment segment(at, at.add(light.direction.scale(-1.0 * parent->render_quality)));
|
2015-11-09 21:38:00 +00:00
|
|
|
if (getResult(segment, true).hit) {
|
2015-10-18 15:35:42 +00:00
|
|
|
light.color = COLOR_BLACK;
|
|
|
|
return false;
|
2015-11-09 21:38:00 +00:00
|
|
|
} else {
|
2015-10-18 15:35:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-09 21:38:00 +00:00
|
|
|
} else {
|
2015-10-18 15:26:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|