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

Generated by: LCOV version 2.0-1