2013-11-05 10:45:26 +00:00
|
|
|
#include "ExplorerChunkTerrain.h"
|
2012-06-08 12:28:46 +00:00
|
|
|
|
2013-11-05 10:45:26 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <GL/gl.h>
|
2013-11-14 17:47:03 +00:00
|
|
|
#include "CameraDefinition.h"
|
|
|
|
#include "renderer.h"
|
|
|
|
#include "terrain/public.h"
|
2012-06-08 12:28:46 +00:00
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
ExplorerChunkTerrain::ExplorerChunkTerrain(Renderer* renderer, double x, double z, double size, int nbchunks, double water_height) : BaseExplorerChunk(renderer)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_startx = x;
|
|
|
|
_startz = z;
|
|
|
|
_size = size;
|
2013-04-27 19:41:57 +00:00
|
|
|
_overall_step = size * (double) nbchunks;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
_distance_to_camera = 0.0;
|
|
|
|
|
|
|
|
_water_height = water_height;
|
|
|
|
_overwater = false;
|
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
_tessellation_max_size = 32;
|
2012-06-17 09:40:40 +00:00
|
|
|
_tessellation = new double[(_tessellation_max_size + 1) * (_tessellation_max_size + 1)];
|
2012-06-08 12:28:46 +00:00
|
|
|
_tessellation_current_size = 0;
|
2013-04-27 19:41:57 +00:00
|
|
|
_tessellation_step = _size / (double) _tessellation_max_size;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
setMaxTextureSize(128);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
maintain();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExplorerChunkTerrain::~ExplorerChunkTerrain()
|
|
|
|
{
|
|
|
|
_lock_data.lock();
|
|
|
|
delete [] _tessellation;
|
|
|
|
_lock_data.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExplorerChunkTerrain::onResetEvent()
|
|
|
|
{
|
|
|
|
_tessellation_current_size = 0;
|
2013-02-05 21:25:30 +00:00
|
|
|
_overwater = false;
|
2012-06-08 12:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExplorerChunkTerrain::onMaintainEvent()
|
|
|
|
{
|
|
|
|
Renderer* renderer = this->renderer();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
// Improve heightmap resolution
|
|
|
|
if (_tessellation_current_size < _tessellation_max_size)
|
|
|
|
{
|
|
|
|
int new_tessellation_size = _tessellation_current_size ? _tessellation_current_size * 4 : 2;
|
|
|
|
int old_tessellation_inc = _tessellation_current_size ? _tessellation_max_size / _tessellation_current_size : 1;
|
|
|
|
int new_tessellation_inc = _tessellation_max_size / new_tessellation_size;
|
|
|
|
for (int j = 0; j <= _tessellation_max_size; j += new_tessellation_inc)
|
|
|
|
{
|
|
|
|
for (int i = 0; i <= _tessellation_max_size; i += new_tessellation_inc)
|
|
|
|
{
|
|
|
|
if (_tessellation_current_size == 0 || i % old_tessellation_inc != 0 || j % old_tessellation_inc != 0)
|
|
|
|
{
|
2013-04-27 19:41:57 +00:00
|
|
|
double height = renderer->terrain->getHeight(renderer, _startx + _tessellation_step * (double) i, _startz + _tessellation_step * (double) j, 1);
|
2013-02-05 21:25:30 +00:00
|
|
|
if (height >= _water_height)
|
|
|
|
{
|
|
|
|
_overwater = true;
|
|
|
|
}
|
2012-06-08 12:28:46 +00:00
|
|
|
_tessellation[j * (_tessellation_max_size + 1) + i] = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_lock_data.lock();
|
|
|
|
_tessellation_current_size = new_tessellation_size;
|
|
|
|
_lock_data.unlock();
|
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
if (_tessellation_current_size < 4 && _tessellation_current_size < _tessellation_max_size)
|
2013-01-30 21:36:49 +00:00
|
|
|
{
|
|
|
|
onMaintainEvent();
|
|
|
|
}
|
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExplorerChunkTerrain::onCameraEvent(CameraDefinition* camera)
|
|
|
|
{
|
2013-11-14 17:47:03 +00:00
|
|
|
Vector3 camera_location = camera->getLocation();
|
2013-04-27 19:41:57 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
// Handle position
|
|
|
|
_lock_data.lock();
|
2013-04-27 19:41:57 +00:00
|
|
|
if (camera_location.x > _startx + _overall_step * 0.5)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_startx += _overall_step;
|
|
|
|
askReset();
|
|
|
|
}
|
2013-04-27 19:41:57 +00:00
|
|
|
if (camera_location.z > _startz + _overall_step * 0.5)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_startz += _overall_step;
|
|
|
|
askReset();
|
|
|
|
}
|
2013-04-27 19:41:57 +00:00
|
|
|
if (camera_location.x < _startx - _overall_step * 0.5)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_startx -= _overall_step;
|
|
|
|
askReset();
|
|
|
|
}
|
2013-04-27 19:41:57 +00:00
|
|
|
if (camera_location.z < _startz - _overall_step * 0.5)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_startz -= _overall_step;
|
|
|
|
askReset();
|
|
|
|
}
|
2013-02-05 21:25:30 +00:00
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
_distance_to_camera = v3Norm(v3Sub(getCenter(), camera_location));
|
2013-03-31 20:27:21 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
_lock_data.unlock();
|
|
|
|
}
|
|
|
|
|
2012-12-24 15:15:40 +00:00
|
|
|
void ExplorerChunkTerrain::onRenderEvent(QGLWidget*)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
_lock_data.lock();
|
|
|
|
int tessellation_size = _tessellation_current_size;
|
2013-04-27 19:41:57 +00:00
|
|
|
double tsize = 1.0 / (double) _tessellation_max_size;
|
2012-06-08 12:28:46 +00:00
|
|
|
_lock_data.unlock();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
if (tessellation_size <= 1 or not _overwater)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-27 19:41:57 +00:00
|
|
|
int tessellation_inc = _tessellation_max_size / (double) tessellation_size;
|
2012-06-08 12:28:46 +00:00
|
|
|
for (int j = 0; j < _tessellation_max_size; j += tessellation_inc)
|
|
|
|
{
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
|
|
|
for (int i = 0; i <= _tessellation_max_size; i += tessellation_inc)
|
|
|
|
{
|
2013-04-27 19:41:57 +00:00
|
|
|
glTexCoord2d(tsize * (double) i, tsize * (double) j);
|
|
|
|
glVertex3d(_startx + _tessellation_step * (double) i, _tessellation[j * (_tessellation_max_size + 1) + i], _startz + _tessellation_step * (double) j);
|
|
|
|
glTexCoord2d(tsize * (double) i, tsize * (double) (j + tessellation_inc));
|
|
|
|
glVertex3d(_startx + _tessellation_step * (double) i, _tessellation[(j + tessellation_inc) * (_tessellation_max_size + 1) + i], _startz + _tessellation_step * (double) (j + tessellation_inc));
|
2012-06-08 12:28:46 +00:00
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double ExplorerChunkTerrain::getDisplayedSizeHint(CameraDefinition* camera)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double distance;
|
2012-06-08 12:28:46 +00:00
|
|
|
Vector3 center;
|
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
if (not _overwater)
|
|
|
|
{
|
|
|
|
return -1000.0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
center = getCenter();
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
if (camera->isBoxInView(center, _size, 40.0, _size))
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
2013-02-05 21:25:30 +00:00
|
|
|
distance = _distance_to_camera;
|
2012-06-09 20:26:34 +00:00
|
|
|
distance = distance < 0.1 ? 0.1 : distance;
|
2013-04-27 19:41:57 +00:00
|
|
|
return (int) ceil(120.0 - distance / 1.5);
|
2012-06-09 20:26:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -800.0;
|
2012-06-08 12:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
Color ExplorerChunkTerrain::getTextureColor(double x, double y)
|
2012-06-08 12:28:46 +00:00
|
|
|
{
|
|
|
|
Vector3 location = {_startx + x * _size, 0.0, _startz + y * _size};
|
2013-03-31 20:27:21 +00:00
|
|
|
return renderer()->terrain->getFinalColor(renderer(), location, 0.01);
|
2012-06-08 12:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 ExplorerChunkTerrain::getCenter()
|
|
|
|
{
|
|
|
|
Vector3 result;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
result.x = _startx + _size / 2.0;
|
|
|
|
result.y = 0.0;
|
|
|
|
result.z = _startz + _size / 2.0;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-08 12:28:46 +00:00
|
|
|
return result;
|
|
|
|
}
|