-
Notifications
You must be signed in to change notification settings - Fork 0
/
LambertianMaterial.cc
60 lines (54 loc) · 1.68 KB
/
LambertianMaterial.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
#include "LambertianMaterial.h"
#include "HitRecord.h"
#include "Light.h"
#include "Point.h"
#include "Primitive.h"
#include "Ray.h"
#include "RenderContext.h"
#include "Scene.h"
#include "Vector.h"
#include "Math.h"
using namespace std;
LambertianMaterial::LambertianMaterial(const Color& color, float Kd, float Ka)
:color(color), Kd(Kd), Ka(Ka)
{
}
LambertianMaterial::~LambertianMaterial()
{
}
void LambertianMaterial::shade(Color& result, const RenderContext& context,
const Ray& ray, const HitRecord& hit, const Color&, int) const
{
const Scene* scene = context.getScene();
const vector<Light*>& lights = scene->getLights();
Point hitpos = ray.origin()+ray.direction()*hit.minT();
Vector normal;
hit.getPrimitive()->normal(normal, context, hitpos, ray, hit);
double costheta = Dot(normal, ray.direction());
if(costheta > 0)
normal = -normal;
const Object* world = scene->getObject();
Color light = scene->getAmbient()*Ka;
#if 0
for(vector<Light*>::const_iterator iter = lights.begin(); iter != lights.end(); iter++){
#else
Light*const* begin = &lights[0];
Light*const* end = &lights[0]+lights.size();
while(begin != end){
#endif
Color light_color;
Vector light_direction;
double dist = (*begin++)->getLight(light_color, light_direction, context, hitpos);
double cosphi = Dot(normal, light_direction);
if(cosphi > 0){
// Cast shadow rays...
HitRecord shadowhit(dist);
Ray shadowray(hitpos, light_direction);
world->intersect(shadowhit, context, shadowray);
if(!shadowhit.getPrimitive())
// No shadows...
light += light_color*(Kd*cosphi);
}
}
result = light*color;
}