KiCad PCB EDA Suite
material.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include "material.h"
30 #include <3d_math.h>
31 #include <wx/debug.h>
32 
37 
38 // This may be a good value if based on nr of lights
39 // that contribute to the illumination of that point
40 #define AMBIENT_FACTOR (1.0f / 6.0f)
41 #define SPECULAR_FACTOR 1.0f
42 
43 
45 {
46  m_ambientColor = SFVEC3F( 0.2f, 0.2f, 0.2f );
47  m_emissiveColor = SFVEC3F( 0.0f, 0.0f, 0.0f );
48  m_specularColor = SFVEC3F( 1.0f, 1.0f, 1.0f );
49  m_reflectivity = 50.2f;
50  m_transparency = 0.0f; // completely opaque
51  m_castShadows = true;
52  m_reflection = 0.0f;
53  m_absorbance = 1.0f;
58 
59  m_generator = nullptr;
60 }
61 
62 
63 MATERIAL::MATERIAL( const SFVEC3F& aAmbient, const SFVEC3F& aEmissive, const SFVEC3F& aSpecular,
64  float aShinness, float aTransparency, float aReflection )
65 {
66  wxASSERT( aReflection >= 0.0f );
67  wxASSERT( aReflection <= 1.0f );
68 
69  wxASSERT( aTransparency >= 0.0f );
70  wxASSERT( aTransparency <= 1.0f );
71 
72  wxASSERT( aShinness >= 0.0f );
73  wxASSERT( aShinness <= 180.0f );
74 
76 
77  m_emissiveColor = aEmissive;
78  m_specularColor = aSpecular;
79  m_reflectivity = aShinness;
80  m_transparency = glm::clamp( aTransparency, 0.0f, 1.0f );
81  m_absorbance = 1.0f;
82  m_reflection = aReflection;
83  m_castShadows = true;
88 
89  m_generator = nullptr;
90 }
91 
92 
93 void MATERIAL::Generate( SFVEC3F& aNormal, const RAY& aRay, const HITINFO& aHitInfo ) const
94 {
95  if( m_generator )
96  {
97  aNormal = aNormal + m_generator->Generate( aRay, aHitInfo );
98  aNormal = glm::normalize( aNormal );
99  }
100 }
101 
102 
103 // https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model
104 SFVEC3F BLINN_PHONG_MATERIAL::Shade( const RAY& aRay, const HITINFO& aHitInfo, float NdotL,
105  const SFVEC3F& aDiffuseObjColor, const SFVEC3F& aDirToLight,
106  const SFVEC3F& aLightColor,
107  float aShadowAttenuationFactor ) const
108 {
109  wxASSERT( NdotL >= FLT_EPSILON );
110 
111  // This is a hack to get some kind of fake ambient illumination
112  // There is no logic behind this, just pure artistic experimentation
113  if( aShadowAttenuationFactor > FLT_EPSILON )
114  {
115  // Calculate the diffuse light factoring in light color,
116  // power and the attenuation
117  const SFVEC3F diffuse = NdotL * aLightColor;
118 
119  // Calculate the half vector between the light vector and the view vector.
120  const SFVEC3F H = glm::normalize( aDirToLight - aRay.m_Dir );
121 
122  //Intensity of the specular light
123  const float NdotH = glm::dot( H, aHitInfo.m_HitNormal );
124  const float intensitySpecular = glm::pow( glm::max( NdotH, 0.0f ), m_reflectivity );
125 
126  return m_ambientColor +
127  aShadowAttenuationFactor * ( diffuse * aDiffuseObjColor + SPECULAR_FACTOR *
128  aLightColor * intensitySpecular * m_specularColor );
129  }
130 
131  return m_ambientColor;
132 }
133 
134 
136 {
137 }
138 
139 
141 
142 
144 {
145  m_scale = ( 2.0f * glm::pi<float>() ) / aScale;
146 }
147 
148 
149 SFVEC3F BOARD_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
150 {
151  const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
152 
153  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJzaW4oc2luKHNpbih4KSoxLjkpKjEuNSkiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC45NjIxMDU3MDgwNzg1MjYyIiwiNy45NzE0MjYyNjc2MDE0MyIsIi0yLjUxNzYyMDM1MTQ4MjQ0OSIsIjIuOTc5OTM3Nzg3Mzk3NTMwMyJdLCJzaXplIjpbNjQ2LDM5Nl19XQ--
154 
155  // Implement a texture as the "measling crazing blistering" method of FR4
156  const float x = glm::sin( glm::sin( hitPos.x ) * 1.5f ) * 0.06f;
157  const float y = glm::sin( glm::sin( hitPos.y ) * 1.5f ) * 0.03f;
158  const float z = -(x + y) + glm::sin( hitPos.z ) * 0.06f;
159 
160  const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 0.7f ) - 0.5f;
161  const float noise2 = s_perlinNoise.noise( hitPos.x * 0.7f, hitPos.y * 1.0f ) - 0.5f;
162  const float noise3 = s_perlinNoise.noise( hitPos.x * 0.3f, hitPos.z * 1.0f ) - 0.5f;
163 
164  return ( SFVEC3F( noise1, noise2, -( noise3 ) ) * 0.3f + SFVEC3F( x, y, z ) );
165 }
166 
167 
168 COPPER_NORMAL::COPPER_NORMAL( float aScale, const MATERIAL_GENERATOR* aBoardNormalGenerator )
169 {
170  m_board_normal_generator = aBoardNormalGenerator;
171  m_scale = 1.0f / aScale;
172 }
173 
174 
175 SFVEC3F COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
176 {
178  {
179  const SFVEC3F boardNormal = m_board_normal_generator->Generate( aRay, aHitInfo );
180 
181  SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
182 
183  const float noise =
184  ( s_perlinNoise.noise( hitPos.x + boardNormal.y + aRay.m_Origin.x * 0.2f,
185  hitPos.y + boardNormal.x ) - 0.5f ) * 2.0f;
186 
187  float scratchPattern =
188  ( s_perlinNoise.noise( noise + hitPos.x / 100.0f, hitPos.y * 100.0f ) - 0.5f );
189 
190  const float x = scratchPattern * 0.14f;
191  const float y = (noise + noise * scratchPattern) * 0.14f;
192 
193  return SFVEC3F( x, y, - ( x + y ) ) + boardNormal * 0.25f;
194  }
195  else
196  {
197  return SFVEC3F( 0.0f );
198  }
199 }
200 
201 
203 {
204  m_copper_normal_generator = aCopperNormalGenerator;
205 }
206 
207 
208 SFVEC3F SOLDER_MASK_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
209 {
211  {
212  const SFVEC3F copperNormal = m_copper_normal_generator->Generate( aRay, aHitInfo );
213 
214  return copperNormal * 0.05f;
215  }
216  else
217  {
218  return SFVEC3F( 0.0f );
219  }
220 }
221 
222 
223 SFVEC3F PLATED_COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
224 {
225  SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
226 
227  const float noise1 = ( s_perlinNoise.noise( hitPos.x, hitPos.y ) - 0.5f );
228  const float noise2 = ( s_perlinNoise.noise( hitPos.y, hitPos.x ) - 0.5f );
229 
230  return SFVEC3F( noise1, noise2, -( noise1 + noise2 ) ) * 0.02f;
231 }
232 
233 
235 {
236  m_scale = 1.0f / aScale;
237 }
238 
239 
240 SFVEC3F PLASTIC_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
241 {
242  const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
243 
244  const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
245  hitPos.z * 1.2f ) - 0.5f;
246 
247  const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.0f,
248  hitPos.z * 1.5f ) - 0.5f;
249 
250  const float noise3 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
251  hitPos.z * 1.8f ) - 0.5f;
252 
253  const float distanceReduction = 1.0f / ( aHitInfo.m_tHit + 0.5f );
254 
255  return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( distanceReduction );
256 }
257 
258 
260 {
261  m_scale = 1.0f / aScale;
262 }
263 
264 
265 SFVEC3F PLASTIC_SHINE_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
266 {
267  const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
268 
269  const float noise1 = s_perlinNoise.noise( hitPos.x * 0.01f, hitPos.y * 0.01f,
270  hitPos.z * 0.01f ) - 0.5f;
271 
272  const float noise2 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
273  hitPos.z * 1.6f ) - 0.5f;
274 
275  float noise3 = s_perlinNoise.noise( hitPos.x * 1.5f, hitPos.y * 1.5f,
276  hitPos.z * 1.0f ) - 0.5f;
277  noise3 = noise3 * noise3 * noise3;
278 
279  return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( 0.1f, 0.2f, 1.0f );
280 }
281 
282 
284 {
285  m_scale = 1.0f / aScale;
286 }
287 
288 
289 SFVEC3F BRUSHED_METAL_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
290 {
291  const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
292 
293  const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
294  hitPos.z * 1.2f ) - 0.5f;
295 
296  const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.4f,
297  hitPos.z * 1.5f ) - 0.5f;
298 
299  const float noise3 = s_perlinNoise.noise( hitPos.x * 0.1f, hitPos.y * 0.1f,
300  hitPos.z * 1.0f ) - 0.5f;
301 
302  return SFVEC3F( noise1 * 0.15f + noise3 * 0.35f, noise2 * 0.25f, noise1 * noise2 * noise3 );
303 }
304 
305 
307 {
308  m_scale = 1.0f / aScale;
309 }
310 
311 
312 SFVEC3F SILK_SCREEN_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
313 {
314  const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
315 
316  const float noise1 = s_perlinNoise.noise( hitPos.x * 2.7f, hitPos.y * 2.6f, hitPos.z );
317 
318  const float noise2 = s_perlinNoise.noise( hitPos.x * 1.1f, hitPos.y * 1.2f, hitPos.z );
319 
320  SFVEC3F t =
321  glm::abs( ( 1.8f / ( SFVEC3F( noise1, noise2, hitPos.z ) + 0.4f ) ) - 1.5f ) - 0.25f;
322  t = t * t * t * 0.1f;
323 
324  return t;
325 }
float m_reflection
1.0 completely reflective, 0.0 no reflective.
Definition: material.h:355
#define SPECULAR_FACTOR
Definition: material.cpp:41
bool m_castShadows
true if this object will block the light.
Definition: material.h:356
void Generate(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: material.cpp:93
static int m_defaultReflectionRayCount
Definition: material.h:374
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:149
SFVEC3F m_emissiveColor
Definition: material.h:348
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:265
SFVEC3F m_ambientColor
Definition: material.h:342
float m_scale
Definition: material.h:166
float m_scale
Definition: material.h:98
Definition: ray.h:67
float m_tHit
( 4) distance
Definition: hitinfo.h:43
float m_transparency
Definition: material.h:353
static int m_defaultFeflectionRecursionCount
Definition: material.h:376
SFVEC3F m_HitPoint
(12) hit position
Definition: hitinfo.h:49
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:289
unsigned int m_reflectionRecursionCount
Definition: material.h:368
unsigned int m_refractionRayCount
Number of rays that will be interpolated for this material if it is reflective.
Definition: material.h:359
A base class that can be used to derive procedurally generated materials.
Definition: material.h:39
unsigned int m_reflectionRayCount
Number of levels it allows for refraction recursiveness.
Definition: material.h:362
unsigned int m_refractionRecursionCount
Number of levels it allows for reflection recursiveness.
Definition: material.h:365
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:312
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:240
const MATERIAL_GENERATOR * m_generator
Definition: material.h:370
static int m_defaultRefractionRecursionCount
Definition: material.h:375
SFVEC3F m_Dir
Definition: ray.h:72
#define H(x, y, z)
Definition: md5_hash.cpp:17
static PerlinNoise s_perlinNoise
Definition: material.cpp:140
SFVEC3F m_specularColor
Definition: material.h:349
SFVEC3F m_Origin
Definition: ray.h:69
const MATERIAL_GENERATOR * m_copper_normal_generator
Definition: material.h:142
static int m_defaultRefractionRayCount
Definition: material.h:373
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
SFVEC3F Shade(const RAY &aRay, const HITINFO &aHitInfo, float NdotL, const SFVEC3F &aDiffuseObjColor, const SFVEC3F &aDirToLight, const SFVEC3F &aLightColor, float aShadowAttenuationFactor) const override
Shade an intersection point.
Definition: material.cpp:104
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:208
const MATERIAL_GENERATOR * m_board_normal_generator
Definition: material.h:97
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:175
SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const override
Generate a 3D vector based on the ray and hit information depending on the implementation.
Definition: material.cpp:223
Defines math related functions.
float m_absorbance
absorbance factor for the transparent material.
Definition: material.h:354
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:42
MATERIAL()
Definition: material.cpp:44
BOARD_NORMAL()
Definition: material.h:62
float m_reflectivity
1.0 is completely transparent, 0.0 completely opaque.
Definition: material.h:350
float noise(float x, float y, float z) const
float m_scale
Definition: material.h:72
virtual SFVEC3F Generate(const RAY &aRay, const HITINFO &aHitInfo) const =0
Generate a 3D vector based on the ray and hit information depending on the implementation.
#define AMBIENT_FACTOR
Definition: material.cpp:40