-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpEngine.cpp
More file actions
333 lines (289 loc) · 9.06 KB
/
SimpEngine.cpp
File metadata and controls
333 lines (289 loc) · 9.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "SimpEngine.hpp"
#include "Light.hpp"
void SimpEngine::runCommands(const std::vector<Command>& commands)
{
for (auto& command : commands)
{
switch (command.operation())
{
case Command::Operation::Filled:
{
currentRenderMode = RenderEngine::RenderMode::Filled;
} break;
case Command::Operation::Wire:
{
currentRenderMode = RenderEngine::RenderMode::Wireframe;
} break;
case Command::Operation::OpenBrace:
{
TransformStack.push(CTM);
} break;
case Command::Operation::CloseBrace:
{
if (!TransformStack.empty())
{
CTM = std::move(TransformStack.top());
TransformStack.pop();
}
} break;
case Command::Operation::Scale:
{
auto params = std::get<Vector3>(command.parameters());
auto scaleMatrix = CTM_t{ params[0], 0.0, 0.0, 0.0,
0.0, params[1], 0.0, 0.0,
0.0, 0.0, params[2], 0.0,
0.0, 0.0, 0.0 , 1.0 };
CTM = CTM * scaleMatrix;
} break;
case Command::Operation::Translate:
{
auto params = std::get<Vector3>(command.parameters());
auto translationMatrix = CTM_t{ 1.0, 0.0, 0.0, params[0],
0.0, 1.0, 0.0, params[1],
0.0, 0.0, 1.0, params[2],
0.0, 0.0, 0.0, 1.0 };
CTM = CTM * translationMatrix;
} break;
case Command::Operation::Rotate:
{
auto params = std::get<RotateParams>(command.parameters());
auto rotationMatrix = getRotationMatrix(params.first, params.second);
CTM = CTM * rotationMatrix;
} break;
case Command::Operation::Line:
{
auto params = std::get<LineParams>(command.parameters());
auto point1 = std::array<double, 4>{ params[0].x, params[0].y, params[0].z, 1 };
auto point2 = std::array<double, 4>{ params[1].x, params[1].y, params[1].z, 1 };
auto transformedPoint1 = cameraCTMInv * (CTM * point1);
auto transformedPoint2 = cameraCTMInv * (CTM * point2);
// Send to rendering engine to render
_renderEngine.RenderLine(Line_t { Point4D {transformedPoint1, params[0].color}, Point4D{transformedPoint2, params[1].color} });
} break;
case Command::Operation::Polygon:
{
auto params = std::get<PolygonParams>(command.parameters());
auto point1 = std::array<double, 4>{ params[0].x, params[0].y, params[0].z, 1 };
auto point2 = std::array<double, 4>{ params[1].x, params[1].y, params[1].z, 1 };
auto point3 = std::array<double, 4>{ params[2].x, params[2].y, params[2].z, 1 };
auto transformedPoint1 = cameraCTMInv * (CTM * point1);
auto transformedPoint2 = cameraCTMInv * (CTM * point2);
auto transformedPoint3 = cameraCTMInv * (CTM * point3);
// Send to rendering engine to render
_renderEngine.RenderTriangle(Polygon_t{ Point4D{transformedPoint1, params[0].color},
Point4D{transformedPoint2, params[1].color},
Point4D{transformedPoint3, params[2].color} },
currentRenderMode);
} break;
case Command::Operation::Ambient:
{
auto color = std::get<Color>(command.parameters());
_renderEngine.SetAmbientColor(color);
} break;
case Command::Operation::Camera:
{
auto params = std::get<CameraParams>(command.parameters());
cameraCTMInv = invert(CTM);
auto camera = Camera{ cameraCTMInv, params.xLow, params.xHigh, params.yLow, params.yHigh, params.near, params.far };
_renderEngine.SetCamera(camera);
} break;
case Command::Operation::Depth:
{
auto params = std::get<DepthParams>(command.parameters());
_renderEngine.SetDepth(Depth{ params.near, params.far, params.color });
} break;
case Command::Operation::VertexNormal:
{
normals.emplace_back(std::get<Vector3>(command.parameters()));
} break;
case Command::Operation::Vertex:
{
Vertex v;
v.location = std::get<Point4D>(command.parameters());
v.location = this->CTM * v.location;
v.location = this->cameraCTMInv * v.location;
vertices.push_back(v);
} break;
case Command::Operation::Face:
{
FaceParam params = std::get<FaceParam>(command.parameters());
std::vector<Vertex*> faceVertices;
faceVertices.reserve(params.size());
std::vector<int> vertexIndices;
vertexIndices.reserve(params.size());
std::for_each(params.begin(), params.end(), [this, &faceVertices, &vertexIndices](auto& vertex)
{
// Vertex
Vertex* v;
std::size_t index;
if (vertex[0] > 0)
{
index = vertex[0] - 1;
}
else
{
index = this->vertices.size() + vertex[0];
}
v = &this->vertices.at(index);
faceVertices.push_back(v);
vertexIndices.push_back(static_cast<int>(index));
// Normal
if (vertex[2] != 0)
{
Point normal;
if (vertex[2] > 0)
{
normal = this->normals.at(vertex[2] - 1);
}
else
{
normal = this->normals.at(this->normals.size() + vertex[2]);
}
v->assignedNormal = normal;
}
return v;
});
//auto thesholdInRadian = getRadianFromDegree(45);
//auto cosThreshold = std::cos(thesholdInRadian);
if (faceVertices.size() > 3)
{
Face tempFace;
tempFace.vertices = faceVertices;
tempFace.vertexIndices = vertexIndices;
auto normal = tempFace.getFaceNormal();
for (auto v : tempFace.vertices)
{
/*if (v->faceNormals.size() > 0)
{
if (std::any_of(v->faceNormals.begin(), v->faceNormals.end(), [cosThreshold, &normal](auto& n) { return dot(normal, n) < cosThreshold; }))
{
v->faceNormals.push_back(normal);
}
}
else
{*/
v->faceNormals.push_back(normal);
//}
}
for (auto i = 1u; i < faceVertices.size() - 1; ++i)
{
Face face;
face.vertices.push_back(faceVertices[0]);
face.vertices.push_back(faceVertices[i]);
face.vertices.push_back(faceVertices[i + 1]);
face.vertexIndices.push_back(vertexIndices[0]);
face.vertexIndices.push_back(vertexIndices[i]);
face.vertexIndices.push_back(vertexIndices[i + 1]);
face.normal = normal;
faces.push_back(face);
//_renderEngine.RenderTriangle(triangle, currentRenderMode);
}
}
else
{
Face face;
face.vertices = faceVertices;
face.vertexIndices = vertexIndices;
auto normal = face.getFaceNormal();
for (auto v : face.vertices)
{
v->faceNormals.push_back(normal);
}
face.normal = normal;
faces.push_back(face);
//_renderEngine.RenderTriangle(faceVertices, currentRenderMode);
}
} break;
case Command::Operation::ObjectFile:
{
if (std::get<std::string>(command.parameters()) == "ENDOFOBJECTFILE"s)
{
// Draw all the faces
for (auto& f : faces)
{
f.vertices.clear();
for (auto i : f.vertexIndices)
{
f.vertices.push_back(&vertices[i]);
}
_renderEngine.RenderFace(f, currentRenderMode);
}
if (!objFileVerticesStack.empty())
{
vertices = objFileVerticesStack.top();
objFileVerticesStack.pop();
}
if (!objFileNormalsStack.empty())
{
normals = objFileNormalsStack.top();
objFileNormalsStack.pop();
}
if (!objFileFacesStack.empty())
{
faces = objFileFacesStack.top();
objFileFacesStack.pop();
}
}
else
{
objFileVerticesStack.push(vertices);
vertices.clear();
objFileNormalsStack.push(normals);
normals.clear();
objFileFacesStack.push(faces);
faces.clear();
}
} break;
case Command::Operation::Surface:
{
auto params = std::get<SurfaceParams>(command.parameters());
_renderEngine.SetSpecularCoefficient(params[0]);
_renderEngine.SetSpecularExponent(params[1]);
} break;
case Command::Operation::Light:
{
auto params = std::get<LightParams>(command.parameters());
auto lightColor = Color::getDenormalizedColor(params[0], params[1], params[2]);
auto lightPosition = CTM * Vector4_t{ 0, 0, 0, 1 };
lightPosition = cameraCTMInv * lightPosition;
auto light = Light{ lightPosition, lightColor, params[3], params[4] };
_renderEngine.AddLight(light);
} break;
case Command::Operation::Phong:
case Command::Operation::Gouraud:
case Command::Operation::Flat:
{
_renderEngine.SetLightingMethod(std::get<LightingMethod>(command.parameters()));
} break;
}
}
}
CTM_t SimpEngine::getRotationMatrix(const Axis& axis, int degree) const
{
auto radian = -getRadianFromDegree(degree);
switch (axis)
{
case Axis::X:
{
return CTM_t{ 1.0, 0.0, 0.0, 0.0,
0.0, std::cos(radian), -std::sin(radian), 0.0,
0.0, std::sin(radian), std::cos(radian), 0.0,
0.0, 0.0, 0.0, 1.0 };
} break;
case Axis::Y:
{
return CTM_t{ std::cos(radian), 0.0, std::sin(radian), 0.0,
0.0, 1.0, 0.0, 0.0,
-std::sin(radian), 0.0, std::cos(radian), 0.0,
0.0, 0.0, 0.0, 1.0 };
} break;
case Axis::Z:
default:
{
return CTM_t{ std::cos(radian), -std::sin(radian), 0.0, 0.0,
std::sin(radian), std::cos(radian), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
} break;
}
}