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