diff --git a/modules/base/rendering/renderablemodel.cpp b/modules/base/rendering/renderablemodel.cpp index 80c8a3a655..af68455cc3 100644 --- a/modules/base/rendering/renderablemodel.cpp +++ b/modules/base/rendering/renderablemodel.cpp @@ -606,16 +606,6 @@ void RenderableModel::initializeGL() { (void*)(2 * sizeof(GLfloat)) ); - - createFramebuffers(); - - _geometry->initialize(); - _geometry->calculateBoundingRadius(); -} - -void RenderableModel::createFramebuffers() { - glm::vec2 resolution = global::windowDelegate->currentDrawBufferResolution(); - // Generate textures and the frame buffer glGenTextures(1, &_colorTexture); glGenTextures(1, &_positionTexture); @@ -623,93 +613,11 @@ void RenderableModel::createFramebuffers() { glGenTextures(1, &_depthTexture); glGenFramebuffers(1, &_framebuffer); - // Create the textures - // Color - glBindTexture(GL_TEXTURE_2D, _colorTexture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGBA32F, - static_cast(resolution.x), - static_cast(resolution.y), - 0, - GL_RGBA, - GL_FLOAT, - nullptr - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - if (glbinding::Binding::ObjectLabel.isResolved()) { - glObjectLabel(GL_TEXTURE, _colorTexture, -1, "RenderableModel Color"); - } + // Create Textures + _resolution = global::windowDelegate->currentDrawBufferResolution(); + updateResolution(); - - // Position - glBindTexture(GL_TEXTURE_2D, _positionTexture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGBA32F, - static_cast(resolution.x), - static_cast(resolution.y), - 0, - GL_RGBA, - GL_FLOAT, - nullptr - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - if (glbinding::Binding::ObjectLabel.isResolved()) { - glObjectLabel(GL_TEXTURE, _positionTexture, -1, "RenderableModel Position"); - } - - // Normal - glBindTexture(GL_TEXTURE_2D, _normalTexture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGBA32F, - static_cast(resolution.x), - static_cast(resolution.y), - 0, - GL_RGBA, - GL_FLOAT, - nullptr - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - if (glbinding::Binding::ObjectLabel.isResolved()) { - glObjectLabel(GL_TEXTURE, _normalTexture, -1, "RenderableModel Normal"); - } - - // Depth - glBindTexture(GL_TEXTURE_2D, _depthTexture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_DEPTH_COMPONENT32F, - static_cast(resolution.x), - static_cast(resolution.y), - 0, - GL_DEPTH_COMPONENT, - GL_FLOAT, - nullptr - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - if (glbinding::Binding::ObjectLabel.isResolved()) { - glObjectLabel(GL_TEXTURE, _depthTexture, -1, "RenderableModel Depth"); - } - - // Create buffers + // Bind textures to the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer); glFramebufferTexture( GL_FRAMEBUFFER, @@ -742,10 +650,104 @@ void RenderableModel::createFramebuffers() { // Check status GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) { + if (status != GL_FRAMEBUFFER_COMPLETE) { LERROR("Framebuffer is not complete!"); } glBindFramebuffer(GL_FRAMEBUFFER, 0); + + // Initialize geometry + _geometry->initialize(); + _geometry->calculateBoundingRadius(); +} + +void RenderableModel::updateResolution() { + ZoneScoped + + // Create the textures + // Color + glBindTexture(GL_TEXTURE_2D, _colorTexture); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGBA32F, + static_cast(_resolution.x), + static_cast(_resolution.y), + 0, + GL_RGBA, + GL_FLOAT, + nullptr + ); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + if (glbinding::Binding::ObjectLabel.isResolved()) { + glObjectLabel(GL_TEXTURE, _colorTexture, -1, "RenderableModel Color"); + } + + + // Position + glBindTexture(GL_TEXTURE_2D, _positionTexture); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGBA32F, + static_cast(_resolution.x), + static_cast(_resolution.y), + 0, + GL_RGBA, + GL_FLOAT, + nullptr + ); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + if (glbinding::Binding::ObjectLabel.isResolved()) { + glObjectLabel(GL_TEXTURE, _positionTexture, -1, "RenderableModel Position"); + } + + // Normal + glBindTexture(GL_TEXTURE_2D, _normalTexture); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGBA32F, + static_cast(_resolution.x), + static_cast(_resolution.y), + 0, + GL_RGBA, + GL_FLOAT, + nullptr + ); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + if (glbinding::Binding::ObjectLabel.isResolved()) { + glObjectLabel(GL_TEXTURE, _normalTexture, -1, "RenderableModel Normal"); + } + + // Depth + glBindTexture(GL_TEXTURE_2D, _depthTexture); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_DEPTH_COMPONENT32F, + static_cast(_resolution.x), + static_cast(_resolution.y), + 0, + GL_DEPTH_COMPONENT, + GL_FLOAT, + nullptr + ); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + if (glbinding::Binding::ObjectLabel.isResolved()) { + glObjectLabel(GL_TEXTURE, _depthTexture, -1, "RenderableModel Depth"); + } } void RenderableModel::deinitializeGL() { @@ -975,6 +977,12 @@ void RenderableModel::update(const UpdateData& data) { ghoul::opengl::updateUniformLocations(*_program, _uniformCache, UniformNames); } + glm::ivec2 resolution = global::windowDelegate->currentDrawBufferResolution(); + if (resolution != _resolution) { + _resolution = resolution; + updateResolution(); + } + setBoundingSphere(_geometry->boundingRadius() * _modelScale * glm::compMax(data.modelTransform.scale) ); diff --git a/modules/base/rendering/renderablemodel.h b/modules/base/rendering/renderablemodel.h index 895dd2fdfe..7a24ad73c3 100644 --- a/modules/base/rendering/renderablemodel.h +++ b/modules/base/rendering/renderablemodel.h @@ -118,7 +118,7 @@ private: properties::PropertyOwner _lightSourcePropertyOwner; - // Frame buffer stuff + // Framebuffer and its textures GLuint _framebuffer; GLuint _colorTexture; GLuint _depthTexture; @@ -126,7 +126,8 @@ private: GLuint _normalTexture; GLuint _quadVao; GLuint _quadVbo; - void createFramebuffers(); + glm::ivec2 _resolution = glm::ivec2(0); + void updateResolution(); ghoul::opengl::ProgramObject* _quadProgram = nullptr; UniformCache(opacity, opacityBlending, colorTexture, depthTexture, positionTexture,