LCOV - code coverage report
Current view: top level - engine/render - renderva.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 0.7 % 1959 14
Test Date: 2025-12-16 06:27:13 Functions: 3.2 % 126 4

            Line data    Source code
       1              : // renderva.cpp: handles the occlusion and rendering of vertex arrays
       2              : 
       3              : #include "../libprimis-headers/cube.h"
       4              : #include "../../shared/geomexts.h"
       5              : #include "../../shared/glemu.h"
       6              : #include "../../shared/glexts.h"
       7              : 
       8              : #include <memory>
       9              : #include <optional>
      10              : 
      11              : #include "csm.h"
      12              : #include "grass.h"
      13              : #include "octarender.h"
      14              : #include "radiancehints.h"
      15              : #include "rendergl.h"
      16              : #include "renderlights.h"
      17              : #include "rendermodel.h"
      18              : #include "renderva.h"
      19              : #include "renderwindow.h"
      20              : #include "rendersky.h"
      21              : #include "shaderparam.h"
      22              : #include "shader.h"
      23              : #include "texture.h"
      24              : 
      25              : #include "interface/control.h"
      26              : 
      27              : #include "world/entities.h"
      28              : #include "world/light.h"
      29              : #include "world/octaedit.h"
      30              : #include "world/octaworld.h"
      31              : #include "world/raycube.h"
      32              : #include "world/bih.h"
      33              : #include "world/world.h"
      34              : 
      35              : 
      36              : #include "model/model.h"
      37              : 
      38              : //oqfrags, outlinecolor are both extern vars
      39              : VAR(oqfrags, 0, 8, 64); //occlusion query fragments
      40            0 : CVARP(outlinecolor, 0); //color of edit mode outlines
      41              : 
      42              : float shadowradius = 0,
      43              :       shadowbias = 0;
      44              : size_t shadowside = 0;
      45              : int shadowspot = 0;
      46              : vec shadoworigin(0, 0, 0),
      47              :     shadowdir(0, 0, 0);
      48              : 
      49              : vtxarray *visibleva = nullptr;
      50              : vfc view;
      51              : 
      52              : int deferquery = 0;
      53              : 
      54              : struct shadowmesh final
      55              : {
      56              :     vec origin;
      57              :     float radius;
      58              :     vec spotloc;
      59              :     int spotangle;
      60              :     int type;
      61              :     std::array<int, 6> draws;
      62              : };
      63              : 
      64              : Occluder occlusionengine;
      65              : 
      66              : /* internally relevant functionality */
      67              : ///////////////////////////////////////
      68              : 
      69              : namespace
      70              : {
      71            0 :     void drawtris(GLsizei numindices, const GLvoid *indices, GLuint minvert, GLuint maxvert)
      72              :     {
      73            0 :         glDrawRangeElements(GL_TRIANGLES, minvert, maxvert, numindices, GL_UNSIGNED_SHORT, indices);
      74            0 :         glde++;
      75            0 :     }
      76              : 
      77            0 :     void drawvatris(const vtxarray &va, GLsizei numindices, int offset)
      78              :     {
      79            0 :         drawtris(numindices, static_cast<ushort *>(nullptr) + va.eoffset + offset, va.minvert, va.maxvert);
      80            0 :     }
      81              : 
      82            0 :     void drawvaskytris(const vtxarray &va)
      83              :     {
      84            0 :         drawtris(va.sky, static_cast<ushort *>(nullptr) + va.skyoffset, va.minvert, va.maxvert);
      85            0 :     }
      86              : 
      87              :     ///////// view frustrum culling ///////////////////////
      88              : 
      89            0 :     float vadist(const vtxarray &va, const vec &p)
      90              :     {
      91            0 :         return p.dist_to_bb(va.bbmin, va.bbmax);
      92              :     }
      93              : 
      94            0 :     void addvisibleva(vtxarray *va, std::array<vtxarray *, vasortsize> &vasort)
      95              :     {
      96            0 :         float dist = vadist(*va, camera1->o);
      97            0 :         va->distance = static_cast<int>(dist); /*cv.dist(camera1->o) - va->size*SQRT3/2*/
      98              : 
      99            0 :         int hash = std::clamp(static_cast<int>(dist*vasortsize/rootworld.mapsize()), 0, vasortsize-1);
     100            0 :         vtxarray **prev = &vasort[hash],
     101            0 :                   *cur = vasort[hash];
     102              : 
     103            0 :         while(cur && va->distance >= cur->distance)
     104              :         {
     105            0 :             prev = &cur->next;
     106            0 :             cur = cur->next;
     107              :         }
     108              : 
     109            0 :         va->next = cur;
     110            0 :         *prev = va;
     111            0 :     }
     112              : 
     113            0 :     void sortvisiblevas(const std::array<vtxarray *, vasortsize> &vasort)
     114              :     {
     115            0 :         visibleva = nullptr;
     116            0 :         vtxarray **last = &visibleva;
     117            0 :         for(vtxarray *i : vasort)
     118              :         {
     119            0 :             if(i)
     120              :             {
     121            0 :                 vtxarray *va = i;
     122            0 :                 *last = va;
     123            0 :                 while(va->next)
     124              :                 {
     125            0 :                     va = va->next;
     126              :                 }
     127            0 :                 last = &va->next;
     128              :             }
     129              :         }
     130            0 :     }
     131              : 
     132              :     template<bool fullvis, bool resetocclude>
     133            0 :     void findvisiblevas(std::vector<vtxarray *> &vas, std::array<vtxarray *, vasortsize> &vasort)
     134              :     {
     135            0 :         for(uint i = 0; i < vas.size(); i++)
     136              :         {
     137            0 :             vtxarray &v = *vas[i];
     138            0 :             int prevvfc = v.curvfc;
     139            0 :             v.curvfc = fullvis ? ViewFrustumCull_FullyVisible : view.isvisiblecube(v.o, v.size);
     140            0 :             if(v.curvfc != ViewFrustumCull_NotVisible)
     141              :             {
     142            0 :                 bool resetchildren = prevvfc >= ViewFrustumCull_NotVisible || resetocclude;
     143            0 :                 if(resetchildren)
     144              :                 {
     145            0 :                     v.occluded = !v.texs ? Occlude_Geom : Occlude_Nothing;
     146            0 :                     v.query = nullptr;
     147              :                 }
     148            0 :                 addvisibleva(&v, vasort);
     149            0 :                 if(v.children.size())
     150              :                 {
     151            0 :                     if(fullvis || v.curvfc == ViewFrustumCull_FullyVisible)
     152              :                     {
     153            0 :                         if(resetchildren)
     154              :                         {
     155            0 :                             findvisiblevas<true, true>(v.children, vasort);
     156              :                         }
     157              :                         else
     158              :                         {
     159            0 :                             findvisiblevas<true, false>(v.children, vasort);
     160              :                         }
     161              :                     }
     162            0 :                     else if(resetchildren)
     163              :                     {
     164            0 :                         findvisiblevas<false, true>(v.children, vasort);
     165              :                     }
     166              :                     else
     167              :                     {
     168            0 :                         findvisiblevas<false, false>(v.children, vasort);
     169              :                     }
     170              :                 }
     171              :             }
     172              :         }
     173            0 :     }
     174              : 
     175            0 :     void findvisiblevas()
     176              :     {
     177              :         std::array<vtxarray *, vasortsize> vasort;
     178            0 :         vasort.fill(nullptr);
     179            0 :         findvisiblevas<false, false>(varoot, vasort);
     180            0 :         sortvisiblevas(vasort);
     181            0 :     }
     182              : 
     183              :     ///////// occlusion queries /////////////
     184              : 
     185            0 :     VARF(oqany, 0, 0, 2, occlusionengine.clearqueries()); //occlusion query settings: 0: GL_SAMPLES_PASSED, 1: GL_ANY_SAMPLES_PASSED, 2: GL_ANY_SAMPLES_PASSED_CONSERVATIVE
     186              :     VAR(oqwait, 0, 1, 1);
     187              : 
     188              :     /**
     189              :      * @brief Returns query target
     190              :      *
     191              :      * GL_SAMPLES_PASSED if oqany is 0
     192              :      * GL_ANY_SAMPLES_PASSED if oqany is 1 or ES3 is not present
     193              :      * GL_ANY_SAMPLES_PASSED if oqany is 2 and ES3 is present
     194              :      */
     195            0 :     GLenum querytarget()
     196              :     {
     197            0 :         return oqany ?
     198            0 :             (oqany > 1 && hasES3 ?
     199              :                 GL_ANY_SAMPLES_PASSED_CONSERVATIVE :
     200              :                 GL_ANY_SAMPLES_PASSED) :
     201            0 :              GL_SAMPLES_PASSED;
     202              :     }
     203              : 
     204              :     GLuint bbvbo = 0,
     205              :            bbebo = 0;
     206              : 
     207            0 :     void setupbb()
     208              :     {
     209            0 :         if(!bbvbo)
     210              :         {
     211            0 :             glGenBuffers(1, &bbvbo);
     212            0 :             gle::bindvbo(bbvbo);
     213            0 :             vec verts[8];
     214            0 :             for(int i = 0; i < 8; ++i)
     215              :             {
     216            0 :                 verts[i] = vec(i&1, (i>>1)&1, (i>>2)&1);
     217              :             }
     218            0 :             glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
     219            0 :             gle::clearvbo();
     220              :         }
     221            0 :         if(!bbebo)
     222              :         {
     223            0 :             glGenBuffers(1, &bbebo);
     224            0 :             gle::bindebo(bbebo);
     225              :             std::array<ushort, 3*2*6> tris;
     226              :             //======================================== GENFACEVERT GENFACEORIENT
     227              :             #define GENFACEORIENT(orient, v0, v1, v2, v3) do { \
     228              :                 int offset = orient*3*2; \
     229              :                 tris[offset + 0] = v0; \
     230              :                 tris[offset + 1] = v1; \
     231              :                 tris[offset + 2] = v2; \
     232              :                 tris[offset + 3] = v0; \
     233              :                 tris[offset + 4] = v2; \
     234              :                 tris[offset + 5] = v3; \
     235              :             } while(0);
     236              :             #define GENFACEVERT(orient, vert, ox,oy,oz, rx,ry,rz) (ox | oy | oz)
     237            0 :             GENFACEVERTS(0, 1, 0, 2, 0, 4, , , , , , )
     238              :             #undef GENFACEORIENT
     239              :             #undef GENFACEVERT
     240              :             //==================================================================
     241            0 :             glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ushort)*tris.size(), tris.data(), GL_STATIC_DRAW);
     242            0 :             gle::clearebo();
     243              :         }
     244            0 :     }
     245              : 
     246            0 :     void cleanupbb()
     247              :     {
     248            0 :         if(bbvbo)
     249              :         {
     250            0 :             glDeleteBuffers(1, &bbvbo);
     251            0 :             bbvbo = 0;
     252              :         }
     253            0 :         if(bbebo)
     254              :         {
     255            0 :             glDeleteBuffers(1, &bbebo);
     256            0 :             bbebo = 0;
     257              :         }
     258            0 :     }
     259              : 
     260              :     octaentities *visiblemms,
     261              :                 **lastvisiblemms;
     262              : 
     263            0 :     void findvisiblemms(const std::vector<extentity *> &ents, bool doquery)
     264              :     {
     265            0 :         visiblemms = nullptr;
     266            0 :         lastvisiblemms = &visiblemms;
     267            0 :         for(vtxarray *va = visibleva; va; va = va->next)
     268              :         {
     269            0 :             if(va->occluded < Occlude_BB && va->curvfc < ViewFrustumCull_Fogged)
     270              :             {
     271            0 :                 for(octaentities *oe : va->mapmodels)
     272              :                 {
     273            0 :                     if(view.isfoggedcube(oe->o, oe->size))
     274              :                     {
     275            0 :                         continue;
     276              :                     }
     277            0 :                     bool occluded = doquery && oe->query && oe->query->owner == oe && occlusionengine.checkquery(oe->query);
     278            0 :                     if(occluded)
     279              :                     {
     280            0 :                         oe->distance = -1;
     281            0 :                         oe->next = nullptr;
     282            0 :                         *lastvisiblemms = oe;
     283            0 :                         lastvisiblemms = &oe->next;
     284              :                     }
     285              :                     else
     286              :                     {
     287            0 :                         int visible = 0;
     288            0 :                         for(const int &i : oe->mapmodels)
     289              :                         {
     290            0 :                             extentity &e = *ents[i];
     291            0 :                             if(e.flags&EntFlag_NoVis)
     292              :                             {
     293            0 :                                 continue;
     294              :                             }
     295            0 :                             e.flags |= EntFlag_Render;
     296            0 :                             ++visible;
     297              :                         }
     298            0 :                         if(!visible)
     299              :                         {
     300            0 :                             continue;
     301              :                         }
     302            0 :                         oe->distance = static_cast<int>(camera1->o.dist_to_bb(oe->o, ivec(oe->o).add(oe->size)));
     303              : 
     304            0 :                         octaentities **prev = &visiblemms;
     305            0 :                         octaentities *cur = visiblemms;
     306            0 :                         while(cur && cur->distance >= 0 && oe->distance > cur->distance)
     307              :                         {
     308            0 :                             prev = &cur->next;
     309            0 :                             cur = cur->next;
     310              :                         }
     311              : 
     312            0 :                         if(*prev == nullptr)
     313              :                         {
     314            0 :                             lastvisiblemms = &oe->next;
     315              :                         }
     316            0 :                         oe->next = *prev;
     317            0 :                         *prev = oe;
     318              :                     }
     319              :                 }
     320              :             }
     321              :         }
     322            0 :     }
     323              : 
     324              :     VAR(oqmm, 0, 4, 8); //`o`cclusion `q`uery `m`ap `m`odel
     325              : 
     326            0 :     void rendermapmodel(const extentity &e)
     327              :     {
     328            0 :         int anim = +Anim_Mapmodel | +Anim_Loop, //unary plus to promote to an integer, c++20 deprecates arithmetic conversion on enums (see C++ document P2864R2)
     329            0 :             basetime = 0;
     330            0 :         rendermapmodel(e.attr1, anim, e.o, e.attr2, e.attr3, e.attr4, Model_CullVFC | Model_CullDist, basetime, e.attr5 > 0 ? e.attr5/100.0f : 1.0f);
     331            0 :     }
     332              : 
     333            0 :     bool bbinsideva(const ivec &bo, const ivec &br, const vtxarray &va)
     334              :     {
     335            0 :         return bo.x >= va.bbmin.x && bo.y >= va.bbmin.y && bo.z >= va.bbmin.z &&
     336            0 :             br.x <= va.bbmax.x && br.y <= va.bbmax.y && br.z <= va.bbmax.z;
     337              :     }
     338              : 
     339            0 :     bool bboccluded(const ivec &bo, const ivec &br, const std::array<cube, 8> &c, const ivec &o, int size)
     340              :     {
     341            0 :         LOOP_OCTA_BOX(o, size, bo, br)
     342              :         {
     343            0 :             ivec co(i, o, size);
     344            0 :             if(c[i].ext && c[i].ext->va)
     345              :             {
     346            0 :                 vtxarray *va = c[i].ext->va;
     347            0 :                 if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
     348              :                 {
     349            0 :                     continue;
     350              :                 }
     351              :             }
     352            0 :             if(c[i].children && bboccluded(bo, br, *(c[i].children), co, size>>1))
     353              :             {
     354            0 :                 continue;
     355              :             }
     356            0 :             return false;
     357              :         }
     358            0 :         return true;
     359              :     }
     360              : 
     361              :     VAR(dtoutline, 0, 1, 1); //`d`epth `t`est `outline`s
     362              : 
     363            0 :     int calcbbsidemask(const ivec &bbmin, const ivec &bbmax, const vec &lightpos, float lightradius, float bias)
     364              :     {
     365            0 :         vec pmin = vec(bbmin).sub(lightpos).div(lightradius),
     366            0 :             pmax = vec(bbmax).sub(lightpos).div(lightradius);
     367            0 :         int mask = 0x3F;
     368            0 :         float dp1 = pmax.x + pmax.y,
     369            0 :               dn1 = pmax.x - pmin.y,
     370            0 :               ap1 = std::fabs(dp1),
     371            0 :               an1 = std::fabs(dn1),
     372            0 :               dp2 = pmin.x + pmin.y,
     373            0 :               dn2 = pmin.x - pmax.y,
     374            0 :               ap2 = std::fabs(dp2),
     375            0 :               an2 = std::fabs(dn2);
     376            0 :         if(ap1 > bias*an1 && ap2 > bias*an2)
     377              :         {
     378            0 :             mask &= (3<<4)
     379            0 :                 | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
     380            0 :                 | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
     381              :         }
     382            0 :         if(an1 > bias*ap1 && an2 > bias*ap2)
     383              :         {
     384            0 :             mask &= (3<<4)
     385            0 :                 | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
     386            0 :                 | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
     387              :         }
     388            0 :         dp1 = pmax.y + pmax.z,
     389            0 :         dn1 = pmax.y - pmin.z,
     390            0 :         ap1 = std::fabs(dp1),
     391            0 :         an1 = std::fabs(dn1),
     392            0 :         dp2 = pmin.y + pmin.z,
     393            0 :         dn2 = pmin.y - pmax.z,
     394            0 :         ap2 = std::fabs(dp2),
     395            0 :         an2 = std::fabs(dn2);
     396            0 :         if(ap1 > bias*an1 && ap2 > bias*an2)
     397              :         {
     398            0 :             mask &= (3<<0)
     399            0 :                 | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
     400            0 :                 | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
     401              :         }
     402            0 :         if(an1 > bias*ap1 && an2 > bias*ap2)
     403              :         {
     404            0 :             mask &= (3<<0)
     405            0 :                 | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
     406            0 :                 | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
     407              :         }
     408            0 :         dp1 = pmax.z + pmax.x,
     409            0 :         dn1 = pmax.z - pmin.x,
     410            0 :         ap1 = std::fabs(dp1),
     411            0 :         an1 = std::fabs(dn1),
     412            0 :         dp2 = pmin.z + pmin.x,
     413            0 :         dn2 = pmin.z - pmax.x,
     414            0 :         ap2 = std::fabs(dp2),
     415            0 :         an2 = std::fabs(dn2);
     416            0 :         if(ap1 > bias*an1 && ap2 > bias*an2)
     417              :         {
     418            0 :             mask &= (3<<2)
     419            0 :                 | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
     420            0 :                 | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
     421              :         }
     422            0 :         if(an1 > bias*ap1 && an2 > bias*ap2)
     423              :         {
     424            0 :             mask &= (3<<2)
     425            0 :                 | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
     426            0 :                 | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
     427              :         }
     428            0 :         return mask;
     429              :     }
     430              : 
     431              :     VAR(smbbcull, 0, 1, 1);
     432              :     VAR(smdistcull, 0, 1, 1);
     433              :     VAR(smnodraw, 0, 0, 1);
     434              : 
     435              :     vtxarray *shadowva = nullptr;
     436              : 
     437            0 :     void addshadowva(vtxarray * const va, float dist, std::array<vtxarray *, vasortsize> &vasort)
     438              :     {
     439            0 :         va->rdistance = static_cast<int>(dist);
     440              : 
     441            0 :         int hash = std::clamp(static_cast<int>(dist*vasortsize/shadowradius), 0, vasortsize-1);
     442            0 :         vtxarray **prev = &vasort[hash],
     443            0 :                    *cur = vasort[hash];
     444              : 
     445            0 :         while(cur && va->rdistance > cur->rdistance)
     446              :         {
     447            0 :             prev = &cur->rnext;
     448            0 :             cur = cur->rnext;
     449              :         }
     450              : 
     451            0 :         va->rnext = cur;
     452            0 :         *prev = va;
     453            0 :     }
     454              : 
     455            0 :     void sortshadowvas(std::array<vtxarray *, vasortsize> &vasort)
     456              :     {
     457            0 :         shadowva = nullptr;
     458            0 :         vtxarray **last = &shadowva;
     459            0 :         for(vtxarray *i : vasort)
     460              :         {
     461            0 :             if(i)
     462              :             {
     463            0 :                 vtxarray *va = i;
     464            0 :                 *last = va;
     465            0 :                 while(va->rnext)
     466              :                 {
     467            0 :                     va = va->rnext;
     468              :                 }
     469            0 :                 last = &va->rnext;
     470              :             }
     471              :         }
     472            0 :     }
     473              : 
     474              :     octaentities *shadowmms = nullptr;
     475              : 
     476              :     struct geombatch final
     477              :     {
     478              :         const ElementSet &es;
     479              :         VSlot &vslot;
     480              :         int offset;
     481              :         const vtxarray * const va;
     482              :         int next, batch;
     483              : 
     484            0 :         geombatch(const ElementSet &es, int offset, const vtxarray *va)
     485            0 :           : es(es), vslot(lookupvslot(es.texture)), offset(offset), va(va),
     486            0 :             next(-1), batch(-1)
     487            0 :         {}
     488              : 
     489              :         void renderbatch() const;
     490              : 
     491            0 :         int compare(const geombatch &b) const
     492              :         {
     493            0 :             if(va->vbuf < b.va->vbuf)
     494              :             {
     495            0 :                 return -1;
     496              :             }
     497            0 :             if(va->vbuf > b.va->vbuf)
     498              :             {
     499            0 :                 return 1;
     500              :             }
     501            0 :             if(es.attrs.layer&BlendLayer_Bottom)
     502              :             {
     503            0 :                 if(!(b.es.attrs.layer&BlendLayer_Bottom))
     504              :                 {
     505            0 :                     return 1;
     506              :                 }
     507            0 :                 int x1 = va->o.x&~0xFFF,
     508            0 :                     x2 = b.va->o.x&~0xFFF;
     509            0 :                 if(x1 < x2)
     510              :                 {
     511            0 :                     return -1;
     512              :                 }
     513            0 :                 if(x1 > x2)
     514              :                 {
     515            0 :                     return 1;
     516              :                 }
     517            0 :                 int y1 = va->o.y&~0xFFF,
     518            0 :                     y2 = b.va->o.y&~0xFFF;
     519            0 :                 if(y1 < y2)
     520              :                 {
     521            0 :                     return -1;
     522              :                 }
     523            0 :                 if(y1 > y2)
     524              :                 {
     525            0 :                     return 1;
     526              :                 }
     527              :             }
     528            0 :             else if(b.es.attrs.layer&BlendLayer_Bottom)
     529              :             {
     530            0 :                 return -1;
     531              :             }
     532            0 :             if(vslot.slot->shader < b.vslot.slot->shader)
     533              :             {
     534            0 :                 return -1;
     535              :             }
     536            0 :             if(vslot.slot->shader > b.vslot.slot->shader)
     537              :             {
     538            0 :                 return 1;
     539              :             }
     540            0 :             if(es.texture < b.es.texture)
     541              :             {
     542            0 :                 return -1;
     543              :             }
     544            0 :             if(es.texture > b.es.texture)
     545              :             {
     546            0 :                 return 1;
     547              :             }
     548            0 :             if(vslot.slot->params.size() < b.vslot.slot->params.size())
     549              :             {
     550            0 :                 return -1;
     551              :             }
     552            0 :             if(vslot.slot->params.size() > b.vslot.slot->params.size())
     553              :             {
     554            0 :                 return 1;
     555              :             }
     556            0 :             if(es.attrs.orient < b.es.attrs.orient)
     557              :             {
     558            0 :                 return -1;
     559              :             }
     560            0 :             if(es.attrs.orient > b.es.attrs.orient)
     561              :             {
     562            0 :                 return 1;
     563              :             }
     564            0 :             return 0;
     565              :         }
     566              :     };
     567              : 
     568              :     class RenderState final
     569              :     {
     570              :         public:
     571              :             bool colormask, depthmask;
     572              :             int alphaing;
     573              :             GLuint vbuf;
     574              :             bool vattribs, vquery;
     575              :             int globals;
     576              : 
     577              :             void disablevquery();
     578              :             void disablevbuf();
     579              :             void enablevquery();
     580              :             void cleanupgeom();
     581              :             void enablevattribs(bool all = true);
     582              :             void disablevattribs(bool all = true);
     583              :             void renderbatches(int pass);
     584              :             void renderzpass(const vtxarray &va);
     585              :             void invalidatetexgenorient();
     586              :             void invalidatealphascale();
     587              :             void cleartexgenmillis();
     588              : 
     589            0 :             RenderState() : colormask(true), depthmask(true), alphaing(0), vbuf(0), vattribs(false),
     590            0 :                             vquery(false), globals(-1), alphascale(0), texgenorient(-1),
     591            0 :                             texgenmillis(lastmillis), tmu(-1), colorscale(1, 1, 1),
     592            0 :                             vslot(nullptr), texgenslot(nullptr), texgenvslot(nullptr),
     593            0 :                             texgenscroll(0, 0), refractscale(0), refractcolor(1, 1, 1)
     594              :             {
     595            0 :                 for(int k = 0; k < 7; ++k)
     596              :                 {
     597            0 :                     textures[k] = 0;
     598              :                 }
     599            0 :             }
     600              :         private:
     601              : 
     602              :             float alphascale;
     603              :             int texgenorient, texgenmillis;
     604              :             int tmu;
     605              :             std::array<GLuint, 7> textures;
     606              :             vec colorscale;
     607              :             const VSlot *vslot;
     608              :             const Slot *texgenslot;
     609              :             const VSlot *texgenvslot;
     610              :             vec2 texgenscroll;
     611              :             float refractscale;
     612              :             vec refractcolor;
     613              : 
     614              :             void changetexgen(int orient, Slot &slot, VSlot &vslot);
     615              :             void changebatchtmus();
     616              :             void changeslottmus(int pass, Slot &newslot, VSlot &newvslot);
     617              :             void bindslottex(int type, const Texture *tex, GLenum target = GL_TEXTURE_2D);
     618              :             void changeshader(int pass, const geombatch &b);
     619              : 
     620              :     };
     621              : 
     622            0 :     void RenderState::invalidatetexgenorient()
     623              :     {
     624            0 :         texgenorient = -1;
     625            0 :     }
     626              : 
     627            0 :     void RenderState::invalidatealphascale()
     628              :     {
     629            0 :         alphascale = -1;
     630            0 :     }
     631              : 
     632            0 :     void RenderState::cleartexgenmillis()
     633              :     {
     634            0 :         texgenmillis = 0;
     635            0 :     }
     636              : 
     637            0 :     void RenderState::disablevbuf()
     638              :     {
     639            0 :         gle::clearvbo();
     640            0 :         gle::clearebo();
     641            0 :         vbuf = 0;
     642            0 :     }
     643              : 
     644            0 :     void RenderState::enablevquery()
     645              :     {
     646            0 :         if(colormask)
     647              :         {
     648            0 :             colormask = false;
     649            0 :             glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
     650              :         }
     651            0 :         if(depthmask)
     652              :         {
     653            0 :             depthmask = false;
     654            0 :             glDepthMask(GL_FALSE);
     655              :         }
     656            0 :         startbb(false);
     657            0 :         vquery = true;
     658            0 :     }
     659              : 
     660            0 :     void RenderState::disablevquery()
     661              :     {
     662            0 :         endbb(false);
     663            0 :         vquery = false;
     664            0 :     }
     665              : 
     666            0 :     void renderquery(RenderState &cur, const occludequery &query, const vtxarray &va, bool full = true)
     667              :     {
     668            0 :         if(!cur.vquery)
     669              :         {
     670            0 :             cur.enablevquery();
     671              :         }
     672            0 :         query.startquery();
     673            0 :         if(full)
     674              :         {
     675            0 :             drawbb(ivec(va.bbmin).sub(1), ivec(va.bbmax).sub(va.bbmin).add(2));
     676              :         }
     677              :         else
     678              :         {
     679            0 :             drawbb(va.geommin, ivec(va.geommax).sub(va.geommin));
     680              :         }
     681            0 :         occlusionengine.endquery();
     682            0 :     }
     683              : 
     684              :     enum RenderPass
     685              :     {
     686              :         RenderPass_GBuffer = 0,
     687              :         RenderPass_Z,
     688              :         RenderPass_Caustics,
     689              :         RenderPass_GBufferBlend,
     690              :         RenderPass_ReflectiveShadowMap,
     691              :         RenderPass_ReflectiveShadowMapBlend
     692              :     };
     693              : 
     694              :     std::vector<geombatch> geombatches;
     695              :     int firstbatch = -1,
     696              :         numbatches = 0;
     697              : 
     698            0 :     void mergetexs(const RenderState &cur, const vtxarray &va, ElementSet *texs = nullptr, int offset = 0)
     699              :     {
     700            0 :         int numtexs = 0;
     701            0 :         if(!texs)
     702              :         {
     703            0 :             texs = va.texelems;
     704            0 :             numtexs = va.texs;
     705            0 :             if(cur.alphaing)
     706              :             {
     707            0 :                 texs += va.texs;
     708            0 :                 offset += 3*(va.tris);
     709            0 :                 numtexs = va.alphaback;
     710            0 :                 if(cur.alphaing > 1)
     711              :                 {
     712            0 :                     numtexs += va.alphafront + va.refract;
     713              :                 }
     714              :             }
     715              :         }
     716              : 
     717            0 :         if(firstbatch < 0)
     718              :         {
     719            0 :             firstbatch = geombatches.size();
     720            0 :             numbatches = numtexs;
     721            0 :             for(int i = 0; i < numtexs-1; ++i)
     722              :             {
     723            0 :                 geombatches.emplace_back(texs[i], offset, &va);
     724            0 :                 geombatches.back().next = i+1;
     725            0 :                 offset += texs[i].length;
     726              :             }
     727            0 :             geombatches.emplace_back(texs[numtexs-1], offset, &va);
     728            0 :             return;
     729              :         }
     730              : 
     731            0 :         int prevbatch = -1,
     732            0 :             curbatch = firstbatch,
     733            0 :             curtex = 0;
     734              :         do
     735              :         {
     736            0 :             geombatches.emplace_back(texs[curtex], offset, &va);
     737            0 :             geombatch &b = geombatches.back();
     738            0 :             offset += texs[curtex].length;
     739            0 :             int dir = -1;
     740            0 :             while(curbatch >= 0)
     741              :             {
     742            0 :                 dir = b.compare(geombatches[curbatch]);
     743            0 :                 if(dir <= 0)
     744              :                 {
     745            0 :                     break;
     746              :                 }
     747            0 :                 prevbatch = curbatch;
     748            0 :                 curbatch = geombatches[curbatch].next;
     749              :             }
     750            0 :             if(!dir)
     751              :             {
     752            0 :                 int last = curbatch, next;
     753              :                 for(;;)
     754              :                 {
     755            0 :                     next = geombatches[last].batch;
     756            0 :                     if(next < 0)
     757              :                     {
     758            0 :                         break;
     759              :                     }
     760            0 :                     last = next;
     761              :                 }
     762            0 :                 if(last==curbatch)
     763              :                 {
     764            0 :                     b.batch = curbatch;
     765            0 :                     b.next = geombatches[curbatch].next;
     766            0 :                     if(prevbatch < 0)
     767              :                     {
     768            0 :                         firstbatch = geombatches.size()-1;
     769              :                     }
     770              :                     else
     771              :                     {
     772            0 :                         geombatches[prevbatch].next = geombatches.size()-1;
     773              :                     }
     774            0 :                     curbatch = geombatches.size()-1;
     775              :                 }
     776              :                 else
     777              :                 {
     778            0 :                     b.batch = next;
     779            0 :                     geombatches[last].batch = geombatches.size()-1;
     780              :                 }
     781              :             }
     782              :             else
     783              :             {
     784            0 :                 numbatches++;
     785            0 :                 b.next = curbatch;
     786            0 :                 if(prevbatch < 0)
     787              :                 {
     788            0 :                     firstbatch = geombatches.size()-1;
     789              :                 }
     790              :                 else
     791              :                 {
     792            0 :                     geombatches[prevbatch].next = geombatches.size()-1;
     793              :                 }
     794            0 :                 prevbatch = geombatches.size()-1;
     795              :             }
     796            0 :         } while(++curtex < numtexs);
     797              :     }
     798              : 
     799            0 :     void RenderState::enablevattribs(bool all)
     800              :     {
     801            0 :         gle::enablevertex();
     802            0 :         if(all)
     803              :         {
     804            0 :             gle::enabletexcoord0();
     805            0 :             gle::enablenormal();
     806            0 :             gle::enabletangent();
     807              :         }
     808            0 :         vattribs = true;
     809            0 :     }
     810              : 
     811            0 :     void RenderState::disablevattribs(bool all)
     812              :     {
     813            0 :         gle::disablevertex();
     814            0 :         if(all)
     815              :         {
     816            0 :             gle::disabletexcoord0();
     817            0 :             gle::disablenormal();
     818            0 :             gle::disabletangent();
     819              :         }
     820            0 :         vattribs = false;
     821            0 :     }
     822              : 
     823            0 :     void changevbuf(RenderState &cur, int pass, const vtxarray &va)
     824              :     {
     825            0 :         gle::bindvbo(va.vbuf);
     826            0 :         gle::bindebo(va.ebuf);
     827            0 :         cur.vbuf = va.vbuf;
     828              : 
     829            0 :         vertex *vdata = nullptr;
     830            0 :         gle::vertexpointer(sizeof(vertex), vdata->pos.data());
     831            0 :         gle::vertexpointer(sizeof(vertex), vdata->pos.data());
     832              : 
     833            0 :         if(pass==RenderPass_GBuffer || pass==RenderPass_ReflectiveShadowMap)
     834              :         {
     835            0 :             gle::normalpointer(sizeof(vertex), vdata->norm.data(), GL_BYTE);
     836            0 :             gle::texcoord0pointer(sizeof(vertex), vdata->tc.data());
     837            0 :             gle::tangentpointer(sizeof(vertex), vdata->tangent.data(), GL_BYTE);
     838              :         }
     839            0 :     }
     840              : 
     841            0 :     void RenderState::changebatchtmus()
     842              :     {
     843            0 :         if(tmu != 0)
     844              :         {
     845            0 :             tmu = 0;
     846            0 :             glActiveTexture(GL_TEXTURE0);
     847              :         }
     848            0 :     }
     849              : 
     850            0 :     void RenderState::bindslottex(int type, const Texture *tex, GLenum target)
     851              :     {
     852            0 :         if(textures[type] != tex->id)
     853              :         {
     854            0 :             if(tmu != type)
     855              :             {
     856            0 :                 tmu = type;
     857            0 :                 glActiveTexture(GL_TEXTURE0 + type);
     858              :             }
     859            0 :             glBindTexture(target, textures[type] = tex->id);
     860              :         }
     861            0 :     }
     862              : 
     863            0 :     void RenderState::changeslottmus(int pass, Slot &newslot, VSlot &newvslot)
     864              :     {
     865            0 :         Texture *diffuse = newslot.sts.empty() ? notexture : newslot.sts[0].t;
     866            0 :         if(pass==RenderPass_GBuffer || pass==RenderPass_ReflectiveShadowMap)
     867              :         {
     868            0 :             bindslottex(Tex_Diffuse, diffuse);
     869              : 
     870            0 :             if(pass == RenderPass_GBuffer)
     871              :             {
     872            0 :                 if(msaasamples)
     873              :                 {
     874            0 :                     GLOBALPARAMF(hashid, newvslot.index);
     875              :                 }
     876            0 :                 if(newslot.shader->type & Shader_Triplanar)
     877              :                 {
     878            0 :                     float scale = defaulttexscale/newvslot.scale;
     879            0 :                     GLOBALPARAMF(texgenscale, scale/diffuse->xs, scale/diffuse->ys);
     880              :                 }
     881              :             }
     882              :         }
     883              : 
     884            0 :         if(alphaing)
     885              :         {
     886            0 :             float alpha = alphaing > 1 ? newvslot.alphafront : newvslot.alphaback;
     887            0 :             if(alphascale != alpha)
     888              :             {
     889            0 :                 alphascale = alpha;
     890            0 :                 refractscale = 0;
     891            0 :                 goto changecolorparams; //also run next if statement
     892              :             }
     893            0 :             if(colorscale != newvslot.colorscale)
     894              :             {
     895            0 :             changecolorparams:
     896            0 :                 colorscale = newvslot.colorscale;
     897            0 :                 GLOBALPARAMF(colorparams,
     898              :                              alpha*newvslot.colorscale.x,
     899              :                              alpha*newvslot.colorscale.y,
     900              :                              alpha*newvslot.colorscale.z,
     901              :                              alpha);
     902              :             }
     903            0 :             if(alphaing > 1 && newvslot.refractscale > 0 &&
     904            0 :                   (refractscale != newvslot.refractscale || refractcolor != newvslot.refractcolor))
     905              :             {
     906            0 :                 refractscale = newvslot.refractscale;
     907            0 :                 refractcolor = newvslot.refractcolor;
     908            0 :                 float refractscale = 0.5f/ldrscale*(1-alpha);
     909            0 :                 GLOBALPARAMF(refractparams,
     910              :                              newvslot.refractcolor.x*refractscale,
     911              :                              newvslot.refractcolor.y*refractscale,
     912              :                              newvslot.refractcolor.z*refractscale,
     913              :                              newvslot.refractscale*viewh);
     914              :             }
     915              :         }
     916            0 :         else if(colorscale != newvslot.colorscale)
     917              :         {
     918            0 :             colorscale = newvslot.colorscale;
     919            0 :             GLOBALPARAMF(colorparams, newvslot.colorscale.x, newvslot.colorscale.y, newvslot.colorscale.z, 1);
     920              :         }
     921              : 
     922            0 :         for(const Slot::Tex &t : newslot.sts)
     923              :         {
     924            0 :             switch(t.type)
     925              :             {
     926            0 :                 case Tex_Normal:
     927              :                 case Tex_Glow:
     928              :                 {
     929            0 :                     bindslottex(t.type, t.t);
     930            0 :                     break;
     931              :                 }
     932              :             }
     933              :         }
     934            0 :         GLOBALPARAM(rotate, vec(newvslot.angle.y, newvslot.angle.z, diffuse->ratio()));
     935            0 :         if(tmu != 0)
     936              :         {
     937            0 :             tmu = 0;
     938            0 :             glActiveTexture(GL_TEXTURE0);
     939              :         }
     940            0 :         vslot = &newvslot;
     941            0 :     }
     942              : 
     943            0 :     void RenderState::changetexgen(int orient, Slot &slot, VSlot &vslot)
     944              :     {
     945            0 :         if(texgenslot != &slot || texgenvslot != &vslot)
     946              :         {
     947            0 :             const Texture *curtex = !texgenslot || texgenslot->sts.empty() ? notexture : texgenslot->sts[0].t;
     948            0 :             const Texture *tex = slot.sts.empty() ? notexture : slot.sts[0].t;
     949            0 :             if(!texgenvslot || slot.sts.empty() ||
     950            0 :                 (curtex->xs != tex->xs || curtex->ys != tex->ys ||
     951            0 :                  texgenvslot->rotation != vslot.rotation || texgenvslot->scale != vslot.scale ||
     952            0 :                  texgenvslot->offset != vslot.offset || texgenvslot->scroll != vslot.scroll) ||
     953            0 :                  texgenvslot->angle != vslot.angle)
     954              :             {
     955            0 :                 const TexRotation &r = texrotations[vslot.rotation];
     956            0 :                 float xs = r.flipx ? -tex->xs : tex->xs,
     957            0 :                       ys = r.flipy ? -tex->ys : tex->ys;
     958            0 :                 vec2 scroll(vslot.scroll);
     959            0 :                 if(r.swapxy)
     960              :                 {
     961            0 :                     std::swap(scroll.x, scroll.y);
     962              :                 }
     963            0 :                 scroll.x *= texgenmillis*tex->xs/xs;
     964            0 :                 scroll.y *= texgenmillis*tex->ys/ys;
     965            0 :                 if(texgenscroll != scroll)
     966              :                 {
     967            0 :                     texgenscroll = scroll;
     968            0 :                     texgenorient = -1;
     969              :                 }
     970              :             }
     971            0 :             texgenslot = &slot;
     972            0 :             texgenvslot = &vslot;
     973              :         }
     974              : 
     975            0 :         if(texgenorient == orient)
     976              :         {
     977            0 :             return;
     978              :         }
     979            0 :         GLOBALPARAM(texgenscroll, texgenscroll);
     980              : 
     981            0 :         texgenorient = orient;
     982              :     }
     983              : 
     984            0 :     void RenderState::changeshader(int pass, const geombatch &b)
     985              :     {
     986            0 :         VSlot &vslot = b.vslot;
     987            0 :         Slot &slot = *vslot.slot;
     988            0 :         if(pass == RenderPass_ReflectiveShadowMap)
     989              :         {
     990            0 :             if(b.es.attrs.layer&BlendLayer_Bottom)
     991              :             {
     992            0 :                 rsmworldshader->setvariant(0, 0, slot, vslot);
     993              :             }
     994              :             else
     995              :             {
     996            0 :                 rsmworldshader->set(slot, vslot);
     997              :             }
     998              :         }
     999            0 :         else if(alphaing)
    1000              :         {
    1001            0 :             slot.shader->setvariant(alphaing > 1 && vslot.refractscale > 0 ? 1 : 0, 1, slot, vslot);
    1002              :         }
    1003            0 :         else if(b.es.attrs.layer&BlendLayer_Bottom)
    1004              :         {
    1005            0 :             slot.shader->setvariant(0, 0, slot, vslot);
    1006              :         }
    1007              :         else
    1008              :         {
    1009            0 :             slot.shader->set(slot, vslot);
    1010              :         }
    1011            0 :         globals = GlobalShaderParamState::nextversion;
    1012            0 :     }
    1013              : 
    1014              :     template<class T>
    1015            0 :     void updateshader(T &cur)
    1016              :     {
    1017            0 :         if(cur.globals != GlobalShaderParamState::nextversion)
    1018              :         {
    1019            0 :             if(Shader::lastshader)
    1020              :             {
    1021            0 :                 Shader::lastshader->flushparams();
    1022              :             }
    1023            0 :             cur.globals = GlobalShaderParamState::nextversion;
    1024              :         }
    1025            0 :     }
    1026              : 
    1027            0 :     void geombatch::renderbatch() const
    1028              :     {
    1029            0 :         gbatches++;
    1030            0 :         for(const geombatch *curbatch = this;; curbatch = &geombatches[curbatch->batch])
    1031              :         {
    1032            0 :             ushort len = curbatch->es.length;
    1033            0 :             if(len)
    1034              :             {
    1035            0 :                 drawtris(len, static_cast<ushort *>(nullptr) + curbatch->va->eoffset + curbatch->offset, curbatch->es.minvert, curbatch->es.maxvert);
    1036            0 :                 vtris += len/3;
    1037              :             }
    1038            0 :             if(curbatch->batch < 0)
    1039              :             {
    1040            0 :                 break;
    1041              :             }
    1042            0 :         }
    1043            0 :     }
    1044              : 
    1045            0 :     void resetbatches()
    1046              :     {
    1047            0 :         geombatches.clear();
    1048            0 :         firstbatch = -1;
    1049            0 :         numbatches = 0;
    1050            0 :     }
    1051              : 
    1052            0 :     void RenderState::renderbatches(int pass)
    1053              :     {
    1054            0 :         vslot = nullptr;
    1055            0 :         int curbatch = firstbatch;
    1056            0 :         if(curbatch >= 0)
    1057              :         {
    1058            0 :             if(!depthmask)
    1059              :             {
    1060            0 :                 depthmask = true;
    1061            0 :                 glDepthMask(GL_TRUE);
    1062              :             }
    1063            0 :             if(!colormask)
    1064              :             {
    1065            0 :                 colormask = true;
    1066            0 :                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    1067              :             }
    1068            0 :             if(!vattribs)
    1069              :             {
    1070            0 :                 if(vquery)
    1071              :                 {
    1072            0 :                     disablevquery();
    1073              :                 }
    1074            0 :                 enablevattribs();
    1075              :             }
    1076              :         }
    1077            0 :         while(curbatch >= 0)
    1078              :         {
    1079            0 :             const geombatch &b = geombatches[curbatch];
    1080            0 :             curbatch = b.next;
    1081              : 
    1082            0 :             if(vbuf != b.va->vbuf)
    1083              :             {
    1084            0 :                 changevbuf(*this, pass, *b.va);
    1085              :             }
    1086            0 :             if(pass == RenderPass_GBuffer || pass == RenderPass_ReflectiveShadowMap)
    1087              :             {
    1088            0 :                 changebatchtmus();
    1089              :             }
    1090            0 :             if(vslot != &b.vslot)
    1091              :             {
    1092            0 :                 changeslottmus(pass, *b.vslot.slot, b.vslot);
    1093            0 :                 if(texgenorient != b.es.attrs.orient || (texgenorient < Orient_Any && texgenvslot != &b.vslot))
    1094              :                 {
    1095            0 :                     changetexgen(b.es.attrs.orient, *b.vslot.slot, b.vslot);
    1096              :                 }
    1097            0 :                 changeshader(pass, b);
    1098              :             }
    1099              :             else
    1100              :             {
    1101            0 :                 if(texgenorient != b.es.attrs.orient)
    1102              :                 {
    1103            0 :                     changetexgen(b.es.attrs.orient, *b.vslot.slot, b.vslot);
    1104              :                 }
    1105            0 :                 updateshader(*this);
    1106              :             }
    1107              : 
    1108            0 :             b.renderbatch();
    1109              :         }
    1110              : 
    1111            0 :         resetbatches();
    1112            0 :     }
    1113              : 
    1114            0 :     void RenderState::renderzpass(const vtxarray &va)
    1115              :     {
    1116            0 :         if(!vattribs)
    1117              :         {
    1118            0 :             if(vquery)
    1119              :             {
    1120            0 :                 disablevquery();
    1121              :             }
    1122            0 :             enablevattribs(false);
    1123              :         }
    1124            0 :         if(vbuf!=va.vbuf)
    1125              :         {
    1126            0 :             changevbuf(*this, RenderPass_Z, va);
    1127              :         }
    1128            0 :         if(!depthmask)
    1129              :         {
    1130            0 :             depthmask = true;
    1131            0 :             glDepthMask(GL_TRUE);
    1132              :         }
    1133            0 :         if(colormask)
    1134              :         {
    1135            0 :             colormask = false;
    1136            0 :             glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    1137              :         }
    1138            0 :         int firsttex = 0,
    1139            0 :             numtris = va.tris,
    1140            0 :             offset = 0;
    1141            0 :         if(alphaing)
    1142              :         {
    1143            0 :             firsttex += va.texs;
    1144            0 :             offset += 3*(va.tris);
    1145            0 :             numtris = va.alphabacktris + va.alphafronttris + va.refracttris;
    1146            0 :             xtravertsva += 3*numtris;
    1147              :         }
    1148              :         else
    1149              :         {
    1150            0 :             xtravertsva += va.verts;
    1151              :         }
    1152            0 :         nocolorshader->set();
    1153            0 :         drawvatris(va, 3*numtris, offset);
    1154            0 :     }
    1155              : 
    1156              :     VAR(batchgeom, 0, 1, 1);
    1157              : 
    1158            0 :     void renderva(RenderState &cur, const vtxarray &va, int pass = RenderPass_GBuffer, bool doquery = false)
    1159              :     {
    1160            0 :         switch(pass)
    1161              :         {
    1162            0 :             case RenderPass_GBuffer:
    1163            0 :                 if(!cur.alphaing)
    1164              :                 {
    1165            0 :                     vverts += va.verts;
    1166              :                 }
    1167            0 :                 if(doquery && va.query)
    1168              :                 {
    1169            0 :                     if(geombatches.size())
    1170              :                     {
    1171            0 :                         cur.renderbatches(pass);
    1172              :                     }
    1173            0 :                     va.query->startquery();
    1174              :                 }
    1175            0 :                 mergetexs(cur, va);
    1176            0 :                 if(doquery)
    1177              :                 {
    1178            0 :                     if(va.query)
    1179              :                     {
    1180            0 :                         if(geombatches.size())
    1181              :                         {
    1182            0 :                             cur.renderbatches(pass);
    1183              :                         }
    1184            0 :                         occlusionengine.endquery();
    1185              :                     }
    1186              :                 }
    1187            0 :                 else if(!batchgeom && geombatches.size())
    1188              :                 {
    1189            0 :                     cur.renderbatches(pass);
    1190              :                 }
    1191            0 :                 break;
    1192              : 
    1193            0 :             case RenderPass_GBufferBlend:
    1194            0 :                 if(doquery && va.query)
    1195              :                 {
    1196            0 :                     if(geombatches.size())
    1197              :                     {
    1198            0 :                         cur.renderbatches(RenderPass_GBuffer);
    1199              :                     }
    1200            0 :                     va.query->startquery();
    1201              :                 }
    1202            0 :                 mergetexs(cur, va, &va.texelems[va.texs], 3*va.tris);
    1203            0 :                 if(doquery)
    1204              :                 {
    1205            0 :                     if(va.query)
    1206              :                     {
    1207            0 :                         if(geombatches.size())
    1208              :                         {
    1209            0 :                             cur.renderbatches(RenderPass_GBuffer);
    1210              :                         }
    1211            0 :                         occlusionengine.endquery();
    1212              :                     }
    1213              :                 }
    1214            0 :                 else if(!batchgeom && geombatches.size())
    1215              :                 {
    1216            0 :                     cur.renderbatches(RenderPass_GBuffer);
    1217              :                 }
    1218            0 :                 break;
    1219              : 
    1220            0 :             case RenderPass_Caustics:
    1221            0 :                 if(!cur.vattribs)
    1222              :                 {
    1223            0 :                     cur.enablevattribs(false);
    1224              :                 }
    1225            0 :                 if(cur.vbuf!=va.vbuf)
    1226              :                 {
    1227            0 :                     changevbuf(cur, pass, va);
    1228              :                 }
    1229            0 :                 drawvatris(va, 3*va.tris, 0);
    1230            0 :                 xtravertsva += va.verts;
    1231            0 :                 break;
    1232              : 
    1233            0 :             case RenderPass_Z:
    1234            0 :                 if(doquery && va.query)
    1235              :                 {
    1236            0 :                     va.query->startquery();
    1237              :                 }
    1238            0 :                 cur.renderzpass(va);
    1239            0 :                 if(doquery && va.query)
    1240              :                 {
    1241            0 :                     occlusionengine.endquery();
    1242              :                 }
    1243            0 :                 break;
    1244              : 
    1245            0 :             case RenderPass_ReflectiveShadowMap:
    1246            0 :                 mergetexs(cur, va);
    1247            0 :                 if(!batchgeom && geombatches.size())
    1248              :                 {
    1249            0 :                     cur.renderbatches(pass);
    1250              :                 }
    1251            0 :                 break;
    1252              : 
    1253            0 :             case RenderPass_ReflectiveShadowMapBlend:
    1254            0 :                 mergetexs(cur, va, &va.texelems[va.texs], 3*va.tris);
    1255            0 :                 if(!batchgeom && geombatches.size())
    1256              :                 {
    1257            0 :                     cur.renderbatches(RenderPass_ReflectiveShadowMap);
    1258              :                 }
    1259            0 :                 break;
    1260              :         }
    1261            0 :     }
    1262              : 
    1263            0 :     void setupgeom()
    1264              :     {
    1265            0 :         glActiveTexture(GL_TEXTURE0);
    1266            0 :         GLOBALPARAMF(colorparams, 1, 1, 1, 1);
    1267            0 :     }
    1268              : 
    1269            0 :     void RenderState::cleanupgeom()
    1270              :     {
    1271            0 :         if(vattribs)
    1272              :         {
    1273            0 :             disablevattribs();
    1274              :         }
    1275            0 :         if(vbuf)
    1276              :         {
    1277            0 :             disablevbuf();
    1278              :         }
    1279            0 :     }
    1280              : 
    1281              :     VAR(oqgeom, 0, 1, 1); //occlusion query geometry
    1282              : 
    1283              :     std::vector<const vtxarray *> alphavas;
    1284              : 
    1285            0 :     CVARP(explicitskycolor, 0x800080);
    1286              : 
    1287              :     struct decalbatch final
    1288              :     {
    1289              :         const ElementSet &es;
    1290              :         DecalSlot &slot;
    1291              :         int offset;
    1292              :         const vtxarray &va;
    1293              :         int next, batch;
    1294              : 
    1295            0 :         decalbatch(const ElementSet &es, int offset, const vtxarray &va)
    1296            0 :           : es(es), slot(lookupdecalslot(es.texture)), offset(offset), va(va),
    1297            0 :             next(-1), batch(-1)
    1298            0 :         {}
    1299              : 
    1300              :         void renderdecalbatch();
    1301              : 
    1302            0 :         int compare(const decalbatch &b) const
    1303              :         {
    1304            0 :             if(va.vbuf < b.va.vbuf)
    1305              :             {
    1306            0 :                 return -1;
    1307              :             }
    1308            0 :             if(va.vbuf > b.va.vbuf)
    1309              :             {
    1310            0 :                 return 1;
    1311              :             }
    1312            0 :             if(slot.shader < b.slot.shader)
    1313              :             {
    1314            0 :                 return -1;
    1315              :             }
    1316            0 :             if(slot.shader > b.slot.shader)
    1317              :             {
    1318            0 :                 return 1;
    1319              :             }
    1320            0 :             if(es.texture < b.es.texture)
    1321              :             {
    1322            0 :                 return -1;
    1323              :             }
    1324            0 :             if(es.texture > b.es.texture)
    1325              :             {
    1326            0 :                 return 1;
    1327              :             }
    1328            0 :             if(slot.Slot::params.size() < b.slot.Slot::params.size())
    1329              :             {
    1330            0 :                 return -1;
    1331              :             }
    1332            0 :             if(slot.Slot::params.size() > b.slot.Slot::params.size())
    1333              :             {
    1334            0 :                 return 1;
    1335              :             }
    1336            0 :             if(es.reuse < b.es.reuse)
    1337              :             {
    1338            0 :                 return -1;
    1339              :             }
    1340            0 :             if(es.reuse > b.es.reuse)
    1341              :             {
    1342            0 :                 return 1;
    1343              :             }
    1344            0 :             return 0;
    1345              :         }
    1346              :     };
    1347              : 
    1348              :     std::vector<decalbatch> decalbatches;
    1349              : 
    1350              :     class decalrenderer final
    1351              :     {
    1352              :         public:
    1353              :             GLuint vbuf;
    1354              :             int globals;
    1355              : 
    1356              :             void renderdecalbatches(int pass);
    1357              : 
    1358            0 :             decalrenderer() : vbuf(0), globals(-1), colorscale(1, 1, 1), tmu(-1), slot(nullptr)
    1359              :             {
    1360            0 :                 for(int i = 0; i < 7; ++i)
    1361              :                 {
    1362            0 :                     textures[i] = 0;
    1363              :                 }
    1364            0 :             }
    1365              :         private:
    1366              :             vec colorscale;
    1367              :             int tmu;
    1368              :             std::array<GLuint, 7> textures;
    1369              :             DecalSlot *slot;
    1370              : 
    1371              :             void changebatchtmus();
    1372              :             void bindslottex(int type, const Texture *tex, GLenum target = GL_TEXTURE_2D);
    1373              :             void changeslottmus(DecalSlot &slot);
    1374              :             void changeshader(int pass, const decalbatch &b);
    1375              :     };
    1376              : 
    1377            0 :     void mergedecals(const vtxarray &va)
    1378              :     {
    1379            0 :         ElementSet *texs = va.decalelems;
    1380            0 :         int numtexs = va.decaltexs,
    1381            0 :             offset  = 0;
    1382              : 
    1383            0 :         if(firstbatch < 0)
    1384              :         {
    1385            0 :             firstbatch = decalbatches.size();
    1386            0 :             numbatches = numtexs;
    1387            0 :             for(int i = 0; i < numtexs-1; ++i)
    1388              :             {
    1389            0 :                 decalbatches.emplace_back(decalbatch(texs[i], offset, va));
    1390            0 :                 decalbatches.back().next = i+1;
    1391            0 :                 offset += texs[i].length;
    1392              :             }
    1393            0 :             decalbatches.emplace_back(decalbatch(texs[numtexs-1], offset, va));
    1394            0 :             return;
    1395              :         }
    1396              : 
    1397            0 :         int prevbatch = -1,
    1398            0 :             curbatch = firstbatch,
    1399            0 :             curtex = 0;
    1400              :         do
    1401              :         {
    1402            0 :             decalbatch b = decalbatch(texs[curtex], offset, va);
    1403            0 :             offset += texs[curtex].length;
    1404            0 :             int dir = -1;
    1405            0 :             while(curbatch >= 0)
    1406              :             {
    1407            0 :                 dir = b.compare(decalbatches[curbatch]);
    1408            0 :                 if(dir <= 0)
    1409              :                 {
    1410            0 :                     break;
    1411              :                 }
    1412            0 :                 prevbatch = curbatch;
    1413            0 :                 curbatch = decalbatches[curbatch].next;
    1414              :             }
    1415            0 :             if(!dir)
    1416              :             {
    1417            0 :                 int last = curbatch, next;
    1418              :                 for(;;)
    1419              :                 {
    1420            0 :                     next = decalbatches[last].batch;
    1421            0 :                     if(next < 0)
    1422              :                     {
    1423            0 :                         break;
    1424              :                     }
    1425            0 :                     last = next;
    1426              :                 }
    1427            0 :                 if(last==curbatch)
    1428              :                 {
    1429            0 :                     b.batch = curbatch;
    1430            0 :                     b.next = decalbatches[curbatch].next;
    1431            0 :                     if(prevbatch < 0)
    1432              :                     {
    1433            0 :                         firstbatch = decalbatches.size()-1;
    1434              :                     }
    1435              :                     else
    1436              :                     {
    1437            0 :                         decalbatches[prevbatch].next = decalbatches.size()-1;
    1438              :                     }
    1439            0 :                     curbatch = decalbatches.size()-1;
    1440              :                 }
    1441              :                 else
    1442              :                 {
    1443            0 :                     b.batch = next;
    1444            0 :                     decalbatches[last].batch = decalbatches.size()-1;
    1445              :                 }
    1446              :             }
    1447              :             else
    1448              :             {
    1449            0 :                 numbatches++;
    1450            0 :                 b.next = curbatch;
    1451            0 :                 if(prevbatch < 0)
    1452              :                 {
    1453            0 :                     firstbatch = decalbatches.size()-1;
    1454              :                 }
    1455              :                 else
    1456              :                 {
    1457            0 :                     decalbatches[prevbatch].next = decalbatches.size()-1;
    1458              :                 }
    1459            0 :                 prevbatch = decalbatches.size()-1;
    1460              :             }
    1461            0 :             decalbatches.push_back(b);
    1462            0 :         } while(++curtex < numtexs);
    1463              :     }
    1464              : 
    1465            0 :     void resetdecalbatches()
    1466              :     {
    1467            0 :         decalbatches.clear();
    1468            0 :         firstbatch = -1;
    1469            0 :         numbatches = 0;
    1470            0 :     }
    1471              : 
    1472            0 :     void changevbuf(decalrenderer &cur, const vtxarray &va)
    1473              :     {
    1474            0 :         gle::bindvbo(va.vbuf);
    1475            0 :         gle::bindebo(va.decalbuf);
    1476            0 :         cur.vbuf = va.vbuf;
    1477            0 :         const vertex *vdata = nullptr;
    1478              :         //note inane bikeshedding: use of offset from dereferenced null ptr (aka 0)
    1479            0 :         gle::vertexpointer(sizeof(vertex), vdata->pos.data());
    1480            0 :         gle::normalpointer(sizeof(vertex), vdata->norm.data(), GL_BYTE, 4);
    1481            0 :         gle::texcoord0pointer(sizeof(vertex), vdata->tc.data(), GL_FLOAT, 3);
    1482            0 :         gle::tangentpointer(sizeof(vertex), vdata->tangent.data(), GL_BYTE);
    1483            0 :     }
    1484              : 
    1485            0 :     void decalrenderer::changebatchtmus()
    1486              :     {
    1487            0 :         if(tmu != 0)
    1488              :         {
    1489            0 :             tmu = 0;
    1490            0 :             glActiveTexture(GL_TEXTURE0);
    1491              :         }
    1492            0 :     }
    1493              : 
    1494            0 :     void decalrenderer::bindslottex(int type, const Texture *tex, GLenum target)
    1495              :     {
    1496            0 :         if(textures[type] != tex->id)
    1497              :         {
    1498            0 :             if(tmu != type)
    1499              :             {
    1500            0 :                 tmu = type;
    1501            0 :                 glActiveTexture(GL_TEXTURE0 + type);
    1502              :             }
    1503            0 :             glBindTexture(target, textures[type] = tex->id);
    1504              :         }
    1505            0 :     }
    1506              : 
    1507            0 :     void decalrenderer::changeslottmus(DecalSlot &dslot)
    1508              :     {
    1509            0 :         const Texture *diffuse = dslot.sts.empty() ? notexture : dslot.sts[0].t;
    1510            0 :         bindslottex(Tex_Diffuse, diffuse);
    1511            0 :         for(const Slot::Tex &t : dslot.sts)
    1512              :         {
    1513            0 :             switch(t.type)
    1514              :             {
    1515            0 :                 case Tex_Normal:
    1516              :                 case Tex_Glow:
    1517              :                 {
    1518            0 :                     bindslottex(t.type, t.t);
    1519            0 :                     break;
    1520              :                 }
    1521            0 :                 case Tex_Spec:
    1522              :                 {
    1523            0 :                     if(t.combined < 0)
    1524              :                     {
    1525            0 :                         bindslottex(Tex_Glow, t.t);
    1526              :                     }
    1527            0 :                     break;
    1528              :                 }
    1529              :             }
    1530              :         }
    1531            0 :         if(tmu != 0)
    1532              :         {
    1533            0 :             tmu = 0;
    1534            0 :             glActiveTexture(GL_TEXTURE0);
    1535              :         }
    1536            0 :         if(colorscale != dslot.colorscale)
    1537              :         {
    1538            0 :             colorscale = dslot.colorscale;
    1539            0 :             GLOBALPARAMF(colorparams, dslot.colorscale.x, dslot.colorscale.y, dslot.colorscale.z, 1);
    1540              :         }
    1541            0 :         slot = &dslot;
    1542            0 :     }
    1543              : 
    1544            0 :     void decalrenderer::changeshader(int pass, const decalbatch &b)
    1545              :     {
    1546            0 :         DecalSlot &slot = b.slot;
    1547            0 :         if(b.es.reuse)
    1548              :         {
    1549            0 :             VSlot &reuse = lookupvslot(b.es.reuse);
    1550            0 :             if(pass)
    1551              :             {
    1552            0 :                 slot.shader->setvariant(0, 0, slot, reuse);
    1553              :             }
    1554              :             else
    1555              :             {
    1556            0 :                 slot.shader->set(slot, reuse);
    1557              :             }
    1558              :         }
    1559            0 :         else if(pass)
    1560              :         {
    1561            0 :             slot.shader->setvariantandslot(0, 0);
    1562              :         }
    1563              :         else
    1564              :         {
    1565            0 :             slot.shader->setslot();
    1566              :         }
    1567            0 :         globals = GlobalShaderParamState::nextversion;
    1568            0 :     }
    1569              : 
    1570            0 :     void decalbatch::renderdecalbatch()
    1571              :     {
    1572            0 :         gbatches++;
    1573            0 :         for(decalbatch *curbatch = this;; curbatch = &decalbatches[curbatch->batch])
    1574              :         {
    1575            0 :             ushort len = curbatch->es.length;
    1576            0 :             if(len)
    1577              :             {
    1578            0 :                 drawtris(len, reinterpret_cast<ushort *>(curbatch->va.decaloffset) + curbatch->offset, curbatch->es.minvert, curbatch->es.maxvert);
    1579            0 :                 vtris += len/3;
    1580              :             }
    1581            0 :             if(curbatch->batch < 0)
    1582              :             {
    1583            0 :                 break;
    1584              :             }
    1585            0 :         }
    1586            0 :     }
    1587              : 
    1588            0 :     void decalrenderer::renderdecalbatches(int pass)
    1589              :     {
    1590            0 :         slot = nullptr;
    1591            0 :         int curbatch = firstbatch;
    1592            0 :         while(curbatch >= 0)
    1593              :         {
    1594            0 :             decalbatch &b = decalbatches[curbatch];
    1595            0 :             curbatch = b.next;
    1596              : 
    1597            0 :             if(pass && !b.slot.shader->numvariants(0))
    1598              :             {
    1599            0 :                 continue;
    1600              :             }
    1601            0 :             if(vbuf != b.va.vbuf)
    1602              :             {
    1603            0 :                 changevbuf(*this, b.va);
    1604              :             }
    1605            0 :             changebatchtmus();
    1606            0 :             if(slot != &b.slot)
    1607              :             {
    1608            0 :                 changeslottmus(b.slot);
    1609            0 :                 changeshader(pass, b);
    1610              :             }
    1611              :             else
    1612              :             {
    1613            0 :                 updateshader(*this);
    1614              :             }
    1615              : 
    1616            0 :             b.renderdecalbatch();
    1617              :         }
    1618              : 
    1619            0 :         resetdecalbatches();
    1620            0 :     }
    1621              : 
    1622            0 :     void setupdecals()
    1623              :     {
    1624            0 :         gle::enablevertex();
    1625            0 :         gle::enablenormal();
    1626            0 :         gle::enabletexcoord0();
    1627            0 :         gle::enabletangent();
    1628              : 
    1629            0 :         glDepthMask(GL_FALSE);
    1630            0 :         glEnable(GL_BLEND);
    1631            0 :         enablepolygonoffset(GL_POLYGON_OFFSET_FILL);
    1632              : 
    1633            0 :         GLOBALPARAMF(colorparams, 1, 1, 1, 1);
    1634            0 :     }
    1635              : 
    1636            0 :     void cleanupdecals()
    1637              :     {
    1638            0 :         disablepolygonoffset(GL_POLYGON_OFFSET_FILL);
    1639            0 :         glDisable(GL_BLEND);
    1640            0 :         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1641            0 :         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    1642            0 :         glDepthMask(GL_TRUE);
    1643            0 :         maskgbuffer("cnd");
    1644              : 
    1645            0 :         gle::disablevertex();
    1646            0 :         gle::disablenormal();
    1647            0 :         gle::disabletexcoord0();
    1648            0 :         gle::disabletangent();
    1649              : 
    1650            0 :         gle::clearvbo();
    1651            0 :         gle::clearebo();
    1652            0 :     }
    1653              : 
    1654              :     VAR(batchdecals, 0, 1, 1);
    1655              : 
    1656              :     struct shadowdraw final
    1657              :     {
    1658              :         GLuint ebuf, vbuf;
    1659              :         int offset, tris, next;
    1660              :         GLuint minvert, maxvert;
    1661              :     };
    1662              : 
    1663              :     struct shadowverts final
    1664              :     {
    1665              :         static constexpr int tablesize = 1<<13;
    1666              :         std::array<int, tablesize> table;
    1667              :         std::vector<vec> verts;
    1668              :         std::vector<int> chain;
    1669              : 
    1670            1 :         shadowverts() { clear(); }
    1671              : 
    1672            1 :         void clear()
    1673              :         {
    1674            1 :             table.fill(-1);
    1675            1 :             chain.clear();
    1676            1 :             verts.clear();
    1677            1 :         }
    1678              : 
    1679            0 :         int add(const vec &v)
    1680              :         {
    1681              :             auto vechash = std::hash<vec>();
    1682            0 :             uint h = vechash(v)&(tablesize-1);
    1683            0 :             for(int i = table[h]; i>=0; i = chain[i])
    1684              :             {
    1685            0 :                 if(verts[i] == v)
    1686              :                 {
    1687            0 :                     return i;
    1688              :                 }
    1689              :             }
    1690            0 :             if(verts.size() >= USHRT_MAX)
    1691              :             {
    1692            0 :                 return -1;
    1693              :             }
    1694            0 :             verts.push_back(v);
    1695            0 :             chain.emplace_back(table[h]);
    1696            0 :             return table[h] = verts.size()-1;
    1697              :         }
    1698              :     } shadowverts;
    1699              :     std::array<std::vector<GLuint>, 6> shadowtris;
    1700              :     std::vector<GLuint> shadowvbos;
    1701              :     std::unordered_map<int, shadowmesh> shadowmeshes;
    1702              :     std::vector<shadowdraw> shadowdraws;
    1703              : 
    1704              :     struct shadowdrawinfo final
    1705              :     {
    1706              :         int last;
    1707              :         GLuint minvert, maxvert;
    1708              : 
    1709            0 :         shadowdrawinfo() : last(-1)
    1710              :         {
    1711            0 :             reset();
    1712            0 :         }
    1713              : 
    1714            0 :         void reset()
    1715              :         {
    1716            0 :             minvert = USHRT_MAX;
    1717            0 :             maxvert = 0;
    1718            0 :         }
    1719              :     };
    1720              : 
    1721            0 :     void flushshadowmeshdraws(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws)
    1722              :     {
    1723            0 :         int numindexes = 0;
    1724            0 :         for(int i = 0; i < sides; ++i)
    1725              :         {
    1726            0 :             numindexes += shadowtris[i].size();
    1727              :         }
    1728            0 :         if(!numindexes)
    1729              :         {
    1730            0 :             return;
    1731              :         }
    1732              : 
    1733            0 :         GLuint ebuf = 0,
    1734            0 :                vbuf = 0;
    1735            0 :         glGenBuffers(1, &ebuf);
    1736            0 :         glGenBuffers(1, &vbuf);
    1737            0 :         ushort *indexes = new ushort[numindexes];
    1738            0 :         int offset = 0;
    1739            0 :         for(int i = 0; i < sides; ++i)
    1740              :         {
    1741            0 :             if(shadowtris[i].size())
    1742              :             {
    1743            0 :                 if(draws[i].last < 0)
    1744              :                 {
    1745            0 :                     m.draws[i] = shadowdraws.size();
    1746              :                 }
    1747              :                 else
    1748              :                 {
    1749            0 :                     shadowdraws[draws[i].last].next = shadowdraws.size();
    1750              :                 }
    1751            0 :                 draws[i].last = shadowdraws.size();
    1752              : 
    1753              :                 shadowdraw d;
    1754            0 :                 d.ebuf = ebuf;
    1755            0 :                 d.vbuf = vbuf;
    1756            0 :                 d.offset = offset;
    1757            0 :                 d.tris = shadowtris[i].size()/3;
    1758            0 :                 d.minvert = draws[i].minvert;
    1759            0 :                 d.maxvert = draws[i].maxvert;
    1760            0 :                 d.next = -1;
    1761            0 :                 shadowdraws.push_back(d);
    1762              : 
    1763            0 :                 std::memcpy(indexes + offset, shadowtris[i].data(), shadowtris[i].size()*sizeof(ushort));
    1764            0 :                 offset += shadowtris[i].size();
    1765              : 
    1766            0 :                 shadowtris[i].clear();
    1767            0 :                 draws[i].reset();
    1768              :             }
    1769              :         }
    1770              : 
    1771            0 :         gle::bindebo(ebuf);
    1772            0 :         glBufferData(GL_ELEMENT_ARRAY_BUFFER, numindexes*sizeof(ushort), indexes, GL_STATIC_DRAW);
    1773            0 :         gle::clearebo();
    1774            0 :         delete[] indexes;
    1775              : 
    1776            0 :         gle::bindvbo(vbuf);
    1777            0 :         glBufferData(GL_ARRAY_BUFFER, shadowverts.verts.size()*sizeof(vec), shadowverts.verts.data(), GL_STATIC_DRAW);
    1778            0 :         gle::clearvbo();
    1779            0 :         shadowverts.clear();
    1780              : 
    1781            0 :         shadowvbos.push_back(ebuf);
    1782            0 :         shadowvbos.push_back(vbuf);
    1783              :     }
    1784              : 
    1785            0 :     int calctrisidemask(const vec &p1, const vec &p2, const vec &p3, float bias)
    1786              :     {
    1787              :         // p1, p2, p3 are in the cubemap's local coordinate system
    1788              :         // bias = border/(size - border)
    1789            0 :         int mask = 0x3F;
    1790            0 :         float dp1 = p1.x + p1.y,
    1791            0 :               dn1 = p1.x - p1.y,
    1792            0 :               ap1 = std::fabs(dp1),
    1793            0 :               an1 = std::fabs(dn1),
    1794            0 :               dp2 = p2.x + p2.y,
    1795            0 :               dn2 = p2.x - p2.y,
    1796            0 :               ap2 = std::fabs(dp2),
    1797            0 :               an2 = std::fabs(dn2),
    1798            0 :               dp3 = p3.x + p3.y,
    1799            0 :               dn3 = p3.x - p3.y,
    1800            0 :               ap3 = std::fabs(dp3),
    1801            0 :               an3 = std::fabs(dn3);
    1802            0 :         if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
    1803              :         {
    1804            0 :             mask &=  (3<<4)
    1805            0 :                    | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
    1806            0 :                    | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
    1807            0 :                    | (dp3 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
    1808              :         }
    1809            0 :         if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
    1810            0 :             mask &=  (3<<4)
    1811            0 :                    | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
    1812            0 :                    | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
    1813            0 :                    | (dn3 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
    1814            0 :         dp1 = p1.y + p1.z,
    1815            0 :         dn1 = p1.y - p1.z,
    1816            0 :         ap1 = std::fabs(dp1),
    1817            0 :         an1 = std::fabs(dn1),
    1818            0 :         dp2 = p2.y + p2.z,
    1819            0 :         dn2 = p2.y - p2.z,
    1820            0 :         ap2 = std::fabs(dp2),
    1821            0 :         an2 = std::fabs(dn2),
    1822            0 :         dp3 = p3.y + p3.z,
    1823            0 :         dn3 = p3.y - p3.z,
    1824            0 :         ap3 = std::fabs(dp3),
    1825            0 :         an3 = std::fabs(dn3);
    1826            0 :         if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
    1827              :         {
    1828            0 :             mask &= (3<<0)
    1829            0 :                 | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
    1830            0 :                 | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
    1831            0 :                 | (dp3 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
    1832              :         }
    1833            0 :         if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
    1834              :         {
    1835            0 :             mask &= (3<<0)
    1836            0 :                 | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
    1837            0 :                 | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
    1838            0 :                 | (dn3 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
    1839              :         }
    1840            0 :         dp1 = p1.z + p1.x,
    1841            0 :         dn1 = p1.z - p1.x,
    1842            0 :         ap1 = std::fabs(dp1),
    1843            0 :         an1 = std::fabs(dn1),
    1844            0 :         dp2 = p2.z + p2.x,
    1845            0 :         dn2 = p2.z - p2.x,
    1846            0 :         ap2 = std::fabs(dp2),
    1847            0 :         an2 = std::fabs(dn2),
    1848            0 :         dp3 = p3.z + p3.x,
    1849            0 :         dn3 = p3.z - p3.x,
    1850            0 :         ap3 = std::fabs(dp3),
    1851            0 :         an3 = std::fabs(dn3);
    1852            0 :         if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
    1853              :         {
    1854            0 :             mask &= (3<<2)
    1855            0 :                 | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
    1856            0 :                 | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
    1857            0 :                 | (dp3 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
    1858              :         }
    1859            0 :         if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
    1860              :         {
    1861            0 :             mask &= (3<<2)
    1862            0 :                 | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
    1863            0 :                 | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
    1864            0 :                 | (dn3 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
    1865              :         }
    1866            0 :         return mask;
    1867              :     }
    1868              : 
    1869            0 :     void addshadowmeshtri(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws, const vec &v0, const vec &v1, const vec &v2)
    1870              :     {
    1871            0 :         vec l0 = vec(v0).sub(shadoworigin);
    1872            0 :         float side = l0.scalartriple(vec(v1).sub(v0), vec(v2).sub(v0));
    1873            0 :         if(smcullside ? side > 0 : side < 0)
    1874              :         {
    1875            0 :             return;
    1876              :         }
    1877            0 :         vec l1 = vec(v1).sub(shadoworigin),
    1878            0 :             l2 = vec(v2).sub(shadoworigin);
    1879            0 :         if(l0.squaredlen() > shadowradius*shadowradius && l1.squaredlen() > shadowradius*shadowradius && l2.squaredlen() > shadowradius*shadowradius)
    1880              :         {
    1881            0 :             return;
    1882              :         }
    1883            0 :         int sidemask = 0;
    1884            0 :         switch(m.type)
    1885              :         {
    1886            0 :             case ShadowMap_Spot:
    1887              :             {
    1888            0 :                 sidemask = bbinsidespot(shadoworigin, shadowdir, shadowspot, ivec(vec(v0).min(v1).min(v2)), ivec(vec(v0).max(v1).max(v2).add(1))) ? 1 : 0;
    1889            0 :                 break;
    1890              :             }
    1891            0 :             case ShadowMap_CubeMap:
    1892              :             {
    1893            0 :                 sidemask = calctrisidemask(l0.div(shadowradius), l1.div(shadowradius), l2.div(shadowradius), shadowbias);
    1894            0 :                 break;
    1895              :             }
    1896              :         }
    1897            0 :         if(!sidemask)
    1898              :         {
    1899            0 :             return;
    1900              :         }
    1901            0 :         if(shadowverts.verts.size() + 3 >= USHRT_MAX)
    1902              :         {
    1903            0 :             flushshadowmeshdraws(m, sides, draws);
    1904              :         }
    1905            0 :         int i0 = shadowverts.add(v0),
    1906            0 :             i1 = shadowverts.add(v1),
    1907            0 :             i2 = shadowverts.add(v2);
    1908            0 :         GLuint minvert = std::min(i0, std::min(i1, i2)),
    1909            0 :                maxvert = std::max(i0, std::max(i1, i2));
    1910            0 :         for(int k = 0; k < sides; ++k)
    1911              :         {
    1912            0 :             if(sidemask&(1<<k))
    1913              :             {
    1914            0 :                 shadowdrawinfo &d = draws[k];
    1915            0 :                 d.minvert = std::min(d.minvert, minvert);
    1916            0 :                 d.maxvert = std::max(d.maxvert, maxvert);
    1917            0 :                 shadowtris[k].push_back(i0);
    1918            0 :                 shadowtris[k].push_back(i1);
    1919            0 :                 shadowtris[k].push_back(i2);
    1920              :             }
    1921              :         }
    1922              :     }
    1923              : 
    1924            0 :     void genshadowmeshtris(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws, ushort *edata, int numtris, vertex *vdata)
    1925              :     {
    1926            0 :         for(int j = 0; j < 3*numtris; j += 3)
    1927              :         {
    1928            0 :             addshadowmeshtri(m, sides, draws, vdata[edata[j]].pos, vdata[edata[j+1]].pos, vdata[edata[j+2]].pos);
    1929              :         }
    1930            0 :     }
    1931              : 
    1932            0 :     void genshadowmeshmapmodels(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws)
    1933              :     {
    1934            0 :         const std::vector<extentity *> &ents = entities::getents();
    1935            0 :         for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
    1936              :         {
    1937            0 :             for(uint k = 0; k < oe->mapmodels.size(); k++)
    1938              :             {
    1939            0 :                 extentity &e = *ents[oe->mapmodels[k]];
    1940            0 :                 if(e.flags&(EntFlag_NoVis|EntFlag_NoShadow))
    1941              :                 {
    1942            0 :                     continue;
    1943              :                 }
    1944            0 :                 e.flags |= EntFlag_Render;
    1945              :             }
    1946              :         }
    1947            0 :         std::vector<triangle> tris;
    1948            0 :         for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
    1949              :         {
    1950            0 :             for(const int &j : oe->mapmodels)
    1951              :             {
    1952            0 :                 extentity &e = *ents[j];
    1953            0 :                 if(!(e.flags&EntFlag_Render))
    1954              :                 {
    1955            0 :                     continue;
    1956              :                 }
    1957            0 :                 e.flags &= ~EntFlag_Render;
    1958            0 :                 model *mm = loadmapmodel(e.attr1);
    1959            0 :                 if(!mm || !mm->shadow || mm->animated() || (mm->alphashadow && mm->alphatested()))
    1960              :                 {
    1961            0 :                     continue;
    1962              :                 }
    1963            0 :                 matrix4x3 orient;
    1964            0 :                 orient.identity();
    1965            0 :                 if(e.attr2)
    1966              :                 {
    1967            0 :                     orient.rotate_around_z(sincosmod360(e.attr2));
    1968              :                 }
    1969            0 :                 if(e.attr3)
    1970              :                 {
    1971            0 :                     orient.rotate_around_x(sincosmod360(e.attr3));
    1972              :                 }
    1973            0 :                 if(e.attr4)
    1974              :                 {
    1975            0 :                     orient.rotate_around_y(sincosmod360(-e.attr4));
    1976              :                 }
    1977            0 :                 if(e.attr5 > 0)
    1978              :                 {
    1979            0 :                     orient.scale(e.attr5/100.0f);
    1980              :                 }
    1981            0 :                 orient.settranslation(e.o);
    1982            0 :                 tris.clear();
    1983            0 :                 mm->genshadowmesh(tris, orient);
    1984              : 
    1985            0 :                 for(uint i = 0; i < tris.size(); i++)
    1986              :                 {
    1987            0 :                     triangle &t = tris[i];
    1988            0 :                     addshadowmeshtri(m, sides, draws, t.a, t.b, t.c);
    1989              :                 }
    1990              : 
    1991            0 :                 e.flags |= EntFlag_ShadowMesh;
    1992              :             }
    1993              :         }
    1994            0 :     }
    1995              : 
    1996            0 :     void genshadowmesh(int idx, const extentity &e)
    1997              :     {
    1998            0 :         shadowmesh m;
    1999            0 :         m.type = calcshadowinfo(e, m.origin, m.radius, m.spotloc, m.spotangle, shadowbias);
    2000            0 :         if(!m.type)
    2001              :         {
    2002            0 :             return;
    2003              :         }
    2004            0 :         m.draws.fill(-1);
    2005              : 
    2006            0 :         shadowmapping = m.type;
    2007            0 :         shadoworigin = m.origin;
    2008            0 :         shadowradius = m.radius;
    2009            0 :         shadowdir = m.type == ShadowMap_Spot ? vec(m.spotloc).sub(m.origin).normalize() : vec(0, 0, 0);
    2010            0 :         shadowspot = m.spotangle;
    2011              : 
    2012            0 :         findshadowvas();
    2013            0 :         findshadowmms();
    2014              : 
    2015            0 :         int sides = m.type == ShadowMap_Spot ? 1 : 6;
    2016            0 :         std::array<shadowdrawinfo, 6> draws;
    2017            0 :         for(vtxarray *va = shadowva; va; va = va->rnext)
    2018              :         {
    2019            0 :             if(va->shadowmask)
    2020              :             {
    2021            0 :                 if(va->tris)
    2022              :                 {
    2023            0 :                     genshadowmeshtris(m, sides, draws, va->edata + va->eoffset, va->tris, va->vdata);
    2024              :                 }
    2025            0 :                 if(skyshadow && va->sky)
    2026              :                 {
    2027            0 :                     genshadowmeshtris(m, sides, draws, va->skydata + va->skyoffset, va->sky/3, va->vdata);
    2028              :                 }
    2029              :             }
    2030              :         }
    2031            0 :         if(shadowmms)
    2032              :         {
    2033            0 :             genshadowmeshmapmodels(m, sides, draws);
    2034              :         }
    2035            0 :         flushshadowmeshdraws(m, sides, draws);
    2036              : 
    2037            0 :         shadowmeshes[idx] = m;
    2038              : 
    2039            0 :         shadowmapping = 0;
    2040              :     }
    2041              : 
    2042            0 :     VARF(smmesh, 0, 1, 1, { if(!smmesh) clearshadowmeshes(); });
    2043              : }
    2044              : 
    2045              : /* externally relevant functionality */
    2046              : ///////////////////////////////////////
    2047              : 
    2048              : // vfc - view frustum culling
    2049              : 
    2050            0 : int vfc::isfoggedcube(const ivec &o, int size) const
    2051              : {
    2052            0 :     for(int i = 0; i < 4; ++i)
    2053              :     {
    2054            0 :         if(o.dist(vfcP[i]) < -vfcDfar[i]*size)
    2055              :         {
    2056            0 :             return true;
    2057              :         }
    2058              :     }
    2059            0 :     float dist = o.dist(vfcP[4]);
    2060            0 :     return dist < -vfcDfar[4]*size || dist > vfcDfog - vfcDnear[4]*size;
    2061              : }
    2062              : 
    2063            0 : int vfc::isvisiblecube(const ivec &o, int size) const
    2064              : {
    2065            0 :     int v = ViewFrustumCull_FullyVisible;
    2066              :     float dist;
    2067              : 
    2068            0 :     for(int i = 0; i < 5; ++i)
    2069              :     {
    2070            0 :         dist = o.dist(vfcP[i]);
    2071            0 :         if(dist < -vfcDfar[i]*size)
    2072              :         {
    2073            0 :             return ViewFrustumCull_NotVisible;
    2074              :         }
    2075            0 :         if(dist < -vfcDnear[i]*size)
    2076              :         {
    2077            0 :             v = ViewFrustumCull_PartlyVisible;
    2078              :         }
    2079              :     }
    2080              : 
    2081            0 :     dist -= vfcDfog;
    2082            0 :     if(dist > -vfcDnear[4]*size)
    2083              :     {
    2084            0 :         return ViewFrustumCull_Fogged;
    2085              :     }
    2086            0 :     if(dist > -vfcDfar[4]*size)
    2087              :     {
    2088            0 :         v = ViewFrustumCull_PartlyVisible;
    2089              :     }
    2090              : 
    2091            0 :     return v;
    2092              : }
    2093              : 
    2094            0 : void vfc::calcvfcD()
    2095              : {
    2096            0 :     for(int i = 0; i < 5; ++i)
    2097              :     {
    2098            0 :         plane &p = vfcP[i];
    2099            0 :         vfcDnear[i] = vfcDfar[i] = 0;
    2100            0 :         for(int k = 0; k < 3; ++k)
    2101              :         {
    2102            0 :             if(p[k] > 0)
    2103              :             {
    2104            0 :                 vfcDfar[i] += p[k];
    2105              :             }
    2106              :             else
    2107              :             {
    2108            0 :                 vfcDnear[i] += p[k];
    2109              :             }
    2110              :         }
    2111              :     }
    2112            0 : }
    2113              : 
    2114            0 : void vfc::visiblecubes(bool cull)
    2115              : {
    2116            0 :     if(cull)
    2117              :     {
    2118            0 :         setvfcP();
    2119            0 :         findvisiblevas();
    2120              :     }
    2121              :     else
    2122              :     {
    2123            0 :         for(int i = 0; i < 5; ++i)
    2124              :         {
    2125            0 :             vfcP[i].x = vfcP[i].y = vfcP[i].z = vfcP[i].offset = 0;
    2126              :         };
    2127            0 :         vfcDfog = farplane;
    2128            0 :         vfcDnear.fill(0);
    2129            0 :         vfcDfar.fill(0);
    2130            0 :         visibleva = nullptr;
    2131            0 :         for(uint i = 0; i < valist.size(); i++)
    2132              :         {
    2133            0 :             vtxarray *va = valist[i];
    2134            0 :             va->distance = 0;
    2135            0 :             va->curvfc = ViewFrustumCull_FullyVisible;
    2136            0 :             va->occluded = !va->texs ? Occlude_Geom : Occlude_Nothing;
    2137            0 :             va->query = nullptr;
    2138            0 :             va->next = visibleva;
    2139            0 :             visibleva = va;
    2140              :         }
    2141              :     }
    2142            0 : }
    2143              : 
    2144            0 : bool vfc::isfoggedsphere(float rad, const vec &cv) const
    2145              : {
    2146            0 :     for(int i = 0; i < 4; ++i)
    2147              :     {
    2148            0 :         if(vfcP[i].dist(cv) < -rad)
    2149              :         {
    2150            0 :             return true;
    2151              :         }
    2152              :     }
    2153            0 :     float dist = vfcP[4].dist(cv);
    2154            0 :     return dist < -rad || dist > vfcDfog + rad; // true if abs(dist) is large
    2155              : }
    2156              : 
    2157            0 : int vfc::isvisiblesphere(float rad, const vec &cv) const
    2158              : {
    2159            0 :     int v = ViewFrustumCull_FullyVisible;
    2160              :     float dist;
    2161              : 
    2162            0 :     for(int i = 0; i < 5; ++i)
    2163              :     {
    2164            0 :         dist = vfcP[i].dist(cv);
    2165            0 :         if(dist < -rad)
    2166              :         {
    2167            0 :             return ViewFrustumCull_NotVisible;
    2168              :         }
    2169            0 :         if(dist < rad)
    2170              :         {
    2171            0 :             v = ViewFrustumCull_PartlyVisible;
    2172              :         }
    2173              :     }
    2174              : 
    2175            0 :     dist -= vfcDfog;
    2176            0 :     if(dist > rad)
    2177              :     {
    2178            0 :         return ViewFrustumCull_Fogged;  //ViewFrustumCull_NotVisible;    // culling when fog is closer than size of world results in HOM
    2179              :     }
    2180            0 :     if(dist > -rad)
    2181              :     {
    2182            0 :         v = ViewFrustumCull_PartlyVisible;
    2183              :     }
    2184            0 :     return v;
    2185              : }
    2186              : 
    2187            0 : int vfc::isvisiblebb(const ivec &bo, const ivec &br) const
    2188              : {
    2189            0 :     int v = ViewFrustumCull_FullyVisible;
    2190              :     float dnear, dfar;
    2191              : 
    2192            0 :     for(int i = 0; i < 5; ++i)
    2193              :     {
    2194            0 :         const plane &p = vfcP[i];
    2195            0 :         dnear = dfar = bo.dist(p);
    2196            0 :         if(p.x > 0)
    2197              :         {
    2198            0 :             dfar += p.x*br.x;
    2199              :         }
    2200              :         else
    2201              :         {
    2202            0 :             dnear += p.x*br.x;
    2203              :         }
    2204            0 :         if(p.y > 0)
    2205              :         {
    2206            0 :             dfar += p.y*br.y;
    2207              :         }
    2208              :         else
    2209              :         {
    2210            0 :             dnear += p.y*br.y;
    2211              :         }
    2212            0 :         if(p.z > 0)
    2213              :         {
    2214            0 :             dfar += p.z*br.z;
    2215              :         }
    2216              :         else
    2217              :         {
    2218            0 :             dnear += p.z*br.z;
    2219              :         }
    2220            0 :         if(dfar < 0)
    2221              :         {
    2222            0 :             return ViewFrustumCull_NotVisible;
    2223              :         }
    2224            0 :         if(dnear < 0)
    2225              :         {
    2226            0 :             v = ViewFrustumCull_PartlyVisible;
    2227              :         }
    2228              :     }
    2229              : 
    2230            0 :     if(dnear > vfcDfog)
    2231              :     {
    2232            0 :         return ViewFrustumCull_Fogged;
    2233              :     }
    2234            0 :     if(dfar > vfcDfog)
    2235              :     {
    2236            0 :         v = ViewFrustumCull_PartlyVisible;
    2237              :     }
    2238            0 :     return v;
    2239              : }
    2240              : 
    2241            0 : bool cubeworld::bboccluded(const ivec &bo, const ivec &br) const
    2242              : {
    2243            0 :     int diff = (bo.x^br.x) | (bo.y^br.y) | (bo.z^br.z);
    2244            0 :     if(diff&~((1<<worldscale)-1))
    2245              :     {
    2246            0 :         return false;
    2247              :     }
    2248            0 :     int scale = worldscale-1;
    2249            0 :     if(diff&(1<<scale))
    2250              :     {
    2251            0 :         return ::bboccluded(bo, br, *worldroot, ivec(0, 0, 0), 1<<scale);
    2252              :     }
    2253            0 :     const cube *c = &(*worldroot)[OCTA_STEP(bo.x, bo.y, bo.z, scale)];
    2254            0 :     if(c->ext && c->ext->va)
    2255              :     {
    2256            0 :         vtxarray *va = c->ext->va;
    2257            0 :         if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
    2258              :         {
    2259            0 :             return true;
    2260              :         }
    2261              :     }
    2262            0 :     scale--;
    2263            0 :     while(c->children && !(diff&(1<<scale)))
    2264              :     {
    2265            0 :         c = &(*c->children)[OCTA_STEP(bo.x, bo.y, bo.z, scale)];
    2266            0 :         if(c->ext && c->ext->va)
    2267              :         {
    2268            0 :             vtxarray *va = c->ext->va;
    2269            0 :             if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
    2270              :             {
    2271            0 :                 return true;
    2272              :             }
    2273              :         }
    2274            0 :         scale--;
    2275              :     }
    2276            0 :     if(c->children)
    2277              :     {
    2278            0 :         return ::bboccluded(bo, br, *(c->children), ivec(bo).mask(~((2<<scale)-1)), 1<<scale);
    2279              :     }
    2280            0 :     return false;
    2281              : }
    2282              : 
    2283            0 : void startbb(bool mask)
    2284              : {
    2285            0 :     setupbb();
    2286            0 :     gle::bindvbo(bbvbo);
    2287            0 :     gle::bindebo(bbebo);
    2288            0 :     gle::vertexpointer(sizeof(vec), nullptr);
    2289            0 :     gle::enablevertex();
    2290            0 :     SETSHADER(bbquery);
    2291            0 :     if(mask)
    2292              :     {
    2293            0 :         glDepthMask(GL_FALSE);
    2294            0 :         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    2295              :     }
    2296            0 : }
    2297              : 
    2298            0 : void endbb(bool mask)
    2299              : {
    2300            0 :     gle::disablevertex();
    2301            0 :     gle::clearvbo();
    2302            0 :     gle::clearebo();
    2303            0 :     if(mask)
    2304              :     {
    2305            0 :         glDepthMask(GL_TRUE);
    2306            0 :         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    2307              :     }
    2308            0 : }
    2309              : 
    2310            0 : void drawbb(const ivec &bo, const ivec &br)
    2311              : {
    2312            0 :     LOCALPARAMF(bborigin, bo.x, bo.y, bo.z);
    2313            0 :     LOCALPARAMF(bbsize, br.x, br.y, br.z);
    2314            0 :     glDrawRangeElements(GL_TRIANGLES, 0, 8-1, 3*2*6, GL_UNSIGNED_SHORT, nullptr);
    2315            0 :     xtraverts += 8;
    2316            0 : }
    2317              : 
    2318            0 : void vfc::setvfcP(const vec &bbmin, const vec &bbmax)
    2319              : {
    2320            0 :     vec4<float> px = camprojmatrix.rowx(),
    2321            0 :          py = camprojmatrix.rowy(),
    2322            0 :          pz = camprojmatrix.rowz(),
    2323            0 :          pw = camprojmatrix.roww();
    2324            0 :     vfcP[0] = plane(vec4<float>(pw).mul(-bbmin.x).add(px)).normalize(); // left plane
    2325            0 :     vfcP[1] = plane(vec4<float>(pw).mul(bbmax.x).sub(px)).normalize(); // right plane
    2326            0 :     vfcP[2] = plane(vec4<float>(pw).mul(-bbmin.y).add(py)).normalize(); // bottom plane
    2327            0 :     vfcP[3] = plane(vec4<float>(pw).mul(bbmax.y).sub(py)).normalize(); // top plane
    2328            0 :     vfcP[4] = plane(vec4<float>(pw).add(pz)).normalize(); // near/far planes
    2329              : 
    2330            0 :     vfcDfog = std::min(calcfogcull(), static_cast<float>(farplane));
    2331            0 :     view.calcvfcD();
    2332            0 : }
    2333              : 
    2334              : //oq
    2335              : 
    2336            0 : void Occluder::clearqueries()
    2337              : {
    2338            0 :     for(QueryFrame &i : queryframes)
    2339              :     {
    2340            0 :         i.cleanup();
    2341              :     }
    2342            0 : }
    2343              : 
    2344            0 : void Occluder::flipqueries()
    2345              : {
    2346            0 :     flipquery = (flipquery + 1) % maxqueryframes;
    2347            0 :     queryframes[flipquery].flip();
    2348            0 : }
    2349              : 
    2350            0 : void Occluder::endquery() const
    2351              : {
    2352            0 :     glEndQuery(querytarget());
    2353            0 : }
    2354              : 
    2355            0 : bool Occluder::checkquery(occludequery *query, bool nowait)
    2356              : {
    2357            0 :     if(query->fragments < 0)
    2358              :     {
    2359            0 :         if(nowait || !oqwait)
    2360              :         {
    2361              :             GLint avail;
    2362            0 :             glGetQueryObjectiv(query->id, GL_QUERY_RESULT_AVAILABLE, &avail);
    2363            0 :             if(!avail)
    2364              :             {
    2365            0 :                 return false;
    2366              :             }
    2367              :         }
    2368              : 
    2369              :         GLuint fragments;
    2370            0 :         glGetQueryObjectuiv(query->id, GL_QUERY_RESULT, &fragments);
    2371            0 :         query->fragments = querytarget() == GL_SAMPLES_PASSED || !fragments ? static_cast<int>(fragments) : oqfrags;
    2372              :     }
    2373            0 :     return query->fragments < oqfrags;
    2374              : }
    2375              : 
    2376            0 : void Occluder::resetqueries()
    2377              : {
    2378            0 :     for(QueryFrame &i : queryframes)
    2379              :     {
    2380            0 :         i.reset();
    2381              :     }
    2382            0 : }
    2383              : 
    2384            1 : int Occluder::getnumqueries() const
    2385              : {
    2386            1 :     return queryframes[flipquery].cur;
    2387              : }
    2388              : 
    2389            0 : void Occluder::QueryFrame::flip()
    2390              : {
    2391            0 :     for(int i = 0; i < cur; ++i)
    2392              :     {
    2393            0 :         queries[i].owner = nullptr;
    2394              :     }
    2395            0 :     for(; defer > 0 && max < maxquery; defer--)
    2396              :     {
    2397            0 :         queries[max].owner = nullptr;
    2398            0 :         queries[max].fragments = -1;
    2399            0 :         glGenQueries(1, &queries[max++].id);
    2400              :     }
    2401            0 :     cur = defer = 0;
    2402            0 : }
    2403              : 
    2404            0 : occludequery *Occluder::QueryFrame::newquery(const void *owner)
    2405              : {
    2406            0 :     if(cur >= max)
    2407              :     {
    2408            0 :         if(max >= maxquery)
    2409              :         {
    2410            0 :             return nullptr;
    2411              :         }
    2412            0 :         if(deferquery)
    2413              :         {
    2414            0 :             if(max + defer < maxquery)
    2415              :             {
    2416            0 :                 defer++;
    2417              :             }
    2418            0 :             return nullptr;
    2419              :         }
    2420            0 :         glGenQueries(1, &queries[max++].id);
    2421              :     }
    2422            0 :     occludequery *query = &queries[cur++];
    2423            0 :     query->owner = owner;
    2424            0 :     query->fragments = -1;
    2425            0 :     return query;
    2426              : }
    2427              : 
    2428            0 : void Occluder::QueryFrame::reset()
    2429              : {
    2430            0 :     for(int i = 0; i < max; ++i)
    2431              :     {
    2432            0 :         queries[i].owner = nullptr;
    2433              :     }
    2434            0 : }
    2435              : 
    2436            0 : void Occluder::QueryFrame::cleanup()
    2437              : {
    2438            0 :     for(int i = 0; i < max; ++i)
    2439              :     {
    2440            0 :         glDeleteQueries(1, &queries[i].id);
    2441            0 :         queries[i].owner = nullptr;
    2442              :     }
    2443            0 :     cur = max = defer = 0;
    2444            0 : }
    2445              : 
    2446            0 : void occludequery::startquery() const
    2447              : {
    2448            0 :     glBeginQuery(querytarget(), this->id);
    2449            0 : }
    2450              : 
    2451            0 : void rendermapmodels()
    2452              : {
    2453              :     static int skipoq = 0;
    2454            0 :     bool doquery = !drawtex && oqfrags && oqmm;
    2455            0 :     const std::vector<extentity *> &ents = entities::getents();
    2456            0 :     findvisiblemms(ents, doquery);
    2457              : 
    2458            0 :     for(octaentities *oe = visiblemms; oe; oe = oe->next)
    2459              :     {
    2460            0 :         if(oe->distance>=0)
    2461              :         {
    2462            0 :             bool rendered = false;
    2463            0 :             for(uint i = 0; i < oe->mapmodels.size(); i++)
    2464              :             {
    2465            0 :                 extentity &e = *ents[oe->mapmodels[i]];
    2466            0 :                 if(!(e.flags&EntFlag_Render))
    2467              :                 {
    2468            0 :                     continue;
    2469              :                 }
    2470            0 :                 if(!rendered)
    2471              :                 {
    2472            0 :                     rendered = true;
    2473            0 :                     oe->query = doquery && oe->distance>0 && !(++skipoq%oqmm) ? occlusionengine.newquery(oe) : nullptr;
    2474            0 :                     if(oe->query)
    2475              :                     {
    2476            0 :                         occlusionengine.setupmodelquery(oe->query);
    2477              :                     }
    2478              :                 }
    2479            0 :                 rendermapmodel(e);
    2480            0 :                 e.flags &= ~EntFlag_Render;
    2481              :             }
    2482            0 :             if(rendered && oe->query)
    2483              :             {
    2484            0 :                 occlusionengine.endmodelquery();
    2485              :             }
    2486              :         }
    2487              :     }
    2488            0 :     batching::rendermapmodelbatches();
    2489            0 :     batching::clearbatchedmapmodels();
    2490              : 
    2491            0 :     bool queried = false;
    2492            0 :     for(octaentities *oe = visiblemms; oe; oe = oe->next)
    2493              :     {
    2494            0 :         if(oe->distance<0)
    2495              :         {
    2496            0 :             oe->query = doquery && !camera1->o.insidebb(oe->bbmin, oe->bbmax, 1) ? occlusionengine.newquery(oe) : nullptr;
    2497            0 :             if(!oe->query)
    2498              :             {
    2499            0 :                 continue;
    2500              :             }
    2501            0 :             if(!queried)
    2502              :             {
    2503            0 :                 startbb();
    2504            0 :                 queried = true;
    2505              :             }
    2506            0 :             oe->query->startquery();
    2507            0 :             drawbb(oe->bbmin, ivec(oe->bbmax).sub(oe->bbmin));
    2508            0 :             occlusionengine.endquery();
    2509              :         }
    2510              :     }
    2511            0 :     if(queried)
    2512              :     {
    2513            0 :         endbb();
    2514              :     }
    2515            0 : }
    2516              : 
    2517            0 : void renderoutline()
    2518              : {
    2519            0 :     ldrnotextureshader->set();
    2520              : 
    2521            0 :     gle::enablevertex();
    2522              : 
    2523            0 :     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    2524            0 :     gle::color(outlinecolor);
    2525              : 
    2526            0 :     enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
    2527              : 
    2528            0 :     if(!dtoutline)
    2529              :     {
    2530            0 :         glDisable(GL_DEPTH_TEST);
    2531              :     }
    2532            0 :     const vtxarray *prev = nullptr;
    2533            0 :     for(const vtxarray *va = visibleva; va; va = va->next)
    2534              :     {
    2535            0 :         if(va->occluded < Occlude_BB)
    2536              :         {
    2537            0 :             if((!va->texs || va->occluded >= Occlude_Geom) && !va->alphaback && !va->alphafront && !va->refracttris)
    2538              :             {
    2539            0 :                 continue;
    2540              :             }
    2541            0 :             if(!prev || va->vbuf != prev->vbuf)
    2542              :             {
    2543            0 :                 gle::bindvbo(va->vbuf);
    2544            0 :                 gle::bindebo(va->ebuf);
    2545            0 :                 const vertex *ptr = nullptr;
    2546            0 :                 gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note: intentional dereference of null pointer
    2547              :             }
    2548            0 :             if(va->texs && va->occluded < Occlude_Geom)
    2549              :             {
    2550            0 :                 drawvatris(*va, 3*va->tris, 0);
    2551            0 :                 xtravertsva += va->verts;
    2552              :             }
    2553            0 :             if(va->alphaback || va->alphafront || va->refract)
    2554              :             {
    2555            0 :                 drawvatris(*va, 3*(va->alphabacktris + va->alphafronttris + va->refracttris), 3*(va->tris));
    2556            0 :                 xtravertsva += 3*(va->alphabacktris + va->alphafronttris + va->refracttris);
    2557              :             }
    2558            0 :             prev = va;
    2559              :         }
    2560              :     }
    2561            0 :     if(!dtoutline)
    2562              :     {
    2563            0 :         glEnable(GL_DEPTH_TEST);
    2564              :     }
    2565            0 :     disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
    2566            0 :     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    2567            0 :     gle::clearvbo();
    2568            0 :     gle::clearebo();
    2569            0 :     gle::disablevertex();
    2570            0 : }
    2571              : 
    2572            0 : bool renderexplicitsky(bool outline)
    2573              : {
    2574            0 :     const vtxarray *prev = nullptr;
    2575            0 :     for(const vtxarray *va = visibleva; va; va = va->next)
    2576              :     {
    2577            0 :         if(va->sky && va->occluded < Occlude_BB &&
    2578            0 :             ((va->skymax.x >= 0 && view.isvisiblebb(va->skymin, ivec(va->skymax).sub(va->skymin)) != ViewFrustumCull_NotVisible) ||
    2579            0 :             !insideworld(camera1->o)))
    2580              :         {
    2581            0 :             if(!prev || va->vbuf != prev->vbuf)
    2582              :             {
    2583            0 :                 if(!prev)
    2584              :                 {
    2585            0 :                     gle::enablevertex();
    2586            0 :                     if(outline)
    2587              :                     {
    2588            0 :                         ldrnotextureshader->set();
    2589            0 :                         gle::color(explicitskycolor);
    2590            0 :                         glDepthMask(GL_FALSE);
    2591            0 :                         enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
    2592            0 :                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    2593              :                     }
    2594            0 :                     else if(editmode)
    2595              :                     {
    2596            0 :                         maskgbuffer("d");
    2597            0 :                         SETSHADER(depth);
    2598              :                     }
    2599              :                     else
    2600              :                     {
    2601            0 :                         nocolorshader->set();
    2602            0 :                         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    2603              :                     }
    2604              :                 }
    2605            0 :                 gle::bindvbo(va->vbuf);
    2606            0 :                 gle::bindebo(va->skybuf);
    2607            0 :                 const vertex *ptr = 0;
    2608            0 :                 gle::vertexpointer(sizeof(vertex), ptr->pos.data());
    2609              :             }
    2610            0 :             drawvaskytris(*va);
    2611            0 :             xtraverts += va->sky;
    2612            0 :             prev = va;
    2613              :         }
    2614              :     }
    2615            0 :     if(!prev)
    2616              :     {
    2617            0 :         return false;
    2618              :     }
    2619            0 :     if(outline)
    2620              :     {
    2621            0 :         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    2622            0 :         disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
    2623            0 :         glDepthMask(GL_TRUE);
    2624              :     }
    2625            0 :     else if(editmode)
    2626              :     {
    2627            0 :         maskgbuffer("cnd");
    2628              :     }
    2629              :     else
    2630              :     {
    2631            0 :         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    2632              :     }
    2633            0 :     gle::disablevertex();
    2634            0 :     gle::clearvbo();
    2635            0 :     gle::clearebo();
    2636            0 :     return true;
    2637              : }
    2638              : 
    2639            0 : void cubeworld::cleanupva()
    2640              : {
    2641            0 :     clearvas(*worldroot);
    2642            0 :     occlusionengine.clearqueries();
    2643            0 :     cleanupbb();
    2644            0 :     cleanupgrass();
    2645            0 : }
    2646              : 
    2647            0 : GBuffer::AlphaInfo GBuffer::findalphavas()
    2648              : {
    2649            0 :     alphavas.clear();
    2650              :     AlphaInfo a;
    2651            0 :     a.alphafrontsx1 = a.alphafrontsy1 = a.alphabacksx1 = a.alphabacksy1 = a.alpharefractsx1 = a.alpharefractsy1 = 1;
    2652            0 :     a.alphafrontsx2 = a.alphafrontsy2 = a.alphabacksx2 = a.alphabacksy2 = a.alpharefractsx2 = a.alpharefractsy2 = -1;
    2653            0 :     int alphabackvas = 0,
    2654            0 :         alpharefractvas = 0;
    2655            0 :     alphatiles.fill(0);
    2656            0 :     for(const vtxarray *va = visibleva; va; va = va->next)
    2657              :     {
    2658            0 :         if(va->alphabacktris || va->alphafronttris || va->refracttris)
    2659              :         {
    2660            0 :             if(va->occluded >= Occlude_BB)
    2661              :             {
    2662            0 :                 continue;
    2663              :             }
    2664            0 :             if(va->curvfc==ViewFrustumCull_Fogged)
    2665              :             {
    2666            0 :                 continue;
    2667              :             }
    2668            0 :             float sx1 = -1,
    2669            0 :                   sx2 =  1,
    2670            0 :                   sy1 = -1,
    2671            0 :                   sy2 =  1;
    2672            0 :             if(!calcbbscissor(va->alphamin, va->alphamax, sx1, sy1, sx2, sy2))
    2673              :             {
    2674            0 :                 continue;
    2675              :             }
    2676            0 :             alphavas.push_back(va);
    2677            0 :             masktiles(alphatiles.data(), sx1, sy1, sx2, sy2);
    2678            0 :             a.alphafrontsx1 = std::min(a.alphafrontsx1, sx1);
    2679            0 :             a.alphafrontsy1 = std::min(a.alphafrontsy1, sy1);
    2680            0 :             a.alphafrontsx2 = std::max(a.alphafrontsx2, sx2);
    2681            0 :             a.alphafrontsy2 = std::max(a.alphafrontsy2, sy2);
    2682            0 :             if(va->alphabacktris)
    2683              :             {
    2684            0 :                 alphabackvas++;
    2685            0 :                 a.alphabacksx1 = std::min(a.alphabacksx1, sx1);
    2686            0 :                 a.alphabacksy1 = std::min(a.alphabacksy1, sy1);
    2687            0 :                 a.alphabacksx2 = std::max(a.alphabacksx2, sx2);
    2688            0 :                 a.alphabacksy2 = std::max(a.alphabacksy2, sy2);
    2689              :             }
    2690            0 :             if(va->refracttris)
    2691              :             {
    2692            0 :                 if(!calcbbscissor(va->refractmin, va->refractmax, sx1, sy1, sx2, sy2))
    2693              :                 {
    2694            0 :                     continue;
    2695              :                 }
    2696            0 :                 alpharefractvas++;
    2697            0 :                 a.alpharefractsx1 = std::min(a.alpharefractsx1, sx1);
    2698            0 :                 a.alpharefractsy1 = std::min(a.alpharefractsy1, sy1);
    2699            0 :                 a.alpharefractsx2 = std::max(a.alpharefractsx2, sx2);
    2700            0 :                 a.alpharefractsy2 = std::max(a.alpharefractsy2, sy2);
    2701              :             }
    2702              :         }
    2703              :     }
    2704            0 :     a.hasalphavas = (alpharefractvas ? 4 : 0) | (alphavas.size() ? 2 : 0) | (alphabackvas ? 1 : 0);
    2705            0 :     return a;
    2706              : }
    2707              : 
    2708            0 : void renderrefractmask()
    2709              : {
    2710            0 :     gle::enablevertex();
    2711              : 
    2712            0 :     const vtxarray *prev = nullptr;
    2713            0 :     for(const vtxarray *va : alphavas)
    2714              :     {
    2715            0 :         if(!va->refracttris)
    2716              :         {
    2717            0 :             continue;
    2718              :         }
    2719            0 :         if(!prev || va->vbuf != prev->vbuf)
    2720              :         {
    2721            0 :             gle::bindvbo(va->vbuf);
    2722            0 :             gle::bindebo(va->ebuf);
    2723            0 :             const vertex *ptr = nullptr;
    2724            0 :             gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note: intentional deref of null pointer
    2725              :         }
    2726            0 :         drawvatris(*va, 3*va->refracttris, 3*(va->tris + va->alphabacktris + va->alphafronttris));
    2727            0 :         xtravertsva += 3*va->refracttris;
    2728            0 :         prev = va;
    2729              :     }
    2730              : 
    2731            0 :     gle::clearvbo();
    2732            0 :     gle::clearebo();
    2733            0 :     gle::disablevertex();
    2734            0 : }
    2735              : 
    2736            0 : void renderalphageom(int side)
    2737              : {
    2738            0 :     resetbatches();
    2739              : 
    2740            0 :     RenderState cur;
    2741            0 :     cur.alphaing = side;
    2742            0 :     cur.invalidatealphascale();
    2743              : 
    2744            0 :     setupgeom();
    2745              : 
    2746            0 :     if(side == 2)
    2747              :     {
    2748            0 :         for(const vtxarray *i : alphavas)
    2749              :         {
    2750            0 :             renderva(cur, *i, RenderPass_GBuffer);
    2751              :         }
    2752            0 :         if(geombatches.size())
    2753              :         {
    2754            0 :             cur.renderbatches(RenderPass_GBuffer);
    2755              :         }
    2756              :     }
    2757              :     else
    2758              :     {
    2759            0 :         glCullFace(GL_FRONT);
    2760            0 :         for(const vtxarray *i : alphavas)
    2761              :         {
    2762            0 :             if(i->alphabacktris)
    2763              :             {
    2764            0 :                 renderva(cur, *i, RenderPass_GBuffer);
    2765              :             }
    2766              :         }
    2767            0 :         if(geombatches.size())
    2768              :         {
    2769            0 :             cur.renderbatches(RenderPass_GBuffer);
    2770              :         }
    2771            0 :         glCullFace(GL_BACK);
    2772              :     }
    2773              : 
    2774            0 :     cur.cleanupgeom();
    2775            0 : }
    2776              : 
    2777            0 : void GBuffer::rendergeom()
    2778              : {
    2779            0 :     bool doOQ = oqfrags && oqgeom && !drawtex,
    2780            0 :          multipassing = false;
    2781            0 :     RenderState cur;
    2782              : 
    2783            0 :     if(doOQ)
    2784              :     {
    2785            0 :         for(vtxarray *va = visibleva; va; va = va->next)
    2786              :         {
    2787            0 :             if(va->texs)
    2788              :             {
    2789            0 :                 if(!camera1->o.insidebb(va->o, va->size, 2))
    2790              :                 {
    2791            0 :                     if(va->parent && va->parent->occluded >= Occlude_BB)
    2792              :                     {
    2793            0 :                         va->query = nullptr;
    2794            0 :                         va->occluded = Occlude_Parent;
    2795            0 :                         continue;
    2796              :                     }
    2797            0 :                     va->occluded = va->query && va->query->owner == va && occlusionengine.checkquery(va->query) ?
    2798            0 :                                    std::min(va->occluded+1, static_cast<int>(Occlude_BB)) :
    2799              :                                    Occlude_Nothing;
    2800            0 :                     va->query = occlusionengine.newquery(va);
    2801            0 :                     if(!va->query || !va->occluded)
    2802              :                     {
    2803            0 :                         va->occluded = Occlude_Nothing;
    2804              :                     }
    2805            0 :                     if(va->occluded >= Occlude_Geom)
    2806              :                     {
    2807            0 :                         if(va->query)
    2808              :                         {
    2809            0 :                             if(cur.vattribs)
    2810              :                             {
    2811            0 :                                 cur.disablevattribs(false);
    2812              :                             }
    2813            0 :                             if(cur.vbuf)
    2814              :                             {
    2815            0 :                                 cur.disablevbuf();
    2816              :                             }
    2817            0 :                             renderquery(cur, *va->query, *va);
    2818              :                         }
    2819            0 :                         continue;
    2820              :                     }
    2821              :                 }
    2822              :                 else
    2823              :                 {
    2824            0 :                     va->query = nullptr;
    2825            0 :                     va->occluded = Occlude_Nothing;
    2826            0 :                     if(va->occluded >= Occlude_Geom)
    2827              :                     {
    2828            0 :                         continue;
    2829              :                     }
    2830              :                 }
    2831            0 :                 renderva(cur, *va, RenderPass_Z, true);
    2832              :             }
    2833              :         }
    2834              : 
    2835            0 :         if(cur.vquery)
    2836              :         {
    2837            0 :             cur.disablevquery();
    2838              :         }
    2839            0 :         if(cur.vattribs)
    2840              :         {
    2841            0 :             cur.disablevattribs(false);
    2842              :         }
    2843            0 :         if(cur.vbuf)
    2844              :         {
    2845            0 :             cur.disablevbuf();
    2846              :         }
    2847            0 :         glFlush();
    2848            0 :         if(cur.colormask)
    2849              :         {
    2850            0 :             cur.colormask = false;
    2851            0 :             glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    2852              :         }
    2853            0 :         if(cur.depthmask)
    2854              :         {
    2855            0 :             cur.depthmask = false;
    2856            0 :             glDepthMask(GL_FALSE);
    2857              :         }
    2858            0 :         workinoq();
    2859            0 :         if(!cur.colormask)
    2860              :         {
    2861            0 :             cur.colormask = true;
    2862            0 :             glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    2863              :         }
    2864            0 :         if(!cur.depthmask)
    2865              :         {
    2866            0 :             cur.depthmask = true;
    2867            0 :             glDepthMask(GL_TRUE);
    2868              :         }
    2869            0 :         if(!multipassing)
    2870              :         {
    2871            0 :             multipassing = true;
    2872            0 :             glDepthFunc(GL_LEQUAL);
    2873              :         }
    2874            0 :         cur.invalidatetexgenorient();
    2875            0 :         setupgeom();
    2876            0 :         resetbatches();
    2877            0 :         for(const vtxarray *va = visibleva; va; va = va->next)
    2878              :         {
    2879            0 :             if(va->texs && va->occluded < Occlude_Geom)
    2880              :             {
    2881            0 :                 renderva(cur, *va, RenderPass_GBuffer);
    2882              :             }
    2883              :         }
    2884            0 :         if(geombatches.size())
    2885              :         {
    2886            0 :             cur.renderbatches(RenderPass_GBuffer);
    2887            0 :             glFlush();
    2888              :         }
    2889            0 :         for(vtxarray *va = visibleva; va; va = va->next)
    2890              :         {
    2891            0 :             if(va->texs && va->occluded >= Occlude_Geom)
    2892              :             {
    2893            0 :                 if((va->parent && va->parent->occluded >= Occlude_BB) || (va->query && occlusionengine.checkquery(va->query)))
    2894              :                 {
    2895            0 :                     va->occluded = Occlude_BB;
    2896            0 :                     continue;
    2897              :                 }
    2898              :                 else
    2899              :                 {
    2900            0 :                     va->occluded = Occlude_Nothing;
    2901            0 :                     if(va->occluded >= Occlude_Geom)
    2902              :                     {
    2903            0 :                         continue;
    2904              :                     }
    2905              :                 }
    2906              : 
    2907            0 :                 renderva(cur, *va, RenderPass_GBuffer);
    2908              :             }
    2909              :         }
    2910            0 :         if(geombatches.size())
    2911              :         {
    2912            0 :             cur.renderbatches(RenderPass_GBuffer);
    2913              :         }
    2914              :     }
    2915              :     else
    2916              :     {
    2917            0 :         setupgeom();
    2918            0 :         resetbatches();
    2919            0 :         for(vtxarray *va = visibleva; va; va = va->next)
    2920              :         {
    2921            0 :             if(va->texs)
    2922              :             {
    2923            0 :                 va->query = nullptr;
    2924            0 :                 va->occluded = Occlude_Nothing;
    2925            0 :                 if(va->occluded >= Occlude_Geom)
    2926              :                 {
    2927            0 :                     continue;
    2928              :                 }
    2929            0 :                 renderva(cur, *va, RenderPass_GBuffer);
    2930              :             }
    2931              :         }
    2932            0 :         if(geombatches.size())
    2933              :         {
    2934            0 :             cur.renderbatches(RenderPass_GBuffer);
    2935              :         }
    2936              :     }
    2937            0 :     if(multipassing)
    2938              :     {
    2939            0 :         glDepthFunc(GL_LESS);
    2940              :     }
    2941            0 :     cur.cleanupgeom();
    2942            0 :     if(!doOQ)
    2943              :     {
    2944            0 :         glFlush();
    2945            0 :         if(cur.colormask)
    2946              :         {
    2947            0 :             cur.colormask = false;
    2948            0 :             glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    2949              :         }
    2950            0 :         if(cur.depthmask)
    2951              :         {
    2952            0 :             cur.depthmask = false;
    2953            0 :             glDepthMask(GL_FALSE);
    2954              :         }
    2955            0 :         workinoq();
    2956            0 :         if(!cur.colormask)
    2957              :         {
    2958            0 :             cur.colormask = true;
    2959            0 :             glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    2960              :         }
    2961            0 :         if(!cur.depthmask)
    2962              :         {
    2963            0 :             cur.depthmask = true;
    2964            0 :             glDepthMask(GL_TRUE);
    2965              :         }
    2966              :     }
    2967            0 : }
    2968              : 
    2969            0 : void renderdecals()
    2970              : {
    2971              :     const vtxarray *decalva;
    2972            0 :     for(decalva = visibleva; decalva; decalva = decalva->next)
    2973              :     {
    2974            0 :         if(decalva->decaltris && decalva->occluded < Occlude_BB)
    2975              :         {
    2976            0 :             break;
    2977              :         }
    2978              :     }
    2979            0 :     if(!decalva)
    2980              :     {
    2981            0 :         return;
    2982              :     }
    2983            0 :     decalrenderer cur;
    2984              : 
    2985            0 :     setupdecals();
    2986            0 :     resetdecalbatches();
    2987              : 
    2988            0 :     if(maxdualdrawbufs)
    2989              :     {
    2990            0 :         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC1_ALPHA);
    2991            0 :         maskgbuffer("c");
    2992            0 :         for(const vtxarray *va = decalva; va; va = va->next)
    2993              :         {
    2994            0 :             if(va->decaltris && va->occluded < Occlude_BB)
    2995              :             {
    2996            0 :                 mergedecals(*va);
    2997            0 :                 if(!batchdecals && decalbatches.size())
    2998              :                 {
    2999            0 :                     cur.renderdecalbatches(0);
    3000              :                 }
    3001              :             }
    3002              :         }
    3003            0 :         if(decalbatches.size())
    3004              :         {
    3005            0 :             cur.renderdecalbatches(0);
    3006              :         }
    3007            0 :         if(usepacknorm())
    3008              :         {
    3009            0 :             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    3010            0 :             glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
    3011              :         }
    3012              :         else
    3013              :         {
    3014            0 :             glBlendFunc(GL_SRC1_ALPHA, GL_ONE_MINUS_SRC1_ALPHA);
    3015              :         }
    3016            0 :         maskgbuffer("n");
    3017            0 :         cur.vbuf = 0;
    3018            0 :         for(const vtxarray *va = decalva; va; va = va->next)
    3019              :         {
    3020            0 :             if(va->decaltris && va->occluded < Occlude_BB)
    3021              :             {
    3022            0 :                 mergedecals(*va);
    3023            0 :                 if(!batchdecals && decalbatches.size())
    3024              :                 {
    3025            0 :                     cur.renderdecalbatches(1);
    3026              :                 }
    3027              :             }
    3028              :         }
    3029            0 :         if(decalbatches.size())
    3030              :         {
    3031            0 :             cur.renderdecalbatches(1);
    3032              :         }
    3033              :     }
    3034              :     else
    3035              :     {
    3036            0 :         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    3037            0 :         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
    3038            0 :         maskgbuffer("cn");
    3039            0 :         for(const vtxarray *va = decalva; va; va = va->next)
    3040              :         {
    3041            0 :             if(va->decaltris && va->occluded < Occlude_BB)
    3042              :             {
    3043            0 :                 mergedecals(*va);
    3044            0 :                 if(!batchdecals && decalbatches.size())
    3045              :                 {
    3046            0 :                     cur.renderdecalbatches(0);
    3047              :                 }
    3048              :             }
    3049              :         }
    3050            0 :         if(decalbatches.size())
    3051              :         {
    3052            0 :             cur.renderdecalbatches(0);
    3053              :         }
    3054              :     }
    3055            0 :     cleanupdecals();
    3056              : }
    3057              : 
    3058              : //shadowmeshes
    3059              : 
    3060            1 : void clearshadowmeshes()
    3061              : {
    3062            1 :     if(shadowvbos.size())
    3063              :     {
    3064            0 :         glDeleteBuffers(shadowvbos.size(), shadowvbos.data());
    3065            0 :         shadowvbos.clear();
    3066              :     }
    3067            1 :     if(shadowmeshes.size())
    3068              :     {
    3069            0 :         std::vector<extentity *> &ents = entities::getents();
    3070            0 :         for(uint i = 0; i < ents.size(); i++)
    3071              :         {
    3072            0 :             extentity &e = *ents[i];
    3073            0 :             if(e.flags&EntFlag_ShadowMesh)
    3074              :             {
    3075            0 :                 e.flags &= ~EntFlag_ShadowMesh;
    3076              :             }
    3077              :         }
    3078              :     }
    3079            1 :     shadowmeshes.clear();
    3080            1 :     shadowdraws.clear();
    3081            1 : }
    3082              : 
    3083            0 : void genshadowmeshes()
    3084              : {
    3085            0 :     clearshadowmeshes();
    3086              : 
    3087            0 :     if(!smmesh)
    3088              :     {
    3089            0 :         return;
    3090              :     }
    3091            0 :     renderprogress(0, "generating shadow meshes..");
    3092              : 
    3093            0 :     std::vector<extentity *> &ents = entities::getents();
    3094            0 :     for(uint i = 0; i < ents.size(); i++)
    3095              :     {
    3096            0 :         extentity &e = *ents[i];
    3097            0 :         if(e.type != EngineEnt_Light)
    3098              :         {
    3099            0 :             continue;
    3100              :         }
    3101            0 :         genshadowmesh(i, e);
    3102              :     }
    3103              : }
    3104              : 
    3105            0 : shadowmesh *findshadowmesh(int idx, const extentity &e)
    3106              : {
    3107            0 :     auto itr = shadowmeshes.find(idx);
    3108            0 :     if(itr == shadowmeshes.end()
    3109            0 :     || (*itr).second.type != shadowmapping
    3110            0 :     || (*itr).second.origin != shadoworigin
    3111            0 :     || (*itr).second.radius < shadowradius)
    3112              :     {
    3113            0 :         return nullptr;
    3114              :     }
    3115            0 :     switch((*itr).second.type)
    3116              :     {
    3117            0 :         case ShadowMap_Spot:
    3118              :         {
    3119            0 :             if(!e.attached || e.attached->type != EngineEnt_Spotlight || (*itr).second.spotloc != e.attached->o || (*itr).second.spotangle < std::clamp(static_cast<int>(e.attached->attr1), 1, 89))
    3120              :             {
    3121            0 :                 return nullptr;
    3122              :             }
    3123            0 :             break;
    3124              :         }
    3125              :     }
    3126            0 :     return &(*itr).second;
    3127              : }
    3128              : 
    3129            0 : void rendershadowmesh(const shadowmesh *m)
    3130              : {
    3131            0 :     int draw = m->draws[shadowside];
    3132            0 :     if(draw < 0)
    3133              :     {
    3134            0 :         return;
    3135              :     }
    3136            0 :     SETSHADER(shadowmapworld);
    3137            0 :     gle::enablevertex();
    3138            0 :     GLuint ebuf = 0,
    3139            0 :            vbuf = 0;
    3140            0 :     while(draw >= 0)
    3141              :     {
    3142            0 :         shadowdraw &d = shadowdraws[draw];
    3143            0 :         if(ebuf != d.ebuf)
    3144              :         {
    3145            0 :             gle::bindebo(d.ebuf);
    3146            0 :             ebuf = d.ebuf;
    3147              :         }
    3148            0 :         if(vbuf != d.vbuf)
    3149              :         {
    3150            0 :             gle::bindvbo(d.vbuf);
    3151            0 :             vbuf = d.vbuf; gle::vertexpointer(sizeof(vec), 0);
    3152              :         }
    3153            0 :         drawtris(3*d.tris, static_cast<ushort *>(nullptr) + d.offset, d.minvert, d.maxvert);
    3154            0 :         xtravertsva += 3*d.tris;
    3155            0 :         draw = d.next;
    3156              :     }
    3157            0 :     gle::disablevertex();
    3158            0 :     gle::clearebo();
    3159            0 :     gle::clearvbo();
    3160              : }
    3161              : 
    3162              : //external api functions
    3163            0 : int calcspheresidemask(const vec &p, float radius, float bias)
    3164              : {
    3165              :     // p is in the cubemap's local coordinate system
    3166              :     // bias = border/(size - border)
    3167            0 :     float dxyp = p.x + p.y,
    3168            0 :           dxyn = p.x - p.y,
    3169            0 :           axyp = std::fabs(dxyp),
    3170            0 :           axyn = std::fabs(dxyn),
    3171            0 :           dyzp = p.y + p.z,
    3172            0 :           dyzn = p.y - p.z,
    3173            0 :           ayzp = std::fabs(dyzp),
    3174            0 :           ayzn = std::fabs(dyzn),
    3175            0 :           dzxp = p.z + p.x,
    3176            0 :           dzxn = p.z - p.x,
    3177            0 :           azxp = std::fabs(dzxp),
    3178            0 :           azxn = std::fabs(dzxn);
    3179            0 :     int mask = 0x3F;
    3180            0 :     radius *= SQRT2;
    3181            0 :     if(axyp > bias*axyn + radius)
    3182              :     {
    3183            0 :         mask &= dxyp < 0 ? ~((1<<0)|(1<<2)) : ~((2<<0)|(2<<2));
    3184              :     }
    3185            0 :     if(axyn > bias*axyp + radius)
    3186              :     {
    3187            0 :         mask &= dxyn < 0 ? ~((1<<0)|(2<<2)) : ~((2<<0)|(1<<2));
    3188              :     }
    3189            0 :     if(ayzp > bias*ayzn + radius)
    3190              :     {
    3191            0 :         mask &= dyzp < 0 ? ~((1<<2)|(1<<4)) : ~((2<<2)|(2<<4));
    3192              :     }
    3193            0 :     if(ayzn > bias*ayzp + radius)
    3194              :     {
    3195            0 :         mask &= dyzn < 0 ? ~((1<<2)|(2<<4)) : ~((2<<2)|(1<<4));
    3196              :     }
    3197            0 :     if(azxp > bias*azxn + radius)
    3198              :     {
    3199            0 :         mask &= dzxp < 0 ? ~((1<<4)|(1<<0)) : ~((2<<4)|(2<<0));
    3200              :     }
    3201            0 :     if(azxn > bias*azxp + radius)
    3202              :     {
    3203            0 :         mask &= dzxn < 0 ? ~((1<<4)|(2<<0)) : ~((2<<4)|(1<<0));
    3204              :     }
    3205            0 :     return mask;
    3206              : }
    3207              : 
    3208            0 : int vfc::cullfrustumsides(const vec &lightpos, float lightradius, float size, float border)
    3209              : {
    3210            0 :     int sides = 0x3F,
    3211            0 :         masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
    3212            0 :     float scale = (size - 2*border)/size,
    3213            0 :           bias = border / static_cast<float>(size - border);
    3214              :     // check if cone enclosing side would cross frustum plane
    3215            0 :     scale = 2 / (scale*scale + 2);
    3216            0 :     for(int i = 0; i < 5; ++i)
    3217              :     {
    3218            0 :         if(vfcP[i].dist(lightpos) <= -0.03125f)
    3219              :         {
    3220            0 :             vec n = vec(vfcP[i]).div(lightradius);
    3221            0 :             float len = scale*n.squaredlen();
    3222            0 :             if(n.x*n.x > len)
    3223              :             {
    3224            0 :                 sides &= n.x < 0 ? ~(1<<0) : ~(2 << 0);
    3225              :             }
    3226            0 :             if(n.y*n.y > len)
    3227              :             {
    3228            0 :                 sides &= n.y < 0 ? ~(1<<2) : ~(2 << 2);
    3229              :             }
    3230            0 :             if(n.z*n.z > len)
    3231              :             {
    3232            0 :                 sides &= n.z < 0 ? ~(1<<4) : ~(2 << 4);
    3233              :             }
    3234              :         }
    3235              :     }
    3236            0 :     if (vfcP[4].dist(lightpos) >= vfcDfog + 0.03125f)
    3237              :     {
    3238            0 :         vec n = vec(vfcP[4]).div(lightradius);
    3239            0 :         float len = scale*n.squaredlen();
    3240            0 :         if(n.x*n.x > len)
    3241              :         {
    3242            0 :             sides &= n.x >= 0 ? ~(1<<0) : ~(2 << 0);
    3243              :         }
    3244            0 :         if(n.y*n.y > len)
    3245              :         {
    3246            0 :             sides &= n.y >= 0 ? ~(1<<2) : ~(2 << 2);
    3247              :         }
    3248            0 :         if(n.z*n.z > len)
    3249              :         {
    3250            0 :             sides &= n.z >= 0 ? ~(1<<4) : ~(2 << 4);
    3251              :         }
    3252              :     }
    3253              :     // this next test usually clips off more sides than the former, but occasionally clips fewer/different ones, so do both and combine results
    3254              :     // check if frustum corners/origin cross plane sides
    3255              :     // infinite version, assumes frustum corners merely give direction and extend to infinite distance
    3256            0 :     vec p = vec(camera1->o).sub(lightpos).div(lightradius);
    3257            0 :     float dp = p.x + p.y,
    3258            0 :           dn = p.x - p.y,
    3259            0 :           ap = std::fabs(dp),
    3260            0 :           an = std::fabs(dn);
    3261            0 :     masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
    3262            0 :     masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
    3263            0 :     dp = p.y + p.z, dn = p.y - p.z,
    3264            0 :     ap = std::fabs(dp),
    3265            0 :     an = std::fabs(dn);
    3266            0 :     masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
    3267            0 :     masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
    3268            0 :     dp = p.z + p.x,
    3269            0 :     dn = p.z - p.x,
    3270            0 :     ap = std::fabs(dp),
    3271            0 :     an = std::fabs(dn);
    3272            0 :     masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
    3273            0 :     masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
    3274            0 :     for(int i = 0; i < 4; ++i)
    3275              :     {
    3276            0 :         vec n;
    3277            0 :         switch(i)
    3278              :         {
    3279            0 :             case 0:
    3280              :             {
    3281            0 :                 n.cross(vfcP[0], vfcP[2]);
    3282            0 :                 break;
    3283              :             }
    3284            0 :             case 1:
    3285              :             {
    3286            0 :                 n.cross(vfcP[3], vfcP[0]);
    3287            0 :                 break;
    3288              :             }
    3289            0 :             case 2:
    3290              :             {
    3291            0 :                 n.cross(vfcP[2], vfcP[1]);
    3292            0 :                 break;
    3293              :             }
    3294            0 :             case 3:
    3295              :             {
    3296            0 :                 n.cross(vfcP[1], vfcP[3]);
    3297            0 :                 break;
    3298              :             }
    3299              :         }
    3300            0 :         dp = n.x + n.y,
    3301            0 :         dn = n.x - n.y,
    3302            0 :         ap = std::fabs(dp),
    3303            0 :         an = std::fabs(dn);
    3304            0 :         if(ap > 0)
    3305              :         {
    3306            0 :             masks[0] |= dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2);
    3307              :         }
    3308            0 :         if(an > 0)
    3309              :         {
    3310            0 :             masks[1] |= dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2);
    3311              :         }
    3312            0 :         dp = n.y + n.z,
    3313            0 :         dn = n.y - n.z,
    3314            0 :         ap = std::fabs(dp),
    3315            0 :         an = std::fabs(dn);
    3316            0 :         if(ap > 0)
    3317              :         {
    3318            0 :             masks[2] |= dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4);
    3319              :         }
    3320            0 :         if(an > 0)
    3321              :         {
    3322            0 :             masks[3] |= dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4);
    3323              :         }
    3324            0 :         dp = n.z + n.x,
    3325            0 :         dn = n.z - n.x,
    3326            0 :         ap = std::fabs(dp),
    3327            0 :         an = std::fabs(dn);
    3328            0 :         if(ap > 0)
    3329              :         {
    3330            0 :             masks[4] |= dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0);
    3331              :         }
    3332            0 :         if(an > 0)
    3333              :         {
    3334            0 :             masks[5] |= dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0);
    3335              :         }
    3336              :     }
    3337            0 :     return sides & masks[0] & masks[1] & masks[2] & masks[3] & masks[4] & masks[5];
    3338              : }
    3339              : 
    3340            0 : static void findshadowvas(std::vector<vtxarray *> &vas, std::array<vtxarray *, vasortsize> &vasort)
    3341              : {
    3342            0 :     for(vtxarray *&v : vas)
    3343              :     {
    3344            0 :         float dist = vadist(*v, shadoworigin);
    3345            0 :         if(dist < shadowradius || !smdistcull)
    3346              :         {
    3347            0 :             v->shadowmask = !smbbcull ? 0x3F : (v->children.size() || v->mapmodels.size() ?
    3348            0 :                                 calcbbsidemask(v->bbmin, v->bbmax, shadoworigin, shadowradius, shadowbias) :
    3349            0 :                                 calcbbsidemask(v->geommin, v->geommax, shadoworigin, shadowradius, shadowbias));
    3350            0 :             addshadowva(v, dist, vasort);
    3351            0 :             if(v->children.size())
    3352              :             {
    3353            0 :                 findshadowvas(v->children, vasort);
    3354              :             }
    3355              :         }
    3356              :     }
    3357            0 : }
    3358              : 
    3359            0 : void renderrsmgeom(bool dyntex)
    3360              : {
    3361            0 :     RenderState cur;
    3362            0 :     if(!dyntex)
    3363              :     {
    3364            0 :         cur.cleartexgenmillis();
    3365              :     }
    3366            0 :     setupgeom();
    3367            0 :     if(skyshadow)
    3368              :     {
    3369            0 :         cur.enablevattribs(false);
    3370            0 :         SETSHADER(rsmsky);
    3371            0 :         vtxarray *prev = nullptr;
    3372            0 :         for(vtxarray *va = shadowva; va; va = va->rnext)
    3373              :         {
    3374            0 :             if(va->sky)
    3375              :             {
    3376            0 :                 if(!prev || va->vbuf != prev->vbuf)
    3377              :                 {
    3378            0 :                     gle::bindvbo(va->vbuf);
    3379            0 :                     gle::bindebo(va->skybuf);
    3380            0 :                     const vertex *ptr = nullptr; //note: offset of nullptr is technically UB
    3381            0 :                     gle::vertexpointer(sizeof(vertex), ptr->pos.data());
    3382              :                 }
    3383            0 :                 drawvaskytris(*va);
    3384            0 :                 xtravertsva += va->sky/3;
    3385            0 :                 prev = va;
    3386              :             }
    3387              :         }
    3388            0 :         if(cur.vattribs)
    3389              :         {
    3390            0 :             cur.disablevattribs(false);
    3391              :         }
    3392              :     }
    3393            0 :     resetbatches();
    3394            0 :     for(vtxarray *va = shadowva; va; va = va->rnext)
    3395              :     {
    3396            0 :         if(va->texs)
    3397              :         {
    3398            0 :             renderva(cur, *va, RenderPass_ReflectiveShadowMap);
    3399              :         }
    3400              :     }
    3401            0 :     if(geombatches.size())
    3402              :     {
    3403            0 :         cur.renderbatches(RenderPass_ReflectiveShadowMap);
    3404              :     }
    3405            0 :     cur.cleanupgeom();
    3406            0 : }
    3407              : 
    3408            0 : void dynamicshadowvabounds(int mask, vec &bbmin, vec &bbmax)
    3409              : {
    3410            0 :     for(vtxarray *va = shadowva; va; va = va->rnext)
    3411              :     {
    3412            0 :         if(va->shadowmask&mask && va->dyntexs)
    3413              :         {
    3414            0 :             bbmin.min(vec(va->geommin));
    3415            0 :             bbmax.max(vec(va->geommax));
    3416              :         }
    3417              :     }
    3418            0 : }
    3419              : 
    3420            0 : void findshadowmms()
    3421              : {
    3422            0 :     shadowmms = nullptr;
    3423            0 :     octaentities **lastmms = &shadowmms;
    3424            0 :     for(vtxarray *va = shadowva; va; va = va->rnext)
    3425              :     {
    3426            0 :         for(uint j = 0; j < va->mapmodels.size(); j++)
    3427              :         {
    3428            0 :             octaentities *oe = va->mapmodels[j];
    3429            0 :             switch(shadowmapping)
    3430              :             {
    3431            0 :                 case ShadowMap_Reflect:
    3432              :                 {
    3433            0 :                     break;
    3434              :                 }
    3435            0 :                 case ShadowMap_Cascade:
    3436              :                 {
    3437            0 :                     if(!csm.calcbbsplits(oe->bbmin, oe->bbmax))
    3438              :                     {
    3439            0 :                         continue;
    3440              :                     }
    3441            0 :                     break;
    3442              :                 }
    3443            0 :                 case ShadowMap_CubeMap:
    3444              :                 {
    3445            0 :                     if(smdistcull && shadoworigin.dist_to_bb(oe->bbmin, oe->bbmax) >= shadowradius)
    3446              :                     {
    3447            0 :                         continue;
    3448              :                     }
    3449            0 :                     break;
    3450              :                 }
    3451            0 :                 case ShadowMap_Spot:
    3452              :                 {
    3453            0 :                     if(smdistcull && shadoworigin.dist_to_bb(oe->bbmin, oe->bbmax) >= shadowradius)
    3454              :                     {
    3455            0 :                         continue;
    3456              :                     }
    3457            0 :                     if(smbbcull && !bbinsidespot(shadoworigin, shadowdir, shadowspot, oe->bbmin, oe->bbmax))
    3458              :                     {
    3459            0 :                         continue;
    3460              :                     }
    3461            0 :                     break;
    3462              :                 }
    3463              :             }
    3464            0 :             oe->rnext = nullptr;
    3465            0 :             *lastmms = oe;
    3466            0 :             lastmms = &oe->rnext;
    3467              :         }
    3468              :     }
    3469            0 : }
    3470              : 
    3471            0 : void rendershadowmapworld()
    3472              : {
    3473            0 :     SETSHADER(shadowmapworld);
    3474              : 
    3475            0 :     gle::enablevertex();
    3476              : 
    3477            0 :     vtxarray *prev = nullptr;
    3478            0 :     for(vtxarray *va = shadowva; va; va = va->rnext)
    3479              :     {
    3480            0 :         if(va->tris && va->shadowmask&(1<<shadowside))
    3481              :         {
    3482            0 :             if(!prev || va->vbuf != prev->vbuf)
    3483              :             {
    3484            0 :                 gle::bindvbo(va->vbuf);
    3485            0 :                 gle::bindebo(va->ebuf);
    3486            0 :                 const vertex *ptr = 0;
    3487            0 :                 gle::vertexpointer(sizeof(vertex), ptr->pos.data());
    3488              :             }
    3489            0 :             if(!smnodraw)
    3490              :             {
    3491            0 :                 drawvatris(*va, 3*va->tris, 0);
    3492              :             }
    3493            0 :             xtravertsva += va->verts;
    3494            0 :             prev = va;
    3495              :         }
    3496              :     }
    3497            0 :     if(skyshadow)
    3498              :     {
    3499            0 :         prev = nullptr;
    3500            0 :         for(vtxarray *va = shadowva; va; va = va->rnext)
    3501              :         {
    3502            0 :             if(va->sky && va->shadowmask&(1<<shadowside))
    3503              :             {
    3504            0 :                 if(!prev || va->vbuf != prev->vbuf)
    3505              :                 {
    3506            0 :                     gle::bindvbo(va->vbuf);
    3507            0 :                     gle::bindebo(va->skybuf);
    3508            0 :                     const vertex *ptr = 0;
    3509            0 :                     gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note offset from nullptr
    3510              :                 }
    3511            0 :                 if(!smnodraw)
    3512              :                 {
    3513            0 :                     drawvaskytris(*va);
    3514              :                 }
    3515            0 :                 xtravertsva += va->sky/3;
    3516            0 :                 prev = va;
    3517              :             }
    3518              :         }
    3519              :     }
    3520              : 
    3521            0 :     gle::clearvbo();
    3522            0 :     gle::clearebo();
    3523            0 :     gle::disablevertex();
    3524            0 : }
    3525              : 
    3526            0 : void batchshadowmapmodels(bool skipmesh)
    3527              : {
    3528            0 :     if(!shadowmms)
    3529              :     {
    3530            0 :         return;
    3531              :     }
    3532            0 :     int nflags = EntFlag_NoVis|EntFlag_NoShadow;
    3533            0 :     if(skipmesh)
    3534              :     {
    3535            0 :         nflags |= EntFlag_ShadowMesh;
    3536              :     }
    3537            0 :     const std::vector<extentity *> &ents = entities::getents();
    3538            0 :     for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
    3539              :     {
    3540            0 :         for(const int &k : oe->mapmodels)
    3541              :         {
    3542            0 :             extentity &e = *ents[k];
    3543            0 :             if(e.flags&nflags)
    3544              :             {
    3545            0 :                 continue;
    3546              :             }
    3547            0 :             e.flags |= EntFlag_Render;
    3548              :         }
    3549              :     }
    3550            0 :     for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
    3551              :     {
    3552            0 :         for(const int &j : oe->mapmodels)
    3553              :         {
    3554            0 :             extentity &e = *ents[j];
    3555            0 :             if(!(e.flags&EntFlag_Render))
    3556              :             {
    3557            0 :                 continue;
    3558              :             }
    3559            0 :             rendermapmodel(e);
    3560            0 :             e.flags &= ~EntFlag_Render;
    3561              :         }
    3562              :     }
    3563              : }
    3564              : 
    3565            0 : void vtxarray::findrsmshadowvas(std::array<vtxarray *, vasortsize> &vasort)
    3566              : {
    3567            0 :     ivec bbmin, bbmax;
    3568            0 :     if(children.size() || mapmodels.size())
    3569              :     {
    3570            0 :         bbmin = bbmin;
    3571            0 :         bbmax = bbmax;
    3572              :     }
    3573              :     else
    3574              :     {
    3575            0 :         bbmin = geommin;
    3576            0 :         bbmax = geommax;
    3577              :     }
    3578            0 :     shadowmask = calcbbrsmsplits(bbmin, bbmax);
    3579            0 :     if(shadowmask)
    3580              :     {
    3581            0 :         float dist = shadowdir.project_bb(bbmin, bbmax) - shadowbias;
    3582            0 :         addshadowva(this, dist, vasort);
    3583            0 :         for(vtxarray *child : children)
    3584              :         {
    3585            0 :             child->findrsmshadowvas(vasort);
    3586              :         }
    3587              :     }
    3588            0 : }
    3589              : 
    3590            0 : void vtxarray::findcsmshadowvas(std::array<vtxarray *, vasortsize> &vasort)
    3591              : {
    3592            0 :     ivec bbmin, bbmax;
    3593            0 :     if(children.size() || mapmodels.size())
    3594              :     {
    3595            0 :         bbmin = bbmin;
    3596            0 :         bbmax = bbmax;
    3597              :     }
    3598              :     else
    3599              :     {
    3600            0 :         bbmin = geommin;
    3601            0 :         bbmax = geommax;
    3602              :     }
    3603            0 :     shadowmask = csm.calcbbsplits(bbmin, bbmax);
    3604            0 :     if(shadowmask)
    3605              :     {
    3606            0 :         float dist = shadowdir.project_bb(bbmin, bbmax) - shadowbias;
    3607            0 :         addshadowva(this, dist, vasort);
    3608            0 :         for(vtxarray *child : children)
    3609              :         {
    3610            0 :             child->findcsmshadowvas(vasort);
    3611              :         }
    3612              :     }
    3613            0 : }
    3614              : 
    3615            0 : void vtxarray::findspotshadowvas(std::array<vtxarray *, vasortsize> &vasort)
    3616              : {
    3617            0 :     float dist = vadist(*this, shadoworigin);
    3618            0 :     if(dist < shadowradius || !smdistcull)
    3619              :     {
    3620            0 :         shadowmask = !smbbcull || (children.size() || mapmodels.size() ?
    3621            0 :                             bbinsidespot(shadoworigin, shadowdir, shadowspot, bbmin, bbmax) :
    3622            0 :                             bbinsidespot(shadoworigin, shadowdir, shadowspot, geommin, geommax)) ? 1 : 0;
    3623            0 :         addshadowva(this, dist, vasort);
    3624            0 :         for(vtxarray *child : children)
    3625              :         {
    3626            0 :             child->findspotshadowvas(vasort);
    3627              :         }
    3628              :     }
    3629            0 : }
    3630              : 
    3631            0 : void findshadowvas()
    3632              : {
    3633              :     std::array<vtxarray *, vasortsize> vasort;
    3634            0 :     vasort.fill(nullptr);
    3635            0 :     switch(shadowmapping)
    3636              :     {
    3637            0 :         case ShadowMap_Reflect:
    3638              :         {
    3639            0 :             for(size_t i = 0; i < varoot.size(); ++i)
    3640              :             {
    3641            0 :                 varoot[i]->findrsmshadowvas(vasort);
    3642              :             }
    3643            0 :             break;
    3644              :         }
    3645            0 :         case ShadowMap_CubeMap:
    3646              :         {
    3647            0 :             findshadowvas(varoot, vasort);
    3648            0 :             break;
    3649              :         }
    3650            0 :         case ShadowMap_Cascade:
    3651              :         {
    3652            0 :             for(size_t i = 0; i < varoot.size(); ++i)
    3653              :             {
    3654            0 :                 varoot[i]->findcsmshadowvas(vasort);
    3655              :             }
    3656            0 :             break;
    3657              :         }
    3658            0 :         case ShadowMap_Spot:
    3659              :         {
    3660            0 :             for(size_t i = 0; i < varoot.size(); ++i)
    3661              :             {
    3662            0 :                 varoot[i]->findspotshadowvas(vasort);
    3663              :             }
    3664            0 :             break;
    3665              :         }
    3666              :     }
    3667            0 :     sortshadowvas(vasort);
    3668            0 : }
        

Generated by: LCOV version 2.0-1