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