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();
if (preset == VEGETATION_PRESET_TEMPERATE) {
/*if (preset == VEGETATION_PRESET_TEMPERATE) {
layer.applyPreset(VegetationLayerDefinition::VEGETATION_BASIC_TREES, random);
layer.setName("Basic tree");
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());
}
void OpenGLVegetationLayer::threadedUpdate() {
if (camera_changed) {
camera_changed = false;
void OpenGLVegetationLayer::updateInstances() {
// Compute new area around camera
double newxmin, newxmax, newzmin, newzmax;
newxmin = camera_location->x - range;
@ -98,10 +95,19 @@ void OpenGLVegetationLayer::threadedUpdate() {
}
sort(instances.begin(), instances.end(), compareInstances);
lock_instances->release();
}
// Update impostor texture
void OpenGLVegetationLayer::updateImpostor() {
bool interrupted = false;
impostor->prepareTexture(*definition->getModel(), *parent->getScenery(), &interrupted);
}
void OpenGLVegetationLayer::threadedUpdate() {
if (camera_changed) {
camera_changed = false;
updateInstances();
updateImpostor();
}
}

View file

@ -34,6 +34,20 @@ class OPENGLSHARED_EXPORT OpenGLVegetationLayer {
virtual void removeInstancesOutsideArea(double xmin, double xmax, double zmin, double zmax,
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.
*

View file

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