/
Material.cpp
67 lines (60 loc) · 1.79 KB
/
Material.cpp
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
#include "Material.h"
Material::Material( const Vector3f& d_color ,const Vector3f& s_color, float s,
float r):
diffuseColor( d_color),refractionIndex(r),shininess(s),specularColor(s_color)
{
}
Material::~Material()
{
}
Vector3f Material::getDiffuseColor() const
{ return diffuseColor;}
Vector3f Material::Shade( const Ray& ray, const Hit& hit,
const Vector3f& dirToLight, const Vector3f& lightColor ) {
Vector3f kd;
if(t.valid() && hit.hasTex){
Vector2f texCoord = hit.texCoord;
Vector3f texColor = t(texCoord[0],texCoord[1]);
kd = texColor;
}else{
kd = this->diffuseColor;
}
//Diffuse Shading
if(noise.valid()){
kd = noise.getColor(ray.getOrigin()+ray.getDirection()*hit.getT());
}
Vector3f n = hit.getNormal().normalized();
Vector3f color = clampedDot( dirToLight ,n )*pointwiseDot( lightColor , kd);
return color;
}
Vector3f Material::pointwiseDot( const Vector3f& v1 , const Vector3f& v2 ) {
Vector3f out = Vector3f( v1[0]*v2[0] , v1[1]*v2[1] , v1[2]*v2[2]);
return out;
}
Vector3f Material::ambientShade(const Hit& hit, const Vector3f& ambient) {
Vector3f d_col = diffuseColor;
if(hit.hasTex && t.valid()) {
d_col = t(hit.texCoord[0], hit.texCoord[1]);
}
return Vector3f(ambient[0] * d_col[0], ambient[1] * d_col[1], ambient[2] * d_col[2]);
}
float Material::clampedDot( const Vector3f& L , const Vector3f& N )const {
float d = Vector3f::dot(L,N);
return (d>0)? d : 0 ;
}
void Material::loadTexture(const char * filename){
t.load(filename);
}
float Material::getRefractionIndex(){
return refractionIndex;
}
Vector3f Material::getDiffuseColor(){
return diffuseColor;
}
Vector3f Material::getSpecularColor(){
return specularColor;
}
void Material::setNoise(const Noise & n)
{
noise=n;
}