diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index 3bf6f8d4621a..9e71c9a4f7a7 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -1066,6 +1066,7 @@ SET(Inventor_CPP_SRCS SoFCSelectionAction.cpp SoFCVectorizeSVGAction.cpp SoFCVectorizeU3DAction.cpp + SoDevicePixelRatioElement.cpp SoTextLabel.cpp SoDatumLabel.cpp SoTouchEvents.cpp @@ -1097,6 +1098,7 @@ SET(Inventor_SRCS SoFCSelectionAction.h SoFCVectorizeSVGAction.h SoFCVectorizeU3DAction.h + SoDevicePixelRatioElement.h SoTextLabel.h SoDatumLabel.h SoTouchEvents.h diff --git a/src/Gui/Inventor/SoAxisCrossKit.cpp b/src/Gui/Inventor/SoAxisCrossKit.cpp index ad11a4fc6007..e894ff1ae26b 100644 --- a/src/Gui/Inventor/SoAxisCrossKit.cpp +++ b/src/Gui/Inventor/SoAxisCrossKit.cpp @@ -51,11 +51,10 @@ #endif #include "SoAxisCrossKit.h" - +#include "SoDevicePixelRatioElement.h" using namespace Gui; - SO_KIT_SOURCE(SoShapeScale) // Constructor. @@ -101,6 +100,9 @@ SoShapeScale::GLRender(SoGLRenderAction * action) float nsize = this->scaleFactor.getValue() / float(vp.getViewportSizePixels()[0]); SoModelMatrixElement::get(state).multVecMatrix(center, center); // world coords float sf = vv.getWorldToScreenScale(center, nsize); + + sf *= SoDevicePixelRatioElement::get(state); + SbVec3f v(sf, sf, sf); if (scale->scaleFactor.getValue() != v){ scale->scaleFactor = v; diff --git a/src/Gui/SoDevicePixelRatioElement.cpp b/src/Gui/SoDevicePixelRatioElement.cpp new file mode 100644 index 000000000000..31cf7fcf841a --- /dev/null +++ b/src/Gui/SoDevicePixelRatioElement.cpp @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + /**************************************************************************** + * * + * Copyright (c) 2024 Pierre-Louis Boyer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +#include +# include +#endif + +#include "SoDevicePixelRatioElement.h" + +SO_ELEMENT_SOURCE(SoDevicePixelRatioElement); + +void SoDevicePixelRatioElement::initClass() { + SO_ELEMENT_INIT_CLASS(SoDevicePixelRatioElement, SoFloatElement); + // Ensure the element is enabled for GLRenderAction + SO_ENABLE(SoGLRenderAction, SoDevicePixelRatioElement); +} + +void SoDevicePixelRatioElement::init(SoState* state) { + SoFloatElement::init(state); + data = 1.0f; // Default to a device pixel ratio of 1.0 +} + +void SoDevicePixelRatioElement::set(SoState* state, float dpr) { + SoFloatElement::set(classStackIndex, state, dpr); +} + +float SoDevicePixelRatioElement::get(SoState* state) { + return SoFloatElement::get(classStackIndex, state); +} + +SoDevicePixelRatioElement::~SoDevicePixelRatioElement() {} \ No newline at end of file diff --git a/src/Gui/SoDevicePixelRatioElement.h b/src/Gui/SoDevicePixelRatioElement.h new file mode 100644 index 000000000000..239a92148a60 --- /dev/null +++ b/src/Gui/SoDevicePixelRatioElement.h @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + /**************************************************************************** + * * + * Copyright (c) 2024 Pierre-Louis Boyer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + +#ifndef SO_DEVICE_PIXEL_RATIO_ELEMENT_H +#define SO_DEVICE_PIXEL_RATIO_ELEMENT_H + +#include + +class SoState; + +class SoDevicePixelRatioElement : public SoFloatElement { + SO_ELEMENT_HEADER(SoDevicePixelRatioElement); + +public: + // Initializes the class + static void initClass(); + + // Initializes the element + virtual void init(SoState* state) override; + + // Sets the device pixel ratio + static void set(SoState* state, float dpr); + + // Retrieves the device pixel ratio + static float get(SoState* state); + +protected: + virtual ~SoDevicePixelRatioElement(); +}; + +#endif // SO_DEVICE_PIXEL_RATIO_ELEMENT_H \ No newline at end of file diff --git a/src/Gui/SoFCDB.cpp b/src/Gui/SoFCDB.cpp index fc746a2fbe95..75db396f8346 100644 --- a/src/Gui/SoFCDB.cpp +++ b/src/Gui/SoFCDB.cpp @@ -53,6 +53,7 @@ #include "GestureNavigationStyle.h" #include "NavigationStyle.h" #include "SelectionObject.h" +#include "SoDevicePixelRatioElement.h" #include "SoFCColorBar.h" #include "SoFCColorGradient.h" #include "SoFCColorLegend.h" @@ -94,6 +95,7 @@ SbBool Gui::SoFCDB::isInitialized() void Gui::SoFCDB::init() { SoInteraction ::init(); + SoDevicePixelRatioElement ::initClass(); SoGLRenderActionElement ::initClass(); SoFCInteractiveElement ::initClass(); SoGLWidgetElement ::initClass(); diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index 3a4b9cea2fc0..e27592adb42f 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -107,6 +107,7 @@ #include "NaviCube.h" #include "NavigationStyle.h" #include "Selection.h" +#include "SoDevicePixelRatioElement.h" #include "SoFCDB.h" #include "SoFCInteractiveElement.h" #include "SoFCOffscreenRenderer.h" @@ -2360,6 +2361,7 @@ void View3DInventorViewer::renderScene() // Render our scenegraph with the image. SoGLRenderAction* glra = this->getSoRenderManager()->getGLRenderAction(); SoState* state = glra->getState(); + SoDevicePixelRatioElement::set(state, devicePixelRatio()); SoGLWidgetElement::set(state, qobject_cast(this->getGLWidget())); SoGLRenderActionElement::set(state, glra); SoGLVBOActivatedElement::set(state, this->vboEnabled);