-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_source.cpp
66 lines (48 loc) · 1.9 KB
/
light_source.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
/***********************************************************
Starter code for Assignment 3
This code was originally written by Jack Wang for
CSC418, SPRING 2005
implements light_source.h
***********************************************************/
#include <cmath>
#include "light_source.h"
void PointLight::shade( Ray3D& ray ) {
// TODO: implement this function to fill in values for ray.col
// using phong shading. Make sure your vectors are normalized, and
// clamp colour values to 1.0.
//
// It is assumed at this point that the intersection information in ray
// is available. So be sure that traverseScene() is called on the ray
// before this function.
// shading the object according to the phong reflection model
// KaIa + Kd.max(0,N.L).Id + Ks.max(0, (V.R)^exp).Is
// R = 2(L.N)*N - L
Colour Ka, Kd, Ks; // colours for ambient diffuse and specular
double Kexp;
Colour ambient, diffuse, specular;
Ka = (*ray.intersection.mat).ambient;
Kd = (*ray.intersection.mat).diffuse;
Ks = (*ray.intersection.mat).specular;
Kexp = (*ray.intersection.mat).specular_exp;
Vector3D N = ray.intersection.normal; // surface normal
N.normalize();
Vector3D L = (_pos - ray.intersection.point); //direction of lightsource from intersection
L.normalize();
Vector3D V = -ray.dir; // direction pointing towards the viewer
V.normalize();
Vector3D R = 2*(L.dot(N))*N - L; //direction that perfectly reflected ray of light would take from the intersection point on the surface
R.normalize();
ambient = Ka*_col_ambient;
ambient.clamp();
if (N.dot(L) < 0)
diffuse = 0*Kd*_col_diffuse;
else
diffuse = N.dot(L)*Kd*_col_diffuse;
diffuse.clamp();
if(pow(V.dot(R), Kexp) < 0)
specular = 0*Ks*_col_specular;
else
specular = pow(V.dot(R), Kexp)*Ks*_col_specular;
specular.clamp();
ray.col = ambient + diffuse + specular;
}