-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdonut.cc
More file actions
40 lines (31 loc) · 1.3 KB
/
Copy pathdonut.cc
File metadata and controls
40 lines (31 loc) · 1.3 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
#include <iostream>
#include <cstring>
#include "mathpls.h" // github.com/Ninter6/mathpls
using namespace mathpls;
constexpr int WIDTH = 80, HEIGHT = 23;
constexpr float R0 = 1, R1 = .5, D0 = 72, D1 = 72;
constexpr std::string_view tb = ".-:+=*#";
const mat4 p = perspective(1.f, (float)WIDTH/HEIGHT/2, .1f, 10.f);
const vec3 ld = normalize(vec3{5, 4, 2});
char img[HEIGHT][WIDTH+1]{};
int main() {
for (float t=0;;t+=.005f) {
vec3 cam = quat({cos(t-.3),sin(t)*3.14, 0}, xyz) * vec3(2);
auto vp = p * lookAt(cam, {}, {0,-1, 0});
float dep[HEIGHT][WIDTH]{};
for (float i = 0; i < pi()*2; i += pi()/D0*2)
for (float j = 0; j < pi()*2; j += pi()/D1*2) {
vec3 u = {cos(j)*(R0+sin(i)*R1), sin(j)*(R0+sin(i)*R1), cos(i)*R1};
auto q = vp * vec4(u, 1.f);
q = q / q.w * .5f + .5f;
int cy = round(q.y * (HEIGHT-1)), cx = round(q.x * (WIDTH-1));
if (dep[cy][cx] >= q.z) continue;
dep[cy][cx] = q.z;
auto N = normalize(u - vec3(vec2(u)).normalized()*R0);
img[cy][cx] = tb[(int)round((tb.size()-1)*pow(dot(N, ld)*.5+.5,2.4))];
}
std::cout << "\E[2J\E[H";
for (auto& s : img)
puts(s), std::memset(s, ' ', WIDTH);
}
}