-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlighting.cpp
More file actions
73 lines (70 loc) · 2.54 KB
/
Copy pathlighting.cpp
File metadata and controls
73 lines (70 loc) · 2.54 KB
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
#include "HW6_2_Utilities.h"
#include "dataStructures.h"
#include "trig.h"
#include<math.h>
////////////////////////////////////////
//private function declarations
////////////////////////////////////////
bool obscured(vect const &point, int iObjectIndex, vect const &light);
//######################################
//end private function declarations
//######################################
////////////////////////////////////////
//private functions
////////////////////////////////////////
bool obscured(vect const &point, int iObjectIndex, vect const &light)
{
float t = (float)(~(1<<31));
Ray lightRay = Ray(light, point - light);
normalize(lightRay.d);
for(int i = 0; i<g_iNumPlanes + g_iNumSpheres; ++i)
{
if(i!=iObjectIndex && g_papoObjects[i]->intersect(lightRay, t))
{
if(length(lightRay.d * t) < length(point - light))
{
return true;
}
}
}
return false;
}
//######################################
//end private functions
//######################################
////////////////////////////////////////
//public functions
////////////////////////////////////////
color getLighting(Ray const &ray, vect const &point, int iObjectIndex)
{
//start with ambient light
color c = g_papoObjects[iObjectIndex]->kamb * g_tAmbientLight, cDiffuse, cSpec;
vect r, tLightVector;
float dotDiffuse, dotSpec;
for(int i = 0; i<g_iNumLights; ++i)
{
if(!obscured(point, iObjectIndex, g_patLightList[i].p))
{
tLightVector = g_patLightList[i].p - point;
normalize(tLightVector);
dotDiffuse = dot(tLightVector, g_papoObjects[iObjectIndex]->getNormal(point));
dotSpec = dot(g_papoObjects[iObjectIndex]->getReflection(tLightVector, point), (-1.0*ray.d));
if(dotDiffuse > 0.0)
{
cDiffuse = cDiffuse + g_patLightList[i].brightness * dotDiffuse;
}
if(dotSpec > 0.0)
{
cSpec = cSpec + g_patLightList[i].brightness * power(dotSpec, g_papoObjects[iObjectIndex]->shininess);
}
}
}
//final lighting calculation
c = c + g_papoObjects[iObjectIndex]->kdiff * cDiffuse + g_papoObjects[iObjectIndex]->kspec * cSpec;
//only the base of the object is shown
c = c * g_papoObjects[iObjectIndex]->c;
return c;
}
//######################################
//end public functions
//######################################