-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
233 lines (207 loc) · 8.14 KB
/
command.cpp
File metadata and controls
233 lines (207 loc) · 8.14 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
#include "command.hpp"
using namespace std::string_literals;
static const std::unordered_map<std::string, Axis> AxisTokens{
{ "X"s, Axis::X },
{ "Y"s, Axis::Y },
{ "Z"s, Axis::Z }
};
Command::Command(const std::vector<std::string>& tokens)
{
static Color defaultVertexColor = Color{ 255, 255, 255 };
_op = OperationTokens.at(tokens[0]);
switch (_op)
{
case Command::Operation::Polygon:
{
if (tokens.size() == 10)
{
params = PolygonParams{ Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, defaultVertexColor },
Point4D{ std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()), 1, defaultVertexColor },
Point4D{ std::atof(tokens[7].c_str()), std::atof(tokens[8].c_str()), std::atof(tokens[9].c_str()), 1, defaultVertexColor } };
}
else
{
auto color1 = Color::getDenormalizedColor(std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()));
auto color2 = Color::getDenormalizedColor(std::atof(tokens[10].c_str()), std::atof(tokens[11].c_str()), std::atof(tokens[12].c_str()));
auto color3 = Color::getDenormalizedColor(std::atof(tokens[16].c_str()), std::atof(tokens[17].c_str()), std::atof(tokens[18].c_str()));
params = PolygonParams{ Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, color1 },
Point4D{ std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()), 1, color2 },
Point4D{ std::atof(tokens[7].c_str()), std::atof(tokens[8].c_str()), std::atof(tokens[9].c_str()), 1, color3 } };
}
} break;
case Command::Operation::Line:
{
// no color
if (tokens.size() == 7)
{
params = LineParams{ Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, defaultVertexColor },
Point4D{ std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()), 1, defaultVertexColor } };
}
else
{
auto color1 = Color::getDenormalizedColor(std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()));
auto color2 = Color::getDenormalizedColor(std::atof(tokens[10].c_str()), std::atof(tokens[11].c_str()), std::atof(tokens[12].c_str()));
params = LineParams{ Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, color1 },
Point4D{ std::atof(tokens[7].c_str()), std::atof(tokens[8].c_str()), std::atof(tokens[9].c_str()), 1, color2 } };
}
} break;
case Command::Operation::VertexNormal:
case Command::Operation::Translate:
case Command::Operation::Scale:
{
params = Vector3{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()) };
} break;
case Command::Operation::Rotate:
{
params = RotateParams{ AxisTokens.at(tokens[1]), std::atoi(tokens[2].c_str()) };
} break;
case Command::Operation::File:
{
auto fileName = std::string(std::next(tokens[1].cbegin()), std::prev(tokens[1].cend()));
fileName += ".simp";
params = fileName;
} break;
case Command::Operation::ObjectFile:
{
auto fileName = std::string(std::next(tokens[1].cbegin()), std::prev(tokens[1].cend()));
fileName += ".obj";
params = fileName;
} break;
case Command::Operation::Ambient:
{
params = Color::getDenormalizedColor( std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()));
} break;
case Command::Operation::Camera:
{
params = CameraParams{ std::atof(tokens[1].c_str()), // xLow
std::atof(tokens[2].c_str()), // xHigh
std::atof(tokens[3].c_str()), // yLow
std::atof(tokens[4].c_str()), // yHigh
std::atof(tokens[5].c_str()), // near
std::atof(tokens[6].c_str()) }; // far
} break;
case Command::Operation::Depth:
{
params = DepthParams{ std::atof(tokens[1].c_str()),
std::atof(tokens[2].c_str()),
Color::getDenormalizedColor(std::atof(tokens[3].c_str()), std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str())) };
} break;
case Command::Operation::Surface:
{
defaultVertexColor = Color::getDenormalizedColor(std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()));
if (tokens.size() > 4)
{
params = SurfaceParams{ std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()) };
}
// Hack
else
{
params = SurfaceParams{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()) };
}
} break;
case Command::Operation::Vertex:
{
// v x y z w r g b
if (tokens.size() == 8)
{
auto color = Color::getDenormalizedColor(std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()), std::atof(tokens[7].c_str()));
params = Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), std::atof(tokens[4].c_str()), color };
}
// v x y z r g b
else if (tokens.size() == 7)
{
auto color = Color::getDenormalizedColor(std::atof(tokens[4].c_str()), std::atof(tokens[5].c_str()), std::atof(tokens[6].c_str()));
params = Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, color };
}
// v x y z w
else if (tokens.size() == 5)
{
params = Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), std::atof(tokens[4].c_str()), defaultVertexColor };
}
// v x y z
else
{
params = Point4D{ std::atof(tokens[1].c_str()), std::atof(tokens[2].c_str()), std::atof(tokens[3].c_str()), 1, defaultVertexColor };
}
} break;
case Command::Operation::Face:
{
FaceParam faceParams;
faceParams.resize(tokens.size() - 1);
std::transform(std::next(tokens.begin()),
tokens.end(),
faceParams.begin(),
[](auto& token)
{
// normal
auto normalDelimIndex = token.find("//");
if (normalDelimIndex != std::string::npos)
{
auto vertex = std::atoi(std::string(token.begin(), token.begin() + normalDelimIndex + 1).c_str());
auto normal = std::atoi(std::string(token.begin() + normalDelimIndex + 3, token.end()).c_str());
return VertexParam{ vertex, 0, normal };
}
else
{
auto textureDelimIndex = token.find("/");
// texture
if (textureDelimIndex != std::string::npos)
{
auto vertex = std::atoi(std::string(token.begin(), token.begin() + textureDelimIndex + 1).c_str());
normalDelimIndex = token.find("/", textureDelimIndex + 1);
// texture + normal
if (normalDelimIndex != std::string::npos)
{
auto texture = std::atoi(std::string(token.begin() + textureDelimIndex + 2, token.begin() + normalDelimIndex + 1).c_str());
auto normal = std::atoi(std::string(token.begin() + normalDelimIndex + 2, token.end()).c_str());
return VertexParam{ vertex, texture, normal };
}
else
{
auto texture = std::atoi(std::string(token.begin() + textureDelimIndex + 2, token.end()).c_str());
return VertexParam{ vertex, texture, 0 };
}
}
// vertex only
else
{
auto vertex = std::atoi(token.c_str());
return VertexParam{ vertex, 0, 0 };
}
}
});
params = faceParams;
} break;
case Command::Operation::Light:
{
params = LightParams{ std::atof(tokens[1].c_str()), // Red
std::atof(tokens[2].c_str()), // Green
std::atof(tokens[3].c_str()), // Blue
std::atof(tokens[4].c_str()), // A attenuation
std::atof(tokens[5].c_str()) }; // B attentuation
} break;
case Command::Operation::Phong:
{
params = LightingMethod::Phong;
} break;
case Command::Operation::Gouraud:
{
params = LightingMethod::Gouraud;
} break;
case Command::Operation::Flat:
{
params = LightingMethod::Flat;
} break;
default:
{
} break;
}
}
Command::Operation Command::operation() const
{
return _op;
}
const CommandParams& Command::parameters() const
{
return params;
}