-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
129 lines (120 loc) · 3.72 KB
/
parser.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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <deque>
#include <stack>
using namespace std;
class Parser {
public:
const char* fileName;
Camera camera;
World world;
stack <Matrix> transformStack;
Material material;
int width;
int height;
int maxDepth;
inline Parser(const char* file) {
fileName = file;
width = 100;
height = 100;
maxDepth = 5;
transformStack.push(Matrix(1.f));
}
inline void parse() {
readFile(fileName);
}
bool readValues(stringstream &s, const int numvals, float* values);
void readFile(const char* filename);
};
bool Parser::readValues(stringstream &s, const int numVals, float* values)
{
for (int i = 0; i < numVals; i++) {
s >> values[i];
if (s.fail()) {
cout << "Failed reading value " << i << " will skip\n";
return false;
}
}
return true;
}
void Parser::readFile(const char* filename)
{
string str, cmd;
ifstream in;
in.open(filename);
if (in.is_open()) {
getline (in, str);
while (in) {
if ((str.find_first_not_of(" \t\r\n") != string::npos) && (str[0] != '#')) {
// Ruled out comment and blank lines
stringstream s(str);
s >> cmd;
int i;
float values[10]; // Position and color for light, colors for others
bool validinput; // Validity of input
if (cmd == "size") {
validinput = readValues(s, 2, values);
if (validinput) {
width = values[0];
height = values[1];
}
} else if (cmd == "camera") {
validinput = readValues(s,10,values); // 10 values eye cen up fov
if (validinput) {
Vector eye = Vector(values[0], values[1], values[2]);
Vector center = Vector(values[3], values[4], values[5]);
Vector up = Vector(values[6], values[7], values[8]);
float fov = values[9];
camera = Camera(eye, center, up, fov, width, height);
}
} else if (cmd == "ambient") {
validinput = readValues(s,3,values);
if (validinput) {
world.ambient = Color(values[0], values[1], values[2]);
}
} else if (cmd == "diffuse") {
validinput = readValues(s,3,values);
if (validinput) {
material.diffuse = Color(values[0], values[1], values[2]);
}
} else if (cmd == "specular") {
validinput = readValues(s,3,values);
if (validinput) {
material.specular = Color(values[0], values[1], values[2]);
}
} else if (cmd == "shininess") {
validinput = readValues(s,1,values);
if (validinput) {
material.shininess = values[0];
}
} else if (cmd == "emission") {
validinput = readValues(s,3,values);
if (validinput) {
material.emission = Color(values[0], values[1], values[2]);
}
} else if (cmd == "sphere") {
validinput = readValues(s,4,values);
if (validinput) {
Vector pos = Vector(values[0], values[1], values[2]);
Material mat = material;
world.primitives.push_back(new Sphere(pos, values[3], mat));
}
} else if (cmd == "directional") {
validinput = readValues(s,6,values);
if (validinput) {
world.lights.push_back(Light(Vector(values[0], values[1], values[2]),
Color(values[3], values[4], values[5])));
}
} else {
cerr << "Unknown Command: " << cmd << " Skipping \n";
}
}
getline (in, str);
}
} else {
cerr << "Unable to Open Input Data File " << filename << "\n";
throw 2;
}
}