-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckerboard.h
78 lines (67 loc) · 2.48 KB
/
Checkerboard.h
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
#pragma once
class Checkerboard: public ObjectMaterial {
public:
Checkerboard() = default;
Checkerboard( const float3& color1, const float3& color2 ): color1( color1 ), color2( color2 ) {
diffuse = 1.0f;
specular = 0.0f;
}
Checkerboard( const float3& color1, const float3& color2, const float& diffuse ): color1( color1 ), color2( color2 ) {
this->diffuse = clamp( diffuse, 0.0f, 1.0f );
specular = 1.0f - this->diffuse;
}
virtual MaterialType getFlag() const override {
if ( diffuse < FLT_EPSILON ) {
return MaterialType::SPECULAR;
}
if ( specular < FLT_EPSILON ) {
return MaterialType::DIFFUSE;
}
return MaterialType::MIX;
}
virtual float3 GetColor( Ray& ray_in ) const override {
float3 I = ray_in.IntersectionPoint();
bool evenX = abs( ( (int) floor( I.x ) ) % 2 ) == 0;
bool evenZ = abs( ( (int) floor( I.z ) ) % 2 ) == 0;
if ( evenX == evenZ ) {
return color1;
} else {
return color2;
}
}
virtual bool scatter( Ray& ray_in, float3 I, float3 N, Ray& ray_out ) const override {
if ( diffuse < FLT_EPSILON ) {
// if diffuse is 0, do reflection
ray_out = Ray( I, normalize( reflect( ray_in.D, N ) ) );
return true;
} else if ( specular < FLT_EPSILON ) {
// if specular is 0, do diffuse reflection
ray_out = Ray( I, DiffuseReflection( N ) );
return false;
}
// if both are > 0 we pick one randomly
if ( RandomFloat() < specular ) {
ray_out = Ray( I, normalize( reflect( ray_in.D, N ) ) );
return true;
}
ray_out = Ray( I, DiffuseReflection( N ) );
return false;
}
virtual float* getColorModifier( Ray& ray_in, float3 N ) const {
float3 I = ray_in.IntersectionPoint();
bool evenX = abs( ( (int) floor( I.x ) ) % 2 ) == 0;
bool evenZ = abs( ( (int) floor( I.z ) ) % 2 ) == 0;
if ( evenX == evenZ ) {
float results[4] = { color1.x, color1.y, color1.z, diffuse };
return new float[4] { color1.x, color1.y, color1.z, diffuse };
} else {
float results[4] = { color2.x, color2.y, color2.z, diffuse };
return new float[4] { color2.x, color2.y, color2.z, diffuse };
}
}
private:
float3 color1;
float3 color2;
float diffuse;
float specular;
};