-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
355 lines (308 loc) · 9.54 KB
/
Copy pathModel.cpp
File metadata and controls
355 lines (308 loc) · 9.54 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "Model.h"
#include "RGBColour.h"
#include <algorithm>
#include <array>
#include <cmath>
Model::Model()
{
}
Model::~Model()
{
}
const std::vector<Polygon3D>& Model::GetPolygons()
{
return _polygons;
}
const std::vector<Vertex>& Model::GetLocalVertices()
{
return _localVertices;
}
const std::vector<Vertex>& Model::GetTransformedVertices()
{
return _transformedVertices;
}
const std::vector<UVCoords>& Model::GetUVCoords()
{
return _uvCoords;
}
size_t Model::GetPolygonCount() const
{
return _polygons.size();
}
size_t Model::GetVertexCount() const
{
return _localVertices.size();
}
Texture& Model::GetTexture()
{
return _texture;
}
void Model::AddVertex(float x, float y, float z)
{
_localVertices.push_back(Vertex(x, y, z));
}
void Model::AddPolygon(int i0, int i1, int i2, int uvIndex0, int uvIndex1, int uvIndex2)
{
_polygons.push_back(Polygon3D(i0, i1, i2, uvIndex0, uvIndex1, uvIndex2));
}
void Model::AddTextureUV(float u, float v)
{
_uvCoords.push_back(UVCoords(u, v));
}
void Model::DehomogenizeVertices()
{
for (Vertex& vertex : _transformedVertices)
{
vertex.DehomogenizeVertex();
}
}
void Model::ApplyTransformToLocalVertices(const Matrix& transform)
{
_transformedVertices.clear();
_transformedVertices.reserve(_localVertices.size());
for (Vertex& vertex : _localVertices)
{
_transformedVertices.push_back(transform * vertex);
}
}
void Model::ApplyTransformToTransformedVertices(const Matrix& transform)
{
for (Vertex& vertex : _transformedVertices)
{
vertex = transform * vertex;
}
}
void Model::CalculateBackfaces(const Vertex& cameraPos)
{
for (Polygon3D& polygon : _polygons)
{
// Get the Vertices that make up the polygon.
Vertex v0 = _transformedVertices[polygon.GetVertexIndex(0)];
Vertex v1 = _transformedVertices[polygon.GetVertexIndex(1)];
Vertex v2 = _transformedVertices[polygon.GetVertexIndex(2)];
// Create the normal vector for the polygon and normalise it.
Vector a = v0 - v2;
Vector b = v0 - v1;
Vector normal = Vector::CrossProduct(b, a);
// Save the normal to the polygon
polygon.SetNormal(normal);
// Create the vector from the polygon to the camera.
// Then get the dot product between that vector and the normal
// to check the polygon is facing us.
Vector eyeVector = cameraPos - v0;
if (Vector::DotProdcut(normal, eyeVector) < 0.0f)
{
polygon.SetCulled(true);
}
else
{
polygon.SetCulled(false);
}
}
}
void Model::CalculateVertexNormals()
{
// Reset normals first
for (Vertex& vertex : _transformedVertices)
{
vertex.SetNormal(Vector(0.0f, 0.0f, 0.0f));
vertex.SetPolygonCount(0);
}
for (Polygon3D& polygon : _polygons)
{
for (int i = 0; i < 3; i++)
{
Vertex& vertex = _transformedVertices[polygon.GetVertexIndex(i)];
vertex.SetNormal(vertex.GetNormal() + polygon.GetNormal());
vertex.SetPolygonCount(vertex.GetPolygonCount() + 1);
}
}
for (Vertex& vertex : _transformedVertices)
{
vertex.SetNormal(vertex.GetNormal() / (float)vertex.GetPolygonCount());
}
}
void Model::Sort()
{
for (Polygon3D& polygon : _polygons)
{
// Calculate the z depth of each polygon
float zDepth = 0.0f;
for (int i = 0; i < 3; i++)
{
zDepth += _transformedVertices[polygon.GetVertexIndex(i)].GetZ();
}
polygon.SetZDepth(zDepth / 3);
}
// Sort the polygons by their z depth from the camera
std::sort(_polygons.begin(), _polygons.end(), [](const Polygon3D& a, const Polygon3D& b)
{
return a.GetZDepth() > b.GetZDepth();
});
}
void Model::CalculateAmbientLighting(const AmbientLight& light)
{
// Light up the polygons with a uniform ambient colour
RGBColour lightColour(light.GetColour());
RGBColour total = lightColour * _ambient;
for (Polygon3D& polygon : _polygons)
{
polygon.SetColour(total.GetColorref());
}
// Same for vertex lighting
for (Vertex& vertex : _transformedVertices)
{
vertex.SetColour(total.GetColorref());
}
}
void Model::CalculateDirectionalLighting(std::vector<DirectionalLight> lights)
{
for (Polygon3D& polygon : _polygons)
{
RGBColour total(polygon.GetColour());
for (DirectionalLight& light : lights)
{
// If the angle is more than 90 Degrees (less than 0 dot product)
// don't bother calculating anything, 0 light will be added to the polygons colour
float angle = Vector::DotProdcut(Vector::Normalise(light.GetDirection() * -1), Vector::Normalise(polygon.GetNormal()));
if (angle > 0.0f)
{
total = total + (RGBColour(light.GetColour()) * _diffuse * angle);
}
}
total.ClampColour();
polygon.SetColour(total.GetColorref());
}
}
void Model::CalculatePointLighting(std::vector<PointLight> lights)
{
for (Polygon3D& polygon : _polygons)
{
RGBColour total(polygon.GetColour());
for (PointLight& light : lights)
{
// Get the Vector to the light source, the length of that vector and normalise it
Vertex polyPos = _transformedVertices[polygon.GetVertexIndex(0)];
Vector lightDir = light.GetPosition() - polyPos;
float distance = lightDir.Magnitude();
lightDir.Normalise();
float angle = Vector::DotProdcut(lightDir, Vector::Normalise(polygon.GetNormal()));
if (angle > 0.0f)
{
// Calculate the attenuation for this light given a distance
float attenuation = light.CalculateAttenuation(distance);
total = total + (RGBColour(light.GetColour()) * _diffuse * angle * attenuation);
}
}
total.ClampColour();
polygon.SetColour(total.GetColorref());
}
}
void Model::CalculateVertexDirectionalLighting(std::vector<DirectionalLight> lights, Vertex cameraPos)
{
for (Vertex& vertex : _transformedVertices)
{
RGBColour total(vertex.GetColour());
for (DirectionalLight& light : lights)
{
Vector lightDir = Vector::Normalise(light.GetDirection() * -1);
float angle = Vector::DotProdcut(lightDir, Vector::Normalise(vertex.GetNormal()));
if (angle > 0.0f)
{
RGBColour diffuseColour = RGBColour(light.GetColour());
diffuseColour = diffuseColour * _diffuse * angle;
total = total + diffuseColour;
// Specular Lighting
Vector eyeVector = Vector::Normalise(cameraPos - vertex);
Vector halfWayVector = (lightDir + eyeVector) / (lightDir.Magnitude() + eyeVector.Magnitude());
float specularAngle = Vector::DotProdcut(Vector::Normalise(vertex.GetNormal()), halfWayVector);
if (specularAngle > 0.0f) {
RGBColour specularColour = RGBColour(light.GetColour()) * _specular * pow(specularAngle, _shininess) * 3;
total = total + specularColour;
}
}
}
total.ClampColour();
vertex.SetColour(total.GetColorref());
}
}
void Model::CalculateVertexPointLighting(std::vector<PointLight> lights, Vertex cameraPos)
{
for (Vertex& vertex : _transformedVertices)
{
RGBColour total(vertex.GetColour());
for (PointLight& light : lights)
{
// Get the Vector to the light source, the length of that vector and normalise it
Vector lightDir = light.GetPosition() - vertex;
float distance = lightDir.Magnitude();
// If the angle is more than 90 Degrees (less than 0 dot product)
// don't bother calculating anything, 0 light will be added to the polygons colour
float angle = Vector::DotProdcut(Vector::Normalise(lightDir), Vector::Normalise(vertex.GetNormal()));
if (angle > 0.0f)
{
// Calculate the attenuation for this light given a distance
float attenuation = light.CalculateAttenuation(distance);
RGBColour diffuseColour = RGBColour(light.GetColour()) * _diffuse * angle * attenuation;
total = total + diffuseColour;
// Specular Lighting
Vector eyeVector = Vector::Normalise(cameraPos - vertex);
Vector halfWayVector = (lightDir + eyeVector) / (lightDir.Magnitude() + eyeVector.Magnitude());
float specularAngle = Vector::DotProdcut(Vector::Normalise(vertex.GetNormal()), halfWayVector);
if (specularAngle > 0.0f) {
RGBColour specularColour = RGBColour(light.GetColour()) * _specular * pow(specularAngle, _shininess) * attenuation * 3;
total = total + specularColour;
}
}
}
total.ClampColour();
vertex.SetColour(total.GetColorref());
}
}
void Model::CalcualteVertexSpotLighting(std::vector<SpotLight> lights, Vertex cameraPos)
{
for (Vertex& vertex : _transformedVertices)
{
RGBColour total(vertex.GetColour());
for (SpotLight& light : lights)
{
// Get the Vector to the light source, the length of that vector and normalise it
Vector lightDir = light.GetPosition() - vertex;
float distance = lightDir.Magnitude();
// Calculate the attenuation for this light given a distance
float attenuation = light.CalculateAttenuation(distance);
float angle = Vector::DotProdcut(Vector::Normalise(lightDir), Vector::Normalise(vertex.GetNormal()));
if (angle > 0.0f)
{
float spotAngle = Vector::DotProdcut(Vector::Normalise(lightDir * -1), Vector::Normalise(light.GetDirection()));
if (spotAngle < cos(light.GetOuterAngle()))
{
// Don't add any light
}
else if (spotAngle > cos(light.GetInnerAngle()))
{
// Add the max amount of light
RGBColour diffuseColour = RGBColour(light.GetColour()) * _diffuse * angle * attenuation;
total = total + diffuseColour;
}
else
{
float t = (spotAngle - cos(light.GetOuterAngle())) / (cos(light.GetInnerAngle() - cos(light.GetOuterAngle())));
float result = (3.0f - 2.0f * t) * (t * t);
// Add the a fall of light between the outer and inner angles
RGBColour diffuseColour = RGBColour(light.GetColour()) * _diffuse * angle * attenuation * result;
total = total + diffuseColour;
}
}
}
total.ClampColour();
vertex.SetColour(total.GetColorref());
}
}
void Model::SetBackfaces(bool culled)
{
for (Polygon3D& polygon : _polygons)
{
polygon.SetCulled(culled);
}
}