-
Notifications
You must be signed in to change notification settings - Fork 0
/
surface.cc
94 lines (82 loc) · 2.22 KB
/
surface.cc
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
#include <math.h>
#include "surface.hh"
/* ############################ light ############################ */
light::light()
{
pos = point();
color = Color();
}
light::light(int type, point pos_, Color color_)
{
pos = point(pos_);
pos[3] = type;
color = color_;
}
/* ########################### surface ########################### */
surface::surface() : model(0,0,1)
{
ambient = diffuse = specular = Color(1.0, 1.0, 1.0);
shininess = refractive_index = reflective_weight = refractive_weight = 0.0;
}
surface::surface(Color ambient_, Color diffuse_, Color specular_,
double shininess_, double refractive_index_,
double reflective_weight_, double refractive_weight_)
: model(0,0,1)
{
ambient = ambient_;
diffuse = diffuse_;
specular = specular_;
shininess = shininess_;
refractive_index = refractive_index_;
reflective_weight = reflective_weight_;
refractive_weight = refractive_weight_;
}
void surface::set_lighting(Color ambient_, Color diffuse_, Color specular_,
double shininess_, double refractive_index_,
double reflective_weight_, double refractive_weight_)
{
ambient = ambient_;
diffuse = diffuse_;
specular = specular_;
shininess = shininess_;
refractive_index = refractive_index_;
reflective_weight = reflective_weight_;
refractive_weight = refractive_weight_;
}
double surface::index() const
{
return refractive_index;
}
double surface::reflection() const
{
return reflective_weight;
}
double surface::refraction() const
{
return refractive_weight;
}
Color surface::phong_ambient() const
{
return ambient * Color(0.3, 0.3, 0.3);
}
Color surface::phong(point dir, light l, int depth, point vertex, point normal)
{
point lightdir = (l.pos[3] ? l.pos - vertex : l.pos).normalize(),
view = -dir.normalize();
if(normal * view < 0.0) normal = - normal;
double NdotL = normal * lightdir;
Color color;
if(NdotL > 0.0)
{
// calculate diffuse lighting
color = diffuse * l.color * NdotL;
// calculate specular highlights
// calculate halfway vector, L+V
point halfway = (lightdir + view).normalize();
float NdotH = normal * halfway;
if(NdotH > 0.0)
color += specular * l.color
* pow(NdotH, shininess);
}
return color;
}