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