From 618f6415dadea3d2d5003cd70b3c35c90d5c5390 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 11 Sep 2014 19:36:37 -0700 Subject: [PATCH] Working on specular bits for deferred lighting. --- .../src/renderer/DeferredLightingEffect.cpp | 4 +-- interface/src/renderer/TextureCache.cpp | 26 +++++++++++++++++-- interface/src/renderer/TextureCache.h | 6 ++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/interface/src/renderer/DeferredLightingEffect.cpp b/interface/src/renderer/DeferredLightingEffect.cpp index 6c91f1bf05..38a92fb153 100644 --- a/interface/src/renderer/DeferredLightingEffect.cpp +++ b/interface/src/renderer/DeferredLightingEffect.cpp @@ -27,8 +27,8 @@ void DeferredLightingEffect::init() { } void DeferredLightingEffect::prepare() { - // clear the normal buffer - Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(false, true); + // clear the normal and specular buffers + Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(false, true, true); glClear(GL_COLOR_BUFFER_BIT); Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); } diff --git a/interface/src/renderer/TextureCache.cpp b/interface/src/renderer/TextureCache.cpp index d97a7c503a..00e9fcd482 100644 --- a/interface/src/renderer/TextureCache.cpp +++ b/interface/src/renderer/TextureCache.cpp @@ -30,6 +30,7 @@ TextureCache::TextureCache() : _blueTextureID(0), _primaryDepthTextureID(0), _primaryNormalTextureID(0), + _primarySpecularTextureID(0), _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL), _tertiaryFramebufferObject(NULL), @@ -48,6 +49,7 @@ TextureCache::~TextureCache() { if (_primaryFramebufferObject) { glDeleteTextures(1, &_primaryDepthTextureID); glDeleteTextures(1, &_primaryNormalTextureID); + glDeleteTextures(1, &_primarySpecularTextureID); } if (_primaryFramebufferObject) { @@ -75,6 +77,8 @@ void TextureCache::setFrameBufferSize(QSize frameBufferSize) { _primaryDepthTextureID = 0; glDeleteTextures(1, &_primaryNormalTextureID); _primaryNormalTextureID = 0; + glDeleteTextures(1, &_primarySpecularTextureID); + _primarySpecularTextureID = 0; } if (_secondaryFramebufferObject) { @@ -222,9 +226,18 @@ QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); + glGenTextures(1, &_primarySpecularTextureID); + glBindTexture(GL_TEXTURE_2D, _primarySpecularTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, 0); + _primaryFramebufferObject->bind(); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _primaryNormalTextureID, 0); + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, _primarySpecularTextureID, 0); _primaryFramebufferObject->release(); } return _primaryFramebufferObject; @@ -242,8 +255,13 @@ GLuint TextureCache::getPrimaryNormalTextureID() { return _primaryNormalTextureID; } -void TextureCache::setPrimaryDrawBuffers(bool color, bool normal) { - GLenum buffers[2]; +GLuint TextureCache::getPrimarySpecularTextureID() { + getPrimaryFramebufferObject(); + return _primarySpecularTextureID; +} + +void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) { + GLenum buffers[3]; int bufferCount = 0; if (color) { buffers[bufferCount++] = GL_COLOR_ATTACHMENT0; @@ -251,6 +269,9 @@ void TextureCache::setPrimaryDrawBuffers(bool color, bool normal) { if (normal) { buffers[bufferCount++] = GL_COLOR_ATTACHMENT1; } + if (specular) { + buffers[bufferCount++] = GL_COLOR_ATTACHMENT2; + } glDrawBuffers(bufferCount, buffers); } @@ -308,6 +329,7 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) { _primaryFramebufferObject = NULL; glDeleteTextures(1, &_primaryDepthTextureID); glDeleteTextures(1, &_primaryNormalTextureID); + glDeleteTextures(1, &_primarySpecularTextureID); } if (_secondaryFramebufferObject && _secondaryFramebufferObject->size() != size) { delete _secondaryFramebufferObject; diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 964f1f8501..be4d16832b 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -64,8 +64,11 @@ public: /// Returns the ID of the primary framebuffer object's normal texture. GLuint getPrimaryNormalTextureID(); + /// Returns the ID of the primary framebuffer object's specular texture. + GLuint getPrimarySpecularTextureID(); + /// Enables or disables draw buffers on the primary framebuffer. Note: the primary framebuffer must be bound. - void setPrimaryDrawBuffers(bool color, bool normal); + void setPrimaryDrawBuffers(bool color, bool normal = false, bool specular = false); /// Returns a pointer to the secondary framebuffer object, used as an additional render target when performing full /// screen effects. @@ -102,6 +105,7 @@ private: GLuint _primaryDepthTextureID; GLuint _primaryNormalTextureID; + GLuint _primarySpecularTextureID; QOpenGLFramebufferObject* _primaryFramebufferObject; QOpenGLFramebufferObject* _secondaryFramebufferObject; QOpenGLFramebufferObject* _tertiaryFramebufferObject;