-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
279 lines (262 loc) · 7.32 KB
/
Copy pathScene.cpp
File metadata and controls
279 lines (262 loc) · 7.32 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
273
274
275
276
277
278
279
/* Deals with the rendered scene */
#include <iostream>
#include <sstream>
#include <fstream>
#include <stack>
#include <string>
#include <vector>
#include "FreeImage.h"
#include "Shapes.h"
#include "Intersection.h"
#include "Light.h"
#include "Scene.h"
using namespace std;
/*** SCENE ***/
// sets default values
Scene::Scene(char* file) {
filename = "OUTPUT";
diffuse = vec3(0,0,0);
specular = vec3(0,0,0);
shininess = 0;
emission = vec3(0,0,0);
indexofrefraction = 1;
refractivity = 0;
antialias = 2;
shadowrays = 1;
lightradius = 0;
isLight = false;
vec3 maxvec = vec3(DBL_MAX,DBL_MAX,DBL_MAX);
vec3 minvec = vec3(DBL_MIN,DBL_MIN,DBL_MIN);
sceneAABB = AABB(maxvec, minvec);
parse(file);
clog << "Constructing KDTree... ";
KDTree = new TreeNode(objects,sceneAABB);
clog << "done"<<endl;
}
Scene::~Scene() {
vector<Shape*>::iterator it;
for(it=objects.begin(); it!=objects.end(); it++){
delete *it;
}
}
Ray Scene::castEyeRay(double i, double j){
double alpha = (2.0*i-width)/width;
alpha *=tan(fovx/2.0);
double beta = (2.0*j-height)/height;
beta *= tan(fovy/2.0);
Ray ray(eye, alpha*u + beta*v - w);
return ray;
}
/***********************************************
** Parsing input and setting global variables **
***********************************************/
void Scene::setCoordinateFrame(vec3& lookat, vec3& up){
w = glm::normalize(eye - lookat);
u = glm::normalize(glm::cross(up, w));
v = glm::cross(w,u);
}
void Scene::updateAABB(vec3& point){
sceneAABB.aabbmin[0] = min(sceneAABB.aabbmin[0],point[0]);
sceneAABB.aabbmax[0] = max(sceneAABB.aabbmax[0],point[0]);
sceneAABB.aabbmin[1] = min(sceneAABB.aabbmin[1],point[1]);
sceneAABB.aabbmax[1] = max(sceneAABB.aabbmax[1],point[1]);
sceneAABB.aabbmin[2] = min(sceneAABB.aabbmin[2],point[2]);
sceneAABB.aabbmax[2] = max(sceneAABB.aabbmax[2],point[2]);
}
void Scene::parseLine(string l, stack<mat4>& mv, vector<vec3>& verts,
vector<vec3>& normverts, vector<vec3>& norms){
stringstream line(l);
string cmd;
line >> cmd;
if (cmd[0]=='#' || cmd=="") { //comment or blank line
return;
} else if (cmd == "size") {
line >> width >> height;
} else if (cmd == "maxdepth") {
line >> maxdepth;
} else if (cmd == "output") {
line >> filename;
filename.erase(filename.end()-4,filename.end());
} else if (cmd == "camera") {
double arg1, arg2, arg3;
line >> arg1 >> arg2 >> arg3;
eye = vec3(arg1,arg2,arg3);
line >> arg1 >> arg2 >> arg3;
vec3 lookat = vec3(arg1,arg2,arg3);
line >> arg1 >> arg2 >> arg3;
vec3 up = vec3(arg1,arg2,arg3);
setCoordinateFrame(lookat,up);
line >> fovy;
fovy *= pi / 180.0;
double d = height / (2.0 * tan( fovy * 0.5 ) );
fovx = 2.0 * atan( width / (2.0 * d) );
} else if (cmd == "sphere") {
double arg1, arg2, arg3, arg4;
line >> arg1;
line >> arg2;
line >> arg3;
line >> arg4;
mat4 trans = mv.top();
trans *= Transform::translate(arg1,arg2,arg3);
trans *= Transform::scale(arg4,arg4,arg4);
Sphere* s = new Sphere(trans);
sceneAABB.aabbmin[0] = min(s->aabb.aabbmin[0],sceneAABB.aabbmin[0]);
sceneAABB.aabbmax[0] = max(s->aabb.aabbmax[0],sceneAABB.aabbmax[0]);
sceneAABB.aabbmin[1] = min(s->aabb.aabbmin[1],sceneAABB.aabbmin[1]);
sceneAABB.aabbmax[1] = max(s->aabb.aabbmax[1],sceneAABB.aabbmax[1]);
sceneAABB.aabbmin[2] = min(s->aabb.aabbmin[2],sceneAABB.aabbmin[2]);
sceneAABB.aabbmax[2] = max(s->aabb.aabbmax[2],sceneAABB.aabbmax[2]);
//s->ambient = ambient;
s->diffuse = diffuse;
s->specular = specular;
s->shininess = shininess;
s->emission = emission;
s->indexofrefraction = indexofrefraction;
s->refractivity = refractivity;
objects.push_back(s);
if (isLight){
lights.push_back(s);
}
} else if (cmd == "maxverts") {
int maxverts;
line >> maxverts;
verts.reserve(maxverts);
} else if (cmd == "maxvertnorms") {
int maxvertnorms;
line >> maxvertnorms;
verts.reserve(maxvertnorms);
verts.reserve(maxvertnorms);
} else if (cmd == "vertex") {
double arg1, arg2, arg3;
line >> arg1;
line >> arg2;
line >> arg3;
vec3 v(arg1,arg2,arg3);
verts.push_back(v);
} else if (cmd == "vertexnormal") {
double arg1, arg2, arg3;
line >> arg1;
line >> arg2;
line >> arg3;
vec3 v(arg1,arg2,arg3);
normverts.push_back(v);
line >> arg1;
line >> arg2;
line >> arg3;
vec3 n(arg1,arg2,arg3);
norms.push_back(n);
} else if (cmd == "tri") {
int a1, a2, a3;
line >> a1 >> a2 >> a3;
mat4 top = mv.top();
vec3 v1 = vec3(top * vec4(verts[a1],1));
vec3 v2 = vec3(top * vec4(verts[a2],1));
vec3 v3 = vec3(top * vec4(verts[a3],1));
Triangle* t = new Triangle(v1,v2,v3);
updateAABB(v1);
updateAABB(v2);
updateAABB(v3);
//t->ambient = ambient;
t->diffuse = diffuse;
t->specular = specular;
t->shininess = shininess;
t->emission = emission;
t->indexofrefraction = indexofrefraction;
t->refractivity = refractivity;
objects.push_back(t);
if (isLight){
lights.push_back(t);
}
} else if(cmd == "trinormal") {
int a1,a2,a3;
line >> a1 >> a2 >> a3;
mat4 top = mv.top();
vec3 v1 = vec3(top * vec4(normverts[a1],1));
vec3 v2 = vec3(top * vec4(normverts[a2],1));
vec3 v3 = vec3(top * vec4(normverts[a3],1));
top = glm::transpose(glm::inverse(top));
vec3 n1 = vec3(top * vec4(norms[a1],0));
vec3 n2 = vec3(top * vec4(norms[a2],0));
vec3 n3 = vec3(top * vec4(norms[a3],0));
NormTriangle* t = new NormTriangle(v1,v2,v3,n1,n2,n3);
//t->ambient = ambient;
t->diffuse = diffuse;
t->specular = specular;
t->shininess = shininess;
t->emission = emission;
t->indexofrefraction = indexofrefraction;
t->refractivity = refractivity;
objects.push_back(t);
if (isLight){
lights.push_back(t);
}
} else if(cmd == "translate") {
double arg1,arg2,arg3;
line >> arg1;
line >> arg2;
line >> arg3;
mv.top() *= Transform::translate(arg1, arg2, arg3);
} else if(cmd == "rotate") {
double arg1,arg2,arg3,arg4;
line >> arg1;
line >> arg2;
line >> arg3;
line >> arg4;
mv.top() *= Transform::rotate(arg4,vec3(arg1,arg2,arg3));
} else if (cmd=="scale") {
double arg1,arg2,arg3;
line >> arg1;
line >> arg2;
line >> arg3;
mv.top() *= Transform::scale(arg1, arg2, arg3);
} else if (cmd == "pushTransform") {
mv.push(mv.top());
} else if (cmd == "popTransform"){
mv.pop();
} else if (cmd == "diffuse") {
double arg1, arg2, arg3;
line >> arg1 >> arg2 >> arg3;
diffuse = vec3(arg1,arg2,arg3);
} else if (cmd == "specular") {
double arg1, arg2, arg3;
line >> arg1 >> arg2 >> arg3;
specular = vec3(arg1,arg2,arg3);
} else if (cmd == "shininess") {
line >> shininess;
} else if (cmd == "emission") {
double arg1, arg2, arg3;
line >> arg1 >> arg2 >> arg3;
emission = vec3(arg1,arg2,arg3);
if (arg1 || arg2 || arg3){
isLight = true;
} else {
isLight = false;
}
} else if (cmd == "indexofrefraction") {
line >> indexofrefraction;
} else if (cmd == "refractivity") {
line >> refractivity;
} else if (cmd == "antialias") {
line >> antialias;
}
//cout << cmd << endl;
}
void Scene::parse(char* filename) {
ifstream file(filename, ifstream::in);
string line;
if (file.is_open()) {
stack<mat4> modelview;
modelview.push(mat4(1.0));
vector<vec3> verts;
vector<vec3> normverts;
vector<vec3> norms;
while (file.good()) {
getline(file, line);
parseLine(line, modelview, verts, normverts, norms);
}
} else {
cerr << "Unable to open file " << filename << endl;
exit(1);
}
file.close();
}