FreeCAD

Форк
0
/
Material.cpp 
379 строк · 13.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2005 Jürgen Riegel <juergen.riegel@web.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
#include <cstring>
28
#include <random>
29
#endif
30

31
#include "Application.h"
32
#include "Material.h"
33

34
// Helper functions to consistently convert between float and long
35
namespace
36
{
37
float fromPercent(long value)
38
{
39
    return std::roundf(value) / 100.0F;
40
}
41

42
long toPercent(float value)
43
{
44
    return std::lround(100.0 * value);
45
}
46
}  // namespace
47

48
using namespace App;
49

50

51
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
52
//===========================================================================
53
// Material
54
//===========================================================================
55
Material::Material()
56
    : shininess {0.9000F}
57
    , transparency {}
58
    , _matType {}
59
{
60
    setType(DEFAULT);
61
}
62

63
Material::Material(const char* MatName)
64
    : shininess {0.9000F}
65
    , transparency {}
66
    , _matType {}
67
{
68
    set(MatName);
69
}
70

71
Material::Material(MaterialType MatType)
72
    : shininess {0.9000F}
73
    , transparency {}
74
    , _matType {}
75
{
76
    setType(MatType);
77
}
78

79
void Material::set(const char* MatName)
80
{
81
    if (strcmp("Brass", MatName) == 0) {
82
        setType(BRASS);
83
    }
84
    else if (strcmp("Bronze", MatName) == 0) {
85
        setType(BRONZE);
86
    }
87
    else if (strcmp("Copper", MatName) == 0) {
88
        setType(COPPER);
89
    }
90
    else if (strcmp("Gold", MatName) == 0) {
91
        setType(GOLD);
92
    }
93
    else if (strcmp("Pewter", MatName) == 0) {
94
        setType(PEWTER);
95
    }
96
    else if (strcmp("Plaster", MatName) == 0) {
97
        setType(PLASTER);
98
    }
99
    else if (strcmp("Plastic", MatName) == 0) {
100
        setType(PLASTIC);
101
    }
102
    else if (strcmp("Silver", MatName) == 0) {
103
        setType(SILVER);
104
    }
105
    else if (strcmp("Steel", MatName) == 0) {
106
        setType(STEEL);
107
    }
108
    else if (strcmp("Stone", MatName) == 0) {
109
        setType(STONE);
110
    }
111
    else if (strcmp("Shiny plastic", MatName) == 0) {
112
        setType(SHINY_PLASTIC);
113
    }
114
    else if (strcmp("Satin", MatName) == 0) {
115
        setType(SATIN);
116
    }
117
    else if (strcmp("Metalized", MatName) == 0) {
118
        setType(METALIZED);
119
    }
120
    else if (strcmp("Neon GNC", MatName) == 0) {
121
        setType(NEON_GNC);
122
    }
123
    else if (strcmp("Chrome", MatName) == 0) {
124
        setType(CHROME);
125
    }
126
    else if (strcmp("Aluminium", MatName) == 0) {
127
        setType(ALUMINIUM);
128
    }
129
    else if (strcmp("Obsidian", MatName) == 0) {
130
        setType(OBSIDIAN);
131
    }
132
    else if (strcmp("Neon PHC", MatName) == 0) {
133
        setType(NEON_PHC);
134
    }
135
    else if (strcmp("Jade", MatName) == 0) {
136
        setType(JADE);
137
    }
138
    else if (strcmp("Ruby", MatName) == 0) {
139
        setType(RUBY);
140
    }
141
    else if (strcmp("Emerald", MatName) == 0) {
142
        setType(EMERALD);
143
    }
144
    else if (strcmp("Default", MatName) == 0) {
145
        setType(DEFAULT);
146
    }
147
    else {
148
        setType(USER_DEFINED);
149
    }
150
}
151

152
void Material::setType(MaterialType MatType)
153
{
154
    _matType = MatType;
155
    switch (MatType) {
156
        case BRASS:
157
            ambientColor.set(0.3294F, 0.2235F, 0.0275F);
158
            diffuseColor.set(0.7804F, 0.5686F, 0.1137F);
159
            specularColor.set(0.9922F, 0.9412F, 0.8078F);
160
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
161
            shininess = 0.2179F;
162
            transparency = 0.0000F;
163
            break;
164
        case BRONZE:
165
            ambientColor.set(0.2125F, 0.1275F, 0.0540F);
166
            diffuseColor.set(0.7140F, 0.4284F, 0.1814F);
167
            specularColor.set(0.3935F, 0.2719F, 0.1667F);
168
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
169
            shininess = 0.2000F;
170
            transparency = 0.0000F;
171
            break;
172
        case COPPER:
173
            ambientColor.set(0.3300F, 0.2600F, 0.2300F);
174
            diffuseColor.set(0.5000F, 0.1100F, 0.0000F);
175
            specularColor.set(0.9500F, 0.7300F, 0.0000F);
176
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
177
            shininess = 0.9300F;
178
            transparency = 0.0000F;
179
            break;
180
        case GOLD:
181
            ambientColor.set(0.3000F, 0.2306F, 0.0953F);
182
            diffuseColor.set(0.4000F, 0.2760F, 0.0000F);
183
            specularColor.set(0.9000F, 0.8820F, 0.7020F);
184
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
185
            shininess = 0.0625F;
186
            transparency = 0.0000F;
187
            break;
188
        case PEWTER:
189
            ambientColor.set(0.1059F, 0.0588F, 0.1137F);
190
            diffuseColor.set(0.4275F, 0.4706F, 0.5412F);
191
            specularColor.set(0.3333F, 0.3333F, 0.5216F);
192
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
193
            shininess = 0.0769F;
194
            transparency = 0.0000F;
195
            break;
196
        case PLASTER:
197
            ambientColor.set(0.0500F, 0.0500F, 0.0500F);
198
            diffuseColor.set(0.1167F, 0.1167F, 0.1167F);
199
            specularColor.set(0.0305F, 0.0305F, 0.0305F);
200
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
201
            shininess = 0.0078F;
202
            transparency = 0.0000F;
203
            break;
204
        case PLASTIC:
205
            ambientColor.set(0.1000F, 0.1000F, 0.1000F);
206
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
207
            specularColor.set(0.0600F, 0.0600F, 0.0600F);
208
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
209
            shininess = 0.0078F;
210
            transparency = 0.0000F;
211
            break;
212
        case SILVER:
213
            ambientColor.set(0.1922F, 0.1922F, 0.1922F);
214
            diffuseColor.set(0.5075F, 0.5075F, 0.5075F);
215
            specularColor.set(0.5083F, 0.5083F, 0.5083F);
216
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
217
            shininess = 0.2000F;
218
            transparency = 0.0000F;
219
            break;
220
        case STEEL:
221
            ambientColor.set(0.0020F, 0.0020F, 0.0020F);
222
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
223
            specularColor.set(0.9800F, 0.9800F, 0.9800F);
224
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
225
            shininess = 0.0600F;
226
            transparency = 0.0000F;
227
            break;
228
        case STONE:
229
            ambientColor.set(0.1900F, 0.1520F, 0.1178F);
230
            diffuseColor.set(0.7500F, 0.6000F, 0.4650F);
231
            specularColor.set(0.0784F, 0.0800F, 0.0480F);
232
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
233
            shininess = 0.1700F;
234
            transparency = 0.0000F;
235
            break;
236
        case SHINY_PLASTIC:
237
            ambientColor.set(0.0880F, 0.0880F, 0.0880F);
238
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
239
            specularColor.set(1.0000F, 1.0000F, 1.0000F);
240
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
241
            shininess = 1.0000F;
242
            transparency = 0.0000F;
243
            break;
244
        case SATIN:
245
            ambientColor.set(0.0660F, 0.0660F, 0.0660F);
246
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
247
            specularColor.set(0.4400F, 0.4400F, 0.4400F);
248
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
249
            shininess = 0.0938F;
250
            transparency = 0.0000F;
251
            break;
252
        case METALIZED:
253
            ambientColor.set(0.1800F, 0.1800F, 0.1800F);
254
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
255
            specularColor.set(0.4500F, 0.4500F, 0.4500F);
256
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
257
            shininess = 0.1300F;
258
            transparency = 0.0000F;
259
            break;
260
        case NEON_GNC:
261
            ambientColor.set(0.2000F, 0.2000F, 0.2000F);
262
            diffuseColor.set(0.0000F, 0.0000F, 0.0000F);
263
            specularColor.set(0.6200F, 0.6200F, 0.6200F);
264
            emissiveColor.set(1.0000F, 1.0000F, 0.0000F);
265
            shininess = 0.0500F;
266
            transparency = 0.0000F;
267
            break;
268
        case CHROME:
269
            ambientColor.set(0.3500F, 0.3500F, 0.3500F);
270
            diffuseColor.set(0.9176F, 0.9176F, 0.9176F);
271
            specularColor.set(0.9746F, 0.9746F, 0.9746F);
272
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
273
            shininess = 0.1000F;
274
            transparency = 0.0000F;
275
            break;
276
        case ALUMINIUM:
277
            ambientColor.set(0.3000F, 0.3000F, 0.3000F);
278
            diffuseColor.set(0.3000F, 0.3000F, 0.3000F);
279
            specularColor.set(0.7000F, 0.7000F, 0.8000F);
280
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
281
            shininess = 0.0900F;
282
            transparency = 0.0000F;
283
            break;
284
        case OBSIDIAN:
285
            ambientColor.set(0.0538F, 0.0500F, 0.0662F);
286
            diffuseColor.set(0.1828F, 0.1700F, 0.2253F);
287
            specularColor.set(0.3327F, 0.3286F, 0.3464F);
288
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
289
            shininess = 0.3000F;
290
            transparency = 0.0000F;
291
            break;
292
        case NEON_PHC:
293
            ambientColor.set(1.0000F, 1.0000F, 1.0000F);
294
            diffuseColor.set(1.0000F, 1.0000F, 1.0000F);
295
            specularColor.set(0.6200F, 0.6200F, 0.6200F);
296
            emissiveColor.set(0.0000F, 0.9000F, 0.4140F);
297
            shininess = 0.0500F;
298
            transparency = 0.0000F;
299
            break;
300
        case JADE:
301
            ambientColor.set(0.1350F, 0.2225F, 0.1575F);
302
            diffuseColor.set(0.5400F, 0.8900F, 0.6300F);
303
            specularColor.set(0.3162F, 0.3162F, 0.3162F);
304
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
305
            shininess = 0.1000F;
306
            transparency = 0.0000F;
307
            break;
308
        case RUBY:
309
            ambientColor.set(0.1745F, 0.0118F, 0.0118F);
310
            diffuseColor.set(0.6142F, 0.0414F, 0.0414F);
311
            specularColor.set(0.7278F, 0.6279F, 0.6267F);
312
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
313
            shininess = 0.6000F;
314
            transparency = 0.0000F;
315
            break;
316
        case EMERALD:
317
            ambientColor.set(0.0215F, 0.1745F, 0.0215F);
318
            diffuseColor.set(0.0757F, 0.6142F, 0.0757F);
319
            specularColor.set(0.6330F, 0.7278F, 0.6330F);
320
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
321
            shininess = 0.6000F;
322
            transparency = 0.0000F;
323
            break;
324
        case USER_DEFINED:
325
            break;
326
        default:
327
            ambientColor.set(0.3333F, 0.3333F, 0.3333F);
328
            diffuseColor.set(0.8000F, 0.8000F, 0.9000F);
329
            specularColor.set(0.5333F, 0.5333F, 0.5333F);
330
            emissiveColor.set(0.0000F, 0.0000F, 0.0000F);
331
            shininess = 0.9000F;
332
            transparency = 0.0000F;
333
            break;
334
    }
335
}
336

337
App::Material Material::getDefaultAppearance()
338
{
339
    ParameterGrp::handle hGrp =
340
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
341

342
    auto getColor = [hGrp](const char* parameter, App::Color& color) {
343
        uint32_t packed = color.getPackedRGB();
344
        packed = hGrp->GetUnsigned(parameter, packed);
345
        color.setPackedRGB(packed);
346
    };
347
    auto intRandom = [](int min, int max) -> int {
348
        static std::mt19937 generator;
349
        std::uniform_int_distribution<int> distribution(min, max);
350
        return distribution(generator);
351
    };
352

353
    App::Material mat(App::Material::DEFAULT);
354
    mat.transparency = fromPercent(hGrp->GetInt("DefaultShapeTransparency", 0));
355
    long shininess = toPercent(mat.shininess);
356
    mat.shininess = fromPercent(hGrp->GetInt("DefaultShapeShininess", shininess));
357

358
    // This is handled in the material code when using the object appearance
359
    bool randomColor = hGrp->GetBool("RandomColor", false);
360

361
    // diffuse color
362
    if (randomColor) {
363
        float red = static_cast<float>(intRandom(0, 255)) / 255.0F;
364
        float green = static_cast<float>(intRandom(0, 255)) / 255.0F;
365
        float blue = static_cast<float>(intRandom(0, 255)) / 255.0F;
366
        mat.diffuseColor = App::Color(red, green, blue);
367
    }
368
    else {
369
        // Color = (204, 204, 230) = 3435980543UL
370
        getColor("DefaultShapeColor", mat.diffuseColor);
371
    }
372

373
    getColor("DefaultAmbientColor", mat.ambientColor);
374
    getColor("DefaultEmissiveColor", mat.emissiveColor);
375
    getColor("DefaultSpecularColor", mat.specularColor);
376

377
    return mat;
378
}
379
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
380

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

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

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

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