From 0fe580d10c6fb73a90eccb046c8dcbf84b062b16 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Mon, 4 Jan 2016 11:38:21 +0100 Subject: OPENGL: Make shader/framebuffer part of pipeline state. --- backends/graphics/opengl/pipeline.cpp | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'backends/graphics/opengl/pipeline.cpp') diff --git a/backends/graphics/opengl/pipeline.cpp b/backends/graphics/opengl/pipeline.cpp index 362f15a36b..500597cc3d 100644 --- a/backends/graphics/opengl/pipeline.cpp +++ b/backends/graphics/opengl/pipeline.cpp @@ -22,9 +22,29 @@ #include "backends/graphics/opengl/pipeline.h" #include "backends/graphics/opengl/shader.h" +#include "backends/graphics/opengl/framebuffer.h" namespace OpenGL { +Pipeline::Pipeline() + : _activeFramebuffer(nullptr) { +} + +Framebuffer *Pipeline::setFramebuffer(Framebuffer *framebuffer) { + Framebuffer *oldFramebuffer = _activeFramebuffer; + if (oldFramebuffer) { + oldFramebuffer->deactivate(); + } + + _activeFramebuffer = framebuffer; + if (_activeFramebuffer) { + _activeFramebuffer->activate(); + setProjectionMatrix(_activeFramebuffer->getProjectionMatrix()); + } + + return oldFramebuffer; +} + #if !USE_FORCED_GLES2 void FixedPipeline::activate() { GL_CALL(glDisable(GL_LIGHTING)); @@ -51,9 +71,21 @@ void FixedPipeline::setDrawCoordinates(const GLfloat *vertices, const GLfloat *t GL_CALL(glTexCoordPointer(2, GL_FLOAT, 0, texCoords)); GL_CALL(glVertexPointer(2, GL_FLOAT, 0, vertices)); } + +void FixedPipeline::setProjectionMatrix(const GLfloat *projectionMatrix) { + GL_CALL(glMatrixMode(GL_PROJECTION)); + GL_CALL(glLoadMatrixf(projectionMatrix)); + + GL_CALL(glMatrixMode(GL_MODELVIEW)); + GL_CALL(glLoadIdentity()); +} #endif // !USE_FORCED_GLES2 #if !USE_FORCED_GLES +ShaderPipeline::ShaderPipeline() + : _activeShader(nullptr) { +} + void ShaderPipeline::activate() { GL_CALL(glEnableVertexAttribArray(kPositionAttribLocation)); GL_CALL(glEnableVertexAttribArray(kTexCoordAttribLocation)); @@ -63,6 +95,17 @@ void ShaderPipeline::activate() { } } +Shader *ShaderPipeline::setShader(Shader *shader) { + Shader *oldShader = _activeShader; + + _activeShader = shader; + if (_activeShader && _activeFramebuffer) { + _activeShader->activate(_activeFramebuffer->getProjectionMatrix()); + } + + return oldShader; +} + void ShaderPipeline::setColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { GL_CALL(glVertexAttrib4f(kColorAttribLocation, r, g, b, a)); } @@ -71,6 +114,12 @@ void ShaderPipeline::setDrawCoordinates(const GLfloat *vertices, const GLfloat * GL_CALL(glVertexAttribPointer(kTexCoordAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, texCoords)); GL_CALL(glVertexAttribPointer(kPositionAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, vertices)); } + +void ShaderPipeline::setProjectionMatrix(const GLfloat *projectionMatrix) { + if (_activeShader) { + _activeShader->activate(projectionMatrix); + } +} #endif // !USE_FORCED_GLES } // End of namespace OpenGL -- cgit v1.2.3