From 8ff83d48e70762e04c2f5f904e845fbd7c5fbfde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 13 Dec 2015 19:06:27 +0100 Subject: [PATCH] OpenGLTerrain does not use Qt containers anymore --- src/render/opengl/OpenGLShaderProgram.h | 2 +- src/render/opengl/OpenGLTerrain.cpp | 75 ++++++++++++++++--------- src/render/opengl/OpenGLTerrain.h | 10 +--- src/render/opengl/OpenGLTerrainChunk.h | 7 ++- src/render/opengl/OpenGLVertexArray.h | 2 +- 5 files changed, 57 insertions(+), 39 deletions(-) diff --git a/src/render/opengl/OpenGLShaderProgram.h b/src/render/opengl/OpenGLShaderProgram.h index c72fec3..35a0448 100644 --- a/src/render/opengl/OpenGLShaderProgram.h +++ b/src/render/opengl/OpenGLShaderProgram.h @@ -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; diff --git a/src/render/opengl/OpenGLTerrain.cpp b/src/render/opengl/OpenGLTerrain.cpp index afa02fc..1fa7c41 100644 --- a/src/render/opengl/OpenGLTerrain.cpp +++ b/src/render/opengl/OpenGLTerrain.cpp @@ -1,10 +1,12 @@ #include "OpenGLTerrain.h" +#include #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 chunks; + vector 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 *) { diff --git a/src/render/opengl/OpenGLTerrain.h b/src/render/opengl/OpenGLTerrain.h index 8c8cd20..04ce9ab 100644 --- a/src/render/opengl/OpenGLTerrain.h +++ b/src/render/opengl/OpenGLTerrain.h @@ -6,13 +6,11 @@ #include "OpenGLPart.h" #include "DefinitionWatcher.h" -#include -#include -#include - 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 _chunks; - QList _updateQueue; - QMutex _lock_chunks; + OpenGLTerrainPV *pv; }; } } diff --git a/src/render/opengl/OpenGLTerrainChunk.h b/src/render/opengl/OpenGLTerrainChunk.h index 159c329..5732e65 100644 --- a/src/render/opengl/OpenGLTerrainChunk.h +++ b/src/render/opengl/OpenGLTerrainChunk.h @@ -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; diff --git a/src/render/opengl/OpenGLVertexArray.h b/src/render/opengl/OpenGLVertexArray.h index e0da792..002c251 100644 --- a/src/render/opengl/OpenGLVertexArray.h +++ b/src/render/opengl/OpenGLVertexArray.h @@ -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.