KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2022 Mario Luzeiro <[email protected]>
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "material.h"
22#include <3d_math.h>
23#include <wx/debug.h>
24
29
30// This may be a good value if based on nr of lights
31// that contribute to the illumination of that point
32#define AMBIENT_FACTOR (1.0f / 6.0f)
33#define SPECULAR_FACTOR 1.0f
34
35
53
54
55MATERIAL::MATERIAL( const SFVEC3F& aAmbient, const SFVEC3F& aEmissive, const SFVEC3F& aSpecular,
56 float aShinness, float aTransparency, float aReflection )
57{
58 wxASSERT( aReflection >= 0.0f );
59 wxASSERT( aReflection <= 1.0f );
60
61 wxASSERT( aTransparency >= 0.0f );
62 wxASSERT( aTransparency <= 1.0f );
63
64 wxASSERT( aShinness >= 0.0f );
65 // No upper bound assertion on shininess since higher values are valid for Blinn-Phong
66 // and needed for realistic dark solder mask rendering
67
69
70 m_emissiveColor = aEmissive;
71 m_specularColor = aSpecular;
72 m_reflectivity = aShinness;
73 m_transparency = glm::clamp( aTransparency, 0.0f, 1.0f );
74 m_absorbance = 1.0f;
75 m_reflection = aReflection;
76 m_castShadows = true;
81
82 m_generator = nullptr;
83}
84
85
86void MATERIAL::Generate( SFVEC3F& aNormal, const RAY& aRay, const HITINFO& aHitInfo ) const
87{
88 if( m_generator )
89 {
90 aNormal = aNormal + m_generator->Generate( aRay, aHitInfo );
91 aNormal = glm::normalize( aNormal );
92 }
93}
94
95
96// https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model
97SFVEC3F BLINN_PHONG_MATERIAL::Shade( const RAY& aRay, const HITINFO& aHitInfo, float NdotL,
98 const SFVEC3F& aDiffuseObjColor, const SFVEC3F& aDirToLight,
99 const SFVEC3F& aLightColor,
100 float aShadowAttenuationFactor ) const
101{
102 wxASSERT( NdotL >= FLT_EPSILON );
103
104 // This is a hack to get some kind of fake ambient illumination
105 // There is no logic behind this, just pure artistic experimentation
106 if( aShadowAttenuationFactor > FLT_EPSILON )
107 {
108 // Calculate the diffuse light factoring in light color,
109 // power and the attenuation
110 const SFVEC3F diffuse = NdotL * aLightColor;
111
112 // Calculate the half vector between the light vector and the view vector.
113 const SFVEC3F H = glm::normalize( aDirToLight - aRay.m_Dir );
114
115 //Intensity of the specular light
116 const float NdotH = glm::dot( H, aHitInfo.m_HitNormal );
117 const float intensitySpecular = glm::pow( glm::max( NdotH, 0.0f ), m_reflectivity );
118
119 return m_ambientColor +
120 aShadowAttenuationFactor * ( diffuse * aDiffuseObjColor + SPECULAR_FACTOR *
121 aLightColor * intensitySpecular * m_specularColor );
122 }
123
124 return m_ambientColor;
125}
126
127
131
132
134
135
137{
138 m_scale = ( 2.0f * glm::pi<float>() ) / aScale;
139}
140
141
142SFVEC3F BOARD_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
143{
144 const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
145
146 // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJzaW4oc2luKHNpbih4KSoxLjkpKjEuNSkiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC45NjIxMDU3MDgwNzg1MjYyIiwiNy45NzE0MjYyNjc2MDE0MyIsIi0yLjUxNzYyMDM1MTQ4MjQ0OSIsIjIuOTc5OTM3Nzg3Mzk3NTMwMyJdLCJzaXplIjpbNjQ2LDM5Nl19XQ--
147
148 // Implement a texture as the "measling crazing blistering" method of FR4
149 const float x = glm::sin( glm::sin( hitPos.x ) * 1.5f ) * 0.06f;
150 const float y = glm::sin( glm::sin( hitPos.y ) * 1.5f ) * 0.03f;
151 const float z = -(x + y) + glm::sin( hitPos.z ) * 0.06f;
152
153 const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 0.7f ) - 0.5f;
154 const float noise2 = s_perlinNoise.noise( hitPos.x * 0.7f, hitPos.y * 1.0f ) - 0.5f;
155 const float noise3 = s_perlinNoise.noise( hitPos.x * 0.3f, hitPos.z * 1.0f ) - 0.5f;
156
157 return ( SFVEC3F( noise1, noise2, -( noise3 ) ) * 0.3f + SFVEC3F( x, y, z ) );
158}
159
160
161COPPER_NORMAL::COPPER_NORMAL( float aScale, const MATERIAL_GENERATOR* aBoardNormalGenerator )
162{
163 m_board_normal_generator = aBoardNormalGenerator;
164 m_scale = 1.0f / aScale;
165}
166
167
168SFVEC3F COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
169{
171 {
172 const SFVEC3F boardNormal = m_board_normal_generator->Generate( aRay, aHitInfo );
173
174 SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
175
176 const float noise =
177 ( s_perlinNoise.noise( hitPos.x + boardNormal.y + aRay.m_Origin.x * 0.2f,
178 hitPos.y + boardNormal.x ) - 0.5f ) * 2.0f;
179
180 float scratchPattern =
181 ( s_perlinNoise.noise( noise + hitPos.x / 100.0f, hitPos.y * 100.0f ) - 0.5f );
182
183 const float x = scratchPattern * 0.14f;
184 const float y = (noise + noise * scratchPattern) * 0.14f;
185
186 return SFVEC3F( x, y, - ( x + y ) ) + boardNormal * 0.25f;
187 }
188 else
189 {
190 return SFVEC3F( 0.0f );
191 }
192}
193
194
196{
197 m_copper_normal_generator = aCopperNormalGenerator;
198}
199
200
201SFVEC3F SOLDER_MASK_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
202{
204 {
205 const SFVEC3F copperNormal = m_copper_normal_generator->Generate( aRay, aHitInfo );
206
207 return copperNormal * 0.05f;
208 }
209 else
210 {
211 return SFVEC3F( 0.0f );
212 }
213}
214
215
216SFVEC3F PLATED_COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
217{
218 SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
219
220 const float noise1 = ( s_perlinNoise.noise( hitPos.x, hitPos.y ) - 0.5f );
221 const float noise2 = ( s_perlinNoise.noise( hitPos.y, hitPos.x ) - 0.5f );
222
223 return SFVEC3F( noise1, noise2, 0.0f ) * 0.1f;
224}
225
226
228{
229 m_scale = 1.0f / aScale;
230}
231
232
233SFVEC3F PLASTIC_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
234{
235 const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
236
237 const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
238 hitPos.z * 1.2f ) - 0.5f;
239
240 const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.0f,
241 hitPos.z * 1.5f ) - 0.5f;
242
243 const float noise3 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
244 hitPos.z * 1.8f ) - 0.5f;
245
246 const float distanceReduction = 1.0f / ( aHitInfo.m_tHit + 0.5f );
247
248 return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( distanceReduction );
249}
250
251
253{
254 m_scale = 1.0f / aScale;
255}
256
257
258SFVEC3F PLASTIC_SHINE_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
259{
260 const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
261
262 const float noise1 = s_perlinNoise.noise( hitPos.x * 0.01f, hitPos.y * 0.01f,
263 hitPos.z * 0.01f ) - 0.5f;
264
265 const float noise2 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
266 hitPos.z * 1.6f ) - 0.5f;
267
268 float noise3 = s_perlinNoise.noise( hitPos.x * 1.5f, hitPos.y * 1.5f,
269 hitPos.z * 1.0f ) - 0.5f;
270 noise3 = noise3 * noise3 * noise3;
271
272 return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( 0.1f, 0.2f, 1.0f );
273}
274
275
277{
278 m_scale = 1.0f / aScale;
279}
280
281
282SFVEC3F BRUSHED_METAL_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
283{
284 const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
285
286 const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
287 hitPos.z * 1.2f ) - 0.5f;
288
289 const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.4f,
290 hitPos.z * 1.5f ) - 0.5f;
291
292 const float noise3 = s_perlinNoise.noise( hitPos.x * 0.1f, hitPos.y * 0.1f,
293 hitPos.z * 1.0f ) - 0.5f;
294
295 return SFVEC3F( noise1 * 0.15f + noise3 * 0.35f, noise2 * 0.25f, noise1 * noise2 * noise3 );
296}
297
298
300{
301 m_scale = 1.0f / aScale;
302}
303
304
305SFVEC3F SILK_SCREEN_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
306{
307 const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
308
309 const float noise1 = s_perlinNoise.noise( hitPos.x * 2.0f, hitPos.y * 2.0f, hitPos.z );
310
311 const float noise2 = s_perlinNoise.noise( hitPos.x * 0.6f, hitPos.y * 0.6f, hitPos.z );
312
313 SFVEC3F t = SFVEC3F( noise1, noise2, 0.0f ) - 0.5f;
314
315 SFVEC3F tt = t * t;
316
317 t = t * tt * tt * 100.0f; // this factor controls the intensity of the effect
318
319 t.z = 0.0f; // this will keep untouch the original z component of the normal
320
321 return t;
322}
Defines math related functions.
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: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:142
float m_scale
Definition material.h:64
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:282
float m_scale
Definition material.h:90
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:168
const MATERIAL_GENERATOR * m_board_normal_generator
Definition material.h:89
SFVEC3F m_emissiveColor
Definition material.h:340
unsigned int m_refractionRayCount
Number of rays that will be interpolated for this material if it is transparent.
Definition material.h:351
unsigned int m_refractionRecursionCount
Number of levels it allows for refraction recursiveness.
Definition material.h:357
float m_absorbance
absorbance factor for the transparent material.
Definition material.h:346
unsigned int m_reflectionRayCount
Number of rays that will be interpolated for this material if it is reflective.
Definition material.h:354
unsigned int m_reflectionRecursionCount
Number of levels it allows for reflection recursiveness.
Definition material.h:360
static int m_defaultReflectionRayCount
Definition material.h:366
static int m_defaultRefractionRayCount
Definition material.h:365
static int m_defaultRefractionRecursionCount
Definition material.h:367
bool m_castShadows
true if this object will block the light.
Definition material.h:348
void Generate(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition material.cpp:86
SFVEC3F m_ambientColor
Definition material.h:334
float m_transparency
1.0 is completely transparent, 0.0 completely opaque.
Definition material.h:345
float m_reflection
1.0 completely reflective, 0.0 no reflective.
Definition material.h:347
const MATERIAL_GENERATOR * m_generator
Definition material.h:362
float m_reflectivity
Definition material.h:342
static int m_defaultFeflectionRecursionCount
Definition material.h:368
SFVEC3F m_specularColor
Definition material.h:341
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:233
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:258
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:216
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:305
const MATERIAL_GENERATOR * m_copper_normal_generator
Definition material.h:134
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:201
static PerlinNoise s_perlinNoise
Definition material.cpp:133
#define AMBIENT_FACTOR
Definition material.cpp:32
#define SPECULAR_FACTOR
Definition material.cpp:33
#define H(x, y, z)
Definition md5_hash.cpp:17
Stores the hit information of a ray with a point on the surface of a object.
Definition hitinfo.h:32
float m_tHit
( 4) distance
Definition hitinfo.h:34
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition hitinfo.h:33
SFVEC3F m_HitPoint
(12) hit position
Definition hitinfo.h:40
Definition ray.h:59
SFVEC3F m_Dir
Definition ray.h:63
SFVEC3F m_Origin
Definition ray.h:60
glm::vec3 SFVEC3F
Definition xv3d_types.h:40