-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
325 lines (274 loc) · 9.74 KB
/
main.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
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
#include <iostream>
#include <iomanip>
#include <SFML/Graphics.hpp>
#include "mesh.h"
#include "matrix.h"
#include "consts.h"
#include "math.h"
#include "camera.h"
#include "light.h"
#include "directionLight.h"
#include "amfunctions.h"
/// <summary>
/// The (0, 0) point is set to be in the center of screen!
/// </summary>
///
/// Global variables
///
Mesh cubeMesh; // temporary
Matrix projectionMatrix(4, 4);
sf::Clock* c = nullptr; // clock from the program initialization
Camera mainCamera;
std::vector<Light*> lightSources;
/// <summary>
/// Recalculates projection matrix for a view of requested width and height
/// </summary>
/// <param name="windowWidth">View width</param>
/// <param name="windowHeight">View height</param>
void calculateProjectionMatrix(int windowWidth, int windowHeight)
{
float aspectRatio = (float)windowHeight / (float)windowWidth;
float radFov = FOV / 180.0f * PI;
float invTg = 1.0f / tanf(radFov * 0.5f);
float zScaleFactor = Z_FAR_PLANE / (Z_FAR_PLANE - Z_NEAR_PLANE);
projectionMatrix[0][0] = aspectRatio * invTg;
projectionMatrix[1][1] = invTg;
projectionMatrix[2][2] = zScaleFactor;
projectionMatrix[2][3] = 1.0f;
projectionMatrix[3][2] = -1.0f * zScaleFactor * Z_NEAR_PLANE;
#ifndef NDEBUG
projectionMatrix.printMatrix();
#endif // NDEBUG
}
/// <summary>
/// Calculate luminance from all the light sources
/// </summary>
/// <param name="normal">Normal vector of the triangle that is illuminated</param>
/// <returns>Resulting luminance</returns>
float calculateLuminance(Vector3d normal)
{
float lum = 0.0f;
for (int i = 0; i < lightSources.size(); i++)
{
float res = lightSources[i]->calculateLuminance(normal);
if (res > 0.0f)
lum += res;
}
if (lum >= 1.0f) return 1.0f;
return lum;
}
/// <summary>
/// Returns a vector projected on 2D view (only x and y coordinates are valid).
/// Uses global variable projectionMatrix.
/// </summary>
/// <param name="v">Vector that we project</param>
/// <returns>Projected vector</returns>
Vector3d projectVector(Vector3d& v)
{
Matrix temp(1, 4);
temp[0][0] = v.x;
temp[0][1] = v.y;
temp[0][2] = v.z;
temp[0][3] = 1.0f;
temp = temp * projectionMatrix; // resulting matrix will be of the same size
Vector3d res(temp[0][0], temp[0][1], temp[0][2]);
if (temp[0][3] != 0.0f)
{
res.x /= temp[0][3];
res.y /= temp[0][3];
}
return res;
}
/// <summary>
/// Creates a 3d space rotation matrix which produces rotation by corresponding angles
/// </summary>
/// <param name="xAngle">X rotation angle in radians</param>
/// <param name="yAngle">Y rotation angle in radians</param>
/// <param name="zAngle">Z rotation angle in radians</param>
/// <returns>Resulting rotation matrix (3x3)</returns>
Matrix createRotationMatrix(float xAngle, float yAngle, float zAngle)
{
Matrix rotX(3, 3);
rotX[0][0] = 1.0f;
rotX[1][1] = cosf(xAngle);
rotX[1][2] = -sinf(xAngle);
rotX[2][1] = sinf(xAngle);
rotX[2][2] = cosf(xAngle);
Matrix rotY(3, 3);
rotY[0][0] = cosf(xAngle);
rotY[0][2] = sinf(xAngle);
rotY[1][1] = 1.0f;
rotY[2][0] = -sinf(xAngle);
rotY[2][2] = cosf(xAngle);
Matrix rotZ(3, 3);
rotZ[0][0] = cosf(xAngle);
rotZ[1][1] = cosf(xAngle);
rotZ[0][1] = -sinf(xAngle);
rotZ[1][0] = sinf(xAngle);
rotZ[2][2] = 1.0f;
return rotX * rotY * rotZ;
}
/// <summary>
/// Draws a line from v1 to v2 (in 2D space) using RenderWindow.
/// </summary>
/// <param name="window">Renderer</param>
/// <param name="v1">Line beginning point (2D)</param>
/// <param name="v2">Line end point (2D)</param>
/// <param name="color">Line color</param>
void DrawLine(sf::RenderWindow& window, Vector3d v1, Vector3d v2, sf::Color color)
{
// Center of the window is the (0, 0) point so offset vectors by half the window size
unsigned int offsetX = window.getSize().x / 2, offsetY = window.getSize().y / 2;
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(v1.x + offsetX, v1.y + offsetY)),
sf::Vertex(sf::Vector2f(v2.x + offsetX, v2.y + offsetY))
};
line->color = color;
window.draw(line, 2, sf::Lines);
}
/// <summary>
/// Draws triangle wireframe.
/// </summary>
/// <param name="window">Renderer</param>
/// <param name="projectedTriangle">The drawn triangle</param>
void DrawTriangle(sf::RenderWindow& window, Triangle projectedTriangle)
{
sf::Color color = sf::Color::White;
DrawLine(window, projectedTriangle[0], projectedTriangle[1], color);
DrawLine(window, projectedTriangle[1], projectedTriangle[2], color);
DrawLine(window, projectedTriangle[0], projectedTriangle[2], color);
}
/// <summary>
/// Draws a filled triangle.
/// </summary>
/// <param name="window">Renderer</param>
/// <param name="projectedTriangle">The drawn triangle</param>
void FillTriangle(sf::RenderWindow& window, Triangle projectedTriangle, float luminance)
{
// safe measures
if (luminance > 1.0f)
luminance = 1.0f;
if (luminance < 0.0f)
luminance = 0.0f;
sf::Color fillColor = sf::Color::Green;
fillColor.r *= luminance;
fillColor.g *= luminance;
fillColor.b *= luminance;
sf::ConvexShape triangle;
triangle.setPointCount(3);
triangle.setFillColor(fillColor);
triangle.setOutlineColor(sf::Color::Transparent);
unsigned int offsetX = window.getSize().x / 2, offsetY = window.getSize().y / 2;
triangle.setPoint(0, sf::Vector2f(projectedTriangle[0].x + offsetX,
projectedTriangle[0].y + offsetY));
triangle.setPoint(1, sf::Vector2f(projectedTriangle[1].x + offsetX,
projectedTriangle[1].y + offsetY));
triangle.setPoint(2, sf::Vector2f(projectedTriangle[2].x + offsetX,
projectedTriangle[2].y + offsetY));
window.draw(triangle);
}
/// <summary>
/// Draws a mesh of triangles
/// </summary>
/// <param name="window"></param>
/// <param name="mesh"></param>
/// <param name="wireframed">If true, additionally draws a wireframe of a triangle</param>
void DrawMesh(sf::RenderWindow& window, Mesh mesh, bool wireframed = false)
{
for (int i = 0; i < mesh.tris.size(); i++)
{
// Project triangle, then draw it
Triangle projectedTriangle = mesh.tris[i];
projectedTriangle.translate(-0.5f, -0.5f, -0.5f); // temporary, for test cube
if (c) // Rotation // temporary, for test cube
{
float fElapsedTime = c->getElapsedTime().asSeconds();
Matrix rotMat = createRotationMatrix(fElapsedTime, 0.0f, fElapsedTime * 0.5f);
//rotMat.printMatrix();
for (int i = 0; i < 3; i++)
projectedTriangle[i] = projectedTriangle[i] * rotMat;
}
projectedTriangle.translate(0.0f, 0.0f, 2.0f); // temporary, for test cube
// Don't draw triangle if can't see it
Vector3d normal = (projectedTriangle[2] - projectedTriangle[0]) *
(projectedTriangle[1] - projectedTriangle[0]);
normal.normalize();
Vector3d rayToCamera = projectedTriangle[0] - mainCamera.getPosition();
rayToCamera.normalize();
if (normal / rayToCamera < 0.0f) continue;
for (int i = 0; i < 3; i++)
projectedTriangle[i] = projectVector(projectedTriangle[i]);
// Scale to the size of a window
projectedTriangle.scaleXY(0.5f * window.getSize().x, 0.5f * window.getSize().y);
// Draw Triangle
FillTriangle(window, projectedTriangle, calculateLuminance(normal));
// Draw wireframe
DrawTriangle(window, projectedTriangle);
}
}
/// <summary>
/// Initializes everything in the beginning of the program.
/// </summary>
void init()
{
std::ios::sync_with_stdio(0);
cubeMesh.tris =
{
// SOUTH FACE
{ {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f} },
{ {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f} },
// EAST FACE
{ {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f} },
{ {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f} },
// NORTH FACE
{ {1.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f} },
{ {1.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f} },
// WEST FACE
{ {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f} },
{ {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f} },
// TOP FACE
{ {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f} },
{ {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 0.0f} },
// BOTTOM FACE
{ {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f} },
{ {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} }
};
calculateProjectionMatrix(SCREEN_WIDTH, SCREEN_HEIGHT);
c = new sf::Clock();
lightSources.push_back(new DirectionLight({0, 0, 1}));
lightSources.push_back(new DirectionLight({ 0, 1, 0 }));
}
int main()
{
init();
sf::ContextSettings settings;
settings.antialiasingLevel = 1;
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT),
"3DRenderer", sf::Style::Default, settings);
#ifdef NDEBUG
window.setFramerateLimit(60);
#else
window.setFramerateLimit(30);
#endif // NDEBUG
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
// recalculate projection matrix
calculateProjectionMatrix(window.getSize().x, window.getSize().y);
// reset the window view
sf::FloatRect newViewRect = sf::FloatRect(0, 0, window.getSize().x, window.getSize().y);
window.setView(sf::View(newViewRect));
}
}
window.clear();
DrawMesh(window, cubeMesh);
window.display();
}
return 0;
}