| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2024 David Carter <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 26 |
#include <Inventor/nodes/SoIndexedFaceSet.h> |
| 27 |
#include <Inventor/nodes/SoMaterial.h> |
| 28 |
#include <Inventor/nodes/SoSwitch.h> |
| 29 |
#include <Inventor/nodes/SoTexture2.h> |
| 30 |
#include <Inventor/nodes/SoTexture3.h> |
| 31 |
|
| 32 |
|
| 33 |
#include "ViewProviderTextureExtension.h" |
| 34 |
#include <Gui/BitmapFactory.h> |
| 35 |
#include <App/Material.h> |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
|
| 41 |
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderTextureExtension, Gui::ViewProviderExtension) |
| 42 |
|
| 43 |
|
| 44 |
ViewProviderTextureExtension::ViewProviderTextureExtension() |
| 45 |
{ |
| 46 |
initExtensionType(ViewProviderTextureExtension::getExtensionClassTypeId()); |
| 47 |
|
| 48 |
pcSwitchAppearance = new SoSwitch; |
| 49 |
pcSwitchAppearance->ref(); |
| 50 |
pcSwitchAppearance->setName("SwitchAppearance"); |
| 51 |
pcSwitchTexture = new SoSwitch; |
| 52 |
pcSwitchTexture->ref(); |
| 53 |
pcSwitchTexture->setName("SwitchTexture"); |
| 54 |
|
| 55 |
pcShapeTexture2D = new SoTexture2; |
| 56 |
pcShapeTexture2D->ref(); |
| 57 |
pcShapeTexture2D->setName("ShapeTexture2D"); |
| 58 |
|
| 59 |
pcTextureGroup3D = new SoGroup; |
| 60 |
pcTextureGroup3D->ref(); |
| 61 |
pcTextureGroup3D->setName("TextureGroup3D"); |
| 62 |
} |
| 63 |
|
| 64 |
void ViewProviderTextureExtension::setup(SoMaterial* pcShapeMaterial) |
| 65 |
{ |
| 66 |
// Materials go first, with textured faces drawing over them |
| 67 |
pcSwitchAppearance->addChild(pcShapeMaterial); |
| 68 |
pcSwitchAppearance->addChild(pcSwitchTexture); |
| 69 |
pcSwitchTexture->addChild(pcShapeTexture2D); |
| 70 |
pcSwitchTexture->addChild(pcTextureGroup3D); |
| 71 |
pcSwitchAppearance->whichChild.setValue(0); |
| 72 |
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE); |
| 73 |
} |
| 74 |
|
| 75 |
ViewProviderTextureExtension::~ViewProviderTextureExtension() |
| 76 |
{ |
| 77 |
pcSwitchAppearance->unref(); |
| 78 |
pcSwitchTexture->unref(); |
| 79 |
pcShapeTexture2D->unref(); |
| 80 |
pcTextureGroup3D->unref(); |
| 81 |
} |
| 82 |
|
| 83 |
SoSwitch* ViewProviderTextureExtension::getAppearance() const |
| 84 |
{ |
| 85 |
return pcSwitchAppearance; |
| 86 |
} |
| 87 |
|
| 88 |
SoGroup* ViewProviderTextureExtension::getTextureGroup3D() const |
| 89 |
{ |
| 90 |
return pcTextureGroup3D; |
| 91 |
} |
| 92 |
|
| 93 |
void ViewProviderTextureExtension::setCoinAppearance( |
| 94 |
SoMaterial* pcShapeMaterial, |
| 95 |
const App::Material& source |
| 96 |
) |
| 97 |
{ |
| 98 |
#if 0 |
| 99 |
if (!source.image.empty()) { |
| 100 |
Base::Console().log("setCoinAppearance(Texture)\n"); |
| 101 |
activateTexture2D(); |
| 102 |
|
| 103 |
QByteArray by = QByteArray::fromBase64(QString::fromStdString(source.image).toUtf8()); |
| 104 |
auto image = QImage::fromData(by, "PNG"); //.scaled(64, 64, Qt::KeepAspectRatio); |
| 105 |
|
| 106 |
SoSFImage texture; |
| 107 |
Gui::BitmapFactory().convert(image, texture); |
| 108 |
pcShapeTexture2D->image = texture; |
| 109 |
} else { |
| 110 |
Base::Console().log("setCoinAppearance(Material)\n"); |
| 111 |
activateMaterial(); |
| 112 |
} |
| 113 |
#endif |
| 114 |
activateMaterial(); |
| 115 |
|
| 116 |
// Always set the material for items such as lines that don't support textures |
| 117 |
pcShapeMaterial->ambientColor |
| 118 |
.setValue(source.ambientColor.r, source.ambientColor.g, source.ambientColor.b); |
| 119 |
pcShapeMaterial->diffuseColor |
| 120 |
.setValue(source.diffuseColor.r, source.diffuseColor.g, source.diffuseColor.b); |
| 121 |
pcShapeMaterial->specularColor |
| 122 |
.setValue(source.specularColor.r, source.specularColor.g, source.specularColor.b); |
| 123 |
pcShapeMaterial->emissiveColor |
| 124 |
.setValue(source.emissiveColor.r, source.emissiveColor.g, source.emissiveColor.b); |
| 125 |
pcShapeMaterial->shininess.setValue(source.shininess); |
| 126 |
pcShapeMaterial->transparency.setValue(source.transparency); |
| 127 |
} |
| 128 |
|
| 129 |
void ViewProviderTextureExtension::activateMaterial() |
| 130 |
{ |
| 131 |
pcSwitchAppearance->whichChild.setValue(0); |
| 132 |
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE); |
| 133 |
} |
| 134 |
|
| 135 |
void ViewProviderTextureExtension::activateTexture2D() |
| 136 |
{ |
| 137 |
pcSwitchAppearance->whichChild.setValue(1); |
| 138 |
pcSwitchTexture->whichChild.setValue(0); |
| 139 |
} |
| 140 |
|
| 141 |
void ViewProviderTextureExtension::activateTexture3D() |
| 142 |
{ |
| 143 |
pcSwitchAppearance->whichChild.setValue(1); |
| 144 |
pcSwitchTexture->whichChild.setValue(1); |
| 145 |
} |
| 146 |
|
| 147 |
void ViewProviderTextureExtension::activateMixed3D() |
| 148 |
{ |
| 149 |
pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL); |
| 150 |
pcSwitchTexture->whichChild.setValue(1); |
| 151 |
} |
| 152 |
|
| 153 |
// ------------------------------------------------------------------------------------------------ |
| 154 |
|
| 155 |
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderFaceTexture, Gui::ViewProviderTextureExtension) |
| 156 |
|
| 157 |
|
| 158 |
ViewProviderFaceTexture::ViewProviderFaceTexture() |
| 159 |
{ |
| 160 |
initExtensionType(ViewProviderFaceTexture::getExtensionClassTypeId()); |
| 161 |
|
| 162 |
// Support for textured faces |
| 163 |
pcShapeTexture3D = new SoTexture3; |
| 164 |
pcShapeTexture3D->ref(); |
| 165 |
pcShapeTexture3D->setName("ShapeTexture3D"); |
| 166 |
pcShapeCoordinates = new SoCoordinate3; |
| 167 |
pcShapeCoordinates->ref(); |
| 168 |
pcShapeCoordinates->setName("ShapeCoordinates"); |
| 169 |
pcShapeFaceset = new SoIndexedFaceSet; |
| 170 |
pcShapeFaceset->ref(); |
| 171 |
pcShapeFaceset->setName("ShapeFaceset"); |
| 172 |
} |
| 173 |
|
| 174 |
ViewProviderFaceTexture::~ViewProviderFaceTexture() |
| 175 |
{ |
| 176 |
pcShapeTexture3D->unref(); |
| 177 |
pcShapeCoordinates->unref(); |
| 178 |
pcShapeFaceset->unref(); |
| 179 |
} |
| 180 |
|
| 181 |
void ViewProviderFaceTexture::setup(SoMaterial* mat) |
| 182 |
{ |
| 183 |
ViewProviderTextureExtension::setup(mat); |
| 184 |
|
| 185 |
getTextureGroup3D()->addChild(pcShapeTexture3D); |
| 186 |
getTextureGroup3D()->addChild(pcShapeCoordinates); |
| 187 |
getTextureGroup3D()->addChild(pcShapeFaceset); |
| 188 |
} |