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

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

Generated by: LCOV version 2.0-1