2015-11-25 22:15:58 +00:00
|
|
|
#include "OpenGLVegetation.h"
|
|
|
|
|
|
|
|
#include "OpenGLRenderer.h"
|
|
|
|
#include "OpenGLShaderProgram.h"
|
|
|
|
#include "OpenGLVegetationLayer.h"
|
|
|
|
#include "Thread.h"
|
|
|
|
#include "Mutex.h"
|
|
|
|
#include "Scenery.h"
|
|
|
|
#include "VegetationDefinition.h"
|
|
|
|
#include "VegetationLayerDefinition.h"
|
|
|
|
|
|
|
|
class paysages::opengl::VegetationUpdater : public Thread {
|
|
|
|
public:
|
|
|
|
VegetationUpdater(OpenGLVegetation *vegetation) : vegetation(vegetation) {
|
|
|
|
interrupted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void interrupt(bool wait = true) {
|
|
|
|
interrupted = true;
|
|
|
|
if (wait) {
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void run() override {
|
|
|
|
while (not interrupted) {
|
2015-12-13 16:16:26 +00:00
|
|
|
vector<OpenGLVegetationLayer *> layers;
|
2015-11-25 22:15:58 +00:00
|
|
|
vegetation->acquireLayers(layers);
|
2015-11-29 18:18:36 +00:00
|
|
|
for (auto layer : layers) {
|
2015-11-25 22:15:58 +00:00
|
|
|
layer->threadedUpdate();
|
|
|
|
}
|
|
|
|
vegetation->releaseLayers(layers);
|
|
|
|
timeSleepMs(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool interrupted;
|
|
|
|
OpenGLVegetation *vegetation;
|
|
|
|
};
|
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
OpenGLVegetation::OpenGLVegetation(OpenGLRenderer *renderer) : OpenGLPart(renderer, "vegetation") {
|
2015-11-25 22:15:58 +00:00
|
|
|
layers_lock = new Mutex();
|
|
|
|
updater = new VegetationUpdater(this);
|
|
|
|
enabled = true;
|
|
|
|
|
|
|
|
// Watch scenery changes
|
|
|
|
renderer->getScenery()->getVegetation()->addWatcher(this, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLVegetation::~OpenGLVegetation() {
|
2015-12-16 09:36:21 +00:00
|
|
|
updater->interrupt();
|
|
|
|
delete updater;
|
2015-11-25 22:15:58 +00:00
|
|
|
|
|
|
|
delete layers_lock;
|
|
|
|
|
2015-12-16 00:33:02 +00:00
|
|
|
for (auto &layer : layers) {
|
2015-12-16 09:36:21 +00:00
|
|
|
delete layer;
|
|
|
|
}
|
|
|
|
layers.clear();
|
2015-11-25 22:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::initialize() {
|
|
|
|
// Start the threaded updater
|
|
|
|
updater->start();
|
|
|
|
|
|
|
|
// Prepare shader programs
|
|
|
|
program = createShader("vegetation");
|
|
|
|
program->addVertexSource("vegetation");
|
|
|
|
program->addFragmentSource("atmosphere");
|
|
|
|
program->addFragmentSource("tonemapping");
|
|
|
|
program->addFragmentSource("ui");
|
|
|
|
program->addFragmentSource("vegetation");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::update() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::render() {
|
|
|
|
if (enabled) {
|
2015-12-13 16:16:26 +00:00
|
|
|
vector<OpenGLVegetationLayer *> layers;
|
2015-11-25 22:15:58 +00:00
|
|
|
acquireLayers(layers);
|
2015-11-29 18:18:36 +00:00
|
|
|
for (auto layer : layers) {
|
2015-11-25 22:15:58 +00:00
|
|
|
layer->render();
|
|
|
|
}
|
|
|
|
releaseLayers(layers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) {
|
|
|
|
if (node->getPath() == "/vegetation") {
|
|
|
|
updateLayers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
Scenery *OpenGLVegetation::getScenery() const {
|
2015-11-25 22:15:58 +00:00
|
|
|
return renderer->getScenery();
|
|
|
|
}
|
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
void OpenGLVegetation::cameraChanged(const CameraDefinition *camera) {
|
2015-12-13 16:16:26 +00:00
|
|
|
vector<OpenGLVegetationLayer *> layers;
|
2015-11-25 22:15:58 +00:00
|
|
|
acquireLayers(layers);
|
2015-11-29 18:18:36 +00:00
|
|
|
for (auto layer : layers) {
|
2015-11-25 22:15:58 +00:00
|
|
|
layer->setCamera(camera);
|
|
|
|
}
|
|
|
|
releaseLayers(layers);
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
void OpenGLVegetation::acquireLayers(vector<OpenGLVegetationLayer *> &layers) {
|
2015-11-25 22:15:58 +00:00
|
|
|
layers_lock->acquire();
|
|
|
|
|
|
|
|
for (auto layer : this->layers) {
|
|
|
|
// TODO Reference count
|
|
|
|
layers.push_back(layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
layers_lock->release();
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:16:26 +00:00
|
|
|
void OpenGLVegetation::releaseLayers(const vector<OpenGLVegetationLayer *> &layers) {
|
2015-11-25 22:15:58 +00:00
|
|
|
// TODO Reference count
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLVegetationLayer *OpenGLVegetation::findLayer(VegetationLayerDefinition *layer) {
|
|
|
|
for (auto &l : layers) {
|
|
|
|
if (l->getDefinition() == layer) {
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::setEnabled(bool enabled) {
|
|
|
|
this->enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLVegetation::updateLayers() {
|
|
|
|
layers_lock->acquire();
|
|
|
|
|
|
|
|
// Add missing layers
|
|
|
|
int n = renderer->getScenery()->getVegetation()->getLayerCount();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
VegetationLayerDefinition *layer = renderer->getScenery()->getVegetation()->getVegetationLayer(i);
|
|
|
|
if (!findLayer(layer)) {
|
|
|
|
layers.push_back(new OpenGLVegetationLayer(this, layer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Mark extraneous layers for deletion
|
|
|
|
|
|
|
|
layers_lock->release();
|
|
|
|
}
|