OpenGLTerrain does not use Qt containers anymore

This commit is contained in:
Michaël Lemaire 2015-12-13 19:06:27 +01:00
parent 5d24edad32
commit 8ff83d48e7
5 changed files with 57 additions and 39 deletions

View file

@ -31,7 +31,7 @@ class OPENGLSHARED_EXPORT OpenGLShaderProgram {
* *
* *state* is optional and may add ponctual variables to the global state. * *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 { inline QOpenGLShaderProgram *getProgram() const {
return program; return program;

View file

@ -1,10 +1,12 @@
#include "OpenGLTerrain.h" #include "OpenGLTerrain.h"
#include <vector>
#include "OpenGLFunctions.h" #include "OpenGLFunctions.h"
#include "OpenGLRenderer.h" #include "OpenGLRenderer.h"
#include "OpenGLShaderProgram.h" #include "OpenGLShaderProgram.h"
#include "ParallelPool.h" #include "ParallelPool.h"
#include "Thread.h" #include "Thread.h"
#include "Mutex.h"
#include "OpenGLTerrainChunk.h" #include "OpenGLTerrainChunk.h"
#include "WaterRenderer.h" #include "WaterRenderer.h"
#include "CameraDefinition.h" #include "CameraDefinition.h"
@ -32,7 +34,22 @@ class ChunkMaintenanceThreads : public ParallelPool {
OpenGLTerrain *terrain; 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") { OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer, "terrain") {
pv = new OpenGLTerrainPV();
work = new ChunkMaintenanceThreads(this); work = new ChunkMaintenanceThreads(this);
paused = false; paused = false;
@ -48,9 +65,11 @@ OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer, "t
OpenGLTerrain::~OpenGLTerrain() { OpenGLTerrain::~OpenGLTerrain() {
delete work; delete work;
for (int i = 0; i < _chunks.count(); i++) { for (auto chunk : pv->chunks) {
delete _chunks[i]; delete chunk;
} }
delete pv;
} }
void OpenGLTerrain::initialize() { void OpenGLTerrain::initialize() {
@ -63,8 +82,8 @@ void OpenGLTerrain::initialize() {
for (int j = 0; j < chunks; j++) { for (int j = 0; j < chunks; j++) {
OpenGLTerrainChunk *chunk = new OpenGLTerrainChunk(renderer, start + chunksize * (double)i, OpenGLTerrainChunk *chunk = new OpenGLTerrainChunk(renderer, start + chunksize * (double)i,
start + chunksize * (double)j, chunksize, chunks); start + chunksize * (double)j, chunksize, chunks);
_chunks.append(chunk); pv->chunks.push_back(chunk);
_updateQueue.append(chunk); pv->queue.push_back(chunk);
} }
} }
@ -77,26 +96,26 @@ void OpenGLTerrain::initialize() {
} }
void OpenGLTerrain::update() { void OpenGLTerrain::update() {
for (auto &chunk : _chunks) { for (auto &chunk : pv->chunks) {
chunk->askReset(true, true); chunk->askReset(true, true);
} }
} }
void OpenGLTerrain::render() { void OpenGLTerrain::render() {
for (int i = 0; i < _chunks.count(); i++) { for (auto chunk : pv->chunks) {
_chunks[i]->render(program); chunk->render(program);
} }
} }
void OpenGLTerrain::interrupt() { void OpenGLTerrain::interrupt() {
for (auto &chunk : _chunks) { for (auto chunk : pv->chunks) {
chunk->askInterrupt(); chunk->askInterrupt();
} }
} }
void OpenGLTerrain::destroy() { void OpenGLTerrain::destroy() {
OpenGLFunctions *functions = getFunctions(); OpenGLFunctions *functions = getFunctions();
for (auto &chunk : _chunks) { for (auto &chunk : pv->chunks) {
chunk->destroy(functions); chunk->destroy(functions);
} }
} }
@ -107,44 +126,44 @@ void OpenGLTerrain::pause() {
} }
void OpenGLTerrain::resume() { void OpenGLTerrain::resume() {
for (auto &chunk : _chunks) { for (auto &chunk : pv->chunks) {
chunk->askResume(); chunk->askResume();
} }
paused = false; paused = false;
} }
void OpenGLTerrain::resetTextures() { void OpenGLTerrain::resetTextures() {
for (auto &chunk : _chunks) { for (auto &chunk : pv->chunks) {
chunk->askReset(false, true); chunk->askReset(false, true);
} }
} }
static bool _cmpChunks(const OpenGLTerrainChunk *c1, const OpenGLTerrainChunk *c2) {
return c1->priority > c2->priority;
}
void OpenGLTerrain::performChunksMaintenance() { void OpenGLTerrain::performChunksMaintenance() {
CameraDefinition *camera = renderer->getScenery()->getCamera(); CameraDefinition *camera = renderer->getScenery()->getCamera();
OpenGLTerrainChunk *chunk; OpenGLTerrainChunk *chunk;
_lock_chunks.lock(); pv->lock.acquire();
if (_updateQueue.count() > 0) { for (auto &chunk : pv->chunks) {
chunk = _updateQueue.takeFirst(); chunk->updatePriority(camera);
_lock_chunks.unlock(); }
if (pv->queue.size() > 0) {
sort(pv->queue.begin(), pv->queue.end(), OpenGLTerrainChunkCmp());
chunk = pv->queue.back();
pv->queue.pop_back();
} else { } else {
_lock_chunks.unlock(); chunk = NULL;
return;
} }
pv->lock.release();
chunk->maintain(); if (chunk) {
chunk->maintain();
_lock_chunks.lock(); pv->lock.acquire();
_updateQueue.append(chunk); pv->queue.push_back(chunk);
for (int i = 0; i < _chunks.count(); i++) { pv->lock.release();
_chunks[i]->updatePriority(camera);
} }
qSort(_updateQueue.begin(), _updateQueue.end(), _cmpChunks);
_lock_chunks.unlock();
} }
void OpenGLTerrain::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) { void OpenGLTerrain::nodeChanged(const DefinitionNode *node, const DefinitionDiff *) {

View file

@ -6,13 +6,11 @@
#include "OpenGLPart.h" #include "OpenGLPart.h"
#include "DefinitionWatcher.h" #include "DefinitionWatcher.h"
#include <QVector>
#include <QList>
#include <QMutex>
namespace paysages { namespace paysages {
namespace opengl { namespace opengl {
class OpenGLTerrainPV;
class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWatcher { class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWatcher {
public: public:
OpenGLTerrain(OpenGLRenderer *renderer); OpenGLTerrain(OpenGLRenderer *renderer);
@ -45,9 +43,7 @@ class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWa
ParallelPool *work; ParallelPool *work;
bool paused; bool paused;
QVector<OpenGLTerrainChunk *> _chunks; OpenGLTerrainPV *pv;
QList<OpenGLTerrainChunk *> _updateQueue;
QMutex _lock_chunks;
}; };
} }
} }

View file

@ -24,6 +24,9 @@ class OPENGLSHARED_EXPORT OpenGLTerrainChunk {
inline int getVerticesLevel() const { inline int getVerticesLevel() const {
return vertices_level; return vertices_level;
} }
inline double getPriority() const {
return priority;
}
inline const OpenGLVertexArray *getVertices() const { inline const OpenGLVertexArray *getVertices() const {
return vertices; return vertices;
} }
@ -57,11 +60,11 @@ class OPENGLSHARED_EXPORT OpenGLTerrainChunk {
*/ */
void fillVerticesFromSquare(OpenGLVertexArray *array, int index_offset, double x, double z, double size); void fillVerticesFromSquare(OpenGLVertexArray *array, int index_offset, double x, double z, double size);
double priority;
private: private:
Vector3 getCenter(); Vector3 getCenter();
double priority;
double _startx; double _startx;
double _startz; double _startz;
double _size; double _size;

View file

@ -34,7 +34,7 @@ class OpenGLVertexArray {
* *
* A shader program must be bound (and uniforms defined) when calling this. * 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. * Set the vertex total count.