-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cpp
223 lines (162 loc) · 5.36 KB
/
World.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* World.cpp
*
* Created on: Jan 22, 2016
* Author: Ruiss
*/
#include <stdio.h>
#include <algorithm>
#include "World.h"
//#419 begin# type = 1 # SRC = http://www.raytracegroundup.com/downloads.html
#include "Constants.h" // with a minor change in ragapixel.h
#include "Matrix.h"
#include "Vector3D.h"
#include "Point3D.h"
#include "Normal.h"
#include "ShadeRec.h"
#include "Maths.h"
//#419 end
//----------------------------------------------------------------------
void World::set_perspective(const bool isPerspective){
perspective = isPerspective;
}
void World::set_sample_number(const double number){
sample_number = number;
}
void World::set_diffuse(Vector3D ld, float kd_, float dif_){
light_dir = ld;
kd = kd_;
dif_illum = dif_;
}
// -------------------------------------------------------------------- default constructor
World::World(void)
: background_color(black),
tracer_ptr(NULL),
Image(NULL),
perspective(false)
{}
//------------------------------------------------------------------ destructor
World::~World(void) {
if(tracer_ptr) {
delete tracer_ptr;
tracer_ptr = NULL;
}
delete_objects();
}
void
World::clamp_to_color(RGBAPixel& raw_color) const {
if (raw_color.red > 255.0) raw_color.red = 255.0;
if (raw_color.green > 255.0) raw_color.green = 255.0;
if (raw_color.blue > 255.0) raw_color.blue = 255.0;
}
//------------------------------------------------------------------ render_scene
// This uses orthographic viewing along the zw axis
// I removed the const here to initialize the Image variable;
void
World::render_scene(void) {
RGBAPixel pixel_color;
Ray ray;
int hres = vp.hres;
int vres = vp.vres;
float s = vp.psize;
float zw = 100.0; // hardwired in (z origin of ray)
Image = new PNG( hres + 1, vres + 1); // not sure if we need to + 1
//if not perspective, then orthographic
if ( !perspective)
{
ray.d = Vector3D(0, 0, -1);
for (int r = 0; r < vres; r++) // up
for (int c = 0; c <= hres; c++) { // across
pixel_color = black;
for (int p = 0; p < sample_number; ++p)
for (int q = 0; q < sample_number; ++q){
double x = s * (c - 0.5 * hres + (q+0.5)/sample_number);
double y = s * (r - 0.5 * vres + (p+0.5)/sample_number);
ray.o = Point3D(x,y,zw);
pixel_color = pixel_color + tracer_ptr->trace_ray(ray); //
}
pixel_color = pixel_color/(sample_number*sample_number);
*( (*Image)(c,vres-r-1) ) = pixel_color;
}
}
else{
ray.o = Point3D(0.0,0.0,zw*2);
for (int r = 0; r < vres; r++) // up
for (int c = 0; c <= hres; c++) { // across
pixel_color = black;
for (int p = 0; p < sample_number; ++p)
for (int q = 0; q < sample_number; ++q){
double x = s * (c - 0.5 * hres + (q+0.5)/sample_number);
double y = s * (r - 0.5 * vres + (p+0.5)/sample_number);
ray.d = Point3D(x,y,-zw);
pixel_color = pixel_color + tracer_ptr->trace_ray(ray); //
}
pixel_color = pixel_color/(sample_number*sample_number);
*( (*Image)(c,vres-r-1) ) = pixel_color;
}
}
}
// ----------------------------------------------------------------------------- hit_bare_bones_objects
//Note all the phong shading occurs in here
ShadeRec
World::hit_bare_bones_objects(const Ray& ray) {
ShadeRec sr(*this);
double t;
float tmin = kHugeValue;
int num_objects = objects.size();
for (int j = 0; j < num_objects; j++){
if (objects[j]->hit(ray, t, sr) && (t < tmin)) {
sr.hit_an_object = true;
tmin = t;
sr.color = objects[j]->get_color();
double factor = std::max(sr.normal * light_dir,0.0)*kd*dif_illum;
sr.color = sr.color*factor;
clamp_to_color(sr.color);
}
}
return (sr);//This is not very efficient...copying all the staff. Definitely replace it with a pointer later
}
//------------------------------------------------------------------ delete_objects
// Deletes the objects in the objects array, and erases the array.
// The objects array still exists, because it's an automatic variable, but it's empty
void
World::delete_objects(void) {
int num_objects = objects.size();
for (int j = 0; j < num_objects; j++) {
delete objects[j];
objects[j] = NULL;
}
objects.erase (objects.begin(), objects.end());
}
// ------------------------------------------------------------------ add_object
void
World::add_object(GeometricObject* object_ptr) {
objects.push_back(object_ptr);
}
// ------------------------------------------------------------------ build function!
void
World::build(void) {
vp.set_hres(200);
vp.set_vres(200);
vp.set_pixel_size(1);
tracer_ptr = new MultipleObjects(this);
background_color = RGBAPixel(black);
// use access functions to set centre and radius
Sphere* sphere_ptr = new Sphere;
sphere_ptr->set_center(0, 0, 0);
sphere_ptr->set_radius(40);
sphere_ptr->set_color(255, 0, 0); // red
add_object(sphere_ptr);
// // // use constructor to set centre and radius
sphere_ptr = new Sphere(Point3D(0, 40, -50), 30);
sphere_ptr->set_color(255, 255, 0); // yellow
add_object(sphere_ptr);
//Add a Triangle
Triangle* tri_ptr = new Triangle(Point3D(10, 10, 70),Point3D(90, 0, 70),Point3D(0, 90, 70));
tri_ptr->set_color(0,0,255); //blue
add_object(tri_ptr);
Plane* plane_ptr = new Plane(Point3D(0), Normal(0, 0, 1));
plane_ptr->set_color(0, 76, 0); // dark green
add_object(plane_ptr);
}
// pixel_color = tracer_ptr->trace_ray(ray);