| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 Jürgen Riegel <[email protected]> * |
| 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 |
#include <cstring> |
| 24 |
#include <random> |
| 25 |
|
| 26 |
#include "Application.h" |
| 27 |
#include "Material.h" |
| 28 |
|
| 29 |
#include <Base/Tools.h> |
| 30 |
|
| 31 |
using namespace App; |
| 32 |
|
| 33 |
|
| 34 |
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |
| 35 |
//=========================================================================== |
| 36 |
// Material |
| 37 |
//=========================================================================== |
| 38 |
Material::Material() |
| 39 |
: shininess {0.9000F} |
| 40 |
, transparency {} |
| 41 |
, _matType {} |
| 42 |
{ |
| 43 |
setType(DEFAULT); |
| 44 |
} |
| 45 |
|
| 46 |
Material::Material(const char* MatName) |
| 47 |
: shininess {0.9000F} |
| 48 |
, transparency {} |
| 49 |
, _matType {} |
| 50 |
{ |
| 51 |
set(MatName); |
| 52 |
} |
| 53 |
|
| 54 |
Material::Material(MaterialType MatType) |
| 55 |
: shininess {0.9000F} |
| 56 |
, transparency {} |
| 57 |
, _matType {} |
| 58 |
{ |
| 59 |
setType(MatType); |
| 60 |
} |
| 61 |
|
| 62 |
void Material::set(const char* MatName) |
| 63 |
{ |
| 64 |
if (strcmp("Brass", MatName) == 0) { |
| 65 |
setType(BRASS); |
| 66 |
} |
| 67 |
else if (strcmp("Bronze", MatName) == 0) { |
| 68 |
setType(BRONZE); |
| 69 |
} |
| 70 |
else if (strcmp("Copper", MatName) == 0) { |
| 71 |
setType(COPPER); |
| 72 |
} |
| 73 |
else if (strcmp("Gold", MatName) == 0) { |
| 74 |
setType(GOLD); |
| 75 |
} |
| 76 |
else if (strcmp("Pewter", MatName) == 0) { |
| 77 |
setType(PEWTER); |
| 78 |
} |
| 79 |
else if (strcmp("Plaster", MatName) == 0) { |
| 80 |
setType(PLASTER); |
| 81 |
} |
| 82 |
else if (strcmp("Plastic", MatName) == 0) { |
| 83 |
setType(PLASTIC); |
| 84 |
} |
| 85 |
else if (strcmp("Silver", MatName) == 0) { |
| 86 |
setType(SILVER); |
| 87 |
} |
| 88 |
else if (strcmp("Steel", MatName) == 0) { |
| 89 |
setType(STEEL); |
| 90 |
} |
| 91 |
else if (strcmp("Stone", MatName) == 0) { |
| 92 |
setType(STONE); |
| 93 |
} |
| 94 |
else if (strcmp("Shiny plastic", MatName) == 0) { |
| 95 |
setType(SHINY_PLASTIC); |
| 96 |
} |
| 97 |
else if (strcmp("Satin", MatName) == 0) { |
| 98 |
setType(SATIN); |
| 99 |
} |
| 100 |
else if (strcmp("Metalized", MatName) == 0) { |
| 101 |
setType(METALIZED); |
| 102 |
} |
| 103 |
else if (strcmp("Neon GNC", MatName) == 0) { |
| 104 |
setType(NEON_GNC); |
| 105 |
} |
| 106 |
else if (strcmp("Chrome", MatName) == 0) { |
| 107 |
setType(CHROME); |
| 108 |
} |
| 109 |
else if (strcmp("Aluminium", MatName) == 0) { |
| 110 |
setType(ALUMINIUM); |
| 111 |
} |
| 112 |
else if (strcmp("Obsidian", MatName) == 0) { |
| 113 |
setType(OBSIDIAN); |
| 114 |
} |
| 115 |
else if (strcmp("Neon PHC", MatName) == 0) { |
| 116 |
setType(NEON_PHC); |
| 117 |
} |
| 118 |
else if (strcmp("Jade", MatName) == 0) { |
| 119 |
setType(JADE); |
| 120 |
} |
| 121 |
else if (strcmp("Ruby", MatName) == 0) { |
| 122 |
setType(RUBY); |
| 123 |
} |
| 124 |
else if (strcmp("Emerald", MatName) == 0) { |
| 125 |
setType(EMERALD); |
| 126 |
} |
| 127 |
else if (strcmp("Default", MatName) == 0) { |
| 128 |
setType(DEFAULT); |
| 129 |
} |
| 130 |
else { |
| 131 |
setType(USER_DEFINED); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void Material::setType(MaterialType MatType) |
| 136 |
{ |
| 137 |
_matType = MatType; |
| 138 |
switch (MatType) { |
| 139 |
case BRASS: |
| 140 |
ambientColor.set(0.3294F, 0.2235F, 0.0275F); |
| 141 |
diffuseColor.set(0.7804F, 0.5686F, 0.1137F); |
| 142 |
specularColor.set(0.9922F, 0.9412F, 0.8078F); |
| 143 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 144 |
shininess = 0.2179F; |
| 145 |
transparency = 0.0000F; |
| 146 |
break; |
| 147 |
case BRONZE: |
| 148 |
ambientColor.set(0.2125F, 0.1275F, 0.0540F); |
| 149 |
diffuseColor.set(0.7140F, 0.4284F, 0.1814F); |
| 150 |
specularColor.set(0.3935F, 0.2719F, 0.1667F); |
| 151 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 152 |
shininess = 0.2000F; |
| 153 |
transparency = 0.0000F; |
| 154 |
break; |
| 155 |
case COPPER: |
| 156 |
ambientColor.set(0.3300F, 0.2600F, 0.2300F); |
| 157 |
diffuseColor.set(0.5000F, 0.1100F, 0.0000F); |
| 158 |
specularColor.set(0.9500F, 0.7300F, 0.0000F); |
| 159 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 160 |
shininess = 0.9300F; |
| 161 |
transparency = 0.0000F; |
| 162 |
break; |
| 163 |
case GOLD: |
| 164 |
ambientColor.set(0.3000F, 0.2306F, 0.0953F); |
| 165 |
diffuseColor.set(0.4000F, 0.2760F, 0.0000F); |
| 166 |
specularColor.set(0.9000F, 0.8820F, 0.7020F); |
| 167 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 168 |
shininess = 0.0625F; |
| 169 |
transparency = 0.0000F; |
| 170 |
break; |
| 171 |
case PEWTER: |
| 172 |
ambientColor.set(0.1059F, 0.0588F, 0.1137F); |
| 173 |
diffuseColor.set(0.4275F, 0.4706F, 0.5412F); |
| 174 |
specularColor.set(0.3333F, 0.3333F, 0.5216F); |
| 175 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 176 |
shininess = 0.0769F; |
| 177 |
transparency = 0.0000F; |
| 178 |
break; |
| 179 |
case PLASTER: |
| 180 |
ambientColor.set(0.0500F, 0.0500F, 0.0500F); |
| 181 |
diffuseColor.set(0.1167F, 0.1167F, 0.1167F); |
| 182 |
specularColor.set(0.0305F, 0.0305F, 0.0305F); |
| 183 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 184 |
shininess = 0.0078F; |
| 185 |
transparency = 0.0000F; |
| 186 |
break; |
| 187 |
case PLASTIC: |
| 188 |
ambientColor.set(0.1000F, 0.1000F, 0.1000F); |
| 189 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 190 |
specularColor.set(0.0600F, 0.0600F, 0.0600F); |
| 191 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 192 |
shininess = 0.0078F; |
| 193 |
transparency = 0.0000F; |
| 194 |
break; |
| 195 |
case SILVER: |
| 196 |
ambientColor.set(0.1922F, 0.1922F, 0.1922F); |
| 197 |
diffuseColor.set(0.5075F, 0.5075F, 0.5075F); |
| 198 |
specularColor.set(0.5083F, 0.5083F, 0.5083F); |
| 199 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 200 |
shininess = 0.2000F; |
| 201 |
transparency = 0.0000F; |
| 202 |
break; |
| 203 |
case STEEL: |
| 204 |
ambientColor.set(0.0020F, 0.0020F, 0.0020F); |
| 205 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 206 |
specularColor.set(0.9800F, 0.9800F, 0.9800F); |
| 207 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 208 |
shininess = 0.0600F; |
| 209 |
transparency = 0.0000F; |
| 210 |
break; |
| 211 |
case STONE: |
| 212 |
ambientColor.set(0.1900F, 0.1520F, 0.1178F); |
| 213 |
diffuseColor.set(0.7500F, 0.6000F, 0.4650F); |
| 214 |
specularColor.set(0.0784F, 0.0800F, 0.0480F); |
| 215 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 216 |
shininess = 0.1700F; |
| 217 |
transparency = 0.0000F; |
| 218 |
break; |
| 219 |
case SHINY_PLASTIC: |
| 220 |
ambientColor.set(0.0880F, 0.0880F, 0.0880F); |
| 221 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 222 |
specularColor.set(1.0000F, 1.0000F, 1.0000F); |
| 223 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 224 |
shininess = 1.0000F; |
| 225 |
transparency = 0.0000F; |
| 226 |
break; |
| 227 |
case SATIN: |
| 228 |
ambientColor.set(0.0660F, 0.0660F, 0.0660F); |
| 229 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 230 |
specularColor.set(0.4400F, 0.4400F, 0.4400F); |
| 231 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 232 |
shininess = 0.0938F; |
| 233 |
transparency = 0.0000F; |
| 234 |
break; |
| 235 |
case METALIZED: |
| 236 |
ambientColor.set(0.1800F, 0.1800F, 0.1800F); |
| 237 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 238 |
specularColor.set(0.4500F, 0.4500F, 0.4500F); |
| 239 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 240 |
shininess = 0.1300F; |
| 241 |
transparency = 0.0000F; |
| 242 |
break; |
| 243 |
case NEON_GNC: |
| 244 |
ambientColor.set(0.2000F, 0.2000F, 0.2000F); |
| 245 |
diffuseColor.set(0.0000F, 0.0000F, 0.0000F); |
| 246 |
specularColor.set(0.6200F, 0.6200F, 0.6200F); |
| 247 |
emissiveColor.set(1.0000F, 1.0000F, 0.0000F); |
| 248 |
shininess = 0.0500F; |
| 249 |
transparency = 0.0000F; |
| 250 |
break; |
| 251 |
case CHROME: |
| 252 |
ambientColor.set(0.3500F, 0.3500F, 0.3500F); |
| 253 |
diffuseColor.set(0.9176F, 0.9176F, 0.9176F); |
| 254 |
specularColor.set(0.9746F, 0.9746F, 0.9746F); |
| 255 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 256 |
shininess = 0.1000F; |
| 257 |
transparency = 0.0000F; |
| 258 |
break; |
| 259 |
case ALUMINIUM: |
| 260 |
ambientColor.set(0.3000F, 0.3000F, 0.3000F); |
| 261 |
diffuseColor.set(0.3000F, 0.3000F, 0.3000F); |
| 262 |
specularColor.set(0.7000F, 0.7000F, 0.8000F); |
| 263 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 264 |
shininess = 0.0900F; |
| 265 |
transparency = 0.0000F; |
| 266 |
break; |
| 267 |
case OBSIDIAN: |
| 268 |
ambientColor.set(0.0538F, 0.0500F, 0.0662F); |
| 269 |
diffuseColor.set(0.1828F, 0.1700F, 0.2253F); |
| 270 |
specularColor.set(0.3327F, 0.3286F, 0.3464F); |
| 271 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 272 |
shininess = 0.3000F; |
| 273 |
transparency = 0.0000F; |
| 274 |
break; |
| 275 |
case NEON_PHC: |
| 276 |
ambientColor.set(1.0000F, 1.0000F, 1.0000F); |
| 277 |
diffuseColor.set(1.0000F, 1.0000F, 1.0000F); |
| 278 |
specularColor.set(0.6200F, 0.6200F, 0.6200F); |
| 279 |
emissiveColor.set(0.0000F, 0.9000F, 0.4140F); |
| 280 |
shininess = 0.0500F; |
| 281 |
transparency = 0.0000F; |
| 282 |
break; |
| 283 |
case JADE: |
| 284 |
ambientColor.set(0.1350F, 0.2225F, 0.1575F); |
| 285 |
diffuseColor.set(0.5400F, 0.8900F, 0.6300F); |
| 286 |
specularColor.set(0.3162F, 0.3162F, 0.3162F); |
| 287 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 288 |
shininess = 0.1000F; |
| 289 |
transparency = 0.0000F; |
| 290 |
break; |
| 291 |
case RUBY: |
| 292 |
ambientColor.set(0.1745F, 0.0118F, 0.0118F); |
| 293 |
diffuseColor.set(0.6142F, 0.0414F, 0.0414F); |
| 294 |
specularColor.set(0.7278F, 0.6279F, 0.6267F); |
| 295 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 296 |
shininess = 0.6000F; |
| 297 |
transparency = 0.0000F; |
| 298 |
break; |
| 299 |
case EMERALD: |
| 300 |
ambientColor.set(0.0215F, 0.1745F, 0.0215F); |
| 301 |
diffuseColor.set(0.0757F, 0.6142F, 0.0757F); |
| 302 |
specularColor.set(0.6330F, 0.7278F, 0.6330F); |
| 303 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 304 |
shininess = 0.6000F; |
| 305 |
transparency = 0.0000F; |
| 306 |
break; |
| 307 |
case USER_DEFINED: |
| 308 |
break; |
| 309 |
default: |
| 310 |
ambientColor.set(0.3333F, 0.3333F, 0.3333F); |
| 311 |
diffuseColor.set(0.8000F, 0.8000F, 0.9000F); |
| 312 |
specularColor.set(0.5333F, 0.5333F, 0.5333F); |
| 313 |
emissiveColor.set(0.0000F, 0.0000F, 0.0000F); |
| 314 |
shininess = 0.9000F; |
| 315 |
transparency = 0.0000F; |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
App::Material Material::getDefaultAppearance() |
| 321 |
{ |
| 322 |
ParameterGrp::handle hGrp = |
| 323 |
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"); |
| 324 |
|
| 325 |
auto getColor = [hGrp](const char* parameter, Base::Color& color) { |
| 326 |
uint32_t packed = color.getPackedRGB(); |
| 327 |
packed = hGrp->GetUnsigned(parameter, packed); |
| 328 |
color.setPackedRGB(packed); |
| 329 |
}; |
| 330 |
auto intRandom = [](int min, int max) -> int { |
| 331 |
static std::mt19937 generator; |
| 332 |
std::uniform_int_distribution<int> distribution(min, max); |
| 333 |
return distribution(generator); |
| 334 |
}; |
| 335 |
|
| 336 |
App::Material mat(App::Material::DEFAULT); |
| 337 |
mat.transparency = Base::fromPercent(hGrp->GetInt("DefaultShapeTransparency", 0)); |
| 338 |
long shininess = Base::toPercent(mat.shininess); |
| 339 |
mat.shininess = Base::fromPercent(hGrp->GetInt("DefaultShapeShininess", shininess)); |
| 340 |
|
| 341 |
// This is handled in the material code when using the object appearance |
| 342 |
bool randomColor = hGrp->GetBool("RandomColor", false); |
| 343 |
|
| 344 |
// diffuse color |
| 345 |
if (randomColor) { |
| 346 |
float red = static_cast<float>(intRandom(0, 255)) / 255.0F; |
| 347 |
float green = static_cast<float>(intRandom(0, 255)) / 255.0F; |
| 348 |
float blue = static_cast<float>(intRandom(0, 255)) / 255.0F; |
| 349 |
mat.diffuseColor = Base::Color(red, green, blue); |
| 350 |
} |
| 351 |
else { |
| 352 |
// Base::Color = (204, 204, 230) = 3435980543UL |
| 353 |
getColor("DefaultShapeColor", mat.diffuseColor); |
| 354 |
} |
| 355 |
|
| 356 |
getColor("DefaultAmbientColor", mat.ambientColor); |
| 357 |
getColor("DefaultEmissiveColor", mat.emissiveColor); |
| 358 |
getColor("DefaultSpecularColor", mat.specularColor); |
| 359 |
|
| 360 |
return mat; |
| 361 |
} |
| 362 |
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |