FreeCAD

Форк
0
/
ViewProviderTextureExtension.cpp 
183 строки · 6.5 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2

3
/***************************************************************************
4
 *   Copyright (c) 2024 David Carter <dcarter@david.carter.ca>             *
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
#include "PreCompiled.h"
25
#ifndef _PreComp_
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>
32
#endif
33

34
#include "ViewProviderTextureExtension.h"
35
#include <Gui/BitmapFactory.h>
36
#include <App/Material.h>
37

38

39
using namespace Gui;
40

41

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

44

45
ViewProviderTextureExtension::ViewProviderTextureExtension()
46
{
47
    initExtensionType(ViewProviderTextureExtension::getExtensionClassTypeId());
48

49
    pcSwitchAppearance = new SoSwitch;
50
    pcSwitchAppearance->ref();
51
    pcSwitchTexture = new SoSwitch;
52
    pcSwitchTexture->ref();
53

54
    pcShapeTexture2D = new SoTexture2;
55
    pcShapeTexture2D->ref();
56

57
    pcTextureGroup3D = new SoGroup;
58
    pcTextureGroup3D->ref();
59
}
60

61
void ViewProviderTextureExtension::setup(SoMaterial* pcShapeMaterial)
62
{
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);
70
}
71

72
ViewProviderTextureExtension::~ViewProviderTextureExtension()
73
{
74
    pcSwitchAppearance->unref();
75
    pcSwitchTexture->unref();
76
    pcShapeTexture2D->unref();
77
    pcTextureGroup3D->unref();
78
}
79

80
SoSwitch* ViewProviderTextureExtension::getAppearance() const
81
{
82
    return pcSwitchAppearance;
83
}
84

85
SoGroup* ViewProviderTextureExtension::getTextureGroup3D() const
86
{
87
    return pcTextureGroup3D;
88
}
89

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

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

100
        SoSFImage texture;
101
        Gui::BitmapFactory().convert(image, texture);
102
        pcShapeTexture2D->image = texture;
103
    } else {
104
        Base::Console().Log("setCoinAppearance(Material)\n");
105
        activateMaterial();
106
    }
107
#endif
108
    activateMaterial();
109

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);
125
}
126

127
void ViewProviderTextureExtension::activateMaterial()
128
{
129
    pcSwitchAppearance->whichChild.setValue(0);
130
    pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
131
}
132

133
void ViewProviderTextureExtension::activateTexture2D()
134
{
135
    pcSwitchAppearance->whichChild.setValue(1);
136
    pcSwitchTexture->whichChild.setValue(0);
137
}
138

139
void ViewProviderTextureExtension::activateTexture3D()
140
{
141
    pcSwitchAppearance->whichChild.setValue(1);
142
    pcSwitchTexture->whichChild.setValue(1);
143
}
144

145
void ViewProviderTextureExtension::activateMixed3D()
146
{
147
    pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL);
148
    pcSwitchTexture->whichChild.setValue(1);
149
}
150

151
// ------------------------------------------------------------------------------------------------
152

153
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderFaceTexture, Gui::ViewProviderTextureExtension)
154

155

156
ViewProviderFaceTexture::ViewProviderFaceTexture()
157
{
158
    initExtensionType(ViewProviderFaceTexture::getExtensionClassTypeId());
159

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();
167
}
168

169
ViewProviderFaceTexture::~ViewProviderFaceTexture()
170
{
171
    pcShapeTexture3D->unref();
172
    pcShapeCoordinates->unref();
173
    pcShapeFaceset->unref();
174
}
175

176
void ViewProviderFaceTexture::setup(SoMaterial* mat)
177
{
178
    ViewProviderTextureExtension::setup(mat);
179

180
    getTextureGroup3D()->addChild(pcShapeTexture3D);
181
    getTextureGroup3D()->addChild(pcShapeCoordinates);
182
    getTextureGroup3D()->addChild(pcShapeFaceset);
183
}
184

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.