2015-10-18 20:15:19 +00:00
|
|
|
#include "VegetationPresenceDefinition.h"
|
|
|
|
|
2015-12-08 23:40:47 +00:00
|
|
|
#include <cmath>
|
2015-10-18 20:15:19 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "TerrainDefinition.h"
|
|
|
|
#include "VegetationLayerDefinition.h"
|
2015-10-18 22:30:20 +00:00
|
|
|
#include "VegetationModelDefinition.h"
|
2015-10-18 20:15:19 +00:00
|
|
|
#include "VegetationInstance.h"
|
2015-10-18 22:30:20 +00:00
|
|
|
#include "FloatNode.h"
|
|
|
|
#include "NoiseNode.h"
|
2016-01-03 19:22:06 +00:00
|
|
|
#include "FractalNoise.h"
|
2015-10-18 20:15:19 +00:00
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
VegetationPresenceDefinition::VegetationPresenceDefinition(VegetationLayerDefinition *parent)
|
|
|
|
: DefinitionNode(parent, "presence") {
|
2015-10-18 22:30:20 +00:00
|
|
|
noise = new NoiseNode(this);
|
|
|
|
interval = new FloatNode(this, "interval", 0.1);
|
2015-10-18 20:15:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
bool VegetationPresenceDefinition::collectInstances(vector<VegetationInstance> *result,
|
2015-11-09 21:38:00 +00:00
|
|
|
const VegetationModelDefinition &model, double xmin, double zmin,
|
|
|
|
double xmax, double zmax, bool outcomers) const {
|
|
|
|
if (outcomers) {
|
2015-11-02 22:00:02 +00:00
|
|
|
// Expand the area to include outcoming instances
|
|
|
|
double max_radius = getMaxHeight();
|
|
|
|
xmin -= max_radius;
|
|
|
|
zmin -= max_radius;
|
|
|
|
xmax += max_radius;
|
|
|
|
zmax += max_radius;
|
|
|
|
}
|
|
|
|
|
2016-01-03 19:22:06 +00:00
|
|
|
int added = 0;
|
2015-10-18 22:30:20 +00:00
|
|
|
|
2016-01-03 19:22:06 +00:00
|
|
|
auto generator = noise->getGenerator();
|
2015-10-18 22:30:20 +00:00
|
|
|
double interval_value = interval->getValue();
|
|
|
|
|
|
|
|
double xstart = xmin - fmod(xmin, interval_value);
|
|
|
|
double zstart = zmin - fmod(zmin, interval_value);
|
2015-11-09 21:38:00 +00:00
|
|
|
for (double x = xstart; x < xmax; x += interval_value) {
|
|
|
|
for (double z = zstart; z < zmax; z += interval_value) {
|
2016-01-03 19:22:06 +00:00
|
|
|
double detail = interval_value * 0.1;
|
|
|
|
double noise_presence = generator->get2d(detail, x * 0.1, z * 0.1);
|
2015-11-09 21:38:00 +00:00
|
|
|
if (noise_presence > 0.0) {
|
|
|
|
double size =
|
2016-01-03 19:22:06 +00:00
|
|
|
0.1 + 0.2 * fabs(generator->get2d(detail, z * 10.0, x * 10.0)) * (noise_presence * 0.5 + 0.5);
|
|
|
|
double angle = 3.0 * generator->get2d(detail, -x * 20.0, z * 20.0); // TODO balanced distribution
|
|
|
|
double xo = x + fabs(generator->get2d(detail, x * 12.0, -z * 12.0));
|
|
|
|
double zo = z + fabs(generator->get2d(detail, -x * 27.0, -z * 27.0));
|
2015-11-29 18:18:36 +00:00
|
|
|
if (xo >= xmin and xo < xmax and zo >= zmin and zo < zmax) {
|
2015-11-25 22:15:58 +00:00
|
|
|
double y = getScenery()->getTerrain()->getInterpolatedHeight(xo, zo, true, true);
|
|
|
|
result->push_back(VegetationInstance(model, Vector3(xo, y, zo), size, angle));
|
|
|
|
added++;
|
|
|
|
}
|
2015-10-18 22:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return added > 0;
|
2015-10-18 20:15:19 +00:00
|
|
|
}
|
2015-11-02 22:00:02 +00:00
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
double VegetationPresenceDefinition::getMaxHeight() const {
|
2015-11-02 22:00:02 +00:00
|
|
|
return 0.3;
|
|
|
|
}
|