2012-05-08 12:07:15 +00:00
|
|
|
#include "wandererchunk.h"
|
|
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QGLWidget>
|
2012-05-08 22:03:39 +00:00
|
|
|
#include <math.h>
|
2012-05-08 12:07:15 +00:00
|
|
|
#include "../lib_paysages/color.h"
|
2012-05-08 22:03:39 +00:00
|
|
|
#include "../lib_paysages/euclid.h"
|
2012-05-08 12:07:15 +00:00
|
|
|
#include "../lib_paysages/tools.h"
|
|
|
|
|
2012-05-08 22:03:39 +00:00
|
|
|
WandererChunk::WandererChunk(Renderer* renderer, double x, double z, double size)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
_renderer = renderer;
|
|
|
|
|
2012-05-08 12:07:15 +00:00
|
|
|
_startx = x;
|
|
|
|
_startz = z;
|
2012-05-08 22:03:39 +00:00
|
|
|
_size = size;
|
|
|
|
|
|
|
|
_ideal_tessellation = 1;
|
|
|
|
_ideal_priority = 0.0;
|
|
|
|
|
|
|
|
_tessellation_max_size = 32;
|
|
|
|
_tessellation = new double[(_tessellation_max_size + 1) * (_tessellation_max_size + 1)];
|
|
|
|
_tessellation_current_size = 0;
|
|
|
|
_tessellation_step = _size / (double)_tessellation_max_size;
|
|
|
|
|
2012-05-09 11:39:03 +00:00
|
|
|
_texture_max_size = 128;
|
2012-05-08 22:03:39 +00:00
|
|
|
_texture_current_size = 0;
|
2012-05-08 14:07:47 +00:00
|
|
|
_texture = new QImage(1, 1, QImage::Format_ARGB32);
|
2012-05-08 12:07:15 +00:00
|
|
|
_texture_id = 0;
|
2012-05-08 22:03:39 +00:00
|
|
|
_texture_changed = false;
|
|
|
|
|
2012-05-09 11:39:03 +00:00
|
|
|
maintain();
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WandererChunk::~WandererChunk()
|
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
_lock_data.lock();
|
2012-05-08 12:07:15 +00:00
|
|
|
delete _texture;
|
2012-05-08 22:03:39 +00:00
|
|
|
delete [] _tessellation;
|
|
|
|
_lock_data.unlock();
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WandererChunk::render(QGLWidget* widget)
|
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
_lock_data.lock();
|
2012-05-08 12:07:15 +00:00
|
|
|
|
2012-05-08 22:03:39 +00:00
|
|
|
if (_texture_changed)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
_texture_changed = false;
|
2012-05-08 12:07:15 +00:00
|
|
|
if (_texture_id)
|
|
|
|
{
|
|
|
|
widget->deleteTexture(_texture_id);
|
|
|
|
}
|
|
|
|
_texture_id = widget->bindTexture(*_texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
2012-05-08 22:03:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _texture_id);
|
|
|
|
}
|
2012-05-08 12:07:15 +00:00
|
|
|
|
2012-05-08 22:03:39 +00:00
|
|
|
int tessellation_size = _tessellation_current_size;
|
|
|
|
int tessellation_inc = _tessellation_max_size / (double)tessellation_size;
|
|
|
|
double tsize = 1.0 / (double)_tessellation_max_size;
|
|
|
|
|
|
|
|
_lock_data.unlock();
|
|
|
|
|
|
|
|
for (int j = 0; j < _tessellation_max_size; j += tessellation_inc)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
|
|
|
glBegin(GL_QUAD_STRIP);
|
2012-05-08 22:03:39 +00:00
|
|
|
for (int i = 0; i <= _tessellation_max_size; i += tessellation_inc)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
|
|
|
glTexCoord2d(tsize * (double)i, 1.0 - tsize * (double)j);
|
2012-05-08 22:03:39 +00:00
|
|
|
glVertex3d(_startx + _tessellation_step * (double)i, _tessellation[j * (_tessellation_max_size + 1) + i], _startz + _tessellation_step * (double)j);
|
|
|
|
glTexCoord2d(tsize * (double)i, 1.0 - 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-05-08 12:07:15 +00:00
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-09 11:39:03 +00:00
|
|
|
void WandererChunk::updatePriority(Vector3 camera_location)
|
|
|
|
{
|
|
|
|
// Compute new priority
|
|
|
|
_lock_data.lock();
|
|
|
|
if (_tessellation_current_size == _tessellation_max_size && _texture_current_size == _texture_max_size)
|
|
|
|
{
|
|
|
|
_ideal_priority = -1000.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double distance = v3Norm(v3Sub(camera_location, getCenter()));
|
|
|
|
distance = distance < 0.1 ? 0.1 : distance;
|
|
|
|
_ideal_tessellation = (int)ceil(120.0 - distance / 3.0);
|
|
|
|
_ideal_priority = _ideal_tessellation - _texture_current_size;
|
|
|
|
if (_texture_current_size == 1)
|
|
|
|
{
|
|
|
|
_ideal_priority += 100.0;
|
|
|
|
}
|
|
|
|
else if (distance < 15.0 && _texture_current_size < _texture_max_size)
|
|
|
|
{
|
|
|
|
_ideal_priority += 75.0;
|
|
|
|
}
|
|
|
|
else if (distance < 30.0 && _texture_current_size < _texture_max_size / 2)
|
|
|
|
{
|
|
|
|
_ideal_priority += 50.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_lock_data.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WandererChunk::maintain()
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
if (_tessellation_current_size < _tessellation_max_size || _texture_current_size < _texture_max_size)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
// Improve heightmap resolution
|
|
|
|
if (_tessellation_current_size < _tessellation_max_size)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
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)
|
2012-05-08 12:07:15 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
for (int i = 0; i <= _tessellation_max_size; i += new_tessellation_inc)
|
2012-05-08 14:07:47 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
if (_tessellation_current_size == 0 || i % old_tessellation_inc != 0 || j % old_tessellation_inc != 0)
|
|
|
|
{
|
|
|
|
double height = _renderer->getTerrainHeight(_renderer, _startx + _tessellation_step * (double)i, _startz + _tessellation_step * (double)j);
|
|
|
|
_tessellation[j * (_tessellation_max_size + 1) + i] = height;
|
|
|
|
}
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|
2012-05-08 22:03:39 +00:00
|
|
|
|
|
|
|
_lock_data.lock();
|
|
|
|
_tessellation_current_size = new_tessellation_size;
|
|
|
|
_lock_data.unlock();
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|
2012-05-08 14:07:47 +00:00
|
|
|
|
2012-05-08 22:03:39 +00:00
|
|
|
// Improve texture resolution
|
|
|
|
if (_texture_current_size < _texture_max_size)
|
2012-05-08 14:07:47 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
int new_texture_size = _texture_current_size ? _texture_current_size * 2 : 1;
|
|
|
|
double step_size = _texture_current_size ? _size / (double)(new_texture_size - 1) : 0.1;
|
|
|
|
QImage* new_image = new QImage(new_texture_size, new_texture_size, QImage::Format_ARGB32);
|
|
|
|
for (int j = 0; j < new_texture_size; j++)
|
2012-05-08 14:07:47 +00:00
|
|
|
{
|
2012-05-08 22:03:39 +00:00
|
|
|
for (int i = 0; i < new_texture_size; i++)
|
2012-05-08 14:07:47 +00:00
|
|
|
{
|
|
|
|
Vector3 location = {_startx + step_size * (double)i, 0.0, _startz + step_size * (double)j};
|
2012-05-08 22:03:39 +00:00
|
|
|
Color color = _renderer->applyTextures(_renderer, location, step_size);
|
2012-05-08 14:07:47 +00:00
|
|
|
new_image->setPixel(i, j, colorTo32BitRGBA(&color));
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 22:03:39 +00:00
|
|
|
|
|
|
|
_lock_data.lock();
|
2012-05-08 14:07:47 +00:00
|
|
|
delete _texture;
|
|
|
|
_texture = new_image;
|
2012-05-08 22:03:39 +00:00
|
|
|
_texture_current_size = new_texture_size;
|
|
|
|
_texture_changed = true;
|
|
|
|
_lock_data.unlock();
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 11:39:03 +00:00
|
|
|
return true;
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-09 11:39:03 +00:00
|
|
|
return false;
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 WandererChunk::getCenter()
|
|
|
|
{
|
|
|
|
Vector3 result;
|
2012-05-08 12:07:15 +00:00
|
|
|
|
2012-05-08 22:03:39 +00:00
|
|
|
result.x = _startx + _size / 2.0;
|
2012-05-08 14:07:47 +00:00
|
|
|
result.y = 0.0;
|
2012-05-08 22:03:39 +00:00
|
|
|
result.z = _startz + _size / 2.0;
|
2012-05-08 14:07:47 +00:00
|
|
|
|
|
|
|
return result;
|
2012-05-08 12:07:15 +00:00
|
|
|
}
|