-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.cpp
248 lines (192 loc) · 8.11 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//**********************************************************//
// //
// World.cpp - member functions for world class //
// //
//**********************************************************//
#include "world.h"
///////////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
World::World()
{
camera = NULL;
light = NULL;
viewplane = NULL;
}
///////////////////////////////////////////////////////////////////////////////////
//
// Deconstructor
//
World::~World()
{
//delete shapes from vector
for (int i=0; i < vecShapes.size(); i++) {
delete vecShapes.at(i);
}
//delete vecShapes;
vecShapes.erase(vecShapes.begin(), vecShapes.end());
if (camera != NULL)
delete camera;
if (viewplane != NULL)
delete viewplane;
if (light != NULL)
delete light;
}
///////////////////////////////////////////////////////////////////////////////////
//
// addShape - adds the shaoe to the shape vector
//
void World::addShape(ThreeDimensionalShape* theShape)
{
vecShapes.push_back(theShape);
}
///////////////////////////////////////////////////////////////////////////////////
//
// addCamera - adds camera - the viewpoint
//
void World::addCamera(Point3d* cam)
{
camera = cam;
}
///////////////////////////////////////////////////////////////////////////////////
//
// addLight - adds camera - the viewpoint
//
void World::addLight(Light* l)
{
light = l;
}
///////////////////////////////////////////////////////////////////////////////////
//
// addViewplane - adds viewplane - the window on the world
//
void World::addViewplane(Viewplane* vp)
{
viewplane = vp;
}
///////////////////////////////////////////////////////////////////////////////////
//
// render - renders the scene viewed through the viewplane to image "output.tga"
//
void World::render()
{
//check everything has been set up
if (camera == NULL || light == NULL || viewplane == NULL) {
cout << "something not initialised for render() to work" << endl;
return;
}
int image_x = viewplane->getImagexSize();
int image_y = viewplane->getImageySize();
double pix_x_cent = viewplane->getBottomLeftx() + (viewplane->getPixelxSize() / 2);
double pix_y_cent = (viewplane->getBottomLefty() + viewplane->getySize() ) - (viewplane->getPixelySize() / 2);
Ray* r = new Ray(0, 0, viewplane->getBottomLeftz() - camera->z); // initialize a ray object for re-use
// Ray* r = new Ray(pix_x_cent - camera->x, pix_y_cent - camera->y, vp->getBottomLeftz() - camera->z);
Ray* normal = new Ray(); // more efficient to use 1 object and modify it
// rather than creating and deleting objects for each intersection
Ray* lray = new Ray(); // ray in direction from intersection to light
Ray* refl = new Ray();
Point3d* intersect = new Point3d(); // used for intersection co-ords
//vector<ThreeDimensionalShape*>::iterator shapeIterator; // vector iterator
//have the ray equation here? in comments
for(int y = 0; y < image_y; y++) {
for(int x = 0; x < image_x; x++) { //for each pixel in image
r->delta_x = (pix_x_cent - camera->x);
r->delta_y = (pix_y_cent - camera->y);
// delta_z doesn't change - viewplane = zplane
/////////////
// find nearest shape hit
double t = 0;
ThreeDimensionalShape* shape_hit = NULL;
for (int i = 0; i < vecShapes.size(); i++) {
//if (s1->intersects(r, camera))
if (vecShapes.at(i)->intersects(r, camera)) {
if ((t == 0) || (r->t < t)) {
//t is distamce of intersect from start of ray (camera, i think)
t = r->t; // set t to first intersection or to nearest so far
shape_hit = vecShapes.at(i);
}
}
}
//
////////////////
if (t != 0) { // if a shape is hit with this ray ie. t!=0
r->t = t; // set r->t back to t just for readability
// calculate point of intersection
intersect->x = camera->x + (r->t * r->delta_x); // x = x0 + t(x1 - x0)
intersect->y = camera->y + (r->t * r->delta_y); // etc.
intersect->z = camera->z + (r->t * r->delta_z);
// calculate unit light ray at point of intersection
lray->delta_x = light->x - intersect->x;
lray->delta_y = light->y - intersect->y;
lray->delta_z = light->z - intersect->z;
shape_hit->calcNormal(normal, intersect); // results stored in normal
bool in_shadow = false;
// check to see if point of intersection is in shadow
for (int i = 0; i < vecShapes.size(); i++) {
if (!in_shadow && (vecShapes.at(i) != shape_hit))
//change intersects to getIntersect?
if (vecShapes.at(i)->intersects(lray, intersect))
if (lray->t < 1 && lray->t > 0) {
in_shadow = true;
break;
}
}
if (!in_shadow) {
lray->normalize(); //normalize after doing shadow detection -this is so 0<t<1 when doing shadows
double dotprod = normal->delta_x * lray->delta_x;
dotprod += normal->delta_y * lray->delta_y;
dotprod += normal->delta_z * lray->delta_z;
double diffuse = light->intensity * shape_hit->diffuse * dotprod;
// cout << intens * s1->red << endl;
if (diffuse < 0) // if negative, normal must point away from light, in shadow
diffuse = 0;
// calculate specular
// calculate reflection ray
refl->delta_x = (normal->delta_x * 2 * dotprod) - lray->delta_x;
refl->delta_y = (normal->delta_y * 2 * dotprod) - lray->delta_y;
refl->delta_z = (normal->delta_z * 2 * dotprod) - lray->delta_z;
// normalize refl ray?
refl->normalize();
dotprod = refl->delta_x * lray->delta_x;
dotprod += refl->delta_y * lray->delta_y;
dotprod += refl->delta_z * lray->delta_z;
// raise R.L to the power of k, surface specular coefficient
double specular = pow(dotprod, shape_hit->specular);
if (specular < 0)
specular = 0;
/* if (specular > 0.5) // experimental
diffuse = 0; // specular stuff
*/
//int red = (diffuse * s1->red) + (specular * l->r);
int red, green, blue;
red = (diffuse * shape_hit->red) + (specular * light->r);
if ( red> 255)
red = 255;
green = (diffuse * shape_hit->green) + (specular * light->g);
if ( green > 255)
green = 255;
blue = (diffuse * shape_hit->blue) + (specular * light->b);
if ( blue > 255)
blue = 255;
// cout << specular << endl;
viewplane->colourPixel(red, green, blue, x, y);
} else // in shadow
viewplane->colourPixel(0, 0, 0, x, y);
} else
viewplane->colourPixel(0, 0, 0, x, y);
if (x == image_x-1) // if last pixel in row
pix_x_cent = viewplane->getBottomLeftx() + (viewplane->getPixelxSize() / 2);
else
pix_x_cent += viewplane->getPixelxSize();
}
pix_y_cent -= viewplane->getPixelySize();
if (y % 100 == 0)
cout << "rendered line " << y << endl;
}
delete r;
delete normal;
delete lray;
delete refl;
delete intersect;
}