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

Generated by: LCOV version 2.0-1