vegetation: Fixed segfault in unit test

This commit is contained in:
Michaël Lemaire 2015-12-13 17:47:22 +01:00
parent 4a710c0977
commit 1d9d24b958
4 changed files with 68 additions and 48 deletions

View file

@ -29,9 +29,9 @@ void VegetationDefinition::applyPreset(VegetationPreset preset, RandomGenerator
clear(); clear();
if (preset == VEGETATION_PRESET_TEMPERATE) { /*if (preset == VEGETATION_PRESET_TEMPERATE) {
layer.applyPreset(VegetationLayerDefinition::VEGETATION_BASIC_TREES, random); layer.applyPreset(VegetationLayerDefinition::VEGETATION_BASIC_TREES, random);
layer.setName("Basic tree"); layer.setName("Basic tree");
addLayer(layer); addLayer(layer);
} }*/
} }

View file

@ -59,10 +59,7 @@ void OpenGLVegetationLayer::removeInstancesOutsideArea(double xmin, double xmax,
instances->erase(remove_if(instances->begin(), instances->end(), isNull), instances->end()); instances->erase(remove_if(instances->begin(), instances->end(), isNull), instances->end());
} }
void OpenGLVegetationLayer::threadedUpdate() { void OpenGLVegetationLayer::updateInstances() {
if (camera_changed) {
camera_changed = false;
// Compute new area around camera // Compute new area around camera
double newxmin, newxmax, newzmin, newzmax; double newxmin, newxmax, newzmin, newzmax;
newxmin = camera_location->x - range; newxmin = camera_location->x - range;
@ -98,11 +95,20 @@ void OpenGLVegetationLayer::threadedUpdate() {
} }
sort(instances.begin(), instances.end(), compareInstances); sort(instances.begin(), instances.end(), compareInstances);
lock_instances->release(); lock_instances->release();
}
// Update impostor texture void OpenGLVegetationLayer::updateImpostor() {
bool interrupted = false; bool interrupted = false;
impostor->prepareTexture(*definition->getModel(), *parent->getScenery(), &interrupted); impostor->prepareTexture(*definition->getModel(), *parent->getScenery(), &interrupted);
} }
void OpenGLVegetationLayer::threadedUpdate() {
if (camera_changed) {
camera_changed = false;
updateInstances();
updateImpostor();
}
} }
void OpenGLVegetationLayer::render() { void OpenGLVegetationLayer::render() {

View file

@ -34,6 +34,20 @@ class OPENGLSHARED_EXPORT OpenGLVegetationLayer {
virtual void removeInstancesOutsideArea(double xmin, double xmax, double zmin, double zmax, virtual void removeInstancesOutsideArea(double xmin, double xmax, double zmin, double zmax,
vector<OpenGLVegetationInstance *> *instances) const; vector<OpenGLVegetationInstance *> *instances) const;
/**
* Update the instances list.
*
* This should be called when the camera has moved enough to make a change.
*/
void updateInstances();
/**
* Update the impostor textures.
*
* This should be called when the camera has moved enough to make a change.
*/
void updateImpostor();
/** /**
* Perform maintenance tasks that can be perform in a thread. * Perform maintenance tasks that can be perform in a thread.
* *

View file

@ -28,7 +28,7 @@ class FakeLayerRenderer : public OpenGLVegetationLayer {
vector<OpenGLVegetationInstance *> static_instances; vector<OpenGLVegetationInstance *> static_instances;
}; };
TEST(OpenGLVegetationLayer, threadedUpdate) { TEST(OpenGLVegetationLayer, updateInstances) {
CameraDefinition camera; CameraDefinition camera;
VegetationLayerDefinition definition(NULL, "test"); VegetationLayerDefinition definition(NULL, "test");
FakeLayerRenderer rendering(&definition); FakeLayerRenderer rendering(&definition);
@ -36,32 +36,32 @@ TEST(OpenGLVegetationLayer, threadedUpdate) {
EXPECT_EQ(0, rendering.getInstanceCount()); EXPECT_EQ(0, rendering.getInstanceCount());
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(0, rendering.getInstanceCount()); EXPECT_EQ(0, rendering.getInstanceCount());
rendering.static_instances.push_back( rendering.static_instances.push_back(
new OpenGLVegetationInstance(VegetationInstance(model, Vector3(0.0, 0.0, 0.0)))); new OpenGLVegetationInstance(VegetationInstance(model, Vector3(0.0, 0.0, 0.0))));
rendering.reset(); rendering.reset();
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(1, rendering.getInstanceCount()); EXPECT_EQ(1, rendering.getInstanceCount());
camera.setLocation(Vector3(-5.0, 0.0, 0.0)); camera.setLocation(Vector3(-5.0, 0.0, 0.0));
rendering.setCamera(&camera); rendering.setCamera(&camera);
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(1, rendering.getInstanceCount()); EXPECT_EQ(1, rendering.getInstanceCount());
camera.setLocation(Vector3(-11.0, 0.0, 0.0)); camera.setLocation(Vector3(-11.0, 0.0, 0.0));
rendering.setCamera(&camera); rendering.setCamera(&camera);
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(0, rendering.getInstanceCount()); EXPECT_EQ(0, rendering.getInstanceCount());
camera.setLocation(Vector3(0.0, 0.0, 5.0)); camera.setLocation(Vector3(0.0, 0.0, 5.0));
rendering.setCamera(&camera); rendering.setCamera(&camera);
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(1, rendering.getInstanceCount()); EXPECT_EQ(1, rendering.getInstanceCount());
camera.setLocation(Vector3(0.0, 0.0, 15.0)); camera.setLocation(Vector3(0.0, 0.0, 15.0));
rendering.setCamera(&camera); rendering.setCamera(&camera);
rendering.threadedUpdate(); rendering.updateInstances();
EXPECT_EQ(0, rendering.getInstanceCount()); EXPECT_EQ(0, rendering.getInstanceCount());
} }