Skip to content

Commit

Permalink
Gui: From textture handling to ViewProviderTextureExtension
Browse files Browse the repository at this point in the history
It should be avoided to add all stuff to the ViewProviderGeometryObject base class when it's only needed in one or more sub-classes
  • Loading branch information
wwmayer committed Jun 4, 2024
1 parent d12632f commit d71801b
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 91 deletions.
4 changes: 4 additions & 0 deletions src/Gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#include "ViewProviderPart.h"
#include "ViewProviderPythonFeature.h"
#include "ViewProviderTextDocument.h"
#include "ViewProviderTextureExtension.h"
#include "ViewProviderVRMLObject.h"
#include "ViewProviderVarSet.h"
#include "WaitCursor.h"
Expand Down Expand Up @@ -1875,6 +1876,7 @@ void Application::initApplication()

void Application::initTypes()
{
// clang-format off
// views
Gui::BaseView ::init();
Gui::MDIView ::init();
Expand Down Expand Up @@ -1925,6 +1927,7 @@ void Application::initTypes()
Gui::ViewProviderMaterialObject ::init();
Gui::ViewProviderMaterialObjectPython ::init();
Gui::ViewProviderTextDocument ::init();
Gui::ViewProviderTextureExtension ::init();
Gui::ViewProviderLinkObserver ::init();
Gui::LinkView ::init();
Gui::ViewProviderLink ::init();
Expand All @@ -1945,6 +1948,7 @@ void Application::initTypes()
// register transaction type
new App::TransactionProducer<TransactionViewProvider>
(ViewProviderDocumentObject::getClassTypeId());
// clang-format on
}

void Application::initOpenInventor()
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ SET(Viewprovider_CPP_SRCS
ViewProviderOrigin.cpp
ViewProviderMaterialObject.cpp
ViewProviderTextDocument.cpp
ViewProviderTextureExtension.cpp
ViewProviderLink.cpp
LinkViewPyImp.cpp
ViewProviderLinkPyImp.cpp
Expand Down Expand Up @@ -981,6 +982,7 @@ SET(Viewprovider_SRCS
ViewProviderOrigin.h
ViewProviderMaterialObject.h
ViewProviderTextDocument.h
ViewProviderTextureExtension.h
ViewProviderLink.h
ViewProviderVarSet.h
AxisOrigin.h
Expand Down
77 changes: 4 additions & 73 deletions src/Gui/ViewProviderGeometryObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSwitch.h>
#include <Inventor/nodes/SoTexture2.h>
#endif

#include <Inventor/nodes/SoResetTransform.h>

#include <App/GeoFeature.h>
#include <App/PropertyGeo.h>
#include <Gui/BitmapFactory.h>

#include "Application.h"
#include "Document.h"
Expand Down Expand Up @@ -96,29 +94,10 @@ ViewProviderGeometryObject::ViewProviderGeometryObject()

Selectable.setValue(isSelectionEnabled());

pcSwitchAppearance = new SoSwitch;
pcSwitchAppearance->ref();
pcSwitchTexture = new SoSwitch;
pcSwitchTexture->ref();

pcShapeMaterial = new SoMaterial;
setCoinAppearance(mat);
pcShapeMaterial->ref();

pcShapeTexture2D = new SoTexture2;
pcShapeTexture2D->ref();

pcTextureGroup3D = new SoGroup;
pcTextureGroup3D->ref();

// Materials go first, with textured faces drawing over them
pcSwitchAppearance->addChild(pcShapeMaterial);
pcSwitchAppearance->addChild(pcSwitchTexture);
pcSwitchTexture->addChild(pcShapeTexture2D);
pcSwitchTexture->addChild(pcTextureGroup3D);
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);

pcBoundingBox = new Gui::SoFCBoundingBox;
pcBoundingBox->ref();

Expand All @@ -130,11 +109,7 @@ ViewProviderGeometryObject::ViewProviderGeometryObject()

ViewProviderGeometryObject::~ViewProviderGeometryObject()
{
pcSwitchAppearance->unref();
pcSwitchTexture->unref();
pcShapeMaterial->unref();
pcShapeTexture2D->unref();
pcTextureGroup3D->unref();
pcBoundingBox->unref();
pcBoundColor->unref();
}
Expand Down Expand Up @@ -273,31 +248,12 @@ unsigned long ViewProviderGeometryObject::getBoundColor() const

void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source)
{
#if 0
if (!source.image.empty()) {
Base::Console().Log("setCoinAppearance(Texture)\n");
activateTexture2D();

QByteArray by = QByteArray::fromBase64(QString::fromStdString(source.image).toUtf8());
auto image = QImage::fromData(by, "PNG"); //.scaled(64, 64, Qt::KeepAspectRatio);

SoSFImage texture;
Gui::BitmapFactory().convert(image, texture);
pcShapeTexture2D->image = texture;
} else {
Base::Console().Log("setCoinAppearance(Material)\n");
activateMaterial();
}
#endif
activateMaterial();

// Always set the material for items such as lines that don't support textures
pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
source.ambientColor.g,
source.ambientColor.b);
source.ambientColor.g,
source.ambientColor.b);
pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
source.diffuseColor.g,
source.diffuseColor.b);
source.diffuseColor.g,
source.diffuseColor.b);
pcShapeMaterial->specularColor.setValue(source.specularColor.r,
source.specularColor.g,
source.specularColor.b);
Expand All @@ -308,31 +264,6 @@ void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source)
pcShapeMaterial->transparency.setValue(source.transparency);
}

void ViewProviderGeometryObject::activateMaterial()
{
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
}

void ViewProviderGeometryObject::activateTexture2D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(0);
}

void ViewProviderGeometryObject::activateTexture3D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(1);
}

void ViewProviderGeometryObject::activateMixed3D()
{
pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL);
pcSwitchTexture->whichChild.setValue(1);
}


namespace
{
float getBoundBoxFontSize()
Expand Down
18 changes: 0 additions & 18 deletions src/Gui/ViewProviderGeometryObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
class SoPickedPointList;
class SoSwitch;
class SoSensor;
class SoTexture2;
class SbVec2s;
class SoBaseColor;

Expand Down Expand Up @@ -110,28 +109,11 @@ class GuiExport ViewProviderGeometryObject: public ViewProviderDragger
const char* PropName) override;
void setCoinAppearance(const App::Material& source);

/**
* Select which appearance type is active
*
*/
/** Material only */
void activateMaterial();
/** 2D Texture */
void activateTexture2D();
/** 3D texture only */
void activateTexture3D();
/** Mix of material and 3D textures */
void activateMixed3D();

private:
bool isSelectionEnabled() const;

protected:
SoSwitch* pcSwitchAppearance {nullptr};
SoSwitch* pcSwitchTexture {nullptr};
SoMaterial* pcShapeMaterial {nullptr};
SoTexture2* pcShapeTexture2D {nullptr};
SoGroup* pcTextureGroup3D {nullptr};
SoFCBoundingBox* pcBoundingBox {nullptr};
SoSwitch* pcBoundSwitch {nullptr};
SoBaseColor* pcBoundColor {nullptr};
Expand Down
141 changes: 141 additions & 0 deletions src/Gui/ViewProviderTextureExtension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
* Copyright (c) 2024 David Carter <dcarter@david.carter.ca> *
* *
* 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/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSwitch.h>
#include <Inventor/nodes/SoTexture2.h>
#endif

#include "ViewProviderTextureExtension.h"
#include <Gui/BitmapFactory.h>
#include <App/Material.h>


using namespace Gui;


EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderTextureExtension, Gui::ViewProviderExtension)


ViewProviderTextureExtension::ViewProviderTextureExtension()
{
initExtensionType(ViewProviderTextureExtension::getExtensionClassTypeId());
}

void ViewProviderTextureExtension::setup(SoMaterial* pcShapeMaterial)
{
pcSwitchAppearance = new SoSwitch;
pcSwitchAppearance->ref();
pcSwitchTexture = new SoSwitch;
pcSwitchTexture->ref();

pcShapeTexture2D = new SoTexture2;
pcShapeTexture2D->ref();

pcTextureGroup3D = new SoGroup;
pcTextureGroup3D->ref();

// Materials go first, with textured faces drawing over them
pcSwitchAppearance->addChild(pcShapeMaterial);
pcSwitchAppearance->addChild(pcSwitchTexture);
pcSwitchTexture->addChild(pcShapeTexture2D);
pcSwitchTexture->addChild(pcTextureGroup3D);
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
}

ViewProviderTextureExtension::~ViewProviderTextureExtension()
{
pcSwitchAppearance->unref();
pcSwitchTexture->unref();
pcShapeTexture2D->unref();
pcTextureGroup3D->unref();
}

SoSwitch* ViewProviderTextureExtension::getAppearance() const
{
return pcSwitchAppearance;
}

void ViewProviderTextureExtension::setCoinAppearance(SoMaterial* pcShapeMaterial, const App::Material& source)
{
#if 0
if (!source.image.empty()) {
Base::Console().Log("setCoinAppearance(Texture)\n");
activateTexture2D();

QByteArray by = QByteArray::fromBase64(QString::fromStdString(source.image).toUtf8());
auto image = QImage::fromData(by, "PNG"); //.scaled(64, 64, Qt::KeepAspectRatio);

SoSFImage texture;
Gui::BitmapFactory().convert(image, texture);
pcShapeTexture2D->image = texture;
} else {
Base::Console().Log("setCoinAppearance(Material)\n");
activateMaterial();
}
#endif
activateMaterial();

// Always set the material for items such as lines that don't support textures
pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
source.ambientColor.g,
source.ambientColor.b);
pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
source.diffuseColor.g,
source.diffuseColor.b);
pcShapeMaterial->specularColor.setValue(source.specularColor.r,
source.specularColor.g,
source.specularColor.b);
pcShapeMaterial->emissiveColor.setValue(source.emissiveColor.r,
source.emissiveColor.g,
source.emissiveColor.b);
pcShapeMaterial->shininess.setValue(source.shininess);
pcShapeMaterial->transparency.setValue(source.transparency);
}

void ViewProviderTextureExtension::activateMaterial()
{
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
}

void ViewProviderTextureExtension::activateTexture2D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(0);
}

void ViewProviderTextureExtension::activateTexture3D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(1);
}

void ViewProviderTextureExtension::activateMixed3D()
{
pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL);
pcSwitchTexture->whichChild.setValue(1);
}

0 comments on commit d71801b

Please sign in to comment.