-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
135 lines (126 loc) · 3.33 KB
/
main.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
/* Rend3D Demo - Terminal 3D rendering demo program powered by Notcurses
*
* Copyright (C) 2021 Łukasz "MasFlam" Drukała
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <math.h>
#include <notcurses/notcurses.h>
#include "rend3d.h"
struct g {
struct notcurses *nc;
struct ncplane *stdp;
struct rend3d *r3d;
};
static void cleanup();
static void init();
struct g g;
void
cleanup()
{
rend3d_destroy(g.r3d);
notcurses_stop(g.nc);
}
void
init()
{
g.nc = notcurses_core_init(&(struct notcurses_options) {
.flags = NCOPTION_SUPPRESS_BANNERS
}, stdout);
if (notcurses_check_pixel_support(g.nc) < 1) {
notcurses_stop(g.nc);
fputs("No pixel support!", stderr);
exit(2);
}
int termw, termh;
g.stdp = notcurses_stddim_yx(g.nc, &termh, &termw);
struct ncplane *drawp = ncplane_create(g.stdp, &(struct ncplane_options) {
.x = 0, .y = 0,
.rows = termh,
.cols = termw
});
g.r3d = rend3d_create(drawp, &(struct rend3d_options) {
.projtype = REND3D_PROJTYPE_PERSP,
.projopts.persp.fovx = M_PI/4,
.projopts.persp.fovy = M_PI/4,
.projopts.persp.near = 0.01,
.projopts.persp.far = 1,
});
}
int
main()
{
init();
struct r3d_obj obj = {
.posx = 0,
.posy = 0,
.posz = 0,
.rotx = 0,
.roty = 0,
.rotz = 0,
.sclx = 0.2,
.scly = 0.2,
.sclz = 0.2,
.vertcount = 1,
.vertices = (struct r3d_vertex[]){
{ 0.7, 0.7, 0 },
},
.edgecount = 12,
.edges = (struct r3d_edge[]) {
// Front face rim
{{ -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }},
{{ -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }},
{{ 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }},
{{ 0.5, -0.5, 0.5 }, { -0.5, -0.5, 0.5 }},
// Back face rim
{{ -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }},
{{ -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }},
{{ 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }},
{{ 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }},
// Side edges
{{ 0.5, 0.5, -0.5 }, { 0.5, 0.5, 0.5 }},
{{ 0.5, -0.5, -0.5 }, { 0.5, -0.5, 0.5 }},
{{ -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }},
{{ -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }},
}
};
static const int N = 3;
for (int x = -N; x <= N; ++x) {
obj.posx = x;
for (int y = -N; y <= N; ++y) {
obj.posy = y;
for (int z = -N; z <= N; ++z) {
obj.posz = z;
rend3d_add_object(g.r3d, &obj);
}
}
}
rend3d_cam_move(g.r3d, 0.3, 0.4, 0.5);
while (1) {
rend3d_render(g.r3d);
notcurses_render(g.nc);
int i = 0;
struct r3d_objref *ref = rend3d_get_first_object(g.r3d);
while (ref) {
ref->obj.rotx += (i%2 == 0 ? 1 : -1) * 0.02 * M_PI;
ref->obj.roty += (i%3 == 0 ? 1 : -1) * 0.015 * M_PI;
ref->obj.rotz += (i%5 == 0 ? 1 : -1) * 0.01 * M_PI;
++i;
ref = rend3d_get_next_object(ref);
}
rend3d_cam_rotate(g.r3d, 0.007 * M_PI, 0.01 * M_PI, 0.005 * M_PI);
}
cleanup();
return 0;
}