From 373bd0cc8a07d1b3e0afc036622fa84ed2034e4a Mon Sep 17 00:00:00 2001 From: David Kelly Date: Mon, 3 Jul 2017 14:23:23 -0700 Subject: [PATCH] aspect ratio preserved upon blitting, minor qml tweak, warnings fixed --- .../resources/qml/hifi/SpectatorCamera.qml | 2 +- .../display-plugins/OpenGLDisplayPlugin.cpp | 21 +++++++++++++++++-- .../src/display-plugins/OpenGLDisplayPlugin.h | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/interface/resources/qml/hifi/SpectatorCamera.qml b/interface/resources/qml/hifi/SpectatorCamera.qml index 4540b0a6ea..e3572a544f 100644 --- a/interface/resources/qml/hifi/SpectatorCamera.qml +++ b/interface/resources/qml/hifi/SpectatorCamera.qml @@ -242,7 +242,7 @@ Rectangle { size: 32; color: hifi.colors.blueHighlight; anchors.top: spectatorCameraPreview.bottom; - anchors.topMargin: 12; + anchors.topMargin: 20; anchors.left: parent.left; } // "Monitor Shows" Switch Label diff --git a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp index a8a80af01e..24fe40873e 100644 --- a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp +++ b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp @@ -841,14 +841,31 @@ void OpenGLDisplayPlugin::copyTextureToQuickFramebuffer(NetworkTexturePointer ne glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTexture, 0); qDebug() << "error" << glGetError(); + GLint texWidth, texHeight; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texWidth); + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texHeight); // setup destination fbo glBindFramebuffer(GL_FRAMEBUFFER, fbo[1]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, targetTexture, 0); qDebug() << "error" << glGetError(); - // TODO: perhaps maintain aspect ratio - glBlitNamedFramebuffer(fbo[0], fbo[1], 0, 0, networkTexture->getWidth(), networkTexture->getHeight(), 0, 0, target->width(), target->height(), GL_COLOR_BUFFER_BIT, GL_NEAREST); + + // maintain aspect ratio, filling the width first if possible. If that makes the height too + // much, fill height instead. + GLint newX = 0; + GLint newY = 0; + float aspectRatio = (float)texHeight / (float)texWidth; + GLint newWidth = target->width(); + GLint newHeight = std::round(aspectRatio * (float) target->width()); + if (newHeight > target->height()) { + newHeight = target->height(); + newWidth = std::round((float)target->height() / aspectRatio); + newX = (target->width() - newWidth) / 2; + } else { + newY = (target->height() - newHeight) / 2; + } + glBlitNamedFramebuffer(fbo[0], fbo[1], 0, 0, texWidth, texHeight, newX, newY, newWidth, newHeight, GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT, GL_NEAREST); qDebug() << "error" << glGetError(); // don't delete the textures! diff --git a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.h b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.h index dc2e251d65..237864ded6 100644 --- a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.h +++ b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.h @@ -79,7 +79,7 @@ public: // Three threads, one for rendering, one for texture transfers, one reserved for the GL driver int getRequiredThreadCount() const override { return 3; } - void copyTextureToQuickFramebuffer(NetworkTexturePointer source, QOpenGLFramebufferObject* target, GLsync* fenceSync); + void copyTextureToQuickFramebuffer(NetworkTexturePointer source, QOpenGLFramebufferObject* target, GLsync* fenceSync) override; protected: friend class PresentThread;