-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
73 lines (57 loc) · 1.87 KB
/
Scene.cpp
File metadata and controls
73 lines (57 loc) · 1.87 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
/* $Rev: 250 $ */
#include "Scene.h"
#include "Colour.h"
#include "Display.h"
#include "utility.h"
Scene::Scene() : backgroundColour(0,0,0), ambientLight(0,0,0), maxRayDepth(3), renderWidth(800), renderHeight(600), filename("render.png"), camera_(), objects_(), lights_() {
}
Scene::~Scene() {
}
void Scene::render() const {
Display display("Render", renderWidth, renderHeight, Colour(128,128,128));
std::cout << "Rendering a scene with " << objects_.size() << " objects" << std::endl;
double halfPixel = 2.0/(2*renderWidth);
for (unsigned int y = 0; y < renderHeight; ++y) {
for (unsigned int x = 0; x < renderWidth; ++x) {
double cx = (x - 0.5*renderWidth)*2.0/renderWidth + halfPixel;
double cy = (y - 0.5*renderHeight)*2.0/renderWidth + halfPixel;
Ray ray = camera_->castRay(cx,cy);
display.set(x, y, computeColour(ray, maxRayDepth));
}
display.refresh();
}
display.save(filename);
display.pause(5);
}
RayIntersection Scene::intersect(const Ray& ray) const {
RayIntersection firstHit;
firstHit.distance = infinity;
for (auto& obj : objects_) {
std::vector<RayIntersection> hits = obj->intersect(ray);
for (auto& hit : hits) {
if (hit.distance > epsilon && hit.distance < firstHit.distance) {
firstHit = hit;
}
}
}
for (auto& obj: objects_) {
auto hits = obj->intersect(ray);
}
return firstHit;
}
Colour Scene::computeColour(const Ray& viewRay, unsigned int rayDepth) const {
RayIntersection hitPoint = intersect(viewRay);
if (hitPoint.distance == infinity) {
return backgroundColour;
}
Colour hitColour = ambientLight * hitPoint.material.ambientColour;
Material& mat = hitPoint.material;
for (auto& light : lights_) {
// Check if we can see this light
}
hitColour.clip();
return hitColour;
}
bool Scene::hasCamera() const {
return bool(camera_);
}