1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-27 14:37:16 +00:00

ads-game:: ds-texture

This commit is contained in:
Zeno Rogue 2022-09-28 19:18:04 +02:00
parent 22a5729d35
commit ade4f9c030
4 changed files with 182 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include "display.cpp"
#include "menu.cpp"
#include "ds-game.cpp"
#include "ds-texture.cpp"
namespace hr {

View File

@ -2,6 +2,9 @@ namespace hr {
namespace ads_game {
void init_textures();
void draw_textures();
vector<ld> shape_disk;
void set_default_keys();
@ -199,6 +202,9 @@ auto future_shown = 3 * TAU;
void init_ds_game() {
init_textures();
pick_textures();
dynamicval<eGeometry> g(geometry, gSpace435);
rockgen.cshift = 0;
@ -405,6 +411,8 @@ transmatrix tpt(ld x, ld y) {
void view_ds_game() {
displayed.clear();
draw_textures();
main_rock->at.shift = current.shift;
if(1) {

167
rogueviz/ads/ds-texture.cpp Normal file
View File

@ -0,0 +1,167 @@
namespace hr {
namespace ads_game {
struct earth_texture {
string filename;
string copyright;
texture::texture_data tex;
basic_textureinfo tinf;
bool loaded;
};
vector<earth_texture> earth_textures;
struct texture_to_use {
ld from, ctr, to;
earth_texture *tx;
};
vector<texture_to_use> textures_to_use;
void init_textures() {
earth_textures.clear();
string dir = "ds-images/";
fhstream f(dir + "list.txt", "rt");
string cur_copyright = "";
while(!feof(f.f)) {
string s = scanline_noblank(f);
if(s == "") continue;
if(s[0] == '#') continue;
if(s[0] == '$') { cur_copyright = s.substr(1); continue; }
if(s.substr(0, 2) == "*/") s = dir + s.substr(2);
earth_textures.emplace_back();
auto& et = earth_textures.back();
println(hlog, "trying '", s, "'");
et.filename = s;
et.copyright = cur_copyright;
et.loaded = false;
auto& tex = et.tex;
tex.readtexture(s);
int tty = tex.theight - 2 * tex.base_y;
int u = 1;
for(int y=0; y<tex.theight; y++)
for(int x=0; x<tex.twidth; x++)
tex.get_texture_pixel(x, y) =
tex.get_texture_pixel(x, tex.base_y + u + gmod(y - tex.base_y - u, tty - u - u));
}
}
void pick_textures() {
if(earth_textures.empty()) return;
bool first = !isize(textures_to_use);
auto last = first ? 0 : textures_to_use.back().to;
if(first || current.shift > last) {
auto et = &(earth_textures[hrand(isize(earth_textures))]);
textures_to_use.emplace_back();
auto& tu = textures_to_use.back();
auto& tex = et->tex;
ld ratio = tex.tx * 1. / tex.ty;
ld length = M_PI * ratio + 6;
ld t = first ? 0 : last + lerp(1, 3, randd()) + length;
tu.ctr = t;
tu.from = t - length;
tu.to = t + length;
tu.tx = et;
}
}
ld smoothstep(ld x) {
if(x < 0) return 0;
if(x > 1) return 1;
return x * x * (3-2*x);
}
void draw_texture(texture_to_use& tu) {
auto& et = *tu.tx;
auto& tex = et.tex;
if(!et.loaded) {
et.loaded = true;
tex.loadTextureGL();
et.tinf.texture_id = tex.textureid;
}
et.tinf.tvertices.clear();
ld MWIDTH = tex.tx * .5 / tex.ty;
println(hlog, "shift = ", current.shift, " end = ", tu.to);
auto add = [&] (int x, int y) {
ld x0 = (y-(YSCALE/2.)) / YSCALE * M_PI * 4;
ld mercator_y0 = (x-(XSCALE/2.)) / (XSCALE/2.) * M_PI * MWIDTH;
ld y0 = asin(tanh(2 * mercator_y0));
ld y1 = y0 - 90 * degree;
et.tinf.tvertices.push_back(glhr::makevertex(x * 1. / XSCALE, .5 + (y-0.5) / MWIDTH / YSCALE, 0));
cross_result cr;
if(1) {
dynamicval<eGeometry> g(geometry, gSpace435);
ld s = current.shift - tu.ctr;
// We actually want to compute this, but this is not precise enough:
// cr = ds_cross0(current.T * lorentz(2, 3, -current.shift) * cspin(0, 1, x0) * cspin(0, 2, y0 - 90*degree));
// Here is what we get for current.T == Id: (computed with sympy)
hyperpoint now;
ld ts = tanh(s) * tanh(s);
ld cy = cos(y1) * cos(y1);
ld sq = sqrt(1 - cy * ts);
now[0] = sin(y1) * cos(x0) / sq;
now[1] = -sin(x0) * sin(y1) / sq;
now[2] = cos(y1) / cosh(s) / sq;
now[3] = 0;
// And here is the derivative over t (t = the local variable from ds_cross0)
hyperpoint der;
sq = -sqrt(2)*tanh(s)/(4*sqrt(-cos(2*y1)*ts - ts + 2));
der[0] = sq*(sin(x0 - 2*y1) - sin(x0 + 2*y1));
der[1] = sq*(cos(x0 - 2*y1) - cos(x0 + 2*y1));
sq = sqrt(-cy*ts + 1);
der[2] = (cy - 1)*sinh(s)/sq;
der[3] = -(cy*sinh(s)*tanh(s) - cosh(s))/sq;
cr.h = now;
// now and der give us our wordline shifted to roughly the time of interest, so we can now use ds_cross0 safely
transmatrix U = Id;
set_column(U, 2, now);
set_column(U, 3, der);
cr = ds_cross0(current.T * U);
}
curvepoint(cr.h);
};
for(int x=0; x<XSCALE; x++)
for(int y=0; y<YSCALE; y++) {
add(x, y);
add(x+1, y);
add(x, y+1);
add(x+1, y);
add(x, y+1);
add(x+1, y+1);
}
color_t col = 0xFFFFFFFF;
part(col, 0) = talpha;
part(col, 0) *= smoothstep(current.shift - tu.from);
part(col, 0) *= smoothstep(tu.to - current.shift);
auto& poly = queuecurve(shiftless(Id), 0, col, PPR::FLOOR);
poly.tinf = &et.tinf;
poly.flags |= POLY_TRIANGLES;
poly.offset_texture = 0;
}
void draw_textures() {
pick_textures();
for(auto& tu: textures_to_use)
if(current.shift > tu.from && current.shift < tu.to)
draw_texture(tu);
}
}}

View File

@ -103,4 +103,10 @@ int draw_per_frame = 1000;
ld ds_split_speed = 0.1;
int XSCALE = 64;
int YSCALE = 64;
int talpha = 32;
void init_textures();
void pick_textures();
void draw_textures();
}}