-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeletonSDL2.cpp
More file actions
340 lines (284 loc) · 8.19 KB
/
skeletonSDL2.cpp
File metadata and controls
340 lines (284 loc) · 8.19 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
//DH2323 skeleton code, Lab2 (SDL2 version)
#include <iostream>
#include <glm/glm.hpp>
#include "SDL2auxiliary.h"
#include "TestModel.h"
#include <algorithm>
#include <cmath>
#include <fstream>
using namespace std;
using glm::vec3;
using glm::mat3;
using glm::cross;
using glm::dot;
// ----------------------------------------------------------------------------
// GLOBAL VARIABLES
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
vector <Triangle> triangles;
float focalLength = 500.0f;
vec3 cameraPos(0.0f, 0, -3.001f);
mat3 R;
float yaw = 0.0f;
vec3 lightPos(0, -0.1, -0.7);
vec3 lightColor = 14.f * vec3(1, 1, 1);
vec3 indirectLight = 0.5f * vec3(1, 1, 1);
SDL2Aux* sdlAux;
int t;
// ----------------------------------------------------------------------------
// FUNCTIONS
void Update(void);
void Draw(void);
void DrawSSAA(void);
void DrawTAA(void);
struct Intersection
{
vec3 position;
float distance;
int triangleIndex;
};
bool ClosestIntersection(
vec3 start,
vec3 dir,
const vector<Triangle>& triangles,
Intersection& closestIntersection
);
vec3 DirectLight(const Intersection& i);
int main(int argc, char* argv[])
{
sdlAux = new SDL2Aux(SCREEN_WIDTH, SCREEN_HEIGHT);
t = SDL_GetTicks(); // Set start value for timer.
LoadTestModel(triangles);
while (!sdlAux->quitEvent())
{
Update();
//Draw();
DrawSSAA();
//DrawTAA();
}
return 0;
}
void Update(void)
{
// Compute frame time:
int t2 = SDL_GetTicks();
float dt = float(t2 - t);
t = t2;
//std::ofstream outFile("render_times-TAA.txt", std::ios_base::app); // from chatgpt
//outFile << "Render time: " << dt << " ms." << std::endl;
cout << "Render time: " << dt << " ms." << endl;
const Uint8* keystate = SDL_GetKeyboardState(NULL);
float speed = 0.2f;
if (keystate[SDL_SCANCODE_UP])
{
cameraPos.z += speed; // Move camera forward
}
if (keystate[SDL_SCANCODE_DOWN])
{
cameraPos.z -= speed; // Move camera backward
}
if (keystate[SDL_SCANCODE_LEFT])
{
yaw -= 0.01f; // Rotate left
}
if (keystate[SDL_SCANCODE_RIGHT])
{
yaw += 0.01f; // Rotate right
}
// updating rotation matrix based on the yaw angle
float co = cos(yaw);
float si = sin(yaw);
// Rotation matrix for rotating around the Y-axis
R = mat3(
vec3(co, 0, -si),
vec3(0, 1, 0),
vec3(si, 0, co)
);
if (keystate[SDL_SCANCODE_W]) { // forward
lightPos.z += speed;
}
if (keystate[SDL_SCANCODE_S]) { // backwards
lightPos.z -= speed;
}
if (keystate[SDL_SCANCODE_D]) { // right
lightPos.x += speed;
}
if (keystate[SDL_SCANCODE_A]) { // left
lightPos.x -= speed;
}
if (keystate[SDL_SCANCODE_Q]) { // up
lightPos.y -= speed;
}
if (keystate[SDL_SCANCODE_E]) { // down
lightPos.y += speed;
}
}
void Draw()
{
sdlAux->clearPixels();
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
vec3 d(x - SCREEN_WIDTH * 0.5f, y - SCREEN_HEIGHT * 0.5f, focalLength);
d = R * d; // applying rotation
Intersection closestIntersection;
bool intersection = ClosestIntersection(cameraPos, d, triangles, closestIntersection);
if (intersection) {
vec3 directLight = DirectLight(closestIntersection);
vec3 color = triangles[closestIntersection.triangleIndex].color * (directLight + indirectLight); // R = p ∗ (D + N)
sdlAux->putPixel(x, y, color);
}
else {
vec3 blackBackground(0.0f, 0.0f, 0.0f); // black
sdlAux->putPixel(x, y, blackBackground);
}
}
}
sdlAux->render();
}
void DrawSSAA()
{
const int N_SAMPLES = 4; // 2x2 supersampling
float sample_offsets[4][2] = {
{-0.25f, -0.25f},
{ 0.25f, -0.25f},
{-0.25f, 0.25f},
{ 0.25f, 0.25f}
};
sdlAux->clearPixels();
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
vec3 pixelColor(0.0f);
// Loopign over each subpixel sample
for (int i = 0; i < N_SAMPLES; ++i)
{
float dx = sample_offsets[i][0];
float dy = sample_offsets[i][1];
vec3 d(x + dx - SCREEN_WIDTH * 0.5f, y + dy - SCREEN_HEIGHT * 0.5f, focalLength);
d = R * d;
Intersection closestIntersection;
bool intersection = ClosestIntersection(cameraPos, d, triangles, closestIntersection);
if (intersection)
{
vec3 directLight = DirectLight(closestIntersection);
vec3 color = triangles[closestIntersection.triangleIndex].color * (directLight + indirectLight);
pixelColor += color;
}
}
// averaging to compute final color
pixelColor /= float(N_SAMPLES);
sdlAux->putPixel(x, y, pixelColor);
}
}
sdlAux->render();
}
// global variables for TAA
std::vector<vec3> currentFramebuffer(SCREEN_WIDTH* SCREEN_HEIGHT);
std::vector<vec3> previousFramebuffer(SCREEN_WIDTH* SCREEN_HEIGHT, vec3(0.0f));
int frameCount = 0;
float alpha = 0.1f; // blending factor
// Function to generate Halton sequence, taken from https://www.wayline.io/blog/halton-sequence-demonstration-and-resources
float Halton(int b, int index) {
float result = 0.0f;
float f = 1.0f;
while (index > 0) {
result += f * float((index % b));
index = index / b;
f = f / float(b);
}
return result;
}
void DrawTAA() {
sdlAux->clearPixels();
// Generating jitter offsets using the Halton sequence from wayline
float jitterX = (Halton(2, frameCount) - 0.5f);
float jitterY = (Halton(3, frameCount) - 0.5f);
for (int y = 0; y < SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SCREEN_WIDTH; ++x) {
// Applying jitter to current pixel
float px = x + jitterX;
float py = y + jitterY;
vec3 d(px - SCREEN_WIDTH * 0.5f, py - SCREEN_HEIGHT * 0.5f, focalLength);
d = R * d; // applying rotation
Intersection closestIntersection;
bool intersection = ClosestIntersection(cameraPos, d, triangles, closestIntersection);
vec3 color(0.0f);
if (intersection) {
vec3 directLight = DirectLight(closestIntersection);
color = triangles[closestIntersection.triangleIndex].color * (directLight + indirectLight);
}
// temporal accumulation (blending the current color with the history color for a smoother image)
int index = y * SCREEN_WIDTH + x;
vec3 historyColor = previousFramebuffer[index];
vec3 accumulatedColor = alpha * color + (1.0f - alpha) * historyColor; // Equation 2 from Yang, L., Liu, S. and Salvi, M. (2020)
currentFramebuffer[index] = accumulatedColor;
sdlAux->putPixel(x, y, accumulatedColor);
}
}
// Swap framebuffers
previousFramebuffer = currentFramebuffer;
frameCount++;
sdlAux->render();
}
bool ClosestIntersection(
vec3 start,
vec3 dir,
const vector<Triangle>& triangles,
Intersection& closestIntersection
)
{
float m = std::numeric_limits<float>::max();
bool foundIntersection = false;
for (int i = 0; i < triangles.size(); ++i)
{
const Triangle& triangle = triangles[i];
vec3 e1 = triangle.v1 - triangle.v0;
vec3 e2 = triangle.v2 - triangle.v0;
vec3 b = start - triangle.v0;
// Cramer's rule - saves a lot of time
float detA = dot(-dir, cross(e1, e2)); // determinant of A
float detT = dot(b, cross(e1, e2));
float t = detT / detA; // calculating t
if (t < 0)
continue; // "if t is negative, there is no need to continue analyzing that intersection, as it will not occur."
float detU = dot(-dir, cross(b, e2));
float u = detU / detA; // calculating u
if (u < 0)
continue;
float detV = dot(-dir, cross(e1, b));
float v = detV / detA; // calculating v
if (v < 0 || (u + v) > 1)
continue;
if (t < m)
{
m = t;
foundIntersection = true;
closestIntersection.position = start + t * dir;
closestIntersection.distance = t;
closestIntersection.triangleIndex = i;
}
}
return foundIntersection;
}
vec3 DirectLight(const Intersection& i)
{
Triangle triangle = triangles[i.triangleIndex];
vec3 e1 = triangle.v1 - triangle.v0;
vec3 e2 = triangle.v2 - triangle.v0;
vec3 normal = glm::normalize(cross(e2, e1));
vec3 r = (lightPos - i.position);
float r2 = dot(r, r);
vec3 r_norm = glm::normalize(r);
Intersection shadowIntersection;
bool shadow = ClosestIntersection(i.position + 0.01f * r, r_norm, triangles, shadowIntersection); // 0.01f to avoid shadow bias
if (shadow && shadowIntersection.distance < r2) {
return vec3(0.0f); // no light
}
float dotproduct = dot(r_norm, normal);
dotproduct = glm::max(dotproduct, 0.0f);
vec3 directLight = lightColor * dotproduct / (4.0f * float(M_PI) * r2);
return directLight;
}