-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cpp
164 lines (137 loc) · 3.49 KB
/
Scene.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <vector>
#include "Scene.h"
#include "GeometricObject.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Sphere.h"
#include "Ray.h"
#include "Hit.h"
#include "AppConstants.h"
#include "Materials.h"
#include "Colors.h"
#include "Light.h"
Scene::Scene()
{
}
Scene::~Scene()
{
deleteScene();
deleteLights();
}
void Scene::addObject(const GeometricObject* object)
{
objects.push_back(object);
}
void Scene::addLight(const Light* light)
{
lights.push_back(light);
}
Hit Scene::getNearestIntersect(const Ray& ray) const
{
Hit hit;
float minDist = appconst::MAX_DIST;
for (const GeometricObject* object : objects)
{
float distance = object->intersect(ray);
if ((distance < minDist) && (distance > appconst::MIN_DIST))
{
minDist = distance;
hit = Hit(object, ray, distance);
}
}
return hit;
}
float Scene::distanceOfNearestIntersect(const Ray& ray) const
{
float minDist = appconst::MAX_DIST;
for (const GeometricObject* object : objects)
{
float distance = object->intersect(ray);
if ((distance < minDist) && (distance > appconst::MIN_DIST))
{
minDist = distance;
}
}
return minDist;
}
const Light* Scene::getLight() const
{
return lights[0];
}
const std::vector<const Light*> Scene::getLights() const
{
return lights;
}
void Scene::buildTestScene01()
{
Light* light = new Light();
light->setPosition(Point3D (-5, 3, 10));
light->setColor(colors::WHITE);
addLight(light);
Triangle* t1 = new Triangle(Point3D(-10, 10, 0), Point3D(-10, 8, 0), Point3D(-8, 10, 0));
t1->setMaterial(&materials::DIFFUSE_MAGENTA);
addObject(t1);
Triangle* t2 = new Triangle(Point3D(10, 10, 0), Point3D(8, 10, 0), Point3D(10, 8, 0));
t2->setMaterial(&materials::DIFFUSE_YELLOW);
addObject(t2);
Triangle* t3 = new Triangle(Point3D(10, -10, 0), Point3D(10, -8, 0), Point3D(8, -10, 0));
t3->setMaterial(&materials::DIFFUSE_CYAN);
addObject(t3);
Triangle* t4 = new Triangle(Point3D(-10, -10, 0), Point3D(-8, -10, 0), Point3D(-10, -8, 0));
t4->setMaterial(&materials::DIFFUSE_WHITE);
addObject(t4);
Sphere* s1 = new Sphere(Point3D(0, 0, 0), 2.0f);
s1->setMaterial(&materials::DIFFUSE_BLUE);
addObject(s1);
}
void Scene::buildTestScene02()
{
//array of lights (soft shadows)
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Light* light = new Light();
light->setPosition(Point3D(-40 + j * (-2), -40 + i * (-2), 100));
light->setColor(colors::WHITE);
light->setIntensity(1000.0f);
addLight(light);
}
}
//adding objects
Rectangle* r1 = new Rectangle(180, 140, Point3D(-90, -88, 0), Vector3D(0, 0, 1));
r1->setMaterial(&materials::TEXTURED_RAINBOW_CHECKER);
addObject(r1);
Rectangle* r2 = new Rectangle(400, 200, Point3D(-200, 50, 0), Vector3D(0, -1, 0));
r2->setMaterial(&materials::TEXTURED_CONCRETE);
addObject(r2);
Sphere* s1 = new Sphere(Point3D(27, 10, 5), 5);
s1->setMaterial(&materials::DIFFUSE_CORAL);
addObject(s1);
Sphere* s2 = new Sphere(Point3D(-7, 10, 10), 10);
s2->setMaterial(&materials::DIFFUSE_EARTH);
addObject(s2);
Sphere* s3 = new Sphere(Point3D(6, 0, 13), 13);
s3->setMaterial(&materials::REFRACTIVE_GLASS);
addObject(s3);
Sphere* s4 = new Sphere(Point3D(40, 30, 19), 19);
s4->setMaterial(&materials::REFLECTIVE_GOLD);
addObject(s4);
Sphere* s5 = new Sphere(Point3D(35, -23, 8), 8);
s5->setMaterial(&materials::DIFFUSE_GRASS);
addObject(s5);
}
void Scene::deleteScene()
{
for (const GeometricObject* object : objects)
{
delete object;
}
}
void Scene::deleteLights()
{
for (const Light* light : lights)
{
delete light;
}
}