LCOV - code coverage report
Current view: top level - engine/render - rendergl.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 1.5 % 984 15
Test Date: 2025-04-16 12:09:02 Functions: 8.5 % 82 7

            Line data    Source code
       1              : /* rendergl.cpp: core opengl rendering stuff
       2              :  *
       3              :  * rendergl.cpp handles the main rendering functions, which render the scene
       4              :  * using OpenGL features aliased in this file. This file also handles the
       5              :  * position of the camera and the projection frustum handling.
       6              :  *
       7              :  * While this file does not handle light and texture rendering, it does handle
       8              :  * the simple world depth fog in libprimis.
       9              :  */
      10              : #include "../libprimis-headers/cube.h"
      11              : #include "../../shared/geomexts.h"
      12              : #include "../../shared/glemu.h"
      13              : #include "../../shared/glexts.h"
      14              : 
      15              : #include <format>
      16              : 
      17              : #include "aa.h"
      18              : #include "grass.h"
      19              : #include "hdr.h"
      20              : #include "hud.h"
      21              : #include "octarender.h"
      22              : #include "postfx.h"
      23              : #include "radiancehints.h"
      24              : #include "rendergl.h"
      25              : #include "renderlights.h"
      26              : #include "rendermodel.h"
      27              : #include "renderparticles.h"
      28              : #include "rendersky.h"
      29              : #include "rendertimers.h"
      30              : #include "renderva.h"
      31              : #include "renderwindow.h"
      32              : #include "shader.h"
      33              : #include "shaderparam.h"
      34              : #include "texture.h"
      35              : #include "water.h"
      36              : 
      37              : #include "world/material.h"
      38              : #include "world/octaedit.h"
      39              : #include "world/octaworld.h"
      40              : #include "world/raycube.h"
      41              : #include "world/world.h"
      42              : 
      43              : #include "interface/console.h"
      44              : #include "interface/control.h"
      45              : #include "interface/input.h"
      46              : #include "interface/menus.h"
      47              : #include "interface/ui.h"
      48              : 
      49              : bool hasFBMSBS = false,
      50              :      hasTQ     = false,
      51              :      hasDBT    = false,
      52              :      hasEGPU4  = false,
      53              :      hasES3    = false,
      54              :      hasCI     = false;
      55              : 
      56              : VAR(outline, 0, 0, 1); //vertex/edge highlighting in edit mode
      57              : 
      58              : //read-only info for gl debugging
      59              : VAR(glversion, 1, 0, 0);
      60              : VAR(glslversion, 1, 0, 0);
      61              : 
      62              : // GL_EXT_framebuffer_blit
      63              : PFNGLBLITFRAMEBUFFERPROC         glBlitFramebuffer_         = nullptr;
      64              : 
      65              : // GL_EXT_framebuffer_multisample
      66              : PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample_ = nullptr;
      67              : 
      68              : // GL_ARB_texture_multisample
      69              : PFNGLTEXIMAGE2DMULTISAMPLEPROC glTexImage2DMultisample_ = nullptr;
      70              : 
      71              : // OpenGL 1.3
      72              : #ifdef WIN32
      73              : PFNGLACTIVETEXTUREPROC    glActiveTexture_    = nullptr;
      74              : 
      75              : PFNGLBLENDEQUATIONEXTPROC glBlendEquation_ = nullptr;
      76              : PFNGLBLENDCOLOREXTPROC    glBlendColor_    = nullptr;
      77              : 
      78              : PFNGLTEXIMAGE3DPROC        glTexImage3D_        = nullptr;
      79              : PFNGLTEXSUBIMAGE3DPROC     glTexSubImage3D_     = nullptr;
      80              : PFNGLCOPYTEXSUBIMAGE3DPROC glCopyTexSubImage3D_ = nullptr;
      81              : 
      82              : PFNGLCOMPRESSEDTEXIMAGE3DPROC    glCompressedTexImage3D_    = nullptr;
      83              : PFNGLCOMPRESSEDTEXIMAGE2DPROC    glCompressedTexImage2D_    = nullptr;
      84              : PFNGLCOMPRESSEDTEXIMAGE1DPROC    glCompressedTexImage1D_    = nullptr;
      85              : PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC glCompressedTexSubImage3D_ = nullptr;
      86              : PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC glCompressedTexSubImage2D_ = nullptr;
      87              : PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC glCompressedTexSubImage1D_ = nullptr;
      88              : PFNGLGETCOMPRESSEDTEXIMAGEPROC   glGetCompressedTexImage_   = nullptr;
      89              : 
      90              : PFNGLDRAWRANGEELEMENTSPROC glDrawRangeElements_ = nullptr;
      91              : #endif
      92              : 
      93              : // GL_EXT_depth_bounds_test
      94              : PFNGLDEPTHBOUNDSEXTPROC glDepthBounds_ = nullptr;
      95              : 
      96              : // GL_ARB_copy_image
      97              : PFNGLCOPYIMAGESUBDATAPROC glCopyImageSubData_ = nullptr;
      98              : 
      99            0 : void masktiles(uint *tiles, float sx1, float sy1, float sx2, float sy2)
     100              : {
     101              :     int tx1, ty1, tx2, ty2;
     102            0 :     calctilebounds(sx1, sy1, sx2, sy2, tx1, ty1, tx2, ty2);
     103            0 :     for(int ty = ty1; ty < ty2; ty++)
     104              :     {
     105            0 :         tiles[ty] |= ((1<<(tx2-tx1))-1)<<tx1;
     106              :     }
     107            0 : }
     108              : 
     109            0 : static void *getprocaddress(const char *name)
     110              : {
     111            0 :     return SDL_GL_GetProcAddress(name);
     112              : }
     113              : 
     114              : VAR(glerr, 0, 0, 1);
     115              : 
     116            0 : void glerror(const char *file, int line, GLenum error)
     117              : {
     118            0 :     const char *desc = "unknown";
     119            0 :     switch(error)
     120              :     {
     121            0 :         case GL_NO_ERROR:
     122              :         {
     123            0 :             desc = "no error";
     124            0 :             break;
     125              :         }
     126            0 :         case GL_INVALID_ENUM:
     127              :         {
     128            0 :             desc = "invalid enum";
     129            0 :             break;
     130              :         }
     131            0 :         case GL_INVALID_VALUE:
     132              :         {
     133            0 :             desc = "invalid value";
     134            0 :             break;
     135              :         }
     136            0 :         case GL_INVALID_OPERATION:
     137              :         {
     138            0 :             desc = "invalid operation";
     139            0 :             break;
     140              :         }
     141            0 :         case GL_STACK_OVERFLOW:
     142              :         {
     143            0 :             desc = "stack overflow";
     144            0 :             break;
     145              :         }
     146            0 :         case GL_STACK_UNDERFLOW:
     147              :         {
     148            0 :             desc = "stack underflow";
     149            0 :             break;
     150              :         }
     151            0 :         case GL_OUT_OF_MEMORY:
     152              :         {
     153            0 :             desc = "out of memory";
     154            0 :             break;
     155              :         }
     156              :     }
     157            0 :     std::printf("GL error: %s:%d: %s (%x)\n", file, line, desc, error);
     158            0 : }
     159              : 
     160              : VAR(intel_texalpha_bug, 0, 0, 1);
     161              : VAR(mesa_swap_bug, 0, 0, 1);
     162              : VAR(usetexgather, 1, 0, 0);
     163              : VAR(maxdrawbufs, 1, 0, 0);
     164              : VAR(maxdualdrawbufs, 1, 0, 0);
     165              : 
     166              : VAR(debugexts, 0, 0, 1);
     167              : 
     168              : static std::unordered_set<std::string> glexts;
     169              : 
     170            0 : static void parseglexts()
     171              : {
     172            0 :     GLint numexts = 0;
     173            0 :     glGetIntegerv(GL_NUM_EXTENSIONS, &numexts);
     174            0 :     for(int i = 0; i < numexts; ++i)
     175              :     {
     176              :         //cast from uchar * to char *
     177            0 :         const char *ext = reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i));
     178            0 :         glexts.insert(ext);
     179              :     }
     180            0 : }
     181              : 
     182            1 : static bool hasext(const char *ext)
     183              : {
     184            3 :     return glexts.find(ext)!=glexts.end();
     185              : }
     186              : 
     187            0 : static bool checkdepthtexstencilrb()
     188              : {
     189            0 :     uint w = 256,
     190            0 :          h = 256;
     191            0 :     GLuint fbo = 0;
     192            0 :     glGenFramebuffers(1, &fbo);
     193            0 :     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
     194              : 
     195            0 :     GLuint depthtex = 0;
     196            0 :     glGenTextures(1, &depthtex);
     197            0 :     createtexture(depthtex, w, h, nullptr, 3, 0, GL_DEPTH_COMPONENT24, GL_TEXTURE_RECTANGLE);
     198            0 :     glBindTexture(GL_TEXTURE_RECTANGLE, 0);
     199            0 :     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_RECTANGLE, depthtex, 0);
     200              : 
     201            0 :     GLuint stencilrb = 0;
     202            0 :     glGenRenderbuffers(1, &stencilrb);
     203            0 :     glBindRenderbuffer(GL_RENDERBUFFER, stencilrb);
     204            0 :     glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
     205            0 :     glBindRenderbuffer(GL_RENDERBUFFER, 0);
     206            0 :     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilrb);
     207              : 
     208            0 :     bool supported = glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
     209              : 
     210            0 :     glBindFramebuffer(GL_FRAMEBUFFER, 0);
     211            0 :     glDeleteFramebuffers(1, &fbo);
     212            0 :     glDeleteTextures(1, &depthtex);
     213            0 :     glDeleteRenderbuffers(1, &stencilrb);
     214              : 
     215            0 :     return supported;
     216              : }
     217              : 
     218            0 : void gl_checkextensions()
     219              : {
     220            0 :     bool mesa   = false,
     221            0 :          intel  = false,
     222            0 :          amd    = false,
     223            0 :          nvidia = false;
     224            0 :     const char *vendor   = reinterpret_cast<const char *>(glGetString(GL_VENDOR)),
     225            0 :                *renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER)),
     226            0 :                *version  = reinterpret_cast<const char *>(glGetString(GL_VERSION));
     227            0 :     conoutf(Console_Init, "Renderer: %s (%s)", renderer, vendor);
     228            0 :     conoutf(Console_Init, "Driver: %s", version);
     229              : 
     230            0 :     if(!renderer || !vendor || !version)
     231              :     {
     232            0 :         fatal("Could not get rendering context information!");
     233              :     }
     234            0 :     if(std::strstr(renderer, "Mesa") || std::strstr(version, "Mesa"))
     235              :     {
     236            0 :         mesa = true;
     237            0 :         if(std::strstr(renderer, "Intel"))
     238              :         {
     239            0 :             intel = true;
     240              :         }
     241              :     }
     242            0 :     else if(std::strstr(vendor, "NVIDIA"))
     243              :     {
     244            0 :         nvidia = true;
     245              :     }
     246            0 :     else if(std::strstr(vendor, "ATI") || std::strstr(vendor, "Advanced Micro Devices"))
     247              :     {
     248            0 :         amd = true;
     249              :     }
     250            0 :     else if(std::strstr(vendor, "Intel"))
     251              :     {
     252            0 :         intel = true;
     253              :     }
     254              : 
     255              :     uint glmajorversion, glminorversion;
     256            0 :     if(std::sscanf(version, " %u.%u", &glmajorversion, &glminorversion) != 2)
     257              :     {
     258            0 :         glversion = 100; //__really__ legacy systems (which won't run anyways)
     259              :     }
     260              :     else
     261              :     {
     262            0 :         glversion = glmajorversion*100 + glminorversion*10;
     263              :     }
     264            0 :     if(glversion < 400)
     265              :     {
     266            0 :         fatal("OpenGL 4.0 or greater is required!");
     267              :     }
     268              : 
     269            0 :     const char *glslstr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
     270            0 :     conoutf(Console_Init, "GLSL: %s", glslstr ? glslstr : "unknown");
     271              : 
     272              :     uint glslmajorversion, glslminorversion;
     273            0 :     if(glslstr && std::sscanf(glslstr, " %u.%u", &glslmajorversion, &glslminorversion) == 2)
     274              :     {
     275            0 :         glslversion = glslmajorversion*100 + glslminorversion;
     276              :     }
     277            0 :     if(glslversion < 400)
     278              :     {
     279            0 :         fatal("GLSL 4.00 or greater is required!");
     280              :     }
     281            0 :     parseglexts();
     282            0 :     GLint texsize = 0,
     283            0 :           texunits = 0,
     284            0 :           vtexunits = 0,
     285            0 :           cubetexsize = 0,
     286            0 :           drawbufs = 0;
     287            0 :     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texsize);
     288            0 :     hwtexsize = texsize;
     289            0 :     if(hwtexsize < 4096)
     290              :     {
     291            0 :         fatal("Large texture support is required!");
     292              :     }
     293            0 :     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texunits);
     294            0 :     hwtexunits = texunits;
     295            0 :     if(hwtexunits < 16)
     296              :     {
     297            0 :         fatal("Hardware does not support at least 16 texture units.");
     298              :     }
     299            0 :     glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &vtexunits);
     300            0 :     hwvtexunits = vtexunits;
     301            0 :     if(hwvtexunits < 16)
     302              :     {
     303            0 :         fatal("Hardware does not support at least 16 vertex texture units.");
     304              :     }
     305            0 :     glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &cubetexsize);
     306            0 :     hwcubetexsize = cubetexsize;
     307            0 :     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &drawbufs);
     308            0 :     maxdrawbufs = drawbufs;
     309            0 :     if(maxdrawbufs < 4)
     310              :     {
     311            0 :         fatal("Hardware does not support at least 4 draw buffers.");
     312              :     }
     313              :     //OpenGL 3.0
     314              : 
     315            0 :     if(hasext("GL_EXT_gpu_shader4"))
     316              :     {
     317            0 :         hasEGPU4 = true;
     318            0 :         if(debugexts)
     319              :         {
     320            0 :             conoutf(Console_Init, "Using GL_EXT_gpu_shader4 extension.");
     321              :         }
     322              :     }
     323            0 :     glRenderbufferStorageMultisample_ = reinterpret_cast<PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC>(getprocaddress("glRenderbufferStorageMultisample"));
     324              : 
     325              :     //OpenGL 3.2
     326            0 :     glTexImage2DMultisample_ = reinterpret_cast<PFNGLTEXIMAGE2DMULTISAMPLEPROC>(getprocaddress("glTexImage2DMultisample"));
     327            0 :     if(hasext("GL_EXT_framebuffer_multisample_blit_scaled"))
     328              :     {
     329            0 :         hasFBMSBS = true;
     330            0 :         if(debugexts)
     331              :         {
     332            0 :             conoutf(Console_Init, "Using GL_EXT_framebuffer_multisample_blit_scaled extension.");
     333              :         }
     334              :     }
     335              :     //OpenGL 3.3
     336            0 :     if(hasext("GL_EXT_texture_filter_anisotropic"))
     337              :     {
     338            0 :         GLint val = 0;
     339            0 :         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &val);
     340            0 :         hwmaxaniso = val;
     341            0 :         if(debugexts)
     342              :         {
     343            0 :             conoutf(Console_Init, "Using GL_EXT_texture_filter_anisotropic extension.");
     344              :         }
     345              :     }
     346              :     else
     347              :     {
     348            0 :         fatal("Anisotropic filtering support is required!");
     349              :     }
     350            0 :     if(hasext("GL_EXT_depth_bounds_test"))
     351              :     {
     352            0 :         glDepthBounds_ = reinterpret_cast<PFNGLDEPTHBOUNDSEXTPROC>(getprocaddress("glDepthBoundsEXT"));
     353            0 :         hasDBT = true;
     354            0 :         if(debugexts)
     355              :         {
     356            0 :             conoutf(Console_Init, "Using GL_EXT_depth_bounds_test extension.");
     357              :         }
     358              :     }
     359            0 :     GLint dualbufs = 0;
     360            0 :     glGetIntegerv(GL_MAX_DUAL_SOURCE_DRAW_BUFFERS, &dualbufs);
     361            0 :     maxdualdrawbufs = dualbufs;
     362            0 :     usetexgather = !intel && !nvidia ? 2 : 1;
     363              :     //OpenGL 4.x
     364            0 :     if(glversion >= 430 || hasext("GL_ARB_ES3_compatibility"))
     365              :     {
     366            0 :         hasES3 = true;
     367            0 :         if(glversion < 430 && debugexts)
     368              :         {
     369            0 :             conoutf(Console_Init, "Using GL_ARB_ES3_compatibility extension.");
     370              :         }
     371              :     }
     372              : 
     373            0 :     if(glversion >= 430 || hasext("GL_ARB_copy_image"))
     374              :     {
     375            0 :         glCopyImageSubData_ = reinterpret_cast<PFNGLCOPYIMAGESUBDATAPROC>(getprocaddress("glCopyImageSubData"));
     376              : 
     377            0 :         hasCI = true;
     378            0 :         if(glversion < 430 && debugexts)
     379              :         {
     380            0 :             conoutf(Console_Init, "Using GL_ARB_copy_image extension.");
     381              :         }
     382              :     }
     383            0 :     else if(hasext("GL_NV_copy_image"))
     384              :     {
     385            0 :         glCopyImageSubData_ = reinterpret_cast<PFNGLCOPYIMAGESUBDATAPROC>(getprocaddress("glCopyImageSubDataNV"));
     386              : 
     387            0 :         hasCI = true;
     388            0 :         if(debugexts)
     389              :         {
     390            0 :             conoutf(Console_Init, "Using GL_NV_copy_image extension.");
     391              :         }
     392              :     }
     393              : 
     394            0 :     if(amd)
     395              :     {
     396            0 :         msaalineardepth = glineardepth = 1; // reading back from depth-stencil still buggy on newer cards, and requires stencil for MSAA
     397              :     }
     398            0 :     else if(nvidia) //no errata on nvidia cards (yet)
     399              :     {
     400              :     }
     401            0 :     else if(intel)
     402              :     {
     403            0 :         smgather = 1; // native shadow filter is slow
     404            0 :         if(mesa)
     405              :         {
     406            0 :             batchsunlight = 0; // causes massive slowdown in linux driver
     407            0 :             msaalineardepth = 1; // MSAA depth texture access is buggy and resolves are slow
     408              :         }
     409              :         else
     410              :         {
     411              :             // causes massive slowdown in windows driver if reading depth-stencil texture
     412            0 :             if(checkdepthtexstencilrb())
     413              :             {
     414            0 :                 gdepthstencil = 1;
     415            0 :                 gstencil = 1;
     416              :             }
     417              :             // sampling alpha by itself from a texture generates garbage on Intel drivers on Windows
     418            0 :             intel_texalpha_bug = 1;
     419              :         }
     420              :     }
     421            0 :     if(mesa)
     422              :     {
     423            0 :         mesa_swap_bug = 1;
     424              :     }
     425            0 :     tqaaresolvegather = 1;
     426            0 : }
     427              : 
     428              : /* glext(): checks for existence of glext
     429              :  *
     430              :  * returns to console 1 if hashtable glexts contains glext (with the name passed)
     431              :  * and returns 0 otherwise
     432              :  *
     433              :  * glexts is a global variable
     434              :  */
     435            1 : void glext(char *ext)
     436              : {
     437            1 :     intret(hasext(ext) ? 1 : 0);
     438            1 : }
     439              : 
     440              : 
     441            0 : void gl_resize()
     442              : {
     443            0 :     gl_setupframe();
     444            0 :     glViewport(0, 0, hudw(), hudh());
     445            0 : }
     446              : 
     447            0 : void gl_init()
     448              : {
     449            0 :     glerror();
     450              : 
     451            0 :     glClearColor(0, 0, 0, 0);
     452            0 :     glClearDepth(1);
     453            0 :     glClearStencil(0);
     454            0 :     glDepthFunc(GL_LESS);
     455            0 :     glDisable(GL_DEPTH_TEST);
     456            0 :     glDisable(GL_STENCIL_TEST);
     457            0 :     glStencilFunc(GL_ALWAYS, 0, ~0);
     458            0 :     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
     459              : 
     460            0 :     glEnable(GL_LINE_SMOOTH);
     461              :     //glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
     462              : 
     463            0 :     glFrontFace(GL_CW);
     464            0 :     glCullFace(GL_BACK);
     465            0 :     glDisable(GL_CULL_FACE);
     466              : 
     467            0 :     gle::setup();
     468            0 :     setupshaders();
     469              : 
     470            0 :     glerror();
     471              : 
     472            0 :     gl_resize();
     473            0 : }
     474              : 
     475              : VAR(wireframe, 0, 0, 1);
     476              : 
     477              : vec worldpos;
     478              : 
     479              : //these three cam() functions replace global variables that previously tracked their respective transforms of cammatrix
     480            0 : vec camdir()
     481              : {
     482            0 :     vec out;
     483            0 :     cammatrix.transposedtransformnormal(vec(viewmatrix.b), out);
     484            0 :     return out;
     485              : }
     486              : 
     487            0 : vec camright()
     488              : {
     489            0 :     vec out;
     490            0 :     cammatrix.transposedtransformnormal(vec(viewmatrix.a).neg(), out);
     491            0 :     return out;
     492              : }
     493              : 
     494            0 : vec camup()
     495              : {
     496            0 :     vec out;
     497            0 :     cammatrix.transposedtransformnormal(vec(viewmatrix.c), out);
     498            0 :     return out;
     499              : }
     500              : 
     501            0 : static void setcammatrix()
     502              : {
     503              :     // move from RH to Z-up LH quake style worldspace
     504            0 :     cammatrix = viewmatrix;
     505            0 :     cammatrix.rotate_around_y(camera1->roll/RAD);
     506            0 :     cammatrix.rotate_around_x(camera1->pitch/-RAD);
     507            0 :     cammatrix.rotate_around_z(camera1->yaw/-RAD);
     508            0 :     cammatrix.translate(vec(camera1->o).neg());
     509              : 
     510            0 :     if(!drawtex)
     511              :     {
     512            0 :         if(raycubepos(camera1->o, camdir(), worldpos, 0, Ray_ClipMat|Ray_SkipFirst) == -1)
     513              :         {
     514            0 :             worldpos = camdir().mul(2*rootworld.mapsize()).add(camera1->o); // if nothing is hit, just far away in the view direction
     515              :         }
     516              :     }
     517            0 : }
     518              : 
     519            0 : void setcamprojmatrix(bool init = true, bool flush = false)
     520              : {
     521            0 :     if(init)
     522              :     {
     523            0 :         setcammatrix();
     524              :     }
     525            0 :     jitteraa();
     526            0 :     camprojmatrix.muld(projmatrix, cammatrix);
     527            0 :     GLOBALPARAM(camprojmatrix, camprojmatrix);
     528            0 :     GLOBALPARAM(lineardepthscale, projmatrix.lineardepthscale()); //(invprojmatrix.c.z, invprojmatrix.d.z));
     529            0 :     if(flush && Shader::lastshader)
     530              :     {
     531            0 :         Shader::lastshader->flushparams();
     532              :     }
     533            0 : }
     534              : 
     535              : matrix4 hudmatrix, hudmatrixstack[64];
     536              : int hudmatrixpos = 0;
     537              : 
     538            0 : void resethudmatrix()
     539              : {
     540            0 :     hudmatrixpos = 0;
     541            0 :     GLOBALPARAM(hudmatrix, hudmatrix);
     542            0 : }
     543              : 
     544            0 : void pushhudmatrix()
     545              : {
     546            0 :     if(hudmatrixpos >= 0 && hudmatrixpos < static_cast<int>(sizeof(hudmatrixstack)/sizeof(hudmatrixstack[0])))
     547              :     {
     548            0 :         hudmatrixstack[hudmatrixpos] = hudmatrix;
     549              :     }
     550            0 :     ++hudmatrixpos;
     551            0 : }
     552              : 
     553            0 : void flushhudmatrix(bool flushparams)
     554              : {
     555            0 :     GLOBALPARAM(hudmatrix, hudmatrix);
     556            0 :     if(flushparams && Shader::lastshader)
     557              :     {
     558            0 :         Shader::lastshader->flushparams();
     559              :     }
     560            0 : }
     561              : 
     562            0 : void pophudmatrix(bool flush, bool flushparams)
     563              : {
     564            0 :     --hudmatrixpos;
     565            0 :     if(hudmatrixpos >= 0 && hudmatrixpos < static_cast<int>(sizeof(hudmatrixstack)/sizeof(hudmatrixstack[0])))
     566              :     {
     567            0 :         hudmatrix = hudmatrixstack[hudmatrixpos];
     568            0 :         if(flush)
     569              :         {
     570            0 :             flushhudmatrix(flushparams);
     571              :         }
     572              :     }
     573            0 : }
     574              : 
     575            0 : void pushhudscale(float scale)
     576              : {
     577            0 :     pushhudmatrix();
     578            0 :     hudmatrix.scale(scale, scale, 1);
     579            0 :     flushhudmatrix();
     580            0 : }
     581              : 
     582            0 : void pushhudtranslate(float tx, float ty, float sx, float sy)
     583              : {
     584            0 :     if(!sy)
     585              :     {
     586            0 :         sy = sx;
     587              :     }
     588            0 :     pushhudmatrix();
     589            0 :     hudmatrix.translate(tx, ty, 0);
     590            0 :     if(sy)
     591              :     {
     592            0 :         hudmatrix.scale(sx, sy, 1);
     593              :     }
     594            0 :     flushhudmatrix();
     595            0 : }
     596              : 
     597              : float curfov, aspect, fovy;
     598              : static float curavatarfov;
     599              : int farplane;
     600              : VARP(zoominvel, 0, 40, 500);
     601              : VARP(zoomoutvel, 0, 50, 500);
     602              : VARP(zoomfov, 10, 42, 90);
     603              : VARP(fov, 10, 100, 150);
     604              : VAR(avatarzoomfov, 1, 1, 1);
     605              : VAR(avatarfov, 10, 40, 100);
     606              : FVAR(avatardepth, 0, 0.7f, 1);
     607              : FVARNP(aspect, forceaspect, 0, 0, 1e3f);
     608              : 
     609              : static float zoomprogress = 0;
     610              : VAR(zoom, -1, 0, 1);
     611              : 
     612            0 : void disablezoom()
     613              : {
     614            0 :     zoom = 0;
     615            0 :     zoomprogress = 0;
     616            0 : }
     617              : 
     618            0 : void computezoom()
     619              : {
     620            0 :     if(!zoom)
     621              :     {
     622            0 :         zoomprogress = 0;
     623            0 :         curfov = fov;
     624            0 :         curavatarfov = avatarfov;
     625            0 :         return;
     626              :     }
     627            0 :     if(zoom > 0)
     628              :     {
     629            0 :         zoomprogress = zoominvel ? std::min(zoomprogress + static_cast<float>(elapsedtime) / zoominvel, 1.0f) : 1;
     630              :     }
     631              :     else
     632              :     {
     633            0 :         zoomprogress = zoomoutvel ? std::max(zoomprogress - static_cast<float>(elapsedtime) / zoomoutvel, 0.0f) : 0;
     634            0 :         if(zoomprogress <= 0)
     635              :         {
     636            0 :             zoom = 0;
     637              :         }
     638              :     }
     639            0 :     curfov = zoomfov*zoomprogress + fov*(1 - zoomprogress);
     640            0 :     curavatarfov = avatarzoomfov*zoomprogress + avatarfov*(1 - zoomprogress);
     641              : }
     642              : 
     643              : FVARP(zoomsens, 1e-4f, 4.5f, 1e4f);
     644              : FVARP(zoomaccel, 0, 0, 1000);
     645              : VARP(zoomautosens, 0, 1, 1);
     646              : FVARP(sensitivity, 0.01f, 3, 100.f);
     647              : FVARP(sensitivityscale, 1e-4f, 100, 1e4f);
     648              : /* Sensitivity scales:
     649              :  * 100: Quake/Source (TF2, Q3, Apex, L4D)
     650              :  * 333: COD, Destiny, Overwatch, ~BL2/3
     651              :  * 400: Cube/RE
     652              :  */
     653              : VARP(invmouse, 0, 0, 1); //toggles inverting the mouse
     654              : FVARP(mouseaccel, 0, 0, 1000);
     655              : 
     656              : physent *camera1 = nullptr;
     657              : //used in iengine.h
     658              : bool detachedcamera = false;
     659              : 
     660            0 : bool isthirdperson()
     661              : {
     662            0 :     return player!=camera1 || detachedcamera;
     663              : }
     664              : 
     665            0 : void fixcamerarange()
     666              : {
     667            0 :     constexpr float maxpitch = 90.0f;
     668            0 :     if(camera1->pitch>maxpitch)
     669              :     {
     670            0 :         camera1->pitch = maxpitch;
     671              :     }
     672            0 :     if(camera1->pitch<-maxpitch)
     673              :     {
     674            0 :         camera1->pitch = -maxpitch;
     675              :     }
     676            0 :     while(camera1->yaw<0.0f)
     677              :     {
     678            0 :         camera1->yaw += 360.0f;
     679              :     }
     680            0 :     while(camera1->yaw>=360.0f)
     681              :     {
     682            0 :         camera1->yaw -= 360.0f;
     683              :     }
     684            0 : }
     685              : 
     686            0 : void modifyorient(float yaw, float pitch)
     687              : {
     688            0 :     camera1->yaw += yaw;
     689            0 :     camera1->pitch += pitch;
     690            0 :     fixcamerarange();
     691            0 :     if(camera1!=player && !detachedcamera)
     692              :     {
     693            0 :         player->yaw = camera1->yaw;
     694            0 :         player->pitch = camera1->pitch;
     695              :     }
     696            0 : }
     697              : 
     698            0 : void mousemove(int dx, int dy)
     699              : {
     700            0 :     float cursens  = sensitivity,
     701            0 :           curaccel = mouseaccel;
     702            0 :     if(zoom)
     703              :     {
     704            0 :         if(zoomautosens)
     705              :         {
     706            0 :             cursens  = static_cast<float>(sensitivity*zoomfov)/fov;
     707            0 :             curaccel = static_cast<float>(mouseaccel*zoomfov)/fov;
     708              :         }
     709              :         else
     710              :         {
     711            0 :             cursens = zoomsens;
     712            0 :             curaccel = zoomaccel;
     713              :         }
     714              :     }
     715            0 :     if(curaccel && curtime && (dx || dy))
     716              :     {
     717            0 :         cursens += curaccel * sqrtf(dx*dx + dy*dy)/curtime;
     718              :     }
     719            0 :     cursens /= (sensitivityscale/4); //hard factor of 4 for 40 dots/deg like Quake/Source/etc.
     720            0 :     modifyorient(dx*cursens, dy*cursens*(invmouse ? 1 : -1));
     721            0 : }
     722              : 
     723              : matrix4 cammatrix, projmatrix, camprojmatrix;
     724              : 
     725              : FVAR(nearplane, 0.01f, 0.54f, 2.0f);
     726              : 
     727            0 : vec calcavatarpos(const vec &pos, float dist)
     728              : {
     729            0 :     vec eyepos;
     730            0 :     cammatrix.transform(pos, eyepos);
     731            0 :     GLdouble ydist = nearplane * std::tan(curavatarfov/(2*RAD)),
     732            0 :              xdist = ydist * aspect;
     733            0 :     vec4<float> scrpos;
     734            0 :     scrpos.x = eyepos.x*nearplane/xdist;
     735            0 :     scrpos.y = eyepos.y*nearplane/ydist;
     736            0 :     scrpos.z = (eyepos.z*(farplane + nearplane) - 2*nearplane*farplane) / (farplane - nearplane);
     737            0 :     scrpos.w = -eyepos.z;
     738              : 
     739            0 :     vec worldpos = camprojmatrix.inverse().perspectivetransform(scrpos);
     740            0 :     vec dir = vec(worldpos).sub(camera1->o).rescale(dist);
     741            0 :     return dir.add(camera1->o);
     742              : }
     743              : 
     744            0 : void renderavatar(void (*hudfxn)())
     745              : {
     746            0 :     if(isthirdperson())
     747              :     {
     748            0 :         return;
     749              :     }
     750            0 :     matrix4 oldprojmatrix = nojittermatrix;
     751            0 :     projmatrix.perspective(curavatarfov, aspect, nearplane, farplane);
     752            0 :     projmatrix.scalez(avatardepth);
     753            0 :     setcamprojmatrix(false);
     754              : 
     755            0 :     enableavatarmask();
     756            0 :     hudfxn();
     757            0 :     disableavatarmask();
     758              : 
     759            0 :     projmatrix = oldprojmatrix;
     760            0 :     setcamprojmatrix(false);
     761              : }
     762              : 
     763              : FVAR(polygonoffsetfactor, -1e4f, -3.0f, 1e4f);
     764              : FVAR(polygonoffsetunits, -1e4f, -3.0f, 1e4f);
     765              : FVAR(depthoffset, -1e4f, 0.01f, 1e4f);
     766              : 
     767              : static matrix4 nooffsetmatrix;
     768              : 
     769            0 : void enablepolygonoffset(GLenum type)
     770              : {
     771            0 :     if(!depthoffset)
     772              :     {
     773            0 :         glPolygonOffset(polygonoffsetfactor, polygonoffsetunits);
     774            0 :         glEnable(type);
     775            0 :         return;
     776              :     }
     777              : 
     778            0 :     projmatrix = nojittermatrix;
     779            0 :     nooffsetmatrix = projmatrix;
     780            0 :     projmatrix.d.z += depthoffset * projmatrix.c.z;
     781            0 :     setcamprojmatrix(false, true);
     782              : }
     783              : 
     784            0 : void disablepolygonoffset(GLenum type)
     785              : {
     786            0 :     if(!depthoffset)
     787              :     {
     788            0 :         glDisable(type);
     789            0 :         return;
     790              :     }
     791              : 
     792            0 :     projmatrix = nooffsetmatrix;
     793            0 :     setcamprojmatrix(false, true);
     794              : }
     795              : 
     796            0 : bool calcspherescissor(const vec &center, float size, float &sx1, float &sy1, float &sx2, float &sy2, float &sz1, float &sz2)
     797              : {
     798              :     //dim must be 0..2
     799              :     //dir should be +/- 1
     800            0 :     auto checkplane = [] (int dim, const float &dc, int dir, float focaldist, float &low, float &high, const float &cz, const float &drt, const vec &e) -> void
     801              :     {
     802            0 :         float nzc = (cz*cz + 1) / (cz + dir*drt) - cz,
     803            0 :               pz = dc/(nzc*e[dim] - e.z);
     804            0 :         if(pz > 0)
     805              :         {
     806            0 :             float c = (focaldist)*nzc,
     807            0 :                   pc = pz*nzc;
     808            0 :             if(pc < e[dim])
     809              :             {
     810            0 :                 low = c;
     811              :             }
     812            0 :             else if(pc > e[dim])
     813              :             {
     814            0 :                 high = c;
     815              :             }
     816              :         }
     817            0 :     };
     818              : 
     819            0 :     vec e;
     820            0 :     cammatrix.transform(center, e);
     821            0 :     if(e.z > 2*size)
     822              :     {
     823            0 :         sx1 = sy1 = sz1 =  1;
     824            0 :         sx2 = sy2 = sz2 = -1;
     825            0 :         return false;
     826              :     }
     827            0 :     if(drawtex == Draw_TexMinimap)
     828              :     {
     829            0 :         vec dir(size, size, size);
     830            0 :         if(projmatrix.a.x < 0)
     831              :         {
     832            0 :             dir.x = -dir.x;
     833              :         }
     834            0 :         if(projmatrix.b.y < 0)
     835              :         {
     836            0 :             dir.y = -dir.y;
     837              :         }
     838            0 :         if(projmatrix.c.z < 0)
     839              :         {
     840            0 :             dir.z = -dir.z;
     841              :         }
     842            0 :         sx1 = std::max(projmatrix.a.x*(e.x - dir.x) + projmatrix.d.x, -1.0f);
     843            0 :         sx2 = std::min(projmatrix.a.x*(e.x + dir.x) + projmatrix.d.x, 1.0f);
     844            0 :         sy1 = std::max(projmatrix.b.y*(e.y - dir.y) + projmatrix.d.y, -1.0f);
     845            0 :         sy2 = std::min(projmatrix.b.y*(e.y + dir.y) + projmatrix.d.y, 1.0f);
     846            0 :         sz1 = std::max(projmatrix.c.z*(e.z - dir.z) + projmatrix.d.z, -1.0f);
     847            0 :         sz2 = std::min(projmatrix.c.z*(e.z + dir.z) + projmatrix.d.z, 1.0f);
     848            0 :         return sx1 < sx2 && sy1 < sy2 && sz1 < sz2;
     849              :     }
     850            0 :     float zzrr = e.z*e.z - size*size,
     851            0 :           dx = e.x*e.x + zzrr,
     852            0 :           dy = e.y*e.y + zzrr,
     853            0 :           focaldist = 1.0f/std::tan(fovy*0.5f/RAD);
     854            0 :     sx1 = sy1 = -1;
     855            0 :     sx2 = sy2 = 1;
     856            0 :     if(dx > 0)
     857              :     {
     858            0 :         float cz  = e.x/e.z,
     859            0 :               drt = sqrtf(dx)/size;
     860            0 :         checkplane(0, dx, -1, focaldist/aspect, sx1, sx2, cz, drt, e);
     861            0 :         checkplane(0, dx,  1, focaldist/aspect, sx1, sx2, cz, drt, e);
     862              :     }
     863            0 :     if(dy > 0)
     864              :     {
     865            0 :         float cz  = e.y/e.z,
     866            0 :               drt = sqrtf(dy)/size;
     867            0 :         checkplane(1, dy, -1, focaldist, sy1, sy2, cz, drt, e);
     868            0 :         checkplane(1, dy,  1, focaldist, sy1, sy2, cz, drt, e);
     869              :     }
     870            0 :     float z1 = std::min(e.z + size, -1e-3f - nearplane),
     871            0 :           z2 = std::min(e.z - size, -1e-3f - nearplane);
     872            0 :     sz1 = (z1*projmatrix.c.z + projmatrix.d.z) / (z1*projmatrix.c.w + projmatrix.d.w);
     873            0 :     sz2 = (z2*projmatrix.c.z + projmatrix.d.z) / (z2*projmatrix.c.w + projmatrix.d.w);
     874            0 :     return sx1 < sx2 && sy1 < sy2 && sz1 < sz2;
     875              : }
     876              : 
     877            0 : bool calcbbscissor(const ivec &bbmin, const ivec &bbmax, float &sx1, float &sy1, float &sx2, float &sy2)
     878              : {
     879            0 :     auto addxyscissor = [&] (const vec4<float> &p)
     880              :     {
     881            0 :         if(p.z >= -p.w)
     882              :         {
     883            0 :             float x = p.x / p.w,
     884            0 :                   y = p.y / p.w;
     885            0 :             sx1 = std::min(sx1, x);
     886            0 :             sy1 = std::min(sy1, y);
     887            0 :             sx2 = std::max(sx2, x);
     888            0 :             sy2 = std::max(sy2, y);
     889              :         }
     890            0 :     };
     891              : 
     892            0 :     std::array<vec4<float>, 8> v;
     893            0 :     sx1 = sy1 = 1;
     894            0 :     sx2 = sy2 = -1;
     895            0 :     camprojmatrix.transform(vec(bbmin.x, bbmin.y, bbmin.z), v[0]);
     896            0 :     addxyscissor(v[0]);
     897            0 :     camprojmatrix.transform(vec(bbmax.x, bbmin.y, bbmin.z), v[1]);
     898            0 :     addxyscissor(v[1]);
     899            0 :     camprojmatrix.transform(vec(bbmin.x, bbmax.y, bbmin.z), v[2]);
     900            0 :     addxyscissor(v[2]);
     901            0 :     camprojmatrix.transform(vec(bbmax.x, bbmax.y, bbmin.z), v[3]);
     902            0 :     addxyscissor(v[3]);
     903            0 :     camprojmatrix.transform(vec(bbmin.x, bbmin.y, bbmax.z), v[4]);
     904            0 :     addxyscissor(v[4]);
     905            0 :     camprojmatrix.transform(vec(bbmax.x, bbmin.y, bbmax.z), v[5]);
     906            0 :     addxyscissor(v[5]);
     907            0 :     camprojmatrix.transform(vec(bbmin.x, bbmax.y, bbmax.z), v[6]);
     908            0 :     addxyscissor(v[6]);
     909            0 :     camprojmatrix.transform(vec(bbmax.x, bbmax.y, bbmax.z), v[7]);
     910            0 :     addxyscissor(v[7]);
     911            0 :     if(sx1 > sx2 || sy1 > sy2)
     912              :     {
     913            0 :         return false;
     914              :     }
     915            0 :     for(int i = 0; i < 8; ++i)
     916              :     {
     917            0 :         const vec4<float> &p = v[i];
     918            0 :         if(p.z >= -p.w)
     919              :         {
     920            0 :             continue;
     921              :         }
     922            0 :         for(int j = 0; j < 3; ++j)
     923              :         {
     924            0 :             const vec4<float> &o = v[i^(1<<j)];
     925            0 :             if(o.z <= -o.w)
     926              :             {
     927            0 :                 continue;
     928              :             }
     929              : 
     930            0 :             float t = (p.z + p.w)/(p.z + p.w - o.z - o.w),
     931            0 :                   w = p.w + t*(o.w - p.w),
     932            0 :                   x = (p.x + t*(o.x - p.x))/w,
     933            0 :                   y = (p.y + t*(o.y - p.y))/w;
     934            0 :             sx1 = std::min(sx1, x);
     935            0 :             sy1 = std::min(sy1, y);
     936            0 :             sx2 = std::max(sx2, x);
     937            0 :             sy2 = std::max(sy2, y);
     938              :         }
     939              :     }
     940              : 
     941              : 
     942            0 :     sx1 = std::max(sx1, -1.0f);
     943            0 :     sy1 = std::max(sy1, -1.0f);
     944            0 :     sx2 = std::min(sx2, 1.0f);
     945            0 :     sy2 = std::min(sy2, 1.0f);
     946            0 :     return true;
     947              : }
     948              : 
     949            0 : bool calcspotscissor(const vec &origin, float radius, const vec &dir, int spot, const vec &spotx, const vec &spoty, float &sx1, float &sy1, float &sx2, float &sy2, float &sz1, float &sz2)
     950              : {
     951            0 :     static auto addxyzscissor = [] (const vec4<float> &p, float &sx1, float &sy1, float &sx2, float &sy2, float &sz1, float &sz2) -> void
     952              :     {
     953            0 :         if(p.z >= -p.w)
     954              :         {
     955            0 :             float x = p.x / p.w,
     956            0 :                   y = p.y / p.w,
     957            0 :                   z = p.z / p.w;
     958            0 :             sx1 = std::min(sx1, x);
     959            0 :             sy1 = std::min(sy1, y);
     960            0 :             sz1 = std::min(sz1, z);
     961            0 :             sx2 = std::max(sx2, x);
     962            0 :             sy2 = std::max(sy2, y);
     963            0 :             sz2 = std::max(sz2, z);
     964              :         }
     965            0 :     };
     966            0 :     float spotscale = radius * tan360(spot);
     967            0 :     vec up     = vec(spotx).mul(spotscale),
     968            0 :         right  = vec(spoty).mul(spotscale),
     969            0 :         center = vec(dir).mul(radius).add(origin);
     970            0 :     std::array<vec4<float>, 5> v;
     971            0 :     sx1 = sy1 = sz1 = 1;
     972            0 :     sx2 = sy2 = sz2 = -1;
     973            0 :     camprojmatrix.transform(vec(center).sub(right).sub(up), v[0]);
     974            0 :     addxyzscissor(v[0], sx1, sy1, sx2, sy2, sz1, sz2);
     975            0 :     camprojmatrix.transform(vec(center).add(right).sub(up), v[1]);
     976            0 :     addxyzscissor(v[1], sx1, sy1, sx2, sy2, sz1, sz2);
     977            0 :     camprojmatrix.transform(vec(center).sub(right).add(up), v[2]);
     978            0 :     addxyzscissor(v[2], sx1, sy1, sx2, sy2, sz1, sz2);
     979            0 :     camprojmatrix.transform(vec(center).add(right).add(up), v[3]);
     980            0 :     addxyzscissor(v[3], sx1, sy1, sx2, sy2, sz1, sz2);
     981            0 :     camprojmatrix.transform(origin, v[4]);
     982            0 :     addxyzscissor(v[4], sx1, sy1, sx2, sy2, sz1, sz2);
     983              : 
     984            0 :     static auto interpxyzscissor = [] (const vec4<float> &p, const vec4<float> &o, float &sx1, float &sy1, float &sx2, float &sy2, float &sz1) -> void
     985              :     {
     986            0 :         float t = (p.z + p.w)/(p.z + p.w - o.z - o.w),
     987            0 :               w = p.w + t*(o.w - p.w),
     988            0 :               x = (p.x + t*(o.x - p.x))/w,
     989            0 :               y = (p.y + t*(o.y - p.y))/w;
     990            0 :         sx1 = std::min(sx1, x);
     991            0 :         sy1 = std::min(sy1, y);
     992            0 :         sz1 = std::min(sz1, -1.0f);
     993            0 :         sx2 = std::max(sx2, x);
     994            0 :         sy2 = std::max(sy2, y);
     995            0 :     };
     996              : 
     997            0 :     if(sx1 > sx2 || sy1 > sy2 || sz1 > sz2)
     998              :     {
     999            0 :         return false;
    1000              :     }
    1001            0 :     for(int i = 0; i < 4; ++i)
    1002              :     {
    1003            0 :         const vec4<float> &p = v[i];
    1004            0 :         if(p.z >= -p.w)
    1005              :         {
    1006            0 :             continue;
    1007              :         }
    1008            0 :         for(int j = 0; j < 2; ++j)
    1009              :         {
    1010            0 :             const vec4<float> &o = v[i^(1<<j)];
    1011            0 :             if(o.z <= -o.w)
    1012              :             {
    1013            0 :                 continue;
    1014              :             }
    1015              : 
    1016            0 :             interpxyzscissor(p, o, sx1, sy1, sx2, sy2, sz1);
    1017              :         }
    1018            0 :         if(v[4].z > -v[4].w)
    1019              :         {
    1020            0 :             interpxyzscissor(p, v[4], sx1, sy1, sx2, sy2, sz1);
    1021              :         }
    1022              :     }
    1023            0 :     if(v[4].z < -v[4].w)
    1024              :     {
    1025            0 :         for(int j = 0; j < 4; ++j)
    1026              :         {
    1027            0 :             const vec4<float> &o = v[j];
    1028            0 :             if(o.z <= -o.w)
    1029              :             {
    1030            0 :                 continue;
    1031              :             }
    1032            0 :             interpxyzscissor(v[4], o, sx1, sy1, sx2, sy2, sz1);
    1033              :         }
    1034              :     }
    1035              : 
    1036            0 :     sx1 = std::max(sx1, -1.0f);
    1037            0 :     sy1 = std::max(sy1, -1.0f);
    1038            0 :     sz1 = std::max(sz1, -1.0f);
    1039            0 :     sx2 = std::min(sx2,  1.0f);
    1040            0 :     sy2 = std::min(sy2,  1.0f);
    1041            0 :     sz2 = std::min(sz2,  1.0f);
    1042            0 :     return true;
    1043              : }
    1044              : 
    1045              : static GLuint screenquadvbo = 0;
    1046              : 
    1047            0 : static void setupscreenquad()
    1048              : {
    1049            0 :     if(!screenquadvbo)
    1050              :     {
    1051            0 :         glGenBuffers(1, &screenquadvbo);
    1052            0 :         gle::bindvbo(screenquadvbo);
    1053            0 :         vec2 verts[4] = { vec2(1, -1), vec2(-1, -1), vec2(1, 1), vec2(-1, 1) };
    1054            0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    1055            0 :         gle::clearvbo();
    1056              :     }
    1057            0 : }
    1058              : 
    1059            0 : static void cleanupscreenquad()
    1060              : {
    1061            0 :     if(screenquadvbo)
    1062              :     {
    1063            0 :         glDeleteBuffers(1, &screenquadvbo);
    1064            0 :         screenquadvbo = 0;
    1065              :     }
    1066            0 : }
    1067              : 
    1068            0 : void screenquad()
    1069              : {
    1070            0 :     setupscreenquad();
    1071            0 :     gle::bindvbo(screenquadvbo);
    1072            0 :     gle::enablevertex();
    1073            0 :     gle::vertexpointer(sizeof(vec2), nullptr, GL_FLOAT, 2);
    1074            0 :     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    1075            0 :     gle::disablevertex();
    1076            0 :     gle::clearvbo();
    1077            0 : }
    1078              : 
    1079              : //sets screentexcoord0,screentexcoord1 in glsl
    1080            0 : static void setscreentexcoord(int i, float w, float h, float x = 0, float y = 0)
    1081              : {
    1082              :     static std::array<LocalShaderParam, 2> screentexcoord =
    1083              :     {
    1084              :         LocalShaderParam("screentexcoord0"),
    1085              :         LocalShaderParam("screentexcoord1")
    1086            0 :     };
    1087            0 :     screentexcoord[i].setf(w*0.5f, h*0.5f, x + w*0.5f, y + std::fabs(h)*0.5f);
    1088            0 : }
    1089              : 
    1090            0 : void screenquad(float sw, float sh)
    1091              : {
    1092            0 :     setscreentexcoord(0, sw, sh);
    1093            0 :     screenquad();
    1094            0 : }
    1095              : 
    1096            0 : void screenquad(float sw, float sh, float sw2, float sh2)
    1097              : {
    1098            0 :     setscreentexcoord(0, sw, sh);
    1099            0 :     setscreentexcoord(1, sw2, sh2);
    1100            0 :     screenquad();
    1101            0 : }
    1102              : 
    1103            0 : void screenquadoffset(float x, float y, float w, float h)
    1104              : {
    1105            0 :     setscreentexcoord(0, w, h, x, y);
    1106            0 :     screenquad();
    1107            0 : }
    1108              : 
    1109              : // creates a hud quad for hudquad, debugquad
    1110            0 : static void createhudquad(float x1, float y1, float x2, float y2, float sx1, float sy1, float sx2, float sy2) {
    1111            0 :     gle::defvertex(2);
    1112            0 :     gle::deftexcoord0();
    1113            0 :     gle::begin(GL_TRIANGLE_STRIP);
    1114            0 :     gle::attribf(x2, y1); gle::attribf(sx2, sy1);
    1115            0 :     gle::attribf(x1, y1); gle::attribf(sx1, sy1);
    1116            0 :     gle::attribf(x2, y2); gle::attribf(sx2, sy2);
    1117            0 :     gle::attribf(x1, y2); gle::attribf(sx1, sy2);
    1118            0 :     gle::end();
    1119            0 : }
    1120              : 
    1121            0 : void hudquad(float x, float y, float w, float h, float tx, float ty, float tw, float th)
    1122              : {
    1123            0 :     createhudquad(x, y, x+w, y+h, tx, ty, tx+tw, ty+th);
    1124            0 : }
    1125              : 
    1126            0 : void debugquad(float x, float y, float w, float h, float tx, float ty, float tw, float th)
    1127              : {
    1128            0 :     createhudquad(x, y, x+w, y+h, tx, ty+th, tx+tw, ty);
    1129            0 : }
    1130              : 
    1131              : VARR(fog, 16, 4000, 1000024);
    1132            0 : CVARR(fogcolor, 0x8099B3);
    1133              : VAR(fogoverlay, 0, 1, 1);
    1134              : 
    1135            0 : static float findsurface(int fogmat, const vec &v, int &abovemat)
    1136              : {
    1137            0 :     fogmat &= MatFlag_Volume;
    1138            0 :     ivec o(v), co;
    1139              :     int csize;
    1140              :     do
    1141              :     {
    1142            0 :         const cube &c = rootworld.lookupcube(o, 0, co, csize);
    1143            0 :         int mat = c.material&MatFlag_Volume;
    1144            0 :         if(mat != fogmat)
    1145              :         {
    1146            0 :             abovemat = IS_LIQUID(mat) ? c.material : +Mat_Air;
    1147            0 :             return o.z;
    1148              :         }
    1149            0 :         o.z = co.z + csize;
    1150            0 :     } while(o.z < rootworld.mapsize());
    1151            0 :     abovemat = Mat_Air;
    1152            0 :     return rootworld.mapsize();
    1153              : }
    1154              : 
    1155            0 : static void blendfog(int fogmat, float below, float blend, float logblend, float &start, float &end, vec &fogc)
    1156              : {
    1157            0 :     switch(fogmat&MatFlag_Volume)
    1158              :     {
    1159            0 :         case Mat_Water:
    1160              :         {
    1161            0 :             const bvec &wcol = getwatercolor(fogmat),
    1162            0 :                        &wdeepcol = getwaterdeepcolor(fogmat);
    1163            0 :             int wfog = getwaterfog(fogmat),
    1164            0 :                 wdeep = getwaterdeep(fogmat);
    1165            0 :             float deepfade = std::clamp(below/std::max(wdeep, wfog), 0.0f, 1.0f);
    1166            0 :             vec color;
    1167            0 :             color.lerp(wcol.tocolor(), wdeepcol.tocolor(), deepfade);
    1168            0 :             fogc.add(vec(color).mul(blend));
    1169            0 :             end += logblend*std::min(fog, std::max(wfog*2, 16));
    1170            0 :             break;
    1171              :         }
    1172            0 :         default:
    1173              :         {
    1174            0 :             fogc.add(fogcolor.tocolor().mul(blend));
    1175            0 :             start += logblend*(fog+64)/8;
    1176            0 :             end += logblend*fog;
    1177            0 :             break;
    1178              :         }
    1179              :     }
    1180            0 : }
    1181              : 
    1182              : static vec curfogcolor(0, 0, 0);
    1183              : 
    1184            0 : void setfogcolor(const vec &v)
    1185              : {
    1186            0 :     GLOBALPARAM(fogcolor, v);
    1187            0 : }
    1188              : 
    1189            0 : void zerofogcolor()
    1190              : {
    1191            0 :     setfogcolor(vec(0, 0, 0));
    1192            0 : }
    1193              : 
    1194            0 : void resetfogcolor()
    1195              : {
    1196            0 :     setfogcolor(curfogcolor);
    1197            0 : }
    1198              : 
    1199              : FVAR(fogintensity, 0, 0.15f, 1);
    1200              : 
    1201            0 : float calcfogdensity(float dist)
    1202              : {
    1203            0 :     return std::log(fogintensity)/(M_LN2*dist);
    1204              : }
    1205              : 
    1206              : FVAR(fogcullintensity, 0, 1e-3f, 1);
    1207              : 
    1208            0 : float calcfogcull()
    1209              : {
    1210            0 :     return std::log(fogcullintensity) / (M_LN2*calcfogdensity(fog - (fog+64)/8));
    1211              : }
    1212              : 
    1213            0 : static void setfog(int fogmat, float below = 0, float blend = 1, int abovemat = Mat_Air)
    1214              : {
    1215            0 :     float start = 0,
    1216            0 :           end = 0,
    1217            0 :           logscale = 256,
    1218            0 :           logblend = std::log(1 + (logscale - 1)*blend) / std::log(logscale);
    1219              : 
    1220            0 :     curfogcolor = vec(0, 0, 0);
    1221            0 :     blendfog(fogmat, below, blend, logblend, start, end, curfogcolor);
    1222            0 :     if(blend < 1)
    1223              :     {
    1224            0 :         blendfog(abovemat, 0, 1-blend, 1-logblend, start, end, curfogcolor);
    1225              :     }
    1226            0 :     curfogcolor.mul(ldrscale);
    1227            0 :     GLOBALPARAM(fogcolor, curfogcolor);
    1228            0 :     float fogdensity = calcfogdensity(end-start);
    1229            0 :     GLOBALPARAMF(fogdensity, fogdensity, 1/std::exp(M_LN2*start*fogdensity));
    1230            0 : }
    1231              : 
    1232            0 : static void blendfogoverlay(int fogmat, float below, float blend, vec &overlay)
    1233              : {
    1234            0 :     switch(fogmat&MatFlag_Volume)
    1235              :     {
    1236            0 :         case Mat_Water:
    1237              :         {
    1238            0 :             const bvec &wcol = getwatercolor(fogmat),
    1239            0 :                        &wdeepcol = getwaterdeepcolor(fogmat);
    1240            0 :             const int wfog = getwaterfog(fogmat),
    1241            0 :                       wdeep = getwaterdeep(fogmat);
    1242            0 :             const float deepfade = std::clamp(below/std::max(wdeep, wfog), 0.0f, 1.0f);
    1243            0 :             vec color = vec(wcol.r(), wcol.g(), wcol.b()).lerp(vec(wdeepcol.r(), wdeepcol.g(), wdeepcol.b()), deepfade);
    1244            0 :             overlay.add(color.div(std::min(32.0f + std::max(color.r(), std::max(color.g(), color.b()))*7.0f/8.0f, 255.0f)).max(0.4f).mul(blend));
    1245            0 :             break;
    1246              :         }
    1247            0 :         default:
    1248              :         {
    1249            0 :             overlay.add(blend);
    1250            0 :             break;
    1251              :         }
    1252              :     }
    1253            0 : }
    1254              : 
    1255            0 : void drawfogoverlay(int fogmat, float fogbelow, float fogblend, int abovemat)
    1256              : {
    1257            0 :     SETSHADER(fogoverlay);
    1258              : 
    1259            0 :     glEnable(GL_BLEND);
    1260            0 :     glBlendFunc(GL_ZERO, GL_SRC_COLOR);
    1261            0 :     vec overlay(0, 0, 0);
    1262            0 :     blendfogoverlay(fogmat, fogbelow, fogblend, overlay);
    1263            0 :     blendfogoverlay(abovemat, 0, 1-fogblend, overlay);
    1264              : 
    1265            0 :     gle::color(overlay);
    1266            0 :     screenquad();
    1267              : 
    1268            0 :     glDisable(GL_BLEND);
    1269            0 : }
    1270              : 
    1271              : int drawtex = 0;
    1272              : 
    1273              : /* =========================== minimap functionality ======================== */
    1274              : 
    1275              : static GLuint minimaptex = 0;
    1276              : vec minimapcenter(0, 0, 0),
    1277              :     minimapradius(0, 0, 0),
    1278              :     minimapscale(0, 0, 0);
    1279              : 
    1280            0 : float calcfrustumboundsphere(float nearplane, float farplane,  const vec &pos, const vec &view, vec &center)
    1281              : {
    1282            0 :     if(drawtex == Draw_TexMinimap)
    1283              :     {
    1284            0 :         center = minimapcenter;
    1285            0 :         return minimapradius.magnitude();
    1286              :     }
    1287              : 
    1288            0 :     float width = std::tan(fov/(2.0f*RAD)),
    1289            0 :           height = width / aspect,
    1290            0 :           cdist = ((nearplane + farplane)/2)*(1 + width*width + height*height);
    1291            0 :     if(cdist <= farplane)
    1292              :     {
    1293            0 :         center = vec(view).mul(cdist).add(pos);
    1294            0 :         return vec(width*nearplane, height*nearplane, cdist-nearplane).magnitude();
    1295              :     }
    1296              :     else
    1297              :     {
    1298            0 :         center = vec(view).mul(farplane).add(pos);
    1299            0 :         return vec(width*farplane, height*farplane, 0).magnitude();
    1300              :     }
    1301              : }
    1302              : 
    1303            0 : void clearminimap()
    1304              : {
    1305            0 :     if(minimaptex)
    1306              :     {
    1307            0 :         glDeleteTextures(1, &minimaptex);
    1308            0 :         minimaptex = 0;
    1309              :     }
    1310            0 : }
    1311              : 
    1312              : VARR(minimapheight, 0, 0, 2<<16); //height above bottom of map to render at
    1313            0 : CVARR(minimapcolor, 0);
    1314              : VARR(minimapclip, 0, 0, 1);
    1315              : VARP(minimapsize, 7, 10, 12);      //2^n size of the minimap texture (along edge)
    1316              : VARP(showminimap, 0, 1, 1);
    1317            0 : CVARP(nominimapcolor, 0x101010);  //color for the part of the minimap that isn't the map texture
    1318              : 
    1319            0 : void bindminimap()
    1320              : {
    1321            0 :     glBindTexture(GL_TEXTURE_2D, minimaptex);
    1322            0 : }
    1323              : 
    1324            0 : void clipminimap(ivec &bbmin, ivec &bbmax, const std::array<cube, 8> &c, const ivec &co = ivec(0, 0, 0), int size = rootworld.mapsize()>>1)
    1325              : {
    1326            0 :     for(int i = 0; i < 8; ++i)
    1327              :     {
    1328            0 :         ivec o(i, co, size);
    1329            0 :         if(c[i].children)
    1330              :         {
    1331            0 :             clipminimap(bbmin, bbmax, *(c[i].children), o, size>>1);
    1332              :         }
    1333            0 :         else if(!(c[i].issolid()) && (c[i].material&MatFlag_Clip)!=Mat_Clip)
    1334              :         {
    1335            0 :             for(int k = 0; k < 3; ++k)
    1336              :             {
    1337            0 :                 bbmin[k] = std::min(bbmin[k], o[k]);
    1338              :             }
    1339            0 :             for(int k = 0; k < 3; ++k)
    1340              :             {
    1341            0 :                 bbmax[k] = std::max(bbmax[k], o[k] + size);
    1342              :             }
    1343              :         }
    1344              :     }
    1345            0 : }
    1346              : 
    1347            0 : void drawminimap(int yaw, int pitch, vec loc, const cubeworld& world, int scalefactor)
    1348              : {
    1349            0 :     if(!showminimap)
    1350              :     {
    1351            0 :         if(!minimaptex)
    1352              :         {
    1353            0 :             glGenTextures(1, &minimaptex);
    1354              :         }
    1355              :         uchar v[3];
    1356            0 :         v[0] = nominimapcolor.r();
    1357            0 :         v[1] = nominimapcolor.g();
    1358            0 :         v[2] = nominimapcolor.b();
    1359            0 :         createtexture(minimaptex, 1, 1, v, 3, 0, GL_RGB, GL_TEXTURE_2D);
    1360            0 :         return;
    1361              :     }
    1362              : 
    1363            0 :     glerror();
    1364              : 
    1365            0 :     drawtex = Draw_TexMinimap;
    1366              : 
    1367            0 :     glerror();
    1368            0 :     gl_setupframe(true);
    1369              : 
    1370            0 :     int size = 1<<minimapsize,
    1371            0 :         sizelimit = std::min(hwtexsize, std::min(gw, gh));
    1372            0 :     while(size > sizelimit)
    1373              :     {
    1374            0 :         size = size - 128;
    1375              :     }
    1376            0 :     if(!minimaptex)
    1377              :     {
    1378            0 :         glGenTextures(1, &minimaptex);
    1379              :     }
    1380            0 :     ivec bbmin(rootworld.mapsize(), rootworld.mapsize(), rootworld.mapsize()),
    1381            0 :          bbmax(0, 0, 0);
    1382            0 :     for(uint i = 0; i < valist.size(); i++)
    1383              :     {
    1384            0 :         const vtxarray *va = valist[i];
    1385            0 :         for(int k = 0; k < 3; ++k)
    1386              :         {
    1387            0 :             if(va->geommin[k]>va->geommax[k])
    1388              :             {
    1389            0 :                 continue;
    1390              :             }
    1391            0 :             bbmin[k] = std::min(bbmin[k], va->geommin[k]);
    1392            0 :             bbmax[k] = std::max(bbmax[k], va->geommax[k]);
    1393              :         }
    1394              :     }
    1395            0 :     if(minimapclip)
    1396              :     {
    1397            0 :         ivec clipmin(rootworld.mapsize(), rootworld.mapsize(), rootworld.mapsize()),
    1398            0 :              clipmax(0, 0, 0);
    1399            0 :         clipminimap(clipmin, clipmax, *world.worldroot);
    1400            0 :         for(int k = 0; k < 2; ++k)
    1401              :         {
    1402            0 :             bbmin[k] = std::max(bbmin[k], clipmin[k]);
    1403              :         }
    1404            0 :         for(int k = 0; k < 2; ++k)
    1405              :         {
    1406            0 :             bbmax[k] = std::min(bbmax[k], clipmax[k]);
    1407              :         }
    1408              :     }
    1409              : 
    1410            0 :     minimapradius = vec(bbmax).sub(vec(bbmin)).div(scalefactor);
    1411            0 :     minimapcenter = loc;
    1412            0 :     minimapradius.x = minimapradius.y = std::max(minimapradius.x, minimapradius.y);
    1413            0 :     minimapscale = vec((0.5f - 1.0f/size)/minimapradius.x, (0.5f - 1.0f/size)/minimapradius.y, 1.0f);
    1414              : 
    1415            0 :     physent *oldcamera = camera1;
    1416            0 :     physent cmcamera = *player;
    1417            0 :     cmcamera.reset();
    1418            0 :     cmcamera.type = physent::PhysEnt_Camera;
    1419            0 :     cmcamera.o = loc;
    1420            0 :     cmcamera.yaw = yaw;
    1421            0 :     cmcamera.pitch = pitch;
    1422            0 :     cmcamera.roll = 0;
    1423            0 :     camera1 = &cmcamera;
    1424              : 
    1425            0 :     float oldldrscale = ldrscale;
    1426            0 :     int oldfarplane = farplane,
    1427            0 :         oldvieww    = vieww,
    1428            0 :         oldviewh    = viewh;
    1429            0 :     farplane = rootworld.mapsize()*2;
    1430            0 :     vieww = viewh = size;
    1431              : 
    1432            0 :     float zscale = std::max(static_cast<float>(minimapheight), minimapcenter.z + minimapradius.z + 1) + 1;
    1433              : 
    1434            0 :     projmatrix.ortho(-minimapradius.x, minimapradius.x, -minimapradius.y, minimapradius.y, 0, 2*zscale);
    1435            0 :     setcamprojmatrix();
    1436              : 
    1437            0 :     glEnable(GL_CULL_FACE);
    1438            0 :     glEnable(GL_DEPTH_TEST);
    1439              : 
    1440            0 :     xtravertsva = xtraverts = glde = gbatches = vtris = vverts = 0;
    1441            0 :     occlusionengine.flipqueries();
    1442              : 
    1443            0 :     ldrscale = 1;
    1444              : 
    1445            0 :     view.visiblecubes(false);
    1446            0 :     gbuf.rendergbuffer();
    1447            0 :     gbuf.rendershadowatlas();
    1448              : 
    1449            0 :     gbuf.shademinimap(minimapcolor.tocolor().mul(ldrscale));
    1450              : 
    1451            0 :     if(minimapheight > 0 && minimapheight < minimapcenter.z + minimapradius.z)
    1452              :     {
    1453            0 :         camera1->o.z = minimapcenter.z + minimapradius.z + 1;
    1454            0 :         projmatrix.ortho(-minimapradius.x, minimapradius.x, -minimapradius.y, minimapradius.y, -zscale, zscale);
    1455            0 :         setcamprojmatrix();
    1456            0 :         gbuf.rendergbuffer(false);
    1457            0 :         gbuf.shademinimap();
    1458              :     }
    1459              : 
    1460            0 :     glDisable(GL_DEPTH_TEST);
    1461            0 :     glDisable(GL_CULL_FACE);
    1462              : 
    1463            0 :     farplane = oldfarplane;
    1464            0 :     vieww = oldvieww;
    1465            0 :     viewh = oldviewh;
    1466            0 :     ldrscale = oldldrscale;
    1467              : 
    1468            0 :     camera1 = oldcamera;
    1469            0 :     drawtex = 0;
    1470              : 
    1471            0 :     createtexture(minimaptex, size, size, nullptr, 3, 1, GL_RGB5, GL_TEXTURE_2D);
    1472            0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    1473            0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    1474            0 :     GLfloat border[4] = { minimapcolor.x/255.0f, minimapcolor.y/255.0f, minimapcolor.z/255.0f, 1.0f };
    1475            0 :     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
    1476            0 :     glBindTexture(GL_TEXTURE_2D, 0);
    1477              : 
    1478            0 :     GLuint fbo = 0;
    1479            0 :     glGenFramebuffers(1, &fbo);
    1480            0 :     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1481            0 :     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, minimaptex, 0);
    1482            0 :     copyhdr(size, size, fbo);
    1483            0 :     glBindFramebuffer(GL_FRAMEBUFFER, 0);
    1484            0 :     glDeleteFramebuffers(1, &fbo);
    1485              : 
    1486            0 :     glViewport(0, 0, hudw(), hudh());
    1487              : }
    1488              : 
    1489              : VAR(modelpreviewfov, 10, 20, 100);    //y axis field of view
    1490              : VAR(modelpreviewpitch, -90, -15, 90); //pitch above model to render
    1491              : 
    1492              : /* ======================== model preview windows =========================== */
    1493              : 
    1494              : 
    1495            0 : void ModelPreview::start(int xcoord, int ycoord, int width, int height, bool bg, bool usescissor)
    1496              : {
    1497            0 :     x = xcoord;
    1498            0 :     y = ycoord;
    1499            0 :     w = width;
    1500            0 :     h = height;
    1501            0 :     background = bg;
    1502            0 :     scissor = usescissor;
    1503              : 
    1504            0 :     gbuf.setupgbuffer();
    1505              : 
    1506            0 :     useshaderbyname("modelpreview");
    1507              : 
    1508            0 :     drawtex = Draw_TexModelPreview;
    1509              : 
    1510            0 :     oldcamera = camera1;
    1511            0 :     camera = *camera1;
    1512            0 :     camera.reset();
    1513            0 :     camera.type = physent::PhysEnt_Camera;
    1514            0 :     camera.o = vec(0, 0, 0);
    1515            0 :     camera.yaw = 0;
    1516            0 :     camera.pitch = modelpreviewpitch;
    1517            0 :     camera.roll = 0;
    1518            0 :     camera1 = &camera;
    1519              : 
    1520            0 :     oldaspect = aspect;
    1521            0 :     oldfovy = fovy;
    1522            0 :     oldfov = curfov;
    1523            0 :     oldldrscale = ldrscale;
    1524            0 :     oldfarplane = farplane;
    1525            0 :     oldvieww = vieww;
    1526            0 :     oldviewh = viewh;
    1527            0 :     oldprojmatrix = projmatrix;
    1528              : 
    1529            0 :     aspect = w/static_cast<float>(h);
    1530            0 :     fovy = modelpreviewfov;
    1531            0 :     curfov = 2*std::atan2(std::tan(fovy/(2*RAD)), 1/aspect)*RAD;
    1532            0 :     farplane = 1024;
    1533            0 :     vieww = std::min(gw, w);
    1534            0 :     viewh = std::min(gh, h);
    1535            0 :     ldrscale = 1;
    1536              : 
    1537            0 :     projmatrix.perspective(fovy, aspect, nearplane, farplane);
    1538            0 :     setcamprojmatrix();
    1539              : 
    1540            0 :     glEnable(GL_CULL_FACE);
    1541            0 :     glEnable(GL_DEPTH_TEST);
    1542            0 : }
    1543              : 
    1544            0 : void ModelPreview::end()
    1545              : {
    1546            0 :     gbuf.rendermodelbatches();
    1547              : 
    1548            0 :     glDisable(GL_DEPTH_TEST);
    1549            0 :     glDisable(GL_CULL_FACE);
    1550              : 
    1551            0 :     gbuf.shademodelpreview(x, y, w, h, background, scissor);
    1552              : 
    1553            0 :     aspect = oldaspect;
    1554            0 :     fovy = oldfovy;
    1555            0 :     curfov = oldfov;
    1556            0 :     farplane = oldfarplane;
    1557            0 :     vieww = oldvieww;
    1558            0 :     viewh = oldviewh;
    1559            0 :     ldrscale = oldldrscale;
    1560              : 
    1561            0 :     camera1 = oldcamera;
    1562            0 :     drawtex = 0;
    1563              : 
    1564            0 :     projmatrix = oldprojmatrix;
    1565            0 :     setcamprojmatrix();
    1566            0 : }
    1567              : 
    1568            0 : vec calcmodelpreviewpos(const vec &radius, float &yaw)
    1569              : {
    1570            0 :     yaw = std::fmod(lastmillis/10000.0f*360.0f, 360.0f);
    1571            0 :     float dist = std::max(radius.magnitude2()/aspect, radius.magnitude())/std::sin(fovy/(2*RAD));
    1572            0 :     return vec(0, dist, 0).rotate_around_x(camera1->pitch/RAD);
    1573              : }
    1574              : 
    1575              : int xtraverts, xtravertsva;
    1576              : 
    1577              : /* ============================= core rendering ============================= */
    1578              : 
    1579              : //main scene rendering function
    1580            0 : void gl_drawview(void (*gamefxn)(), void(*hudfxn)(), void(*editfxn)())
    1581              : {
    1582            0 :     GLuint scalefbo = gbuf.shouldscale();
    1583            0 :     if(scalefbo)
    1584              :     {
    1585            0 :         vieww = gw;
    1586            0 :         viewh = gh;
    1587              :     }
    1588            0 :     float fogmargin = 1 + wateramplitude + nearplane;
    1589            0 :     int fogmat = rootworld.lookupmaterial(vec(camera1->o.x, camera1->o.y, camera1->o.z - fogmargin))&(MatFlag_Volume|MatFlag_Index),
    1590            0 :         abovemat = Mat_Air;
    1591            0 :     float fogbelow = 0;
    1592            0 :     if(IS_LIQUID(fogmat&MatFlag_Volume)) //if in the water
    1593              :     {
    1594            0 :         float z = findsurface(fogmat, vec(camera1->o.x, camera1->o.y, camera1->o.z - fogmargin), abovemat) - wateroffset;
    1595            0 :         if(camera1->o.z < z + fogmargin)
    1596              :         {
    1597            0 :             fogbelow = z - camera1->o.z;
    1598              :         }
    1599              :         else
    1600              :         {
    1601            0 :             fogmat = abovemat;
    1602              :         }
    1603              :     }
    1604              :     else
    1605              :     {
    1606            0 :         fogmat = Mat_Air; //use air fog
    1607              :     }
    1608            0 :     setfog(abovemat);
    1609              :     //setfog(fogmat, fogbelow, 1, abovemat);
    1610              : 
    1611            0 :     farplane = rootworld.mapsize()*2;
    1612              :     //set the camera location
    1613            0 :     projmatrix.perspective(fovy, aspect, nearplane, farplane);
    1614            0 :     setcamprojmatrix();
    1615              : 
    1616            0 :     glEnable(GL_CULL_FACE);
    1617            0 :     glEnable(GL_DEPTH_TEST);
    1618              : 
    1619            0 :     ldrscale = 0.5f;
    1620              :     //do occlusion culling
    1621            0 :     view.visiblecubes();
    1622              :     //set to wireframe if applicable
    1623            0 :     if(wireframe && editmode)
    1624              :     {
    1625            0 :         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    1626              :     }
    1627              :     //construct g-buffer (build basic scene)
    1628            0 :     gbuf.rendergbuffer(true, gamefxn);
    1629            0 :     if(wireframe && editmode) //done with wireframe mode now
    1630              :     {
    1631            0 :         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    1632              :     }
    1633            0 :     else if(limitsky() && editmode)
    1634              :     {
    1635            0 :         renderexplicitsky(true);
    1636              :     }
    1637              : 
    1638              :     //ambient obscurance (ambient occlusion) on geometry & models only
    1639            0 :     gbuf.renderao();
    1640            0 :     glerror();
    1641              : 
    1642              :     // render avatar after AO to avoid weird contact shadows
    1643            0 :     renderavatar(hudfxn);
    1644            0 :     glerror();
    1645              : 
    1646              :     // render grass after AO to avoid disturbing shimmering patterns
    1647            0 :     generategrass();
    1648            0 :     rendergrass();
    1649            0 :     glerror();
    1650              : 
    1651            0 :     glFlush();
    1652              :     //global illumination
    1653            0 :     gbuf.renderradiancehints();
    1654            0 :     glerror();
    1655              :     //lighting
    1656            0 :     gbuf.rendershadowatlas();
    1657            0 :     glerror();
    1658              :     //shading
    1659            0 :     shadegbuffer();
    1660            0 :     glerror();
    1661              : 
    1662              :     //fog
    1663            0 :     if(fogmat)
    1664              :     {
    1665            0 :         setfog(fogmat, fogbelow, 1, abovemat);
    1666              : 
    1667            0 :         gbuf.renderwaterfog(fogmat, fogbelow);
    1668              : 
    1669            0 :         setfog(fogmat, fogbelow, std::clamp(fogbelow, 0.0f, 1.0f), abovemat);
    1670              :     }
    1671              : 
    1672              :     //alpha
    1673            0 :     gbuf.rendertransparent();
    1674            0 :     glerror();
    1675              : 
    1676            0 :     if(fogmat)
    1677              :     {
    1678            0 :         setfog(fogmat, fogbelow, 1, abovemat);
    1679              :     }
    1680              : 
    1681              :     //volumetric lights
    1682            0 :     gbuf.rendervolumetric();
    1683            0 :     glerror();
    1684              : 
    1685            0 :     if(editmode)
    1686              :     {
    1687            0 :         if(!wireframe && outline)
    1688              :         {
    1689            0 :             renderoutline(); //edit mode geometry outline
    1690              :         }
    1691            0 :         glerror();
    1692            0 :         rendereditmaterials();
    1693            0 :         glerror();
    1694            0 :         gbuf.renderparticles();
    1695            0 :         glerror();
    1696            0 :         if(showhud)
    1697              :         {
    1698            0 :             glDepthMask(GL_FALSE);
    1699            0 :             editfxn(); //edit cursor, passed as pointer
    1700            0 :             glDepthMask(GL_TRUE);
    1701              :         }
    1702              :     }
    1703              : 
    1704              :     //we're done with depth/geometry stuff so we don't need this functionality
    1705            0 :     glDisable(GL_CULL_FACE);
    1706            0 :     glDisable(GL_DEPTH_TEST);
    1707              : 
    1708            0 :     if(fogoverlay && fogmat != Mat_Air)
    1709              :     {
    1710            0 :         drawfogoverlay(fogmat, fogbelow, std::clamp(fogbelow, 0.0f, 1.0f), abovemat);
    1711              :     }
    1712              :     //antialiasing
    1713            0 :     doaa(setuppostfx(gbuf, vieww, viewh, scalefbo), gbuf);
    1714              :     //postfx
    1715            0 :     renderpostfx(scalefbo);
    1716            0 :     if(scalefbo)
    1717              :     {
    1718            0 :         gbuf.doscale();
    1719              :     }
    1720            0 : }
    1721              : 
    1722            0 : int renderw()
    1723              : {
    1724            0 :     return std::min(scr_w, screenw);
    1725              : }
    1726              : 
    1727            0 : int renderh()
    1728              : {
    1729            0 :     return std::min(scr_h, screenh);
    1730              : }
    1731              : 
    1732            0 : int hudw()
    1733              : {
    1734            0 :     return screenw;
    1735              : }
    1736              : 
    1737            0 : int hudh()
    1738              : {
    1739            0 :     return screenh;
    1740              : }
    1741              : 
    1742            0 : void gl_setupframe(bool force)
    1743              : {
    1744            0 :     if(!force)
    1745              :     {
    1746            0 :         return;
    1747              :     }
    1748            0 :     setuplights(gbuf);
    1749              : }
    1750              : 
    1751            0 : void gl_drawframe(int crosshairindex, void (*gamefxn)(), void (*hudfxn)(), void (*editfxn)(), void (*hud2d)())
    1752              : {
    1753            0 :     synctimers();
    1754            0 :     xtravertsva = xtraverts = glde = gbatches = vtris = vverts = 0;
    1755            0 :     occlusionengine.flipqueries();
    1756            0 :     aspect = forceaspect ? forceaspect : hudw()/static_cast<float>(hudh());
    1757            0 :     fovy = 2*std::atan2(std::tan(curfov/(2*RAD)), aspect)*RAD;
    1758            0 :     vieww = hudw();
    1759            0 :     viewh = hudh();
    1760            0 :     if(mainmenu)
    1761              :     {
    1762            0 :         gl_drawmainmenu();
    1763              :     }
    1764              :     else
    1765              :     {
    1766            0 :         gl_drawview(gamefxn, hudfxn, editfxn);
    1767              :     }
    1768            0 :     UI::render();
    1769            0 :     gl_drawhud(crosshairindex, hud2d);
    1770            0 : }
    1771              : 
    1772            0 : void cleanupgl()
    1773              : {
    1774            0 :     clearminimap();
    1775            0 :     cleanuptimers();
    1776            0 :     cleanupscreenquad();
    1777            0 :     gle::cleanup();
    1778            0 : }
    1779              : 
    1780            1 : void initrenderglcmds()
    1781              : {
    1782            1 :     addcommand("glext", reinterpret_cast<identfun>(glext), "s", Id_Command);
    1783            2 :     addcommand("getcamyaw", reinterpret_cast<identfun>(+[](){floatret(camera1 ? camera1->yaw : 0);}), "", Id_Command);
    1784            2 :     addcommand("getcampitch", reinterpret_cast<identfun>(+[](){floatret(camera1 ? camera1->pitch : 0);}), "", Id_Command);
    1785            2 :     addcommand("getcamroll", reinterpret_cast<identfun>(+[](){floatret(camera1 ? camera1->roll : 0);}), "", Id_Command);
    1786            1 :     addcommand("getcampos", reinterpret_cast<identfun>(+[]()
    1787              :     {
    1788            1 :         if(!camera1)
    1789              :         {
    1790            1 :             result("no camera");
    1791              :         }
    1792              :         else
    1793              :         {
    1794            0 :             std::string pos = std::format("{} {} {}",floatstr(camera1->o.x), floatstr(camera1->o.y), floatstr(camera1->o.z));
    1795            0 :             result(pos.c_str());
    1796            0 :         }
    1797            2 :     }), "", Id_Command);
    1798            1 : }
        

Generated by: LCOV version 2.0-1