forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staircase.cpp
279 lines (240 loc) · 8.45 KB
/
staircase.cpp
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
// Hyperbolic Rogue: staircase simulation in RogueViz
// Copyright (C) 2011-2018 Zeno and Tehora Rogue, see 'hyper.cpp' for details
// see: https://www.youtube.com/watch?v=HZNRo6mr5pk
#include "rogueviz.h"
namespace rogueviz { namespace staircase {
ld scurvature = 0;
ld acurvature = 0;
ld global_r;
ld szoom = 5;
ld progress = 0;
basic_textureinfo tinf;
hyperpoint spcoord(hyperpoint h) {
ld phi = h[0], y = h[1], z = h[2], r = global_r;
ld aphi = (r+phi + floor(progress))*M_PI/6;
return zpush(acurvature*(y + r - frac(progress))/szoom) * xspinpush0(aphi, acurvature * z);
}
using tv = glhr::textured_vertex;
tv pt(hyperpoint h, hyperpoint c) {
tv result;
h = spcoord(h);
result.coords[0] = h[0];
result.coords[1] = h[1];
result.coords[2] = h[2];
result.coords[3] = h[3];
result.texture[0] = c[0];
result.texture[1] = c[1];
result.texture[2] = c[2];
return result;
}
vector<glvertex> vertices;
void add_pt(tv tv) {
vertices.push_back(tv.coords);
glvertex t;
for(int i=0; i<3; i++) t[i] = tv.texture[i];
tinf.tvertices.push_back(t);
}
void tri(tv a, tv b, tv c) { add_pt(a); add_pt(b); add_pt(c); }
void addRect(hyperpoint h, hyperpoint hx, hyperpoint hy, hyperpoint v, hyperpoint vx, hyperpoint vy, int ix, int iy) {
vector<vector<tv> > rps(iy+1, vector<tv> (ix+1));
for(int y=0; y<=iy; y++)
for(int x=0; x<=ix; x++) {
rps[y][x] = pt(h+hx*x/ix+hy*y/iy, v+vx*x/ix+vy*y/iy);
}
for(int y=0; y<iy; y++)
for(int x=0; x<ix; x++) {
tri(rps[y][x], rps[y+1][x], rps[y][x+1]),
tri(rps[y+1][x+1], rps[y][x+1], rps[y+1][x]);
}
}
void addTri(hyperpoint h, hyperpoint hx, hyperpoint hy, hyperpoint v, hyperpoint vx, hyperpoint vy, int i) {
vector<vector<tv> > rps(i+1, vector<tv> (i+1));
for(int y=0; y<=i; y++)
for(int x=0; x<=i; x++) {
if(x+y <= i)
rps[y][x] = pt(h+hx*x/i+hy*y/i, v+vx*x/i+vy*y/i);
}
for(int y=0; y<i; y++)
for(int x=0; x<i; x++) {
if(x+y < i)
tri(rps[y][x], rps[y+1][x], rps[y][x+1]);
if(x+y+2 <= i)
tri(rps[y+1][x+1], rps[y][x+1], rps[y+1][x]);
}
}
bool need_texture = true;
#if CAP_TEXTURE
texture::texture_data tdata; // = texture::config.data;
#endif
void make_texture() {
#if CAP_TEXTURE
if(!need_texture) return;
printf("make texture\n");
need_texture = false;
tdata.whitetexture();
int tw = tdata.twidth;
printf("tw = %d\n", tw);
int fw = tw / 4;
auto pix = [&] (int k, int x, int y) -> unsigned& {
return tdata.texture_pixels[y * tw + x + (k&3) * fw + (k>>2) * fw * tw];
};
for(int y=0; y<tw; y++)
for(int x=0; x<tw; x++)
pix(0,x,y) = rand();
for(int y=0; y<=fw; y++)
for(int x=0; x<=fw; x++) {
typedef long long ll;
pix(0,x,y) = 0xFF000000 + 0x10101 * ((x*ll(fw-x)*y*(fw-y)*255)/ll(fw/2)/(fw/2)/(fw-fw/2)/(fw-fw/2));
pix(2,x,y) = 0xFF400000 + 0x10000 * (y * 63 / fw);
pix(8,x,y) = 0xFF101010;
pix(10,x,y) = 0xFF000000 + gradient(0, 0xFFD500, 0, x*(fw-x), fw*fw/4);
pix(5,x,y) = 0xFF000000 + gradient(0, 0x804000, -1, sin(2*M_PI*8*y/fw), 1);
pix(7,x,y) = 0xFF000000 + gradient(0, 0x808080, 0, x*ll(fw-x)*y*(fw-y), ll(fw/2)*(fw/2)*(fw-fw/2)*(fw-fw/2));
}
tdata.loadTextureGL();
#endif
}
int savetex;
#if ISWEB
int prec = 1;
int maxr = 100;
#else
int prec = 4;
int maxr = 1000;
#endif
bool on;
void enable_hooks();
void reset_geom(eGeometry g) {
set_geometry(g);
start_game();
View = Id;
enable_hooks();
}
void make_staircase() {
// vid.stereo_mode = current_display->sODS;
println(hlog, "scurvature = ", scurvature, " progress = ", progress);
if(scurvature > -1e-6 && scurvature < 1e-6) {
if(WDIM == 2 || !euclid) reset_geom(gCubeTiling);
acurvature = 1;
}
else if(scurvature < 0) {
if(WDIM == 2 || !hyperbolic) reset_geom(gSpace534);
acurvature = -scurvature;
}
else {
if(WDIM == 2 || !sphere) reset_geom(gCell120);
acurvature = scurvature;
}
// rug::ruggospeed = acurvature;
vid.ipd = 0.15 * acurvature;
staircase::on = true;
if(need_texture) {
make_texture();
}
vertices.clear();
tinf.tvertices.clear();
printf("compute\n");
for(int r=-maxr; r<maxr; r++) {
if(scurvature < -1e-6 && abs(r * acurvature) > 7*12) continue;
if(scurvature > 1e-6 && abs(acurvature*r/szoom) > M_PI) continue;
global_r = r;
// step
addRect(point31(0,0,1), point30(0,0,1), point30(1,0,0), hpxy(0,0), hpxy(.25,0), hpxy(0,.25), prec, prec);
// step connection
addRect(point31(1,0,1), point30(0,0,1), point30(0,1,0), hpxy(0.75,0.25), hpxy(.25,0), hpxy(0,.25), prec, prec);
// triangle inner side
addTri(point31(1,0,1), point30(0,1,0), point30(-1,0,0), hpxy(.5,0), hpxy(.125,.125), hpxy(0,.125), prec);
// rectangle under triangle
addRect(point31(0,0,1), point30(1,1,0), point30(0,1,0), hpxy(.5,.125), hpxy(.125,0), hpxy(0,.125), prec, prec);
// barrier post
addRect(point31(.45,0,1.1), point30(.1,.1,0), point30(0,-3,0), hpxy(0,.5), hpxy(.25,0), hpxy(0,.25), 2, prec);
// barrier
addRect(point31(.45,-3,1), point30(0,0,.2), point30(1,1,0), hpxy(.5,.5), hpxy(.25,0), hpxy(0,.25), 1, prec);
// outer wall
addRect(point31(0,0,2), point30(1,1,0), point30(0,12,0), hpxy(.25,.25), hpxy(.25,0), hpxy(0,.25), prec, prec);
// lower wall
addRect(point31(0,1,1), point30(1,1,0), point30(0,0,1), hpxy(.5,0), hpxy(.25,0), hpxy(0,.25), prec, prec);
}
printf("push\n");
printf("done (%d points)\n", isize(tinf.tvertices));
}
// -0.50 .. 0.16
void showMenu() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen(0);
dialog::init(XLAT("Spiral Staircase"), iinf[itPalace].color, 150, 0);
add_edit(scurvature);
add_edit(prec);
add_edit(maxr);
dialog::addBreak(100);
dialog::addBoolItem(XLAT("low quality"), prec == 1, '1');
dialog::add_action([] { prec = 1; maxr = 100; make_staircase(); });
dialog::addBoolItem(XLAT("medium quality"), prec == 2, '2');
#if !ISWEB
dialog::addBoolItem(XLAT("high quality"), prec == 4, '3');
#endif
dialog::addItem(XLAT("take me back"), 'q');
dialog::display();
}
void draw_staircase() {
shiftmatrix Zero = ggmatrix(cwt.at);
auto &p = queuepoly(Zero, cgi.shGem[0], 0xFFFFFFFF);
p.tab = &vertices;
p.cnt = isize(vertices);
p.flags |= POLY_TRIANGLES;
p.offset = 0;
p.offset_texture = 0;
p.tinf = &tinf;
tinf.texture_id = tdata.textureid;
}
void enable_hooks() {
rv_hook(hooks_o_key, 80, [] (o_funcs& v) { v.push_back(named_dialog("staircase", showMenu)); });
rv_hook(hooks_frame, 100, draw_staircase);
}
void enable() {
enable_hooks();
make_staircase();
make_texture();
}
int phooks = arg::add3("-stair", enable)
+ addHook_rvslides(141, [] (string s, vector<tour::slide>& v) {
if(s != "mixed") return;
using namespace tour;
v.push_back(
tour::slide{"Spiral Staircase", 62, LEGAL::NONE | QUICKGEO,
"Spiral Staircase Demo.\n\n"
"In Euclidean geometry, when we look at a spiral staircase from above, "
"we see a hyperbolic spiral -- not a logarithmic spiral, as some people claim. "
"However, in hyperbolic geometry, we would see a (roughly) logarithmic spiral.\n\n"
"Press '5' to change the curvature or other parameters.",
[] (presmode mode) {
setCanvas(mode, '0');
if(mode == pmStart) staircase::make_staircase();
slidecommand = "staircase menu";
if(mode == pmKey) pushScreen(staircase::showMenu);
slide_url(mode, 'y', "YouTube link", "https://www.youtube.com/watch?v=HZNRo6mr5pk");
}}
);
})
+ addHook(hooks_configfile, 100, [] {
param_f(scurvature, "stair_curvature")
->editable(-1, 1, .05, XLAT("curvature of the space"), "", 'c')
->set_reaction(make_staircase)
->set_extra([] {
dialog::addItem("golden spiral", 'G');
dialog::add_action([] {
scurvature = -4 * log((sqrt(5)+1)/2) / 2.4;
make_staircase();
popScreen();
});
});
param_i(prec, "stair_precision")
->editable(1, 8, 1, XLAT("higher value = better"), "staircase precision", 'p')
->set_sets([] { dialog::bound_low(1); })
->set_reaction(make_staircase);
param_i(maxr, "stair_maxr")
->editable(1, 5000, 100, XLAT("higher value = more levels"), "staircase max", 'm')
->set_sets([] { dialog::bound_low(1); })
->set_reaction(make_staircase);
});
}}