From ade4f9c030633b0af20ea0b7ad74826d10ea4c05 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Wed, 28 Sep 2022 19:18:04 +0200 Subject: [PATCH] ads-game:: ds-texture --- rogueviz/ads/ads-game.cpp | 1 + rogueviz/ads/ds-game.cpp | 8 ++ rogueviz/ads/ds-texture.cpp | 167 ++++++++++++++++++++++++++++++++++++ rogueviz/ads/globals.cpp | 6 ++ 4 files changed, 182 insertions(+) create mode 100644 rogueviz/ads/ds-texture.cpp diff --git a/rogueviz/ads/ads-game.cpp b/rogueviz/ads/ads-game.cpp index 0e77f9f7..74298dda 100644 --- a/rogueviz/ads/ads-game.cpp +++ b/rogueviz/ads/ads-game.cpp @@ -9,6 +9,7 @@ #include "display.cpp" #include "menu.cpp" #include "ds-game.cpp" +#include "ds-texture.cpp" namespace hr { diff --git a/rogueviz/ads/ds-game.cpp b/rogueviz/ads/ds-game.cpp index 4f5fcff4..526342a5 100644 --- a/rogueviz/ads/ds-game.cpp +++ b/rogueviz/ads/ds-game.cpp @@ -2,6 +2,9 @@ namespace hr { namespace ads_game { +void init_textures(); +void draw_textures(); + vector shape_disk; void set_default_keys(); @@ -199,6 +202,9 @@ auto future_shown = 3 * TAU; void init_ds_game() { + init_textures(); + pick_textures(); + dynamicval g(geometry, gSpace435); rockgen.cshift = 0; @@ -404,6 +410,8 @@ transmatrix tpt(ld x, ld y) { void view_ds_game() { displayed.clear(); + + draw_textures(); main_rock->at.shift = current.shift; diff --git a/rogueviz/ads/ds-texture.cpp b/rogueviz/ads/ds-texture.cpp new file mode 100644 index 00000000..501bafed --- /dev/null +++ b/rogueviz/ads/ds-texture.cpp @@ -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_textures; + +struct texture_to_use { + ld from, ctr, to; + earth_texture *tx; + }; + +vector 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 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 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 tu.from && current.shift < tu.to) + draw_texture(tu); + } + +}} diff --git a/rogueviz/ads/globals.cpp b/rogueviz/ads/globals.cpp index 0d8f51c1..5aa99f6f 100644 --- a/rogueviz/ads/globals.cpp +++ b/rogueviz/ads/globals.cpp @@ -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(); }}