OpenGLTerrain does not use Qt containers anymore
This commit is contained in:
parent
5d24edad32
commit
8ff83d48e7
5 changed files with 57 additions and 39 deletions
|
@ -31,7 +31,7 @@ class OPENGLSHARED_EXPORT OpenGLShaderProgram {
|
|||
*
|
||||
* *state* is optional and may add ponctual variables to the global state.
|
||||
*/
|
||||
void draw(OpenGLVertexArray *vertices, OpenGLSharedState *state = NULL, int start=0, int count=-1);
|
||||
void draw(OpenGLVertexArray *vertices, OpenGLSharedState *state = NULL, int start = 0, int count = -1);
|
||||
|
||||
inline QOpenGLShaderProgram *getProgram() const {
|
||||
return program;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#include "OpenGLTerrain.h"
|
||||
|
||||
#include <vector>
|
||||
#include "OpenGLFunctions.h"
|
||||
#include "OpenGLRenderer.h"
|
||||
#include "OpenGLShaderProgram.h"
|
||||
#include "ParallelPool.h"
|
||||
#include "Thread.h"
|
||||
#include "Mutex.h"
|
||||
#include "OpenGLTerrainChunk.h"
|
||||
#include "WaterRenderer.h"
|
||||
#include "CameraDefinition.h"
|
||||
|
@ -32,7 +34,22 @@ class ChunkMaintenanceThreads : public ParallelPool {
|
|||
OpenGLTerrain *terrain;
|
||||
};
|
||||
|
||||
class OpenGLTerrainChunkCmp {
|
||||
public:
|
||||
bool operator()(const OpenGLTerrainChunk *lhs, const OpenGLTerrainChunk *rhs) const {
|
||||
return lhs->getPriority() < rhs->getPriority();
|
||||
}
|
||||
};
|
||||
|
||||
class paysages::opengl::OpenGLTerrainPV {
|
||||
public:
|
||||
Mutex lock;
|
||||
vector<OpenGLTerrainChunk *> chunks;
|
||||
vector<OpenGLTerrainChunk *> queue;
|
||||
};
|
||||
|
||||
OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer, "terrain") {
|
||||
pv = new OpenGLTerrainPV();
|
||||
work = new ChunkMaintenanceThreads(this);
|
||||
paused = false;
|
||||
|
||||
|
@ -48,9 +65,11 @@ OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer, "t
|
|||
OpenGLTerrain::~OpenGLTerrain() {
|
||||
delete work;
|
||||
|
||||
for (int i = 0; i < _chunks.count(); i++) {
|
||||
delete _chunks[i];
|
||||
for (auto chunk : pv->chunks) {
|
||||
delete chunk;
|
||||
}
|
||||
|
||||
delete pv;
|
||||
}
|
||||
|
||||
void OpenGLTerrain::initialize() {
|
||||
|
@ -63,8 +82,8 @@ void OpenGLTerrain::initialize() {
|
|||
for (int j = 0; j < chunks; j++) {
|
||||
OpenGLTerrainChunk *chunk = new OpenGLTerrainChunk(renderer, start + chunksize * (double)i,
|
||||
start + chunksize * (double)j, chunksize, chunks);
|
||||
_chunks.append(chunk);
|
||||
_updateQueue.append(chunk);
|
||||
pv->chunks.push_back(chunk);
|
||||
pv->queue.push_back(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,26 +96,26 @@ void OpenGLTerrain::initialize() {
|
|||
}
|
||||
|
||||
void OpenGLTerrain::update() {
|
||||
for (auto &chunk : _chunks) {
|
||||
for (auto &chunk : pv->chunks) {
|
||||
chunk->askReset(true, true);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTerrain::render() {
|
||||
for (int i = 0; i < _chunks.count(); i++) {
|
||||
_chunks[i]->render(program);
|
||||
for (auto chunk : pv->chunks) {
|
||||
chunk->render(program);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTerrain::interrupt() {
|
||||
for (auto &chunk : _chunks) {
|
||||
for (auto chunk : pv->chunks) {
|
||||
chunk->askInterrupt();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTerrain::destroy() {
|
||||
OpenGLFunctions *functions = getFunctions();
|
||||
for (auto &chunk : _chunks) {
|
||||
for (auto &chunk : pv->chunks) {
|
||||
chunk->destroy(functions);
|
||||
}
|
||||
}
|
||||
|
@ -107,44 +126,44 @@ void OpenGLTerrain::pause() {
|
|||
}
|
||||
|
||||
void OpenGLTerrain::resume() {
|
||||
for (auto &chunk : _chunks) {
|
||||
for (auto &chunk : pv->chunks) {
|
||||
chunk->askResume();
|
||||
}
|
||||
paused = false;
|
||||
}
|
||||
|
||||
void OpenGLTerrain::resetTextures() {
|
||||
for (auto &chunk : _chunks) {
|
||||
for (auto &chunk : pv->chunks) {
|
||||
chunk->askReset(false, true);
|
||||
}
|
||||
}
|
||||
|
||||
static bool _cmpChunks(const OpenGLTerrainChunk *c1, const OpenGLTerrainChunk *c2) {
|
||||
return c1->priority > c2->priority;
|
||||
}
|
||||
|
||||
void OpenGLTerrain::performChunksMaintenance() {
|
||||
CameraDefinition *camera = renderer->getScenery()->getCamera();
|
||||
OpenGLTerrainChunk *chunk;
|
||||
|
||||
_lock_chunks.lock();
|
||||
if (_updateQueue.count() > 0) {
|
||||
chunk = _updateQueue.takeFirst();
|
||||
_lock_chunks.unlock();
|
||||
pv->lock.acquire();
|
||||
for (auto &chunk : pv->chunks) {
|
||||
chunk->updatePriority(camera);
|
||||
}
|
||||
|
||||
if (pv->queue.size() > 0) {
|
||||
sort(pv->queue.begin(), pv->queue.end(), OpenGLTerrainChunkCmp());
|
||||
|
||||
chunk = pv->queue.back();
|
||||
pv->queue.pop_back();
|
||||
} else {
|
||||
_lock_chunks.unlock();
|
||||
return;
|
||||
chunk = NULL;
|
||||
}
|
||||
pv->lock.release();
|
||||
|
||||
chunk->maintain();
|
||||
if (chunk) {
|
||||
chunk->maintain();
|
||||
|
||||
_lock_chunks.lock();
|
||||
_updateQueue.append(chunk);
|
||||
for (int i = 0; i < _chunks.count(); i++) {
|
||||
_chunks[i]->updatePriority(camera);
|
||||
pv->lock.acquire();
|
||||
pv->queue.push_back(chunk);
|
||||
pv->lock.release();
|
||||
}
|
||||
qSort(_updateQueue.begin(), _updateQueue.end(), _cmpChunks);
|
||||
_lock_chunks.unlock();
|
||||
}
|
||||
|
||||
void OpenGLTerrain::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) {
|
||||
|
|
|
@ -6,13 +6,11 @@
|
|||
#include "OpenGLPart.h"
|
||||
#include "DefinitionWatcher.h"
|
||||
|
||||
#include <QVector>
|
||||
#include <QList>
|
||||
#include <QMutex>
|
||||
|
||||
namespace paysages {
|
||||
namespace opengl {
|
||||
|
||||
class OpenGLTerrainPV;
|
||||
|
||||
class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWatcher {
|
||||
public:
|
||||
OpenGLTerrain(OpenGLRenderer *renderer);
|
||||
|
@ -45,9 +43,7 @@ class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWa
|
|||
ParallelPool *work;
|
||||
bool paused;
|
||||
|
||||
QVector<OpenGLTerrainChunk *> _chunks;
|
||||
QList<OpenGLTerrainChunk *> _updateQueue;
|
||||
QMutex _lock_chunks;
|
||||
OpenGLTerrainPV *pv;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,9 @@ class OPENGLSHARED_EXPORT OpenGLTerrainChunk {
|
|||
inline int getVerticesLevel() const {
|
||||
return vertices_level;
|
||||
}
|
||||
inline double getPriority() const {
|
||||
return priority;
|
||||
}
|
||||
inline const OpenGLVertexArray *getVertices() const {
|
||||
return vertices;
|
||||
}
|
||||
|
@ -57,11 +60,11 @@ class OPENGLSHARED_EXPORT OpenGLTerrainChunk {
|
|||
*/
|
||||
void fillVerticesFromSquare(OpenGLVertexArray *array, int index_offset, double x, double z, double size);
|
||||
|
||||
double priority;
|
||||
|
||||
private:
|
||||
Vector3 getCenter();
|
||||
|
||||
double priority;
|
||||
|
||||
double _startx;
|
||||
double _startz;
|
||||
double _size;
|
||||
|
|
|
@ -34,7 +34,7 @@ class OpenGLVertexArray {
|
|||
*
|
||||
* A shader program must be bound (and uniforms defined) when calling this.
|
||||
*/
|
||||
void render(OpenGLFunctions *functions, int start=0, int count=-1);
|
||||
void render(OpenGLFunctions *functions, int start = 0, int count = -1);
|
||||
|
||||
/**
|
||||
* Set the vertex total count.
|
||||
|
|
Loading…
Reference in a new issue