forked from SNMetamorph/PrimeXT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl_cubemap.cpp
More file actions
774 lines (664 loc) · 18.9 KB
/
Copy pathgl_cubemap.cpp
File metadata and controls
774 lines (664 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
gl_cubemaps.cpp - tools for cubemaps search & handling
Copyright (C) 2016 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "gl_cubemap.h"
#include "hud.h"
#include "utils.h"
#include "const.h"
#include "com_model.h"
#include "ref_params.h"
#include "gl_local.h"
#include "gl_decals.h"
#include <mathlib.h>
#include "gl_world.h"
#include "gl_cvars.h"
#include "gl_debug.h"
#include "gl_viewport.h"
#include "gl_unit_cube.h"
static word g_shaderFilterSpecularIBL;
void R_InitCubemaps()
{
gEngfuncs.pfnAddCommand("buildcubemaps", CL_BuildCubemaps_f);
}
void R_InitCubemapShaders()
{
g_shaderFilterSpecularIBL = GL_FindShader("common/ibl_filter_specular", "common/ibl_filter_specular", "common/ibl_filter_specular");
}
/*
=================
CL_FindNearestCubeMap
find the nearest cubemap for a given point
=================
*/
void CL_FindNearestCubeMap( const Vector &pos, mcubemap_t **result )
{
if( !result ) return;
float maxDist = 99999.0f;
*result = NULL;
for( int i = 0; i < world->num_cubemaps; i++ )
{
mcubemap_t *check = &world->cubemaps[i];
float dist = VectorDistance( check->origin, pos );
if( dist < maxDist )
{
*result = check;
maxDist = dist;
}
}
if( !*result )
{
// this may happens if map
// doesn't have any cubemaps
*result = &world->defaultCubemap;
}
}
/*
=================
CL_FindNearestCubeMapForSurface
find the nearest cubemap on front of plane
=================
*/
void CL_FindNearestCubeMapForSurface( const Vector &pos, const msurface_t *surf, mcubemap_t **result )
{
if( !result ) return;
float maxDist = 99999.0f;
mplane_t plane;
*result = NULL;
plane = *surf->plane;
if( FBitSet( surf->flags, SURF_PLANEBACK ))
{
plane.normal = -plane.normal;
plane.dist = -plane.dist;
}
for( int i = 0; i < world->num_cubemaps; i++ )
{
mcubemap_t *check = &world->cubemaps[i];
float dist = VectorDistance( check->origin, pos );
if( dist < maxDist && PlaneDiff( check->origin, &plane ) >= 0.0f )
{
*result = check;
maxDist = dist;
}
}
if( *result ) return;
// fallback to default method
CL_FindNearestCubeMap( pos, result );
}
/*
=================
CL_FindTwoNearestCubeMap
find the two nearest cubemaps for a given point
=================
*/
void CL_FindTwoNearestCubeMap( const Vector &pos, mcubemap_t **result1, mcubemap_t **result2 )
{
if( !result1 || !result2 )
return;
#if 0
// faster, but not reliable
float maxDist1 = 999999.0f;
float maxDist2 = 999999.0f;
*result1 = *result2 = NULL;
for( int i = 0; i < world->num_cubemaps; i++ )
{
mcubemap_t *check = &world->cubemaps[i];
float dist = VectorDistance( check->origin, pos );
if( dist < maxDist1 )
{
*result1 = check;
maxDist1 = dist;
}
else if( dist < maxDist2 && dist > maxDist1 )
{
*result2 = check;
maxDist2 = dist;
}
}
#else
// slower 2x, but reliable
float distances[2] = { 999999.f, 999999.f };
mcubemap_t **results[2] = { result1, result2 };
for (int j = 0; j < 2; ++j)
{
for (int i = 0; i < world->num_cubemaps; i++)
{
mcubemap_t *check = &world->cubemaps[i];
float dist = (check->origin - pos).LengthSqr();
if (dist < distances[j])
{
if (j < 1 || check != *results[j - 1])
{
*results[j] = check;
distances[j] = dist;
}
}
}
}
#endif
if( !*result1 )
{
// this may happens if map
// doesn't have any cubemaps
*result1 = &world->defaultCubemap;
}
if( !*result2 )
{
// this may happens if map
// doesn't have any cubemaps
*result2 = *result1;
}
}
/*
=================
CL_FindTwoNearestCubeMapForSurface
find the two nearest cubemaps on front of plane
=================
*/
void CL_FindTwoNearestCubeMapForSurface( const Vector &pos, const msurface_t *surf, mcubemap_t **result1, mcubemap_t **result2 )
{
if( !result1 || !result2 ) return;
float maxDist1 = 99999.0f;
float maxDist2 = 99999.0f;
mplane_t plane;
*result1 = NULL;
*result2 = NULL;
plane = *surf->plane;
if( FBitSet( surf->flags, SURF_PLANEBACK ))
{
plane.normal = -plane.normal;
plane.dist = -plane.dist;
}
for( int i = 0; i < world->num_cubemaps; i++ )
{
mcubemap_t *check = &world->cubemaps[i];
float dist = VectorDistance( check->origin, pos );
if( dist < maxDist1 && PlaneDiff( check->origin, &plane ) >= 0.0f )
{
*result1 = check;
maxDist1 = dist;
}
else if( dist < maxDist2 && dist > maxDist1 )
{
*result2 = check;
maxDist2 = dist;
}
}
if( *result1 )
{
if( !*result2 )
*result2 = *result1;
return;
}
// fallback to default method
CL_FindTwoNearestCubeMap( pos, result1, result2 );
}
/*
=================
CL_GetCubemapSideViewangles
returns viewangles which needed to render desired cubemap side
=================
*/
Vector CL_GetCubemapSideViewangles(int side)
{
static Vector cubemapSideAngles[] =
{
Vector(0.0f, 0.0f, 90.0f), // GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
Vector(0.0f, 180.0f, -90.0f), // GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
Vector(0.0f, 90.0f, 0.0f), // GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
Vector(0.0f, 270.0f, 180.0f), // GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
Vector(-90.0f, 180.0f, -90.0f), // GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
Vector(90.0f, 0.0f, 90.0f), // GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
};
return cubemapSideAngles[side];
}
/*
==================
Mod_AllocateCubemap
allocate cubemap texture and FBO
==================
*/
static bool Mod_AllocateCubemap(mcubemap_t *cubemap)
{
int flags = TF_CUBEMAP;
if (!cubemap->texture.Initialized())
{
if (GL_Support(R_SEAMLESS_CUBEMAP)) {
SetBits(flags, TF_BORDER); // seamless cubemaps have support for border
}
else {
SetBits(flags, TF_CLAMP); // default method
}
if (CVAR_TO_BOOL(gl_hdr))
{
// set FP16 format for cubemaps (it's important for PBR and reflections)
SetBits(flags, TF_ARB_16BIT);
SetBits(flags, TF_ARB_FLOAT);
}
cubemap->texture = CREATE_TEXTURE(cubemap->name, cubemap->size, cubemap->size, NULL, flags);
}
cubemap->framebuffer.Init(FBO_CUBE, cubemap->size, cubemap->size, FBO_NOTEXTURE);
bool textureTargetValid = cubemap->texture.GetGlTarget() == GL_TEXTURE_CUBE_MAP_ARB;
return textureTargetValid && cubemap->framebuffer.ValidateFBO();
}
/*
==================
Mod_FreeCubemap
unload a given cubemap
==================
*/
static void Mod_FreeCubemap(mcubemap_t *cubemap)
{
if (cubemap->valid && cubemap->texture.Initialized() && cubemap->texture != tr.whiteCubeTexture) {
FREE_TEXTURE(cubemap->texture);
}
cubemap->valid = false;
cubemap->numMips = 0;
cubemap->texture = TextureHandle::Null();
cubemap->framebuffer.Free();
ClearBounds(cubemap->mins, cubemap->maxs);
}
/*
==================
Mod_FreeCubemaps
purge all the cubemaps
from current level
==================
*/
void Mod_FreeCubemaps()
{
for (int i = 0; i < world->num_cubemaps; i++)
Mod_FreeCubemap(&world->cubemaps[i]);
Mod_FreeCubemap(&world->defaultCubemap);
world->build_default_cubemap = false;
world->loading_cubemaps = false;
world->num_cubemaps = 0;
}
/*
==================
Mod_CheckCubemap
checks for cubemap sides existance and validates it
==================
*/
static bool Mod_CheckCubemap(const char *name)
{
const char *suf[6] = { "px", "nx", "py", "ny", "pz", "nz" };
int valid_sides = 0;
char sidename[64];
int iCompare;
// FIXME: potentially unsafe checking: looking for DDS_CUBEMAP bit?
if (FILE_EXISTS(va("maps/env/%s/%s.dds", world->name, name)))
return true;
for (int i = 0; i < 6; i++)
{
Q_snprintf(sidename, sizeof(sidename), "maps/env/%s/%s%s.tga", world->name, name, suf[i]);
if (COMPARE_FILE_TIME(worldmodel->name, sidename, &iCompare) && iCompare <= 0)
valid_sides++;
}
return (valid_sides == 6) ? true : false;
}
/*
==================
Mod_DeleteCubemap
remove cubemap images from HDD
==================
*/
static void Mod_DeleteCubemap(const char *name)
{
const char *suf[6] = { "px", "nx", "py", "ny", "pz", "nz" };
char sidename[64];
for (int i = 0; i < 6; i++)
{
Q_snprintf(sidename, sizeof(sidename), "maps/env/%s/%s%s.tga", world->name, name, suf[i]);
if (FILE_EXISTS(sidename))
Sys_RemoveFile(sidename);
}
}
/*
=================
CL_BuildCubemaps_f
force to rebuilds all the cubemaps
in the scene
=================
*/
void CL_BuildCubemaps_f()
{
Mod_FreeCubemap(&world->defaultCubemap);
for (int i = 0; i < world->num_cubemaps; i++)
{
mcubemap_t *m = &world->cubemaps[i];
Mod_FreeCubemap(m);
}
if (FBitSet(world->features, WORLD_HAS_SKYBOX)) {
world->build_default_cubemap = true;
}
world->loading_cubemaps = true;
}
/*
==================
CL_LinkCubemapsWithSurfaces
assing cubemaps onto world surfaces
so we don't need to search them again
==================
*/
static void CL_LinkCubemapsWithSurfaces()
{
for (int i = 0; i < worldmodel->numsurfaces; ++i)
{
msurface_t *surf = &worldmodel->surfaces[i];
mextrasurf_t *es = surf->info;
CL_FindTwoNearestCubeMapForSurface(es->origin, surf, &es->cubemap[0], &es->cubemap[1]);
// compute lerp factor
float dist0 = (es->cubemap[0]->origin - es->origin).Length();
float dist1 = (es->cubemap[1]->origin - es->origin).Length();
es->lerpFactor = dist0 / (dist0 + dist1);
}
}
static void GL_CreateCubemap(dcubemap_t *src, mcubemap_t *dest, int index)
{
// build a cubemap name like enum
new (dest) mcubemap_t();
Q_snprintf(dest->name, sizeof(dest->name), "cubemap_%i", index);
VectorCopy(src->origin, dest->origin);
ClearBounds(dest->mins, dest->maxs);
dest->size = src->size;
if (dest->size <= 0)
dest->size = DEFAULT_CUBEMAP_SIZE;
dest->valid = false;
dest->size = NearestPOW(bound(1, dest->size, 512), false);
}
static void GL_CreateSkyboxCubemap(mcubemap_t *cubemap)
{
Q_snprintf(cubemap->name, sizeof(cubemap->name), "cubemap_%s", world->name);
cubemap->origin = (worldmodel->mins + worldmodel->maxs) * 0.5f;
cubemap->valid = false;
cubemap->size = 256; // default cubemap larger than others
}
static void GL_CreateStubCubemap(mcubemap_t *cubemap)
{
Q_snprintf(cubemap->name, sizeof(cubemap->name), "*whiteCube");
cubemap->origin = (worldmodel->mins + worldmodel->maxs) * 0.5f;
cubemap->texture = tr.whiteCubeTexture;
cubemap->textureSpecularIBL = tr.blackCubeTexture;
cubemap->valid = true;
cubemap->size = 4;
}
static void GL_CreateCubemapSpecularIBL(mcubemap_t *cubemap)
{
int flags = TF_CUBEMAP;
const int resolution = 128;
if (!cubemap->textureSpecularIBL.Initialized())
{
if (GL_Support(R_SEAMLESS_CUBEMAP)) {
SetBits(flags, TF_BORDER); // seamless cubemaps have support for border
}
else {
SetBits(flags, TF_CLAMP); // default method
}
// set FP16 format for cubemaps (it's important for PBR and reflections)
SetBits(flags, TF_ARB_16BIT);
SetBits(flags, TF_ARB_FLOAT);
cubemap->textureSpecularIBL = CREATE_TEXTURE(va("%s_ibl_specular", cubemap->name), resolution, resolution, NULL, flags);
}
// allocate GPU memory for texture, enable trilinear filtering and generate mip-maps
pglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubemap->textureSpecularIBL.GetGlHandle());
for (int i = 0; i < 6; ++i) {
pglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + i, 0, GL_RGB16F, resolution, resolution, 0, GL_RGB, GL_FLOAT, nullptr);
}
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
pglGenerateMipmap(GL_TEXTURE_CUBE_MAP_ARB);
pglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);
cubemap->fboSpecularIBL.Init(FBO_CUBE, resolution, resolution, FBO_NOTEXTURE);
cubemap->fboSpecularIBL.ValidateFBO();
}
static void GL_ComputeCubemapViewBoxSize(mcubemap_t *cubemap)
{
pmtrace_t pmtrace;
Vector vecStart, vecEnd;
const float distance = 65536.f;
static Vector env_dir[] =
{
Vector(1.0f, 0.0f, 0.0f),
Vector(-1.0f, 0.0f, 0.0f),
Vector(0.0f, 1.0f, 0.0f),
Vector(0.0f, -1.0f, 0.0f),
Vector(0.0f, 0.0f, 1.0f),
Vector(0.0f, 0.0f, -1.0f)
};
for (int j = 0; j < 6; j++)
{
vecStart = cubemap->origin;
vecEnd = vecStart + env_dir[j] * distance;
gEngfuncs.pEventAPI->EV_SetTraceHull(2);
gEngfuncs.pEventAPI->EV_PlayerTrace(vecStart, vecStart + vecEnd, PM_WORLD_ONLY, -1, &pmtrace);
AddPointToBounds(pmtrace.endpos, cubemap->mins, cubemap->maxs);
}
}
static void GL_SetupCubemapSideView(mcubemap_t *cubemap, ref_viewpass_t &rvp, int side)
{
CViewport cubeSideViewport;
cubeSideViewport.SetX(0);
cubeSideViewport.SetY(0);
cubeSideViewport.SetWidth(cubemap->size);
cubeSideViewport.SetHeight(cubemap->size);
cubeSideViewport.WriteToArray(rvp.viewport);
rvp.vieworigin = cubemap->origin;
rvp.viewangles = CL_GetCubemapSideViewangles(side);
rvp.viewentity = 0;
rvp.fov_x = rvp.fov_y = 90.0f;
rvp.flags = RP_DRAW_WORLD;
if (world->build_default_cubemap) {
SetBits(rvp.flags, RP_SKYVIEW);
}
else {
SetBits(rvp.flags, RP_ENVVIEW);
}
}
static void GL_FilterCubemapSpecularIBL(mcubemap_t *cubemap)
{
GLfloat matrixBuffer[16];
matrix4x4 projectionMatrix;
const int resolution = cubemap->fboSpecularIBL.GetWidth();
const int mipLevelCount = 1 + floor(log2(resolution));
GL_BindShader(&glsl_programs[g_shaderFilterSpecularIBL]);
projectionMatrix.CreateProjection(90.0f, 90.0f, 0.1, 10.0);
for (int mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel)
{
int width = resolution * pow(0.5, mipLevel);
int height = resolution * pow(0.5, mipLevel);
float roughness = mipLevel / static_cast<float>(mipLevelCount - 1);
pglViewport(0, 0, width, height);
for (int i = 0; i < 6; ++i)
{
matrix4x4 modelViewMatrix;
cubemap->fboSpecularIBL.Bind(cubemap->textureSpecularIBL, i, mipLevel);
COpenGLUnitCube::GetInstance().CreateModelViewMatrix(
modelViewMatrix, CL_GetCubemapSideViewangles(i)
);
for (int j = 0; j < RI->currentshader->numUniforms; j++)
{
uniform_t *u = &RI->currentshader->uniforms[j];
switch (u->type)
{
case UT_ENVMAP0:
u->SetValue(cubemap->texture.ToInt());
break;
case UT_SCREENWIDTH: // size of source cubemap
u->SetValue((float)cubemap->size);
break;
case UT_SMOOTHNESS: // roughness
u->SetValue(roughness);
break;
case UT_MODELMATRIX:
modelViewMatrix.CopyToArray(matrixBuffer);
u->SetValue(&matrixBuffer[0]);
break;
case UT_REFLECTMATRIX:
projectionMatrix.CopyToArray(matrixBuffer);
u->SetValue(&matrixBuffer[0]);
break;
case UT_MODELVIEWMATRIX:
u->SetValue(&RI->view.worldMatrix);
break;
case UT_MODELVIEWPROJECTIONMATRIX:
u->SetValue(&RI->view.worldProjectionMatrix);
break;
case UT_CLIPPLANE:
{
GLdouble clip[4];
mplane_t *p = &RI->clipPlane;
GL_InitClipPlane(p, clip);
u->SetValue(clip);
break;
}
}
}
COpenGLUnitCube::GetInstance().Draw();
}
}
}
static void GL_RenderCubemapSide(mcubemap_t *cubemap, int side)
{
GL_DEBUG_SCOPE();
ref_viewpass_t rvp;
cubemap->framebuffer.Bind(cubemap->texture, side);
GL_SetupCubemapSideView(cubemap, rvp, side);
R_RenderScene(&rvp, static_cast<RefParams>(rvp.flags));
}
static void GL_RenderCubemap(mcubemap_t *cubemap)
{
GL_DEBUG_SCOPE();
if (Mod_AllocateCubemap(cubemap))
{
GL_ComputeCubemapViewBoxSize(cubemap);
for (int i = 0; i < 6; ++i)
{
GL_RenderCubemapSide(cubemap, i);
R_ResetRefState();
}
// enable trilinear filtering and generate mip-maps
pglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubemap->texture.GetGlHandle());
pglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
pglGenerateMipmap(GL_TEXTURE_CUBE_MAP_ARB);
pglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);
cubemap->numMips = 1 + floor(log2(cubemap->size));
cubemap->valid = true;
}
else {
ALERT(at_warning, "GL_RenderCubemap: failed to allocate cubemap \"%s\"\n", cubemap->name);
}
}
/*
==================
GL_LoadAndRebuildCubemaps
rebuild cubemaps that older than bspfile
loading actual cubemaps into videomemory
==================
*/
void GL_LoadAndRebuildCubemaps(RefParams refParams)
{
GL_DEBUG_SCOPE();
ZoneScoped;
bool realtimeBaking = CVAR_TO_BOOL(r_cubemap_realtime);
if (!world->loading_cubemaps && !realtimeBaking)
return; // job is done
if (RP_CUBEPASS())
return; // already in cubemap-rendering mode
int oldFBO = glState.frameBuffer;
R_PushRefState();
if (world->build_default_cubemap)
{
GL_RenderCubemap(&world->defaultCubemap);
GL_CreateCubemapSpecularIBL(&world->defaultCubemap);
GL_FilterCubemapSpecularIBL(&world->defaultCubemap);
world->build_default_cubemap = false; // done
}
for (int i = 0; i < world->num_cubemaps; i++)
{
mcubemap_t *cm = &world->cubemaps[i];
if (!cm->valid) {
GL_RenderCubemap(cm);
}
}
if (realtimeBaking)
{
mcubemap_t *cubemapFirst, *cubemapSecond;
CL_FindTwoNearestCubeMap(RI->view.origin, &cubemapFirst, &cubemapSecond);
GL_RenderCubemap(cubemapFirst);
if (cubemapFirst != cubemapSecond) {
GL_RenderCubemap(cubemapSecond);
}
}
// perform IBL specular-term filtering
GL_DepthTest(GL_FALSE);
for (int i = 0; i < world->num_cubemaps; i++)
{
mcubemap_t *cm = &world->cubemaps[i];
GL_CreateCubemapSpecularIBL(cm);
GL_FilterCubemapSpecularIBL(cm);
}
// we reached the end of list
// next frame will be restored gamma
SetBits(cv_brightness->flags, FCVAR_CHANGED);
SetBits(cv_gamma->flags, FCVAR_CHANGED);
tr.params_changed = true;
tr.glsl_valid_sequence++;
tr.fClearScreen = false;
world->loading_cubemaps = false;
R_PopRefState();
GL_BindFBO(oldFBO);
GL_BindShader(NULL);
CL_LinkCubemapsWithSurfaces();
}
/*
=================
Mod_LoadCubemaps
=================
*/
void Mod_LoadCubemaps(const byte *base, const dlump_t *l)
{
int count;
dcubemap_t *in = (dcubemap_t *)(base + l->fileofs);
mcubemap_t *out = world->cubemaps;
if (l->filelen % sizeof(*in))
HOST_ERROR("Mod_LoadCubemaps: funny lump size\n");
count = l->filelen / sizeof(*in);
if (count >= MAX_MAP_CUBEMAPS)
{
ALERT(at_error, "Mod_LoadCubemaps: map contain too many cubemaps. Will handle only first %i items\n", MAX_MAP_CUBEMAPS);
count = MAX_MAP_CUBEMAPS;
}
world->num_cubemaps = count;
world->loading_cubemaps = true;
// makes an default cubemap from skybox
if (FBitSet(world->features, WORLD_HAS_SKYBOX) && world->loading_cubemaps)
{
GL_CreateSkyboxCubemap(&world->defaultCubemap);
world->build_default_cubemap = true;
}
else
{
// using stub as default cubemap
GL_CreateStubCubemap(&world->defaultCubemap);
}
for (int i = 0; i < count; i++, in++, out++) {
GL_CreateCubemap(in, out, i);
}
// user request for disable autorebuild
//if (gEngfuncs.CheckParm("-noautorebuildcubemaps", NULL))
//{
// world->build_default_cubemap = false;
//}
}