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