diff --git a/extras/JuceDemo/Source/demos/OpenGLDemo.cpp b/extras/JuceDemo/Source/demos/OpenGLDemo.cpp index 42ebb37a0f..b98eefc845 100644 --- a/extras/JuceDemo/Source/demos/OpenGLDemo.cpp +++ b/extras/JuceDemo/Source/demos/OpenGLDemo.cpp @@ -109,7 +109,9 @@ public: OpenGLHelpers::clear (Colours::darkgrey.withAlpha (1.0f)); updateTextureImage(); // this will update our dynamically-changing texture image. - drawBackground2DStuff(); // draws some 2D content to demonstrate the OpenGLGraphicsContext class + + const float scale = (float) openGLContext.getRenderingScale(); + drawBackground2DStuff (scale); // draws some 2D content to demonstrate the OpenGLGraphicsContext class // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so // we'll put back any important settings before doing our normal GL 3D drawing.. @@ -120,8 +122,9 @@ public: glEnable (GL_TEXTURE_2D); #if JUCE_USE_OPENGL_FIXED_FUNCTION - OpenGLHelpers::prepareFor2D (getContextWidth(), getContextHeight()); - OpenGLHelpers::setPerspective (45.0, getContextWidth() / (double) getContextHeight(), 0.1, 100.0); + OpenGLHelpers::prepareFor2D (roundToInt (scale * getWidth()), + roundToInt (scale * getHeight())); + OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0); glTranslatef (0.0f, 0.0f, -5.0f); draggableOrientation.applyToOpenGLMatrix(); @@ -163,23 +166,23 @@ public: } } - void drawBackground2DStuff() + void drawBackground2DStuff (float scale) { // Create an OpenGLGraphicsContext that will draw into this GL window.. ScopedPointer glRenderer (createOpenGLGraphicsContext (openGLContext, - getContextWidth(), - getContextHeight())); + roundToInt (scale * getWidth()), + roundToInt (scale * getHeight()))); if (glRenderer != nullptr) { Graphics g (glRenderer); - g.addTransform (AffineTransform::scale ((float) getScale())); + g.addTransform (AffineTransform::scale (scale)); // This stuff just creates a spinning star shape and fills it.. Path p; - const float scale = getHeight() * 0.4f; p.addStar (Point (getWidth() * 0.7f, getHeight() * 0.4f), 7, - scale * (float) sizeSlider.getValue(), scale, + getHeight() * 0.4f * (float) sizeSlider.getValue(), + getHeight() * 0.4f, rotation / 50.0f); g.setGradientFill (ColourGradient (Colours::green.withRotatedHue (fabsf (::sinf (rotation / 300.0f))), @@ -190,10 +193,6 @@ public: } } - double getScale() const { return Desktop::getInstance().getDisplays().getDisplayContaining (getScreenBounds().getCentre()).scale; } - int getContextWidth() const { return roundToInt (getScale() * getWidth()); } - int getContextHeight() const { return roundToInt (getScale() * getHeight()); } - void timerCallback() { rotation += (float) speedSlider.getValue(); diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp index 35f5e37953..e239641940 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp @@ -164,6 +164,7 @@ public: if (context.renderer != nullptr) { glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight()); + context.currentRenderScale = scale; context.renderer->renderOpenGL(); clearGLError(); } @@ -527,8 +528,8 @@ private: //============================================================================== OpenGLContext::OpenGLContext() - : nativeContext (nullptr), renderer (nullptr), contextToShareWith (nullptr), - renderComponents (true), useMultisampling (false) + : nativeContext (nullptr), renderer (nullptr), currentRenderScale (1.0), + contextToShareWith (nullptr), renderComponents (true), useMultisampling (false) { } diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.h b/modules/juce_opengl/opengl/juce_OpenGLContext.h index e56b7f6263..5ca6ba3939 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.h +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.h @@ -200,6 +200,16 @@ public: */ int getSwapInterval() const; + //============================================================================== + /** Returns the scale factor used by the display that is being rendered. + + The scale is that of the display - see Desktop::Displays::Display::scale + + Note that this should only be called during an OpenGLRenderer::renderOpenGL() + callback - at other times the value it returns is undefined. + */ + double getRenderingScale() const noexcept { return currentRenderScale; } + //============================================================================== /** Returns an OS-dependent handle to some kind of underlting OS-provided GL context. @@ -209,7 +219,6 @@ public: */ void* getRawContext() const noexcept; - //============================================================================== /** Draws the currently selected texture into this context at its original size. @@ -240,6 +249,7 @@ private: class Attachment; NativeContext* nativeContext; OpenGLRenderer* renderer; + double currentRenderScale; ScopedPointer attachment; OpenGLPixelFormat pixelFormat; void* contextToShareWith;