Fixed underwater terrain detection in opengl renderer

This commit is contained in:
Michaël Lemaire 2015-08-19 17:22:14 +02:00
parent 95d2f55ebf
commit 100ee0eec1
2 changed files with 39 additions and 11 deletions

View file

@ -8,6 +8,8 @@
#include "OpenGLRenderer.h" #include "OpenGLRenderer.h"
#include "TerrainRenderer.h" #include "TerrainRenderer.h"
#include "VertexArray.h" #include "VertexArray.h"
#include "Scenery.h"
#include "TerrainDefinition.h"
ExplorerChunkTerrain::ExplorerChunkTerrain(OpenGLRenderer* renderer, double x, double z, double size, int nbchunks): ExplorerChunkTerrain::ExplorerChunkTerrain(OpenGLRenderer* renderer, double x, double z, double size, int nbchunks):
_renderer(renderer) _renderer(renderer)
@ -31,8 +33,9 @@ ExplorerChunkTerrain::ExplorerChunkTerrain(OpenGLRenderer* renderer, double x, d
_overall_step = size * (double) nbchunks; _overall_step = size * (double) nbchunks;
distance_to_camera = 0.0; distance_to_camera = 0.0;
underwater = false;
overwater = false; lowest = 0.0;
highest = 0.0;
tessellation_count = 33; tessellation_count = 33;
tessellated = new VertexArray<TerrainVertex>(); tessellated = new VertexArray<TerrainVertex>();
@ -62,15 +65,23 @@ bool ExplorerChunkTerrain::maintain()
{ {
_reset_topology = false; _reset_topology = false;
_tessellation_current_size = 0; _tessellation_current_size = 0;
overwater = false; lowest = 10000.0;
highest = -10000.0;
underwater = false;
} }
if (_reset_texture) if (_reset_texture)
{ {
_reset_texture = false; _reset_texture = false;
_texture_current_size = 0; _texture_current_size = 0;
underwater = false;
} }
_lock_data.unlock(); _lock_data.unlock();
if (underwater)
{
return false;
}
// Improve heightmap resolution // Improve heightmap resolution
if (_tessellation_current_size < _tessellation_max_size) if (_tessellation_current_size < _tessellation_max_size)
{ {
@ -90,9 +101,13 @@ bool ExplorerChunkTerrain::maintain()
double z = _startz + _tessellation_step * (float)j; double z = _startz + _tessellation_step * (float)j;
double height = _renderer->getTerrainRenderer()->getHeight(x, z, true, false); double height = _renderer->getTerrainRenderer()->getHeight(x, z, true, false);
if (height >= 0.0) if (height >= highest)
{ {
overwater = true; highest = height;
}
if (height <= lowest)
{
lowest = height;
} }
TerrainVertex v; TerrainVertex v;
@ -113,6 +128,8 @@ bool ExplorerChunkTerrain::maintain()
} }
} }
underwater = highest < -_renderer->getScenery()->getTerrain()->getWaterOffset();
_lock_data.lock(); _lock_data.lock();
_tessellation_current_size = new_tessellation_size; _tessellation_current_size = new_tessellation_size;
tessellated->setAutoGridIndices(tessellation_count, new_tessellation_inc); tessellated->setAutoGridIndices(tessellation_count, new_tessellation_inc);
@ -170,6 +187,14 @@ bool ExplorerChunkTerrain::maintain()
void ExplorerChunkTerrain::updatePriority(CameraDefinition* camera) void ExplorerChunkTerrain::updatePriority(CameraDefinition* camera)
{ {
// Under water check
underwater = highest < -_renderer->getScenery()->getTerrain()->getWaterOffset();
if (underwater)
{
priority = -10000.0;
return;
}
Vector3 camera_location = camera->getLocation(); Vector3 camera_location = camera->getLocation();
// Handle position // Handle position
@ -198,11 +223,7 @@ void ExplorerChunkTerrain::updatePriority(CameraDefinition* camera)
_lock_data.unlock(); _lock_data.unlock();
// Update wanted LOD // Update wanted LOD
if (not overwater) if (distance_to_camera < 60.0)
{
_texture_wanted_size = 2;
}
else if (distance_to_camera < 60.0)
{ {
_texture_wanted_size = _texture_max_size; _texture_wanted_size = _texture_max_size;
} }
@ -237,6 +258,11 @@ void ExplorerChunkTerrain::updatePriority(CameraDefinition* camera)
void ExplorerChunkTerrain::render(QOpenGLShaderProgram* program, OpenGLFunctions* functions) void ExplorerChunkTerrain::render(QOpenGLShaderProgram* program, OpenGLFunctions* functions)
{ {
if (underwater)
{
return;
}
// Put texture in place // Put texture in place
_lock_data.lock(); _lock_data.lock();
if (_texture_changed) if (_texture_changed)

View file

@ -65,8 +65,10 @@ private:
int _texture_max_size; int _texture_max_size;
// LOD control // LOD control
bool underwater;
double lowest;
double highest;
double distance_to_camera; double distance_to_camera;
bool overwater;
}; };
} }