1
// SPDX-License-Identifier: LGPL-2.1-or-later
3
/***************************************************************************
4
* Copyright (c) 2024 David Carter <dcarter@david.carter.ca> *
6
* This file is part of FreeCAD. *
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. *
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. *
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/>. *
22
**************************************************************************/
24
#include "PreCompiled.h"
26
#include <Inventor/nodes/SoCoordinate3.h>
27
#include <Inventor/nodes/SoIndexedFaceSet.h>
28
#include <Inventor/nodes/SoMaterial.h>
29
#include <Inventor/nodes/SoSwitch.h>
30
#include <Inventor/nodes/SoTexture2.h>
31
#include <Inventor/nodes/SoTexture3.h>
34
#include "ViewProviderTextureExtension.h"
35
#include <Gui/BitmapFactory.h>
36
#include <App/Material.h>
42
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderTextureExtension, Gui::ViewProviderExtension)
45
ViewProviderTextureExtension::ViewProviderTextureExtension()
47
initExtensionType(ViewProviderTextureExtension::getExtensionClassTypeId());
49
pcSwitchAppearance = new SoSwitch;
50
pcSwitchAppearance->ref();
51
pcSwitchTexture = new SoSwitch;
52
pcSwitchTexture->ref();
54
pcShapeTexture2D = new SoTexture2;
55
pcShapeTexture2D->ref();
57
pcTextureGroup3D = new SoGroup;
58
pcTextureGroup3D->ref();
61
void ViewProviderTextureExtension::setup(SoMaterial* pcShapeMaterial)
63
// Materials go first, with textured faces drawing over them
64
pcSwitchAppearance->addChild(pcShapeMaterial);
65
pcSwitchAppearance->addChild(pcSwitchTexture);
66
pcSwitchTexture->addChild(pcShapeTexture2D);
67
pcSwitchTexture->addChild(pcTextureGroup3D);
68
pcSwitchAppearance->whichChild.setValue(0);
69
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
72
ViewProviderTextureExtension::~ViewProviderTextureExtension()
74
pcSwitchAppearance->unref();
75
pcSwitchTexture->unref();
76
pcShapeTexture2D->unref();
77
pcTextureGroup3D->unref();
80
SoSwitch* ViewProviderTextureExtension::getAppearance() const
82
return pcSwitchAppearance;
85
SoGroup* ViewProviderTextureExtension::getTextureGroup3D() const
87
return pcTextureGroup3D;
90
void ViewProviderTextureExtension::setCoinAppearance(SoMaterial* pcShapeMaterial, const App::Material& source)
93
if (!source.image.empty()) {
94
Base::Console().Log("setCoinAppearance(Texture)\n");
97
QByteArray by = QByteArray::fromBase64(QString::fromStdString(source.image).toUtf8());
98
auto image = QImage::fromData(by, "PNG"); //.scaled(64, 64, Qt::KeepAspectRatio);
101
Gui::BitmapFactory().convert(image, texture);
102
pcShapeTexture2D->image = texture;
104
Base::Console().Log("setCoinAppearance(Material)\n");
110
// Always set the material for items such as lines that don't support textures
111
pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
112
source.ambientColor.g,
113
source.ambientColor.b);
114
pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
115
source.diffuseColor.g,
116
source.diffuseColor.b);
117
pcShapeMaterial->specularColor.setValue(source.specularColor.r,
118
source.specularColor.g,
119
source.specularColor.b);
120
pcShapeMaterial->emissiveColor.setValue(source.emissiveColor.r,
121
source.emissiveColor.g,
122
source.emissiveColor.b);
123
pcShapeMaterial->shininess.setValue(source.shininess);
124
pcShapeMaterial->transparency.setValue(source.transparency);
127
void ViewProviderTextureExtension::activateMaterial()
129
pcSwitchAppearance->whichChild.setValue(0);
130
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
133
void ViewProviderTextureExtension::activateTexture2D()
135
pcSwitchAppearance->whichChild.setValue(1);
136
pcSwitchTexture->whichChild.setValue(0);
139
void ViewProviderTextureExtension::activateTexture3D()
141
pcSwitchAppearance->whichChild.setValue(1);
142
pcSwitchTexture->whichChild.setValue(1);
145
void ViewProviderTextureExtension::activateMixed3D()
147
pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL);
148
pcSwitchTexture->whichChild.setValue(1);
151
// ------------------------------------------------------------------------------------------------
153
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderFaceTexture, Gui::ViewProviderTextureExtension)
156
ViewProviderFaceTexture::ViewProviderFaceTexture()
158
initExtensionType(ViewProviderFaceTexture::getExtensionClassTypeId());
160
// Support for textured faces
161
pcShapeTexture3D = new SoTexture3;
162
pcShapeTexture3D->ref();
163
pcShapeCoordinates = new SoCoordinate3;
164
pcShapeCoordinates->ref();
165
pcShapeFaceset = new SoIndexedFaceSet;
166
pcShapeFaceset->ref();
169
ViewProviderFaceTexture::~ViewProviderFaceTexture()
171
pcShapeTexture3D->unref();
172
pcShapeCoordinates->unref();
173
pcShapeFaceset->unref();
176
void ViewProviderFaceTexture::setup(SoMaterial* mat)
178
ViewProviderTextureExtension::setup(mat);
180
getTextureGroup3D()->addChild(pcShapeTexture3D);
181
getTextureGroup3D()->addChild(pcShapeCoordinates);
182
getTextureGroup3D()->addChild(pcShapeFaceset);