File: cmaterial.h

package info (click to toggle)
kicad 5.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 234,592 kB
  • sloc: cpp: 505,330; ansic: 57,038; python: 4,886; sh: 879; awk: 294; makefile: 253; xml: 103; perl: 5
file content (275 lines) | stat: -rw-r--r-- 9,862 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
 * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file  cmaterial.h
 * @brief
 */

#ifndef _CMATERIAL_H_
#define _CMATERIAL_H_

#include "ray.h"
#include "hitinfo.h"
#include "PerlinNoise.h"

/// A base class that can be used to derive a procedural generator implementation
class  CPROCEDURALGENERATOR
{
public:
    CPROCEDURALGENERATOR();

    /**
     * @brief Generate - Generates a 3D vector based on the ray and
     * hit information depending on the implementation
     * @param aRay: the camera ray that hits the object
     * @param aHitInfo: the hit information
     * @return the result of the procedural
     */
    virtual SFVEC3F Generate( const RAY &aRay,
                              const HITINFO &aHitInfo ) const = 0;

protected:

};


// Procedural generation of the board normals
class  CBOARDNORMAL : public CPROCEDURALGENERATOR
{
public:
    CBOARDNORMAL() : CPROCEDURALGENERATOR() { m_scale = 1.0f; }
    CBOARDNORMAL( float aScale );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    float m_scale;
};

// Procedural generation of the copper normals
class  CCOPPERNORMAL : public CPROCEDURALGENERATOR
{
public:
    CCOPPERNORMAL() : CPROCEDURALGENERATOR()
    {
        m_board_normal_generator = NULL;
        m_scale = 1.0f;
    }

    CCOPPERNORMAL( float aScale, const CPROCEDURALGENERATOR *aBoardNormalGenerator );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    const CPROCEDURALGENERATOR *m_board_normal_generator;
    PerlinNoise                 m_copper_perlin;
    float                       m_scale;
};

// Procedural generation of the solder mask
class  CSOLDERMASKNORMAL : public CPROCEDURALGENERATOR
{
public:
    CSOLDERMASKNORMAL() : CPROCEDURALGENERATOR() { m_copper_normal_generator = NULL; }
    CSOLDERMASKNORMAL( const CPROCEDURALGENERATOR *aCopperNormalGenerator );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    const CPROCEDURALGENERATOR *m_copper_normal_generator;
};


// Procedural generation of the plastic normals
class  CPLASTICNORMAL : public CPROCEDURALGENERATOR
{
public:
    CPLASTICNORMAL() : CPROCEDURALGENERATOR()
    {
        m_scale = 1.0f;
    }

    CPLASTICNORMAL( float aScale );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    PerlinNoise                 m_perlin;
    float                       m_scale;
};


// Procedural generation of the shiny plastic normals
class  CPLASTICSHINENORMAL : public CPROCEDURALGENERATOR
{
public:
    CPLASTICSHINENORMAL() : CPROCEDURALGENERATOR()
    {
        m_scale = 1.0f;
    }

    CPLASTICSHINENORMAL( float aScale );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    PerlinNoise                 m_perlin;
    float                       m_scale;
};

// Procedural generation of the shiny brushed metal
class  CMETALBRUSHEDNORMAL : public CPROCEDURALGENERATOR
{
public:
    CMETALBRUSHEDNORMAL() : CPROCEDURALGENERATOR()
    {
        m_scale = 1.0f;
    }

    CMETALBRUSHEDNORMAL( float aScale );

    // Imported from CPROCEDURALGENERATOR
    SFVEC3F Generate( const RAY &aRay,
                      const HITINFO &aHitInfo ) const override;
private:
    PerlinNoise                 m_perlin;
    float                       m_scale;
};

/// A base material class that can be used to derive a material implementation
class  CMATERIAL
{
public:
    CMATERIAL();
    CMATERIAL( const SFVEC3F &aAmbient,
               const SFVEC3F &aEmissive,
               const SFVEC3F &aSpecular,
               float aShinness,
               float aTransparency,
               float aReflection );

    const SFVEC3F &GetAmbientColor()  const { return m_ambientColor; }
    const SFVEC3F &GetEmissiveColor() const { return m_emissiveColor; }
    const SFVEC3F &GetSpecularColor() const { return m_specularColor; }

    float GetShinness()     const { return m_shinness; }
    float GetTransparency() const { return m_transparency; }
    float GetReflection()   const { return m_reflection; }
    float GetAbsorvance()   const { return m_absorbance; }
    unsigned int GetNrRefractionsSamples() const { return m_refraction_nr_samples; }
    unsigned int GetNrReflectionsSamples() const { return m_reflections_nr_samples; }

    void SetAbsorvance( float aAbsorvanceFactor ) { m_absorbance = aAbsorvanceFactor; }
    void SetNrRefractionsSamples( unsigned int aNrRefractions ) { m_refraction_nr_samples = aNrRefractions; }
    void SetNrReflectionsSamples( unsigned int aNrReflections ) { m_reflections_nr_samples = aNrReflections; }

    /**
     * @brief SetCastShadows - Set if the material can receive shadows
     * @param aCastShadows - true yes it can, false not it cannot
     */
    void SetCastShadows( bool aCastShadows ) { m_cast_shadows = aCastShadows; }

    bool GetCastShadows() const { return m_cast_shadows; }

    /**
     * @brief Shade - Shades an intersection point
     * @param aRay: the camera ray that hits the object
     * @param aHitInfo: the hit information
     * @param NdotL: the dot product between Normal and Light
     * @param aDiffuseObjColor: diffuse object color
     * @param aDirToLight: a vector of the incident light direction
     * @param aLightColor: the light color
     * @param aShadowAttenuationFactor 0.0f total in shadow, 1.0f completely not in shadow
     * @return the resultant color
     */
    virtual SFVEC3F Shade( const RAY &aRay,
                           const HITINFO &aHitInfo,
                           float NdotL,
                           const SFVEC3F &aDiffuseObjColor,
                           const SFVEC3F &aDirToLight,
                           const SFVEC3F &aLightColor,
                           float aShadowAttenuationFactor ) const = 0;

    void SetNormalPerturbator( const CPROCEDURALGENERATOR *aPerturbator ) { m_normal_perturbator = aPerturbator; }
    const CPROCEDURALGENERATOR *GetNormalPerturbator() const { return m_normal_perturbator; }

    void PerturbeNormal( SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo ) const;

protected:
    SFVEC3F m_ambientColor;

    // NOTE: we will not use diffuse color material here,
    // because it will be stored in object, since there are objects (i.e: triangles)
    // that can have per vertex color

    SFVEC3F m_emissiveColor;
    SFVEC3F m_specularColor;
    float   m_shinness;
    float   m_transparency;                     ///< 1.0 is completely transparent, 0.0 completely opaque
    float   m_absorbance;                       ///< absorvance factor for the transparent material
    float   m_reflection;                       ///< 1.0 completely reflective, 0.0 no reflective
    bool    m_cast_shadows;                     ///< true if this object will block the light
    unsigned int     m_refraction_nr_samples;   ///< nr of rays that will be interpolated for this material if it is a transparent
    unsigned int     m_reflections_nr_samples;  ///< nr of rays that will be interpolated for this material if it is reflective

    const CPROCEDURALGENERATOR *m_normal_perturbator;
};


/// Blinn Phong based material
/// https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model
class  CBLINN_PHONG_MATERIAL : public CMATERIAL
{
public:
    CBLINN_PHONG_MATERIAL() : CMATERIAL() {}

    CBLINN_PHONG_MATERIAL( const SFVEC3F &aAmbient,
                           const SFVEC3F &aEmissive,
                           const SFVEC3F &aSpecular,
                           float aShinness,
                           float aTransparency,
                           float aReflection ) : CMATERIAL( aAmbient,
                                                            aEmissive,
                                                            aSpecular,
                                                            aShinness,
                                                            aTransparency,
                                                            aReflection ) {}

    // Imported from CMATERIAL
    SFVEC3F Shade( const RAY &aRay,
                   const HITINFO &aHitInfo,
                   float NdotL,
                   const SFVEC3F &aDiffuseObjColor,
                   const SFVEC3F &aDirToLight,
                   const SFVEC3F &aLightColor,
                   float aShadowAttenuationFactor ) const override;
};

#endif // _CMATERIAL_H_