-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d_cube.c
116 lines (104 loc) · 3.52 KB
/
3d_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
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
float A, B, C;
float cubeWidth = 20;
int width = 160, height = 44;
float zBuffer[160 * 44];
char buffer[160 * 44];
int backgroundASCIICode = '.';
int distanceFromCam = 100;
float horizontalOffset;
float K1 = 40;
float incrementSpeed = 0.6;
float x, y, z;
float ooz;
int xp, yp;
int idx;
float calculateX(int i, int j, int k) {
return j * sin(A) * sin(B) * cos(C) - k * cos(A) * sin(B) * cos(C) +
j * cos(A) * sin(C) + k * sin(A) * sin(C) + i * cos(B) * cos(C);
}
float calculateY(int i, int j, int k) {
return j * cos(A) * cos(C) + k * sin(A) * cos(C) -
j * sin(A) * sin(B) * sin(C) + k * cos(A) * sin(B) * sin(C) -
i * cos(B) * sin(C);
}
float calculateZ(int i, int j, int k) {
return k * cos(A) * cos(B) - j * sin(A) * cos(B) + i * sin(B);
}
void calculateForSurface(float cubeX, float cubeY, float cubeZ, int ch) {
x = calculateX(cubeX, cubeY, cubeZ);
y = calculateY(cubeX, cubeY, cubeZ);
z = calculateZ(cubeX, cubeY, cubeZ) + distanceFromCam;
ooz = 1 / z;
xp = (int)(width / 2 + horizontalOffset + K1 * ooz * x * 2);
yp = (int)(height / 2 + K1 * ooz * y);
idx = xp + yp * width;
if (idx >= 0 && idx < width * height) {
if (ooz > zBuffer[idx]) {
zBuffer[idx] = ooz;
buffer[idx] = ch;
}
}
}
int main() {
system("color b");
printf("\x1b[2J");
while (1) {
memset(buffer, backgroundASCIICode, width * height);
memset(zBuffer, 0, width * height * 4);
cubeWidth = 20;
horizontalOffset = -2 * cubeWidth;
// first cube
for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
for (float cubeY = -cubeWidth; cubeY < cubeWidth;
cubeY += incrementSpeed) {
calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
calculateForSurface(cubeWidth, cubeY, cubeX, '$');
calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
calculateForSurface(cubeX, cubeWidth, cubeY, '+');
}
}
cubeWidth = 10;
horizontalOffset = 1 * cubeWidth;
// second cube
for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
for (float cubeY = -cubeWidth; cubeY < cubeWidth;
cubeY += incrementSpeed) {
calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
calculateForSurface(cubeWidth, cubeY, cubeX, '$');
calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
calculateForSurface(cubeX, cubeWidth, cubeY, '+');
}
}
cubeWidth = 5;
horizontalOffset = 8 * cubeWidth;
// third cube
for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
for (float cubeY = -cubeWidth; cubeY < cubeWidth;
cubeY += incrementSpeed) {
calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
calculateForSurface(cubeWidth, cubeY, cubeX, '$');
calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
calculateForSurface(cubeX, cubeWidth, cubeY, '+');
}
}
printf("\x1b[H");
for (int k = 0; k < width * height; k++) {
putchar(k % width ? buffer[k] : 10);
}
A += 0.05;
B += 0.05;
C += 0.01;
usleep(8000 * 2);
}
return 0;
}