Skip to content

Commit

Permalink
SoShapeScale: Fix SoShapeScale does not take DPI scaling into account
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddleStroke authored and wwmayer committed Dec 10, 2024
1 parent 3969630 commit 8b6b660
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ SET(Inventor_CPP_SRCS
SoFCSelectionAction.cpp
SoFCVectorizeSVGAction.cpp
SoFCVectorizeU3DAction.cpp
SoDevicePixelRatioElement.cpp
SoTextLabel.cpp
SoDatumLabel.cpp
SoTouchEvents.cpp
Expand Down Expand Up @@ -1097,6 +1098,7 @@ SET(Inventor_SRCS
SoFCSelectionAction.h
SoFCVectorizeSVGAction.h
SoFCVectorizeU3DAction.h
SoDevicePixelRatioElement.h
SoTextLabel.h
SoDatumLabel.h
SoTouchEvents.h
Expand Down
6 changes: 4 additions & 2 deletions src/Gui/Inventor/SoAxisCrossKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@
#endif

#include "SoAxisCrossKit.h"

#include "SoDevicePixelRatioElement.h"

using namespace Gui;


SO_KIT_SOURCE(SoShapeScale)

// Constructor.
Expand Down Expand Up @@ -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;
Expand Down
55 changes: 55 additions & 0 deletions src/Gui/SoDevicePixelRatioElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2024 Pierre-Louis Boyer <development@ondsel.com> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/


#include "PreCompiled.h"

#ifndef _PreComp_
#include <Inventor/actions/SoGLRenderAction.h>
# include <Inventor/misc/SoState.h>
#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() {}
51 changes: 51 additions & 0 deletions src/Gui/SoDevicePixelRatioElement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2024 Pierre-Louis Boyer <development@ondsel.com> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/

#ifndef SO_DEVICE_PIXEL_RATIO_ELEMENT_H
#define SO_DEVICE_PIXEL_RATIO_ELEMENT_H

#include <Inventor/elements/SoFloatElement.h>

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
2 changes: 2 additions & 0 deletions src/Gui/SoFCDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -94,6 +95,7 @@ SbBool Gui::SoFCDB::isInitialized()
void Gui::SoFCDB::init()
{
SoInteraction ::init();
SoDevicePixelRatioElement ::initClass();
SoGLRenderActionElement ::initClass();
SoFCInteractiveElement ::initClass();
SoGLWidgetElement ::initClass();
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/View3DInventorViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<QtGLWidget*>(this->getGLWidget()));
SoGLRenderActionElement::set(state, glra);
SoGLVBOActivatedElement::set(state, this->vboEnabled);
Expand Down

0 comments on commit 8b6b660

Please sign in to comment.