-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_object.cpp
More file actions
executable file
·272 lines (208 loc) · 7.06 KB
/
scene_object.cpp
File metadata and controls
executable file
·272 lines (208 loc) · 7.06 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***********************************************************
Starter code for Assignment 3
Implements scene_object.h
***********************************************************/
#include <cmath>
#include "scene_object.h"
#include "stdlib.h"
#ifndef PI
#define PI 3.14159265358979323846
#endif
bool UnitSquare::intersect(Ray3D& ray, const Matrix4x4& worldToModel,
const Matrix4x4& modelToWorld, double limit) {
// TODO: implement intersection code for UnitSquare, which is
// defined on the xy-plane, with vertices (0.5, 0.5, 0),
// (-0.5, 0.5, 0), (-0.5, -0.5, 0), (0.5, -0.5, 0), and normal
// (0, 0, 1).
//
// Your goal here is to fill ray.intersection with correct values
// should an intersection occur. This includes intersection.point,
// intersection.normal, intersection.none, intersection.t_value.
//
// HINT: Remember to first transform the ray into object space
// to simplify the intersection test.
// Copy the ray to transform to model space
Ray3D rayModelSpace;
rayModelSpace.origin = worldToModel * ray.origin;
rayModelSpace.dir = worldToModel * ray.dir;
// Find the ray parameter/position at the plane.
double t = -rayModelSpace.origin[2] / rayModelSpace.dir[2];
// t must be positive.
if (t < EPSILON) {
// ray.intersection.none = true;
return false;
}
// The object is past the limt.
if (t > limit) {
return false;
}
// Compute the ray's xy-position on the plane.
Point3D rayPosOnPlane = rayModelSpace.origin + (t * rayModelSpace.dir);
double x = rayPosOnPlane[0];
double y = rayPosOnPlane[1];
double u = x + 0.5;
double v = y + 0.5;
Vector3D offset(0, 0, 0);
for (int i = 0; i < this -> normalMap.size(); i++) {
offset = offset + (this -> normalMap)[i] -> bump(Point3D(u, v, 0));
}
if (x >= - 0.5 && x <= 0.5 && y >= -0.5 && y <= 0.5) {
if ((t < ray.intersection.t_value && !ray.intersection.none) ||
ray.intersection.none) {
ray.intersection.point = modelToWorld * (rayPosOnPlane);
ray.intersection.normal =
worldToModel.transpose() * (Vector3D(0, 0,1) + offset);
ray.intersection.normal.normalize();
ray.intersection.none = false;
ray.intersection.t_value = t;
return true;
}
}
return false;
}
bool UnitSphere::intersect(Ray3D& ray, const Matrix4x4& worldToModel,
const Matrix4x4& modelToWorld, double limit) {
// TODO: implement intersection code for UnitSphere, which is centred
// on the origin.
//
// Your goal here is to fill ray.intersection with correct values
// should an intersection occur. This includes intersection.point,
// intersection.normal, intersection.none, intersection.t_value.
//
// HINT: Remember to first transform the ray into object space
// to simplify the intersection test.
Ray3D rayModelSpace;
rayModelSpace.origin = worldToModel * ray.origin;
rayModelSpace.dir = worldToModel * ray.dir;
double* intersections = (double *) malloc(sizeof(double) * 2);
int nIntersections = SolveQuadratic(
rayModelSpace.dir.dot(rayModelSpace.dir),
2.0 * rayModelSpace.dir.dot(rayModelSpace.origin.ToVector()),
(rayModelSpace.origin.ToVector().dot(rayModelSpace.origin.ToVector())) - 1,
intersections
);
Point3D rayPosOnSphere;
if (nIntersections == 0) {
goto no_intersections;
}
double t;
if (nIntersections == 1) {
// Only accept positive t
if (intersections[0] < EPSILON || intersections[0] > limit) {
goto no_intersections;
}
t = intersections[0];
}
if (nIntersections == 2) {
/*
* Find the closest to the camera, given by the intersection with the
* smallest magnitude
*/
if (intersections[0] < EPSILON || intersections[0] > limit) {
if (intersections[1] < EPSILON || intersections[1] > limit) {
goto no_intersections;
}
// Pick 1
t = intersections[1];
} else if (intersections[1] < EPSILON || intersections[1] > limit) {
// pick 0
t = intersections[0];
} else {
// pick the closest to the camera
t = std::min(intersections[0],intersections[1]);
}
}
free(intersections);
// Valid intersection
if ((t < ray.intersection.t_value && !ray.intersection.none) ||
ray.intersection.none) {
rayPosOnSphere = rayModelSpace.origin + (t * rayModelSpace.dir);
Vector3D offset(0, 0, 0);
Vector3D dHat(rayPosOnSphere[0], rayPosOnSphere[1], rayPosOnSphere[2]);
dHat.normalize();
double u = 0.5 + atan2(dHat[2], dHat[0]) / (2 * PI);
double v = 0.5 - asin(dHat[1]) / (PI);
for (int i = 0; i < this -> normalMap.size(); i++) {
offset = offset + (this -> normalMap)[i] -> bump(Point3D(u, v, 0));
}
ray.intersection.point = modelToWorld * (rayPosOnSphere);
ray.intersection.normal = (worldToModel.transpose() *
(rayPosOnSphere.ToVector() + offset));
ray.intersection.normal.normalize();
ray.intersection.t_value = t;
ray.intersection.none = false;
return true;
}
return false;
no_intersections:
free(intersections);
return false;
}
void SceneNode::rotate(char axis, double angle) {
Matrix4x4 rotation;
double toRadian = 2*M_PI/360.0;
int i;
for (i = 0; i < 2; i++) {
switch(axis) {
case 'x':
rotation[0][0] = 1;
rotation[1][1] = cos(angle*toRadian);
rotation[1][2] = -sin(angle*toRadian);
rotation[2][1] = sin(angle*toRadian);
rotation[2][2] = cos(angle*toRadian);
rotation[3][3] = 1;
break;
case 'y':
rotation[0][0] = cos(angle*toRadian);
rotation[0][2] = sin(angle*toRadian);
rotation[1][1] = 1;
rotation[2][0] = -sin(angle*toRadian);
rotation[2][2] = cos(angle*toRadian);
rotation[3][3] = 1;
break;
case 'z':
rotation[0][0] = cos(angle*toRadian);
rotation[0][1] = -sin(angle*toRadian);
rotation[1][0] = sin(angle*toRadian);
rotation[1][1] = cos(angle*toRadian);
rotation[2][2] = 1;
rotation[3][3] = 1;
break;
}
if (i == 0) {
this->trans = this->trans*rotation;
angle = -angle;
}
else {
this->invtrans = rotation*this->invtrans;
}
}
}
void SceneNode::translate(Vector3D trans) {
Matrix4x4 translation;
translation[0][3] = trans[0];
translation[1][3] = trans[1];
translation[2][3] = trans[2];
this->trans = this->trans*translation;
translation[0][3] = -trans[0];
translation[1][3] = -trans[1];
translation[2][3] = -trans[2];
this->invtrans = translation*this->invtrans;
}
void SceneNode::scale(Point3D origin, double factor[3] ) {
Matrix4x4 scale;
scale[0][0] = factor[0];
scale[0][3] = origin[0] - factor[0] * origin[0];
scale[1][1] = factor[1];
scale[1][3] = origin[1] - factor[1] * origin[1];
scale[2][2] = factor[2];
scale[2][3] = origin[2] - factor[2] * origin[2];
this->trans = this->trans*scale;
scale[0][0] = 1/factor[0];
scale[0][3] = origin[0] - 1/factor[0] * origin[0];
scale[1][1] = 1/factor[1];
scale[1][3] = origin[1] - 1/factor[1] * origin[1];
scale[2][2] = 1/factor[2];
scale[2][3] = origin[2] - 1/factor[2] * origin[2];
this->invtrans = scale*this->invtrans;
}