Refactored opengl parts

This commit is contained in:
Michaël Lemaire 2015-12-11 01:12:55 +01:00
parent 7d4989b670
commit 9cece93ec1
9 changed files with 65 additions and 45 deletions

View file

@ -4,7 +4,7 @@
#include "OpenGLShaderProgram.h" #include "OpenGLShaderProgram.h"
#include "OpenGLVertexArray.h" #include "OpenGLVertexArray.h"
OpenGLPart::OpenGLPart(OpenGLRenderer *renderer) : renderer(renderer) { OpenGLPart::OpenGLPart(OpenGLRenderer *renderer, const string &name) : renderer(renderer), name(name) {
} }
OpenGLPart::~OpenGLPart() { OpenGLPart::~OpenGLPart() {
@ -30,6 +30,12 @@ void OpenGLPart::destroy() {
void OpenGLPart::interrupt() { void OpenGLPart::interrupt() {
} }
void OpenGLPart::pause() {
}
void OpenGLPart::resume() {
}
OpenGLShaderProgram *OpenGLPart::createShader(const string &name) { OpenGLShaderProgram *OpenGLPart::createShader(const string &name) {
OpenGLShaderProgram *program = new OpenGLShaderProgram(name, renderer); OpenGLShaderProgram *program = new OpenGLShaderProgram(name, renderer);

View file

@ -14,7 +14,7 @@ namespace opengl {
*/ */
class OPENGLSHARED_EXPORT OpenGLPart { class OPENGLSHARED_EXPORT OpenGLPart {
public: public:
OpenGLPart(OpenGLRenderer *renderer); OpenGLPart(OpenGLRenderer *renderer, const string &name);
virtual ~OpenGLPart(); virtual ~OpenGLPart();
// Initialize the part rendering (create shaders, prepare static textures...) // Initialize the part rendering (create shaders, prepare static textures...)
@ -32,8 +32,18 @@ class OPENGLSHARED_EXPORT OpenGLPart {
// Interrupt the rendering // Interrupt the rendering
virtual void interrupt(); virtual void interrupt();
// Temporarily pause the background working
virtual void pause();
// Resume the background working
virtual void resume();
void updateScenery(bool onlyCommon = false); void updateScenery(bool onlyCommon = false);
inline const string &getName() const {
return name;
}
protected: protected:
/** /**
* Create a shader program. * Create a shader program.
@ -55,6 +65,7 @@ class OPENGLSHARED_EXPORT OpenGLPart {
OpenGLRenderer *renderer; OpenGLRenderer *renderer;
private: private:
string name;
map<string, OpenGLShaderProgram *> shaders; map<string, OpenGLShaderProgram *> shaders;
vector<OpenGLVertexArray *> arrays; vector<OpenGLVertexArray *> arrays;
}; };

View file

@ -38,23 +38,22 @@ OpenGLRenderer::OpenGLRenderer(Scenery *scenery) : SoftwareRenderer(scenery) {
shared_state->set("viewDistance", 300.0); shared_state->set("viewDistance", 300.0);
shared_state->set("exposure", 1.2); shared_state->set("exposure", 1.2);
skybox = new OpenGLSkybox(this); parts.push_back(skybox = new OpenGLSkybox(this));
water = new OpenGLWater(this); parts.push_back(water = new OpenGLWater(this));
terrain = new OpenGLTerrain(this); parts.push_back(terrain = new OpenGLTerrain(this));
} }
OpenGLRenderer::~OpenGLRenderer() { OpenGLRenderer::~OpenGLRenderer() {
terrain->interrupt(); for (auto part : parts) {
water->interrupt(); part->interrupt();
skybox->interrupt(); }
delete mouse_projected; delete mouse_projected;
delete view_matrix; delete view_matrix;
delete skybox; for (auto part : parts) {
delete water; delete part;
delete terrain; }
delete functions; delete functions;
delete shared_state; delete shared_state;
@ -70,11 +69,11 @@ void OpenGLRenderer::checkForErrors(const string &domain) {
void OpenGLRenderer::destroy() { void OpenGLRenderer::destroy() {
shared_state->destroy(functions); shared_state->destroy(functions);
skybox->destroy(); for (auto part : parts) {
terrain->destroy(); part->destroy();
water->destroy(); }
checkForErrors("stopping"); checkForErrors("destroy");
} }
void OpenGLRenderer::initialize() { void OpenGLRenderer::initialize() {
@ -92,14 +91,10 @@ void OpenGLRenderer::initialize() {
getLightingManager()->setSpecularity(false); getLightingManager()->setSpecularity(false);
getGodRaysSampler()->setEnabled(false); getGodRaysSampler()->setEnabled(false);
skybox->initialize(); for (auto part : parts) {
skybox->updateScenery(); part->initialize();
part->updateScenery();
water->initialize(); }
water->updateScenery();
terrain->initialize();
terrain->updateScenery();
cameraChangeEvent(render_camera); cameraChangeEvent(render_camera);
@ -168,14 +163,10 @@ void OpenGLRenderer::paint(bool clear) {
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
skybox->render(); for (auto part : parts) {
checkForErrors("skybox"); part->render();
checkForErrors(part->getName());
terrain->render(); }
checkForErrors("terrain");
water->render();
checkForErrors("water");
if (mouse_tracking) { if (mouse_tracking) {
updateMouseProjection(); updateMouseProjection();
@ -193,22 +184,30 @@ bool OpenGLRenderer::stop() {
void OpenGLRenderer::reset() { void OpenGLRenderer::reset() {
if (ready) { if (ready) {
skybox->updateScenery(); for (auto part : parts) {
water->updateScenery(); part->updateScenery();
terrain->updateScenery(); }
cameraChangeEvent(render_camera); cameraChangeEvent(render_camera);
} }
} }
void OpenGLRenderer::pause() { void OpenGLRenderer::pause() {
if (not paused) {
paused = true; paused = true;
terrain->pause(); for (auto part : parts) {
part->pause();
}
}
} }
void OpenGLRenderer::resume() { void OpenGLRenderer::resume() {
if (paused) {
for (auto part : parts) {
part->resume();
}
paused = false; paused = false;
terrain->resume(); }
} }
void OpenGLRenderer::setMouseLocation(int x, int y) { void OpenGLRenderer::setMouseLocation(int x, int y) {

View file

@ -5,6 +5,8 @@
#include "SoftwareRenderer.h" #include "SoftwareRenderer.h"
#include <vector>
class QMatrix4x4; class QMatrix4x4;
namespace paysages { namespace paysages {
@ -135,6 +137,8 @@ class OPENGLSHARED_EXPORT OpenGLRenderer : public SoftwareRenderer {
OpenGLSkybox *skybox; OpenGLSkybox *skybox;
OpenGLWater *water; OpenGLWater *water;
OpenGLTerrain *terrain; OpenGLTerrain *terrain;
vector<OpenGLPart *> parts;
}; };
} }
} }

View file

@ -11,7 +11,7 @@
#include "AtmosphereModelBruneton.h" #include "AtmosphereModelBruneton.h"
#include "FloatNode.h" #include "FloatNode.h"
OpenGLSkybox::OpenGLSkybox(OpenGLRenderer *renderer) : OpenGLPart(renderer) { OpenGLSkybox::OpenGLSkybox(OpenGLRenderer *renderer) : OpenGLPart(renderer, "skybox") {
program = createShader("skybox"); program = createShader("skybox");
program->addVertexSource("skybox"); program->addVertexSource("skybox");
program->addFragmentSource("atmosphere"); program->addFragmentSource("atmosphere");

View file

@ -32,7 +32,7 @@ class ChunkMaintenanceThreads : public ParallelPool {
OpenGLTerrain *terrain; OpenGLTerrain *terrain;
}; };
OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer) { OpenGLTerrain::OpenGLTerrain(OpenGLRenderer *renderer) : OpenGLPart(renderer, "terrain") {
work = new ChunkMaintenanceThreads(this); work = new ChunkMaintenanceThreads(this);
paused = false; paused = false;

View file

@ -24,8 +24,8 @@ class OPENGLSHARED_EXPORT OpenGLTerrain : public OpenGLPart, public DefinitionWa
virtual void interrupt() override; virtual void interrupt() override;
virtual void destroy() override; virtual void destroy() override;
void pause(); virtual void pause() override;
void resume(); virtual void resume() override;
inline bool isPaused() const { inline bool isPaused() const {
return paused; return paused;
} }

View file

@ -13,7 +13,7 @@
#include "FloatDiff.h" #include "FloatDiff.h"
#include "IntNode.h" #include "IntNode.h"
OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer) { OpenGLWater::OpenGLWater(OpenGLRenderer *renderer) : OpenGLPart(renderer, "water") {
enabled = true; enabled = true;
program = createShader("water"); program = createShader("water");

View file

@ -11,8 +11,8 @@
namespace paysages { namespace paysages {
namespace opengl { namespace opengl {
class WidgetExplorer;
class OpenGLRenderer; class OpenGLRenderer;
class OpenGLPart;
class OpenGLShaderProgram; class OpenGLShaderProgram;
class OpenGLSharedState; class OpenGLSharedState;
class OpenGLVariable; class OpenGLVariable;