-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtty_cube.c
271 lines (237 loc) · 9.51 KB
/
tty_cube.c
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
#include <fcntl.h> // For open
#include <linux/fb.h> // For FBIOGET_VSCREENINFO
#include <sys/ioctl.h> // For ioctl
#include <sys/mman.h> // For mmap
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "config.h"
#include "vectors.h"
#include "fragment_shaders.h"
#include "light.h"
#include "camera.h"
#include "blur.h"
#define PI 3.14159265
volatile sig_atomic_t done = 0;
void term(int signum)
{
done = 1;
}
void paint_pixel(int x, int y, vec4 color, char buffer[], struct fb_var_screeninfo vinfo)
{
color.x = fmin(fmax(color.x, 0), 1);
color.y = fmin(fmax(color.y, 0), 1);
color.z = fmin(fmax(color.z, 0), 1);
color.w = fmin(fmax(color.w, 0), 1);
buffer[(y*vinfo.xres+x)*4] = (unsigned int)(color.z * 255);
buffer[(y*vinfo.xres+x)*4+1] = (unsigned int)(color.y * 255);
buffer[(y*vinfo.xres+x)*4+2] = (unsigned int)(color.x * 255);
buffer[(y*vinfo.xres+x)*4+3] = (unsigned int)(87);
return;
}
int main(int argc, char *argv[])
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGINT, &action, NULL);
int fbfd = open(FB_DEVICE, O_RDWR);
if (fbfd == -1) {
perror("Error opening framebuffer device");
exit(1);
}
struct fb_var_screeninfo vinfo;
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
perror("Error reading variable information");
exit(1);
}
struct fb_fix_screeninfo finfo;
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
perror("Error reading fixed information");
exit(1);
}
long screensize = vinfo.yres_virtual * finfo.line_length;
char* fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((intptr_t)fbp == -1) {
perror("Error mapping framebuffer to memory");
exit(1);
}
char buffer[vinfo.xres * vinfo.yres * 4];
// Save the current state of the fb
memcpy(buffer, fbp, 4 * vinfo.xres * vinfo.yres);
// Declare camera and light
camera rotate_around_origin;
light3 light;
// Get image data
#ifdef IMAGE
FILE* image_file = fopen(IMAGE, "r");
fread(image_data, SIDE_LENGTH*SIDE_LENGTH, 3, image_file);
fclose(image_file);
#endif
// Time for frame limiter
double time = 0;
double time_cyclic = 0;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
unsigned int delta_us = 0;
double delta = delta_us;
while (!done)
{
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
time += SPEED*delta*20;
time_cyclic = ((int)time%100)/(100/2.0);
// Define the camera
rotate_around_origin = (camera){-SIDE_LENGTH,
(vec2){vinfo.xres, vinfo.yres},
(vec3){PI/6*sin(2*time_cyclic*PI),-time_cyclic*PI-PI/2,0},
(vec3){cos(time_cyclic*PI)*SIDE_LENGTH*2,
SIDE_LENGTH*sin(2*time_cyclic*PI),
sin(time_cyclic*PI)*SIDE_LENGTH*2},
(vec3){1,1,1},
(vec2){0,0},
(vec3){0,0,0},
(vec3){0,0,0},
(vec3){0,0,0},
(vec3){0,0,0},
(vec3){0,0,0}};
camera transformed_cam = setup_camera(rotate_around_origin);
// Define the light source
vec3 light_offset = (vec3){SIDE_LENGTH*3,-SIDE_LENGTH*2,SIDE_LENGTH};
light = (light3){(vec3){1,1,1},
add_vec3(transformed_cam.center_point,
add_vec3(scale_vec3(transformed_cam.base_x, light_offset.x),
add_vec3(scale_vec3(transformed_cam.base_y, light_offset.y),
scale_vec3(transformed_cam.base_z, light_offset.z))))};
// Find the bounding box of the cube for the frame
vec3 vertices[8] = {(vec3){SIDE_LENGTH/2, SIDE_LENGTH/2, SIDE_LENGTH/2},
(vec3){SIDE_LENGTH/2, SIDE_LENGTH/2, -SIDE_LENGTH/2},
(vec3){SIDE_LENGTH/2, -SIDE_LENGTH/2, SIDE_LENGTH/2},
(vec3){SIDE_LENGTH/2, -SIDE_LENGTH/2, -SIDE_LENGTH/2},
(vec3){-SIDE_LENGTH/2, SIDE_LENGTH/2, SIDE_LENGTH/2},
(vec3){-SIDE_LENGTH/2, SIDE_LENGTH/2, -SIDE_LENGTH/2},
(vec3){-SIDE_LENGTH/2, -SIDE_LENGTH/2, SIDE_LENGTH/2},
(vec3){-SIDE_LENGTH/2, -SIDE_LENGTH/2, -SIDE_LENGTH/2}};
vec2 min_coords = (vec2){vinfo.xres, vinfo.yres};;
vec2 max_coords = (vec2){0,0};
for (int i = 0; i < 8; i++)
{
vec3 projection_vector = normalize_vec3(subtract_vec3(vertices[i], transformed_cam.focal_point));
vec3 projection_point =
scale_vec3(projection_vector,
-transformed_cam.focal_offset/
dot_product_vec3(projection_vector, transformed_cam.base_z));
max_coords.x = fmax(dot_product_vec3(projection_point, transformed_cam.base_x), max_coords.x);
max_coords.y = fmax(dot_product_vec3(projection_point, transformed_cam.base_y), max_coords.y);
min_coords.x = fmin(dot_product_vec3(projection_point, transformed_cam.base_x), min_coords.x);
min_coords.y = fmin(dot_product_vec3(projection_point, transformed_cam.base_y), min_coords.y);
}
min_coords = add_vec2(min_coords, scale_vec2(transformed_cam.dimensions, 0.5));
min_coords.x = fmax(0, min_coords.x);
min_coords.y = fmax(0, min_coords.y);
max_coords = add_vec2(max_coords, scale_vec2(transformed_cam.dimensions, 0.5));
max_coords.x = fmin(vinfo.xres-1, max_coords.x);
max_coords.y = fmin(vinfo.yres-1, max_coords.y);
for (int j = 0; j < vinfo.yres; j++)
{
for (int i = 0; i < vinfo.xres; i++)
{
// If pixel is inside the cube's projected bounding box,
// render ir
if (i >=(int)min_coords.x && i <=(int)max_coords.x &&
j >=(int)min_coords.y && j <=(int)max_coords.y)
{
if (RENDER_BOUNDING_BOX)
{
if (i == (int)min_coords.x || i == (int)max_coords.x ||
j == (int)min_coords.y || j == (int)max_coords.y)
{
paint_pixel(i, j, (vec4){1,1,1,1}, buffer, vinfo);
continue;
}
}
if (RENDER_OVER_TEXT)
{
vec4 color = get_pixel_through_camera(i, j, transformed_cam, light);
paint_pixel(i, j, color, buffer, vinfo);
}
else
{
// Don't paint over tty text
vec4 fb_color = {buffer[(j*vinfo.xres+i)*4],
buffer[(j*vinfo.xres+i)*4+1],
buffer[(j*vinfo.xres+i)*4+2],
buffer[(j*vinfo.xres+i)*4+3]};
if (fb_color.x == 0 &&
fb_color.y == 0 &&
fb_color.z == 0)
{
vec4 color = get_pixel_through_camera(i, j, transformed_cam, light);
paint_pixel(i, j, color, buffer, vinfo);
}
// Also repaint previously painted pixels
// (marked by w = 87)
else if (fb_color.w == 87)
{
vec4 color = get_pixel_through_camera(i, j, transformed_cam, light);
paint_pixel(i, j, color, buffer, vinfo);
}
}
}
// If pixel is outside the bounding box, just paint it black
else
{
if (RENDER_OVER_TEXT)
{
paint_pixel(i, j, (vec4){0,0,0,0}, buffer, vinfo);
}
else
{
vec4 fb_color = {buffer[(j*vinfo.xres+i)*4],
buffer[(j*vinfo.xres+i)*4+1],
buffer[(j*vinfo.xres+i)*4+2],
buffer[(j*vinfo.xres+i)*4+3]};
if (fb_color.w == 87)
{
paint_pixel(i, j, (vec4){0,0,0,0}, buffer, vinfo);
}
}
}
}
}
// Somewhat remove aliasing applying a gaussian blur
if (BLUR_ANTIALIAS)
{
blur_pixels(buffer, min_coords, max_coords, vinfo.xres, vinfo.yres);
}
// For some reason, the fb doesn't update fast enough
// unless we print something first
printf("\r");
fflush(stdout);
memcpy(fbp, buffer, 4 * vinfo.xres * vinfo.yres);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
if (FRAME_LIMIT > 0)
{
if (delta_us < 1000000.0 / FRAME_LIMIT)
{
usleep(1000000.0 / FRAME_LIMIT - delta_us);
delta = 1.0 / FRAME_LIMIT;
}
else
{
delta = delta_us/1000000.0;
}
}
else
{
delta = delta_us/1000000.0;
}
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}