-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
341 lines (294 loc) · 11.8 KB
/
renderer.cpp
File metadata and controls
341 lines (294 loc) · 11.8 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
#include "renderer.hpp"
#include <array>
#include <cstdint>
void Renderer::initialize(std::string title, std::string mesh_path) {
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_init: %s", SDL_GetError());
throw 1;
}
display_mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
if (display_mode == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Get current display mode: %s", SDL_GetError());
throw 1;
}
int window_width = 2160;
int window_height = 1440;
window = SDL_CreateWindow(title.c_str(), window_width, window_height, 0);
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Create window: %s", SDL_GetError());
throw 1;
}
if (!SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Center window: %s", SDL_GetError());
throw 1;
}
renderer = SDL_CreateRenderer(window, NULL);
if (renderer == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Create renderer: %s", SDL_GetError());
throw 1;
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, window_width, window_height);
if (texture == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Create texture: %s", SDL_GetError());
throw 1;
}
w = window_width;
h = window_height;
c_buf = std::vector<uint32_t>(w*h, 0x00000000);
meshes.push_back(get_mesh_from_obj_file(mesh_path));
camera = Camera { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, 640.0 };
triangles = {};
prev_frame_time = SDL_GetTicksNS();
flags = 0xff;
}
void Renderer::deinitialize() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(texture);
}
bool Renderer::process_input() {
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_W:
camera.rotation.x -= 0.1;
break;
case SDLK_S:
camera.rotation.x += 0.1;
break;
case SDLK_A:
camera.rotation.y += 0.1;
break;
case SDLK_D:
camera.rotation.y -= 0.1;
break;
case SDLK_Q:
camera.rotation.z -= 0.1;
break;
case SDLK_E:
camera.rotation.z += 0.1;
break;
case SDLK_1:
flags &= BackfaceCulling;
flags |= Vertices | Wireframe;
break;
case SDLK_2:
flags &= BackfaceCulling;
flags |= Wireframe;
break;
case SDLK_3:
flags &= BackfaceCulling;
flags |= PolygonFill;
break;
case SDLK_4:
flags &= BackfaceCulling;
flags |= Wireframe | PolygonFill;
break;
case SDLK_C:
flags &= Vertices | Wireframe | PolygonFill;
flags |= BackfaceCulling;
break;
case SDLK_X:
flags &= Vertices | Wireframe | PolygonFill;
flags |= ~BackfaceCulling;
break;
case SDLK_ESCAPE:
deinitialize();
return false;
default:
return true;
}
break;
case SDL_EVENT_QUIT:
deinitialize();
return false;
}
}
return true;
}
void Renderer::update() {
int window_width_offset = w/2;
int window_height_offset = h/2;
Triangle triangle;
Vec2 projected_point;
uint64_t time_to_wait = FRAME_TARGET_TIME_NS - (SDL_GetTicksNS() - prev_frame_time);
if (time_to_wait > 0) {
SDL_DelayNS(time_to_wait);
if (time_to_wait > FRAME_TARGET_TIME_NS) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "wait time exceeded frame time: %llu, frame time: %f", time_to_wait, FRAME_TARGET_TIME_NS);
}
}
prev_frame_time = SDL_GetTicksNS();
for (Mesh& mesh : meshes) {
int i = 0;
// mesh.rot = global_rot;
uint32_t color = 0xccdd33ff;
for (const std::array<int, 3>& face : mesh.faces) {
std::array<Vec3, 3> face_vertices = {
mesh.vertices[face[0] - 1],
mesh.vertices[face[1] - 1],
mesh.vertices[face[2] - 1]
};
std::array<Vec3, 3> transformed_vertices;
for (int i = 0; i < 3; ++i) {
transformed_vertices[i] = rotate_axis_z(rotate_axis_y(rotate_axis_x(face_vertices[i], mesh.rot.x), mesh.rot.y), mesh.rot.z);
transformed_vertices[i] = rotate_axis_z(rotate_axis_y(rotate_axis_x(face_vertices[i], camera.rotation.x), camera.rotation.y), camera.rotation.z);
transformed_vertices[i].z += 5;
}
triangle.color = color;
color = 0x000000ff | color+0x132480ff;
if ((flags & BackfaceCulling) == BackfaceCulling) {
Vec3 normal = cross(transformed_vertices[1] - transformed_vertices[0], transformed_vertices[2] - transformed_vertices[0]);
Vec3 camera_ray = camera.position - transformed_vertices[0];
if (dot(normal, camera_ray) < 0) { continue; }
}
for (int i = 0; i < 3; ++i) {
projected_point = project_perspective(transformed_vertices[i]);
projected_point.x += window_width_offset;
projected_point.y += window_height_offset;
triangle.points[i] = projected_point;
}
triangle.avg_depth = (transformed_vertices[0].z + transformed_vertices[1].z + transformed_vertices[2].z) / 3;
triangles.push_back(triangle);
}
// mesh.rot += 0.01;
}
// sort faces by depth (average z)
uint32_t color = 0xccdd33ff;
for (Triangle& t1 : triangles) {
for (Triangle& t2 : triangles) {
if (t1.avg_depth > t2.avg_depth) { std::swap(t1, t2); }
}
}
}
void Renderer::render() {
draw_grid(0x333333ff);
for (const Triangle& t : triangles) {
draw_triangle(t, t.color, 0x00aabbff, 0xee4444ff);
}
triangles = {};
SDL_UpdateTexture(texture, NULL, c_buf.data(), sizeof(uint32_t) * w);
SDL_RenderTexture(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
clear_buffer();
}
Vec2 Renderer::project_orthographic(const Vec3& p) noexcept {
return { camera.fov_factor * p.x, camera.fov_factor * p.y };
}
Vec2 Renderer::project_perspective(const Vec3& p) noexcept {
return { (camera.fov_factor * p.x) / p.z, (camera.fov_factor * p.y) / p.z };
}
void Renderer::draw_grid(uint32_t color) noexcept {
for (int y = 0; y < h; y += 10) {
for (int x = 0; x < w; x += 1) {
c_buf[(w * y) + x] = color;
}
}
for (int x = 0; x < w; x += 10) {
for (int y = 0; y < h; y += 1) {
c_buf[(w * y) + x] = color;
}
}
}
void Renderer::clear_buffer() noexcept {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
c_buf[(w * y) + x] = 0x000000ff;
}
}
}
void Renderer::draw_pixel(int x, int y, uint32_t color) noexcept {
if (x < 0 || x >= w || y < 0 || y >= h) return;
c_buf[(w * y) + x] = color;
}
void Renderer::draw_line_dda(int x1, int y1, int x2, int y2, uint32_t color) noexcept {
int delta_x = x2 - x1;
int delta_y = y2 - y1;
int longest_side_len = delta_x >= abs(delta_y) ? delta_x : abs(delta_y);
float x_inc = delta_x / static_cast<float>(longest_side_len);
float y_inc = delta_y / static_cast<float>(longest_side_len);
float curr_x = x1;
float curr_y = y1;
for (int i = 0; i < longest_side_len; ++i) {
draw_pixel(round(curr_x), round(curr_y), color);
curr_x += x_inc;
curr_y += y_inc;
}
}
void Renderer::draw_triangle(const Triangle& t, uint32_t fill_color, uint32_t wire_color, uint32_t vertex_color) noexcept {
std::array<Vec2, 3> points = { t.points[0], t.points[1], t.points[2] };
if ((flags & PolygonFill) == PolygonFill) {
if (points[0].y > points[1].y) { std::swap(points[0], points[1]); }
if (points[1].y > points[2].y) { std::swap(points[1], points[2]); }
if (points[0].y > points[1].y) { std::swap(points[0], points[1]); }
Vec2 midpoint = {
(((points[2].x - points[0].x) * (points[1].y - points[0].y)) / (points[2].y - points[0].y)) + points[0].x,
points[1].y
};
bool midpoint_left, vertical_line;
if (points[1].x < midpoint.x) {
midpoint_left = false;
} else {
midpoint_left = true;
}
double dxy_left, dxy_right, x_start, x_end;
if (points[0].y != points[1].y) {
if (!midpoint_left) {
dxy_left = (points[1].x - points[0].x) / (points[1].y - points[0].y);
dxy_right = (midpoint.x - points[0].x) / (midpoint.y - points[0].y);
x_start = points[1].x;
x_end = midpoint.x;
} else {
dxy_left = (midpoint.x - points[0].x) / (midpoint.y - points[0].y);
dxy_right = (points[1].x - points[0].x) / (points[1].y - points[0].y);
x_start = midpoint.x;
x_end = points[1].x;
}
for(int y = midpoint.y; y >= points[0].y; --y) {
for (int x = x_start; x <= x_end; ++x) {
draw_pixel(x, y, fill_color);
}
x_start -= dxy_left;
x_end -= dxy_right;
}
}
if (points[1].y != points[2].y) {
if (!midpoint_left) {
x_start = points[1].x;
x_end = midpoint.x;
dxy_left = (points[2].x - points[1].x) / (points[2].y - points[1].y);
dxy_right = (points[2].x - midpoint.x) / (points[2].y - midpoint.y);
} else {
x_start = midpoint.x;
x_end = points[1].x;
dxy_left = (points[2].x - midpoint.x) / (points[2].y - midpoint.y);
dxy_right = (points[2].x - points[1].x) / (points[2].y - points[1].y);
}
for(int y = midpoint.y; y <= points[2].y; ++y) {
for (int x = x_start; x <= x_end; ++x) {
draw_pixel(x, y, fill_color);
}
x_start += dxy_left;
x_end += dxy_right;
}
}
}
if ((flags & Wireframe) == Wireframe) {
draw_line_dda(t.points[0].x, t.points[0].y, t.points[1].x, t.points[1].y, wire_color);
draw_line_dda(t.points[0].x, t.points[0].y, t.points[2].x, t.points[2].y, wire_color);
draw_line_dda(t.points[1].x, t.points[1].y, t.points[2].x, t.points[2].y, wire_color);
}
if ((flags & Vertices) == Vertices) {
draw_rectangle(t.points[0].x-1, t.points[0].y-1, 4, 4, vertex_color);
draw_rectangle(t.points[1].x-1, t.points[1].y-1, 4, 4, vertex_color);
draw_rectangle(t.points[2].x-1, t.points[2].y-1, 4, 4, vertex_color);
}
}
void Renderer::draw_rectangle(int x, int y, int width, int height, uint32_t color) noexcept {
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
draw_pixel(x+i, y+j, color);
}
}
}