-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneParser.h
292 lines (256 loc) · 10.1 KB
/
SceneParser.h
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
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef _SCENEPARSER_H_
#define _SCENEPARSER_H_
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include "Vect.h"
#include "Ray.h"
#include "Camera.h"
#include "Color.h"
#include "Light.h"
#include "Source.h"
#include "Object.h"
#include "Sphere.h"
#include "Plane.h"
#include "Triangle.h"
#include "Scene.h"
#include "Essentials.h"
using namespace std;
class SceneCreator {
public:
SceneCreator();
/* Function that takes two corners of a cube and creates adequately
* placed triangles to form the cube. Also adds them to the scene_objects vector
*/
private:
void makeCube (Vect corner1, Vect corner2, Color color, vector<Object*>& scene_objects)
{
//corner1
double c1x = corner1.getVectX();
double c1y = corner1.getVectY();
double c1z = corner1.getVectZ();
//corner2
double c2x = corner2.getVectX();
double c2y = corner2.getVectY();
double c2z = corner2.getVectZ();
Vect A(c2x, c1y, c1z);
Vect B(c2x, c1y, c2z);
Vect C(c1x, c1y, c2z);
Vect D(c2x, c2y, c1z);
Vect E(c1x, c2y, c1z);
Vect F(c1x, c2y, c2z);
//left side
scene_objects.push_back(new Triangle (D, A, corner1, color));
scene_objects.push_back(new Triangle (corner1, E, D, color));
//far side
scene_objects.push_back(new Triangle (corner2, B, A, color));
scene_objects.push_back(new Triangle (A, D, corner2, color));
//right side
scene_objects.push_back(new Triangle (F, C, B, color));
scene_objects.push_back(new Triangle (B, corner2, F, color));
//front side
scene_objects.push_back(new Triangle (E, corner1, C, color));
scene_objects.push_back(new Triangle (C, F, E, color));
//top
scene_objects.push_back(new Triangle (D, E, F, color));
scene_objects.push_back(new Triangle (F, corner2, D, color));
//bottom
scene_objects.push_back(new Triangle (corner1, A, B, color));
scene_objects.push_back(new Triangle (B, C, corner1, color));
}
//test if given path is valid by creating a dummy file
private:
void testPath(string path)
{
string dummy = "dummy.txt";
FILE *f1=fopen((path+dummy).c_str(),"wb");
if (f1!=NULL)
{
fclose(f1);
remove((path+dummy).c_str());
}
else
{
throw -2;
}
}
public :
Scene* SceneParser( int &width, int &height, string &path, string &scene_file )
{
vector<Object*> scene_objects; //vector that contains objects, notice that it is a vector of Object
vector<Source*> light_sources;
int aadepth;
double ambientlight;
Vect campos, look_at; //Vectors indicating camera position and where it should look at
ifstream scenefile;
int spheres, planes, sources, cubes;
double xi, yi, zi, ri, cri, cgi, cbi, csi, di;
string input;
scenefile.open(scene_file.c_str());
if(scenefile.is_open()) //if cannot open scenefile, throw proper error
{
getline(scenefile,input); //1
getline(scenefile,input); //2
getline(scenefile,input); //3
getline(scenefile,input); //4
getline(scenefile,input); //5
path.assign(input);
testPath(path);
cout << endl << "Chosen path:" << path << endl;
getline(scenefile,input); //6
scenefile >> width; //7
getline(scenefile,input); //7
getline(scenefile,input); //8
scenefile >> height; //9
getline(scenefile,input); //9
getline(scenefile,input); //10
scenefile >> aadepth; //11
getline(scenefile,input); //11
cout<<endl<<"Image with resolution "<<width<<"x"<<height<<" and AA depth of "<<aadepth<<endl;
getline(scenefile,input); //12
scenefile >> ambientlight; //13
getline(scenefile,input); //13
getline(scenefile,input); //14
scenefile >> sources; //15
getline(scenefile,input); //15
getline(scenefile,input); //16
getline(scenefile,input); //17
cout<<endl<<"Ambient Ligth: "<<ambientlight<<", N Sources: "<< sources <<endl;
if (sources>0)
{
for (int i = 0; i < sources; i++)
{
// READ FORMAT 18
scenefile >> xi >> yi >> zi;
cout <<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi <<endl;
scenefile.ignore();
scenefile >> cri >> cgi >> cbi;
getline(scenefile,input); // FORMAT 18
cout <<"R: "<< cri<< " G: "<< cgi << " B: "<< cbi <<endl;
Vect location (xi, yi, zi);
//cout << location.getVectX() << endl;
Color scolor (cri, cgi, cbi, 0);
//cout << scolor.getColorBlue() << endl;
Light* light_input = new Light(location, scolor);
light_sources.push_back(dynamic_cast<Source*>(light_input));
}
}
else
{
getline(scenefile,input);
}
getline(scenefile,input); // CAMERA POSITION
scenefile >> xi >> yi >> zi; // get
campos.setVect(xi, yi, zi);
cout << endl << "CAM POS "<<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi <<endl;
getline(scenefile,input);
getline(scenefile,input); // Where to look
scenefile >> xi >> yi >> zi; //get
cout << "Look At "<<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi <<endl;
look_at.setVect(xi, yi, zi);
getline(scenefile,input);
getline(scenefile,input); // N PLANES
scenefile >> planes; // get
cout<<endl<<"N of Planes: "<<planes<<endl;
getline(scenefile,input);
getline(scenefile,input); //planes
getline(scenefile,input); // Format planes
if(planes > 0)
{
for (int i = 0; i < planes; i++) //with the number of planes, adds them to the Object vector
{
scenefile >> xi >> yi >> zi >> di;
cout <<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi << " d: " << di <<endl;
scenefile.ignore();
scenefile >> cri >> cgi >> cbi >> csi;
cout <<"R: "<< cri<< " G: "<< cgi << " B: "<< cbi << " S: " << csi <<endl;
Vect location (xi, yi, zi);
Color scolor (cri, cgi, cbi, csi);
Plane* plane_input = new Plane(location, di, scolor);
scene_objects.push_back(dynamic_cast<Object*>(plane_input)); //dynamic cast used to add a Plane object to a Object vector
getline(scenefile,input);
}
}
else
{
getline(scenefile,input);
}
getline(scenefile,input); // N Spheres
scenefile >> spheres; // get
cout<<endl<<"N of Spheres: "<<spheres<<endl;
getline(scenefile,input);
getline(scenefile,input); // Spheres
getline(scenefile,input); // Format Spheres
if (spheres>0)
{
for (int i = 0; i < spheres; i++)
{
scenefile >> xi >> yi >> zi >> ri;
cout <<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi << " r: " << ri <<endl;
scenefile.ignore();
scenefile >> cri >> cgi >> cbi >> csi;
cout <<"R: "<< cri<< " G: "<< cgi << " B: "<< cbi << " S: " << csi <<endl;
Vect location (xi, yi, zi);
//cout << location.getVectX() << endl;
Color scolor (cri, cgi, cbi, csi);
//cout << scolor.getColorBlue() << endl;
Sphere* sphere_input = new Sphere(location, ri, scolor); //had to do this because
//objects are destructed every iteration, and the vector
//doesn't contain them anymore, this way it's just the
//pointer that is destructed, but the memory allocated
//by 'new' is still there
scene_objects.push_back(dynamic_cast<Object*>(sphere_input));
getline(scenefile,input);
}
}
else
{
getline(scenefile,input);
}
getline(scenefile,input); // N Cubes
scenefile >> cubes; // get
cout<<endl<<"N of Cubes: "<< cubes <<endl;
getline(scenefile,input); // Remove line
getline(scenefile,input); // Cubes
getline(scenefile,input); // Format Cubes
if(cubes > 0)
{
for (int i = 0; i < cubes; i++)
{
scenefile >> xi >> yi >> zi;
cout <<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi <<endl;
scenefile.ignore();
Vect location (xi, yi, zi);
scenefile >> xi >> yi >> zi;
cout <<"X: "<< xi<< " Y: "<< yi << " Z: "<< zi <<endl;
scenefile.ignore();
Vect location2 (xi, yi, zi);
scenefile >> cri >> cgi >> cbi >> csi;
cout <<"R: "<< cri<< " G: "<< cgi << " B: "<< cbi << " S: " << csi <<endl;
Color scolor (cri, cgi, cbi, csi);
makeCube(location, location2, scolor, scene_objects);
getline(scenefile,input);
}
}
else
{
getline(scenefile,input);
}
}
else
{
throw -1;
}
Scene* sne = new Scene( height, width, aadepth, ambientlight, campos, look_at, scene_objects, light_sources);
return sne;
}
};
SceneCreator::SceneCreator() {
}
#endif // _SCENEPARSER_H_