From 80ce840b3dfe3ef45c11a1c368712d1ccda6fc6d Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sun, 3 Oct 2021 10:55:07 +0200 Subject: [PATCH] add_function in raygen --- raycaster.cpp | 392 ++++++++++++++++++++++++++------------------------ 1 file changed, 203 insertions(+), 189 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index 38537245..f985fb65 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -296,13 +296,14 @@ int max_celltype = 64; struct raygen { string fsh, vsh, fmain; + void add_if(const string& seek, const string& function); + int deg, irays; bool asonov; bool use_reflect; bool many_cell_types; bool eyes; bool stepbased; - int flat1, flat2; string getM(string s) { if(m_via_texture) @@ -325,17 +326,26 @@ struct raygen { return "uWallstart[" + s + "]"; }; - void compute_which_and_dist(); - void apply_reflect(); + void compute_which_and_dist(int flat1, int flat2); + void apply_reflect(int flat1, int flat2); void move_forward(); void emit_intra_portal(int gid1, int gid2); void emit_iterate(int gid1); void create(); + + string f_xpush() { return hyperbolic ? "xpush_h" : "xpush_s"; } + string f_len() { return hyperbolic ? "len_h" : (sphere && rotspace) ? "len_sr" : sl2 ? "len_sl2" : sphere ? "len_s" : "len_x"; } + void add_functions(); }; raygen our_raygen; -void raygen::compute_which_and_dist() { +void raygen::add_if(const string& seek, const string& function) { + if(fsh.find(seek) != string::npos || fmain.find(seek) != string::npos) + fsh = function + fsh; + } + +void raygen::compute_which_and_dist(int flat1, int flat2) { using glhr::to_glsl; if(!stepbased) { @@ -411,8 +421,8 @@ void raygen::compute_which_and_dist() { fmain += "for(int i=20; i<22; i++) {\n" "mediump float sgn = i == 20 ? -1. : 1.;\n" - "mediump vec4 zpos = xpush(uBLevel*sgn) * position;\n" - "mediump vec4 ztan = xpush(uBLevel*sgn) * tangent;\n" + "mediump vec4 zpos = xpush_h(uBLevel*sgn) * position;\n" + "mediump vec4 ztan = xpush_h(uBLevel*sgn) * tangent;\n" "mediump float Mp = zpos.w - zpos.x;\n" "mediump float Mt = ztan.w - ztan.x;\n" "mediump float a = (Mp*Mp-Mt*Mt);\n" @@ -779,20 +789,19 @@ void raygen::move_forward() { bool reg = hyperbolic || sphere || euclid || sl2 || prod; if(reg) { - fsh += "mediump float len_h(vec4 h) { return 1. - h[3]; }\n"; string s = (rotspace || prod) ? "-2" : ""; fmain += - " mediump float best = len(nposition);\n" + " mediump float best = "+f_len()+"(nposition);\n" " for(int i=0; i 1.) {\n" "mediump float best = 999.;\n" "for(int i=0; i<4; i++) {\n" - "mediump float cand = len(uStraighten * " + getM("i") + " * position);\n" + "mediump float cand = "+f_len()+"(uStraighten * " + getM("i") + " * position);\n" "if(cand < best) { best = cand; which = i;}\n" "}\n" "}\n" "if(sp.z < -1.) {\n" "mediump float best = 999.;\n" "for(int i=6; i<10; i++) {\n" - "mediump float cand = len(uStraighten * " + getM("i") + " * position);\n" + "mediump float cand = "+f_len()+"(uStraighten * " + getM("i") + " * position);\n" "if(cand < best) { best = cand; which = i;}\n" "}\n" "}\n"; @@ -912,17 +921,17 @@ void raygen::move_forward() { "position = position + tangent * dist;\n"; } -void raygen::apply_reflect() { +void raygen::apply_reflect(int flat1, int flat2) { if(prod) fmain += "if(reflect && which >= sides-2) { zspeed = -zspeed; continue; }\n"; if(hyperbolic && bt::in()) fmain += "if(reflect && (which < "+its(flat1)+" || which >= "+its(flat2)+")) {\n" " mediump float x = -log(position.w - position.x);\n" - " mediump vec4 xtan = xpush(-x) * tangent;\n" + " mediump vec4 xtan = xpush_h(-x) * tangent;\n" " mediump float diag = (position.y*position.y+position.z*position.z)/2.;\n" " mediump vec4 normal = vec4(1.-diag, -position.y, -position.z, -diag);\n" " mediump float mdot = dot(xtan.xyz, normal.xyz) - xtan.w * normal.w;\n" " xtan = xtan - normal * mdot * 2.;\n" - " tangent = xpush(x) * xtan;\n" + " tangent = xpush_h(x) * xtan;\n" " continue;\n" " }\n"; if(asonov) { @@ -1109,6 +1118,20 @@ void raygen::emit_iterate(int gid1) { if(intra::in && prod) fmain += " const mediump float uPLevel = " + to_glsl(cgi.plevel/2) + ";\n"; + int flat1 = 0, flat2 = deg; + if(prod || rotspace) flat2 -= 2; + +#if CAP_BT + if(hyperbolic && bt::in()) { + if(intra::in) + fmain += "mediump float uBLevel = " + to_glsl(log(bt::expansion()) / 2) + ";\n"; + else + fsh += "uniform mediump float uBLevel;\n"; + flat1 = bt::dirs_outer(); + flat2 -= bt::dirs_inner(); + } +#endif + fmain += " mediump float dist = 100.;\n"; @@ -1121,14 +1144,14 @@ void raygen::emit_iterate(int gid1) { if(IN_ODS) fmain += " if(go == 0.) {\n" - " mediump float best = len(position);\n" + " mediump float best = "+f_len()+"(position);\n" " for(int i=0; iwall_offset(centerover); /* so raywall is not empty and deg is not zero */ @@ -1607,51 +1669,6 @@ void raygen::create() { if(many_cell_types) fsh += "uniform int uWallOffset, uSides;\n"; - flat1 = 0, flat2 = deg; - if(prod || rotspace) flat2 -= 2; - -#if CAP_BT - if(hyperbolic && bt::in()) { - fsh += "uniform mediump float uBLevel;\n"; - flat1 = bt::dirs_outer(); - flat2 -= bt::dirs_inner(); - } -#endif - - if(hyperbolic) fsh += - - "mediump mat4 xpush(float x) { return mat4(" - "cosh(x), 0., 0., sinh(x),\n" - "0., 1., 0., 0.,\n" - "0., 0., 1., 0.,\n" - "sinh(x), 0., 0., cosh(x)" - ");}\n"; - - if(sphere) fsh += - - "mediump mat4 xpush(float x) { return mat4(" - "cos(x), 0., 0., sin(x),\n" - "0., 1., 0., 0.,\n" - "0., 0., 1., 0.,\n" - "-sin(x), 0., 0., cos(x)" - ");}\n"; - - if(IN_ODS) fsh += - - "mediump mat4 xzspin(float x) { return mat4(" - "cos(x), 0., sin(x), 0.,\n" - "0., 1., 0., 0.,\n" - "-sin(x), 0., cos(x), 0.,\n" - "0., 0., 0., 1." - ");}\n" - - "mediump mat4 yzspin(float x) { return mat4(" - "1., 0., 0., 0.,\n" - "0., cos(x), sin(x), 0.,\n" - "0., -sin(x), cos(x), 0.,\n" - "0., 0., 0., 1." - ");}\n"; - if(many_cell_types) { fsh += "int walloffset, sides;\n"; } @@ -1688,7 +1705,7 @@ void raygen::create() { " mediump float eye;\n" " if(at.y < 0.) { phi = at.y + PI/2.; eye = uIPD / 2.; }\n" // right " else { phi = at.y - PI/2.; eye = -uIPD / 2.; }\n" - " mediump mat4 vw = uStart * xzspin(-lambda) * xpush(eye) * yzspin(phi);\n" + " mediump mat4 vw = uStart * xzspin(-lambda) * "+f_xpush()+"(eye) * yzspin(phi);\n" " mediump vec4 at0 = vec4(0., 0., 1., 0.);\n"; else { @@ -1738,13 +1755,6 @@ void raygen::create() { if(eyes) fmain += " at0.xyz /= uAbsUnit;\n"; } - if(hyperbolic) fsh += " mediump float len(mediump vec4 x) { return x[3]; }\n"; - else if(sphere && rotspace) fsh += " mediump float len(mediump vec4 x) { return 1.+x.x*x.x+x.y*x.y-x.z*x.z-x.w*x.w; }\n"; - else if(sl2) fsh += " mediump float len(mediump vec4 x) { return 1.+x.x*x.x+x.y*x.y; }\n"; - else if(sphere) fsh += " mediump float len(mediump vec4 x) { return 1.-x[3]; }\n"; - - else fsh += " mediump float len(mediump vec4 x) { return length(x.xyz); }\n"; - ld s = 1; #if CAP_VR if(eyes) s *= vrhr::absolute_unit_in_meters; @@ -1857,6 +1867,8 @@ void raygen::create() { fmain += " }"; } + + add_functions(); } void bind_array(vector>& v, GLint t, GLuint& tx, int id, int length) { @@ -2036,6 +2048,8 @@ struct raycast_map { if(p) c2 = p->tcw.at; } + if(intra::in && !intra::intra_id.count(c2)) intra::intra_id[c2] = intra::current; + if(rays_generate) setdist(c2, 7, c); /* if(!cl.listed(c2)) legaldir.push_back(legaldir[i] &~ (1<<((d+3)%6)) ); */