Line data Source code
1 : // renderva.cpp: handles the occlusion and rendering of vertex arrays
2 :
3 : #include "../libprimis-headers/cube.h"
4 : #include "../../shared/geomexts.h"
5 : #include "../../shared/glemu.h"
6 : #include "../../shared/glexts.h"
7 :
8 : #include <memory>
9 : #include <optional>
10 :
11 : #include "csm.h"
12 : #include "grass.h"
13 : #include "octarender.h"
14 : #include "radiancehints.h"
15 : #include "rendergl.h"
16 : #include "renderlights.h"
17 : #include "rendermodel.h"
18 : #include "renderva.h"
19 : #include "renderwindow.h"
20 : #include "rendersky.h"
21 : #include "shaderparam.h"
22 : #include "shader.h"
23 : #include "texture.h"
24 :
25 : #include "interface/control.h"
26 :
27 : #include "world/entities.h"
28 : #include "world/light.h"
29 : #include "world/octaedit.h"
30 : #include "world/octaworld.h"
31 : #include "world/raycube.h"
32 : #include "world/bih.h"
33 : #include "world/world.h"
34 :
35 :
36 : #include "model/model.h"
37 :
38 : //oqfrags, outlinecolor are both extern vars
39 : VAR(oqfrags, 0, 8, 64); //occlusion query fragments
40 0 : CVARP(outlinecolor, 0); //color of edit mode outlines
41 :
42 : float shadowradius = 0,
43 : shadowbias = 0;
44 : size_t shadowside = 0;
45 : int shadowspot = 0;
46 : vec shadoworigin(0, 0, 0),
47 : shadowdir(0, 0, 0);
48 :
49 : vtxarray *visibleva = nullptr;
50 : vfc view;
51 :
52 : int deferquery = 0;
53 :
54 : struct shadowmesh final
55 : {
56 : vec origin;
57 : float radius;
58 : vec spotloc;
59 : int spotangle;
60 : int type;
61 : std::array<int, 6> draws;
62 : };
63 :
64 : Occluder occlusionengine;
65 :
66 : /* internally relevant functionality */
67 : ///////////////////////////////////////
68 :
69 : namespace
70 : {
71 0 : void drawtris(GLsizei numindices, const GLvoid *indices, GLuint minvert, GLuint maxvert)
72 : {
73 0 : glDrawRangeElements(GL_TRIANGLES, minvert, maxvert, numindices, GL_UNSIGNED_SHORT, indices);
74 0 : glde++;
75 0 : }
76 :
77 0 : void drawvatris(const vtxarray &va, GLsizei numindices, int offset)
78 : {
79 0 : drawtris(numindices, static_cast<ushort *>(nullptr) + va.eoffset + offset, va.minvert, va.maxvert);
80 0 : }
81 :
82 0 : void drawvaskytris(const vtxarray &va)
83 : {
84 0 : drawtris(va.sky, static_cast<ushort *>(nullptr) + va.skyoffset, va.minvert, va.maxvert);
85 0 : }
86 :
87 : ///////// view frustrum culling ///////////////////////
88 :
89 0 : float vadist(const vtxarray &va, const vec &p)
90 : {
91 0 : return p.dist_to_bb(va.bbmin, va.bbmax);
92 : }
93 :
94 0 : void addvisibleva(vtxarray *va, std::array<vtxarray *, vasortsize> &vasort)
95 : {
96 0 : float dist = vadist(*va, camera1->o);
97 0 : va->distance = static_cast<int>(dist); /*cv.dist(camera1->o) - va->size*SQRT3/2*/
98 :
99 0 : int hash = std::clamp(static_cast<int>(dist*vasortsize/rootworld.mapsize()), 0, vasortsize-1);
100 0 : vtxarray **prev = &vasort[hash],
101 0 : *cur = vasort[hash];
102 :
103 0 : while(cur && va->distance >= cur->distance)
104 : {
105 0 : prev = &cur->next;
106 0 : cur = cur->next;
107 : }
108 :
109 0 : va->next = cur;
110 0 : *prev = va;
111 0 : }
112 :
113 0 : void sortvisiblevas(const std::array<vtxarray *, vasortsize> &vasort)
114 : {
115 0 : visibleva = nullptr;
116 0 : vtxarray **last = &visibleva;
117 0 : for(vtxarray *i : vasort)
118 : {
119 0 : if(i)
120 : {
121 0 : vtxarray *va = i;
122 0 : *last = va;
123 0 : while(va->next)
124 : {
125 0 : va = va->next;
126 : }
127 0 : last = &va->next;
128 : }
129 : }
130 0 : }
131 :
132 : template<bool fullvis, bool resetocclude>
133 0 : void findvisiblevas(std::vector<vtxarray *> &vas, std::array<vtxarray *, vasortsize> &vasort)
134 : {
135 0 : for(size_t i = 0; i < vas.size(); i++)
136 : {
137 0 : vtxarray &v = *vas[i];
138 0 : int prevvfc = v.curvfc;
139 0 : v.curvfc = fullvis ? ViewFrustumCull_FullyVisible : view.isvisiblecube(v.o, v.size);
140 0 : if(v.curvfc != ViewFrustumCull_NotVisible)
141 : {
142 0 : bool resetchildren = prevvfc >= ViewFrustumCull_NotVisible || resetocclude;
143 0 : if(resetchildren)
144 : {
145 0 : v.occluded = !v.texs ? Occlude_Geom : Occlude_Nothing;
146 0 : v.query = nullptr;
147 : }
148 0 : addvisibleva(&v, vasort);
149 0 : if(v.children.size())
150 : {
151 0 : if(fullvis || v.curvfc == ViewFrustumCull_FullyVisible)
152 : {
153 0 : if(resetchildren)
154 : {
155 0 : findvisiblevas<true, true>(v.children, vasort);
156 : }
157 : else
158 : {
159 0 : findvisiblevas<true, false>(v.children, vasort);
160 : }
161 : }
162 0 : else if(resetchildren)
163 : {
164 0 : findvisiblevas<false, true>(v.children, vasort);
165 : }
166 : else
167 : {
168 0 : findvisiblevas<false, false>(v.children, vasort);
169 : }
170 : }
171 : }
172 : }
173 0 : }
174 :
175 0 : void findvisiblevas()
176 : {
177 : std::array<vtxarray *, vasortsize> vasort;
178 0 : vasort.fill(nullptr);
179 0 : findvisiblevas<false, false>(varoot, vasort);
180 0 : sortvisiblevas(vasort);
181 0 : }
182 :
183 : ///////// occlusion queries /////////////
184 :
185 0 : VARF(oqany, 0, 0, 2, occlusionengine.clearqueries()); //occlusion query settings: 0: GL_SAMPLES_PASSED, 1: GL_ANY_SAMPLES_PASSED, 2: GL_ANY_SAMPLES_PASSED_CONSERVATIVE
186 : VAR(oqwait, 0, 1, 1);
187 :
188 : /**
189 : * @brief Returns query target
190 : *
191 : * GL_SAMPLES_PASSED if oqany is 0
192 : * GL_ANY_SAMPLES_PASSED if oqany is 1 or ES3 is not present
193 : * GL_ANY_SAMPLES_PASSED if oqany is 2 and ES3 is present
194 : */
195 0 : GLenum querytarget()
196 : {
197 0 : return oqany ?
198 0 : (oqany > 1 && hasES3 ?
199 : GL_ANY_SAMPLES_PASSED_CONSERVATIVE :
200 : GL_ANY_SAMPLES_PASSED) :
201 0 : GL_SAMPLES_PASSED;
202 : }
203 :
204 : GLuint bbvbo = 0,
205 : bbebo = 0;
206 :
207 0 : void setupbb()
208 : {
209 0 : if(!bbvbo)
210 : {
211 0 : glGenBuffers(1, &bbvbo);
212 0 : gle::bindvbo(bbvbo);
213 0 : vec verts[8];
214 0 : for(int i = 0; i < 8; ++i)
215 : {
216 0 : verts[i] = vec(i&1, (i>>1)&1, (i>>2)&1);
217 : }
218 0 : glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
219 0 : gle::clearvbo();
220 : }
221 0 : if(!bbebo)
222 : {
223 0 : glGenBuffers(1, &bbebo);
224 0 : gle::bindebo(bbebo);
225 : std::array<ushort, 3*2*6> tris;
226 : //======================================== GENFACEVERT GENFACEORIENT
227 : #define GENFACEORIENT(orient, v0, v1, v2, v3) do { \
228 : int offset = orient*3*2; \
229 : tris[offset + 0] = v0; \
230 : tris[offset + 1] = v1; \
231 : tris[offset + 2] = v2; \
232 : tris[offset + 3] = v0; \
233 : tris[offset + 4] = v2; \
234 : tris[offset + 5] = v3; \
235 : } while(0);
236 : #define GENFACEVERT(orient, vert, ox,oy,oz, rx,ry,rz) (ox | oy | oz)
237 0 : GENFACEVERTS(0, 1, 0, 2, 0, 4, , , , , , )
238 : #undef GENFACEORIENT
239 : #undef GENFACEVERT
240 : //==================================================================
241 0 : glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ushort)*tris.size(), tris.data(), GL_STATIC_DRAW);
242 0 : gle::clearebo();
243 : }
244 0 : }
245 :
246 0 : void cleanupbb()
247 : {
248 0 : if(bbvbo)
249 : {
250 0 : glDeleteBuffers(1, &bbvbo);
251 0 : bbvbo = 0;
252 : }
253 0 : if(bbebo)
254 : {
255 0 : glDeleteBuffers(1, &bbebo);
256 0 : bbebo = 0;
257 : }
258 0 : }
259 :
260 : octaentities *visiblemms,
261 : **lastvisiblemms;
262 :
263 0 : void findvisiblemms(const std::vector<extentity *> &ents, bool doquery)
264 : {
265 0 : visiblemms = nullptr;
266 0 : lastvisiblemms = &visiblemms;
267 0 : for(vtxarray *va = visibleva; va; va = va->next)
268 : {
269 0 : if(va->occluded < Occlude_BB && va->curvfc < ViewFrustumCull_Fogged)
270 : {
271 0 : for(octaentities *oe : va->mapmodels)
272 : {
273 0 : if(view.isfoggedcube(oe->o, oe->size))
274 : {
275 0 : continue;
276 : }
277 0 : bool occluded = doquery && oe->query && oe->query->owner == oe && occlusionengine.checkquery(oe->query);
278 0 : if(occluded)
279 : {
280 0 : oe->distance = -1;
281 0 : oe->next = nullptr;
282 0 : *lastvisiblemms = oe;
283 0 : lastvisiblemms = &oe->next;
284 : }
285 : else
286 : {
287 0 : int visible = 0;
288 0 : for(const int &i : oe->mapmodels)
289 : {
290 0 : extentity &e = *ents[i];
291 0 : if(e.flags&EntFlag_NoVis)
292 : {
293 0 : continue;
294 : }
295 0 : e.flags |= EntFlag_Render;
296 0 : ++visible;
297 : }
298 0 : if(!visible)
299 : {
300 0 : continue;
301 : }
302 0 : oe->distance = static_cast<int>(camera1->o.dist_to_bb(oe->o, ivec(oe->o).add(oe->size)));
303 :
304 0 : octaentities **prev = &visiblemms;
305 0 : octaentities *cur = visiblemms;
306 0 : while(cur && cur->distance >= 0 && oe->distance > cur->distance)
307 : {
308 0 : prev = &cur->next;
309 0 : cur = cur->next;
310 : }
311 :
312 0 : if(*prev == nullptr)
313 : {
314 0 : lastvisiblemms = &oe->next;
315 : }
316 0 : oe->next = *prev;
317 0 : *prev = oe;
318 : }
319 : }
320 : }
321 : }
322 0 : }
323 :
324 : VAR(oqmm, 0, 4, 8); //`o`cclusion `q`uery `m`ap `m`odel
325 :
326 0 : void rendermapmodel(const extentity &e)
327 : {
328 0 : int anim = +Anim_Mapmodel | +Anim_Loop, //unary plus to promote to an integer, c++20 deprecates arithmetic conversion on enums (see C++ document P2864R2)
329 0 : basetime = 0;
330 0 : rendermapmodel(e.attr1, anim, e.o, e.attr2, e.attr3, e.attr4, Model_CullVFC | Model_CullDist, basetime, e.attr5 > 0 ? e.attr5/100.0f : 1.0f);
331 0 : }
332 :
333 0 : bool bbinsideva(const ivec &bo, const ivec &br, const vtxarray &va)
334 : {
335 0 : return bo.x >= va.bbmin.x && bo.y >= va.bbmin.y && bo.z >= va.bbmin.z &&
336 0 : br.x <= va.bbmax.x && br.y <= va.bbmax.y && br.z <= va.bbmax.z;
337 : }
338 :
339 0 : bool bboccluded(const ivec &bo, const ivec &br, const std::array<cube, 8> &c, const ivec &o, int size)
340 : {
341 0 : LOOP_OCTA_BOX(o, size, bo, br)
342 : {
343 0 : const ivec co(i, o, size);
344 0 : if(c[i].ext && c[i].ext->va)
345 : {
346 0 : const vtxarray *va = c[i].ext->va;
347 0 : if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
348 : {
349 0 : continue;
350 : }
351 : }
352 0 : if(c[i].children && bboccluded(bo, br, *(c[i].children), co, size>>1))
353 : {
354 0 : continue;
355 : }
356 0 : return false;
357 : }
358 0 : return true;
359 : }
360 :
361 : VAR(dtoutline, 0, 1, 1); //`d`epth `t`est `outline`s
362 :
363 0 : int calcbbsidemask(const ivec &bbmin, const ivec &bbmax, const vec &lightpos, float lightradius, float bias)
364 : {
365 0 : vec pmin = vec(bbmin).sub(lightpos).div(lightradius),
366 0 : pmax = vec(bbmax).sub(lightpos).div(lightradius);
367 0 : int mask = 0x3F;
368 0 : float dp1 = pmax.x + pmax.y,
369 0 : dn1 = pmax.x - pmin.y,
370 0 : ap1 = std::fabs(dp1),
371 0 : an1 = std::fabs(dn1),
372 0 : dp2 = pmin.x + pmin.y,
373 0 : dn2 = pmin.x - pmax.y,
374 0 : ap2 = std::fabs(dp2),
375 0 : an2 = std::fabs(dn2);
376 0 : if(ap1 > bias*an1 && ap2 > bias*an2)
377 : {
378 0 : mask &= (3<<4)
379 0 : | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
380 0 : | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
381 : }
382 0 : if(an1 > bias*ap1 && an2 > bias*ap2)
383 : {
384 0 : mask &= (3<<4)
385 0 : | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
386 0 : | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
387 : }
388 0 : dp1 = pmax.y + pmax.z,
389 0 : dn1 = pmax.y - pmin.z,
390 0 : ap1 = std::fabs(dp1),
391 0 : an1 = std::fabs(dn1),
392 0 : dp2 = pmin.y + pmin.z,
393 0 : dn2 = pmin.y - pmax.z,
394 0 : ap2 = std::fabs(dp2),
395 0 : an2 = std::fabs(dn2);
396 0 : if(ap1 > bias*an1 && ap2 > bias*an2)
397 : {
398 0 : mask &= (3<<0)
399 0 : | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
400 0 : | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
401 : }
402 0 : if(an1 > bias*ap1 && an2 > bias*ap2)
403 : {
404 0 : mask &= (3<<0)
405 0 : | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
406 0 : | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
407 : }
408 0 : dp1 = pmax.z + pmax.x,
409 0 : dn1 = pmax.z - pmin.x,
410 0 : ap1 = std::fabs(dp1),
411 0 : an1 = std::fabs(dn1),
412 0 : dp2 = pmin.z + pmin.x,
413 0 : dn2 = pmin.z - pmax.x,
414 0 : ap2 = std::fabs(dp2),
415 0 : an2 = std::fabs(dn2);
416 0 : if(ap1 > bias*an1 && ap2 > bias*an2)
417 : {
418 0 : mask &= (3<<2)
419 0 : | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
420 0 : | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
421 : }
422 0 : if(an1 > bias*ap1 && an2 > bias*ap2)
423 : {
424 0 : mask &= (3<<2)
425 0 : | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
426 0 : | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
427 : }
428 0 : return mask;
429 : }
430 :
431 : VAR(smbbcull, 0, 1, 1);
432 : VAR(smdistcull, 0, 1, 1);
433 : VAR(smnodraw, 0, 0, 1);
434 :
435 : vtxarray *shadowva = nullptr;
436 :
437 0 : void addshadowva(vtxarray * const va, float dist, std::array<vtxarray *, vasortsize> &vasort)
438 : {
439 0 : va->rdistance = static_cast<int>(dist);
440 :
441 0 : int hash = std::clamp(static_cast<int>(dist*vasortsize/shadowradius), 0, vasortsize-1);
442 0 : vtxarray **prev = &vasort[hash],
443 0 : *cur = vasort[hash];
444 :
445 0 : while(cur && va->rdistance > cur->rdistance)
446 : {
447 0 : prev = &cur->rnext;
448 0 : cur = cur->rnext;
449 : }
450 :
451 0 : va->rnext = cur;
452 0 : *prev = va;
453 0 : }
454 :
455 0 : void sortshadowvas(std::array<vtxarray *, vasortsize> &vasort)
456 : {
457 0 : shadowva = nullptr;
458 0 : vtxarray **last = &shadowva;
459 0 : for(vtxarray *i : vasort)
460 : {
461 0 : if(i)
462 : {
463 0 : vtxarray *va = i;
464 0 : *last = va;
465 0 : while(va->rnext)
466 : {
467 0 : va = va->rnext;
468 : }
469 0 : last = &va->rnext;
470 : }
471 : }
472 0 : }
473 :
474 : octaentities *shadowmms = nullptr;
475 :
476 : struct geombatch final
477 : {
478 : const ElementSet &es;
479 : VSlot &vslot;
480 : int offset;
481 : const vtxarray * const va;
482 : int next, batch;
483 :
484 0 : geombatch(const ElementSet &es, int offset, const vtxarray *va)
485 0 : : es(es), vslot(lookupvslot(es.texture)), offset(offset), va(va),
486 0 : next(-1), batch(-1)
487 0 : {}
488 :
489 : void renderbatch() const;
490 :
491 0 : int compare(const geombatch &b) const
492 : {
493 0 : if(va->vbuf < b.va->vbuf)
494 : {
495 0 : return -1;
496 : }
497 0 : if(va->vbuf > b.va->vbuf)
498 : {
499 0 : return 1;
500 : }
501 0 : if(es.attrs.layer&BlendLayer_Bottom)
502 : {
503 0 : if(!(b.es.attrs.layer&BlendLayer_Bottom))
504 : {
505 0 : return 1;
506 : }
507 0 : int x1 = va->o.x&~0xFFF,
508 0 : x2 = b.va->o.x&~0xFFF;
509 0 : if(x1 < x2)
510 : {
511 0 : return -1;
512 : }
513 0 : if(x1 > x2)
514 : {
515 0 : return 1;
516 : }
517 0 : int y1 = va->o.y&~0xFFF,
518 0 : y2 = b.va->o.y&~0xFFF;
519 0 : if(y1 < y2)
520 : {
521 0 : return -1;
522 : }
523 0 : if(y1 > y2)
524 : {
525 0 : return 1;
526 : }
527 : }
528 0 : else if(b.es.attrs.layer&BlendLayer_Bottom)
529 : {
530 0 : return -1;
531 : }
532 0 : if(vslot.slot->shader < b.vslot.slot->shader)
533 : {
534 0 : return -1;
535 : }
536 0 : if(vslot.slot->shader > b.vslot.slot->shader)
537 : {
538 0 : return 1;
539 : }
540 0 : if(es.texture < b.es.texture)
541 : {
542 0 : return -1;
543 : }
544 0 : if(es.texture > b.es.texture)
545 : {
546 0 : return 1;
547 : }
548 0 : if(vslot.slot->params.size() < b.vslot.slot->params.size())
549 : {
550 0 : return -1;
551 : }
552 0 : if(vslot.slot->params.size() > b.vslot.slot->params.size())
553 : {
554 0 : return 1;
555 : }
556 0 : if(es.attrs.orient < b.es.attrs.orient)
557 : {
558 0 : return -1;
559 : }
560 0 : if(es.attrs.orient > b.es.attrs.orient)
561 : {
562 0 : return 1;
563 : }
564 0 : return 0;
565 : }
566 : };
567 :
568 : class RenderState final
569 : {
570 : public:
571 : bool colormask, depthmask;
572 : int alphaing;
573 : GLuint vbuf;
574 : bool vattribs, vquery;
575 : int globals;
576 :
577 : void disablevquery();
578 : void disablevbuf();
579 : void enablevquery();
580 : void cleanupgeom();
581 : void enablevattribs(bool all = true);
582 : void disablevattribs(bool all = true);
583 : void renderbatches(int pass);
584 : void renderzpass(const vtxarray &va);
585 : void invalidatetexgenorient();
586 : void invalidatealphascale();
587 : void cleartexgenmillis();
588 :
589 0 : RenderState() : colormask(true), depthmask(true), alphaing(0), vbuf(0), vattribs(false),
590 0 : vquery(false), globals(-1), alphascale(0), texgenorient(-1),
591 0 : texgenmillis(lastmillis), tmu(-1), colorscale(1, 1, 1),
592 0 : vslot(nullptr), texgenslot(nullptr), texgenvslot(nullptr),
593 0 : texgenscroll(0, 0), refractscale(0), refractcolor(1, 1, 1)
594 : {
595 0 : for(int k = 0; k < 7; ++k)
596 : {
597 0 : textures[k] = 0;
598 : }
599 0 : }
600 : private:
601 :
602 : float alphascale;
603 : int texgenorient, texgenmillis;
604 : int tmu;
605 : std::array<GLuint, 7> textures;
606 : vec colorscale;
607 : const VSlot *vslot;
608 : const Slot *texgenslot;
609 : const VSlot *texgenvslot;
610 : vec2 texgenscroll;
611 : float refractscale;
612 : vec refractcolor;
613 :
614 : void changetexgen(int orient, Slot &slot, VSlot &vslot);
615 : void changebatchtmus();
616 : void changeslottmus(int pass, Slot &newslot, VSlot &newvslot);
617 : void bindslottex(int type, const Texture *tex, GLenum target = GL_TEXTURE_2D);
618 : void changeshader(int pass, const geombatch &b);
619 :
620 : };
621 :
622 0 : void RenderState::invalidatetexgenorient()
623 : {
624 0 : texgenorient = -1;
625 0 : }
626 :
627 0 : void RenderState::invalidatealphascale()
628 : {
629 0 : alphascale = -1;
630 0 : }
631 :
632 0 : void RenderState::cleartexgenmillis()
633 : {
634 0 : texgenmillis = 0;
635 0 : }
636 :
637 0 : void RenderState::disablevbuf()
638 : {
639 0 : gle::clearvbo();
640 0 : gle::clearebo();
641 0 : vbuf = 0;
642 0 : }
643 :
644 0 : void RenderState::enablevquery()
645 : {
646 0 : if(colormask)
647 : {
648 0 : colormask = false;
649 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
650 : }
651 0 : if(depthmask)
652 : {
653 0 : depthmask = false;
654 0 : glDepthMask(GL_FALSE);
655 : }
656 0 : startbb(false);
657 0 : vquery = true;
658 0 : }
659 :
660 0 : void RenderState::disablevquery()
661 : {
662 0 : endbb(false);
663 0 : vquery = false;
664 0 : }
665 :
666 0 : void renderquery(RenderState &cur, const occludequery &query, const vtxarray &va, bool full = true)
667 : {
668 0 : if(!cur.vquery)
669 : {
670 0 : cur.enablevquery();
671 : }
672 0 : query.startquery();
673 0 : if(full)
674 : {
675 0 : drawbb(ivec(va.bbmin).sub(1), ivec(va.bbmax).sub(va.bbmin).add(2));
676 : }
677 : else
678 : {
679 0 : drawbb(va.geommin, ivec(va.geommax).sub(va.geommin));
680 : }
681 0 : occlusionengine.endquery();
682 0 : }
683 :
684 : enum RenderPass
685 : {
686 : RenderPass_GBuffer = 0,
687 : RenderPass_Z,
688 : RenderPass_Caustics,
689 : RenderPass_GBufferBlend,
690 : RenderPass_ReflectiveShadowMap,
691 : RenderPass_ReflectiveShadowMapBlend
692 : };
693 :
694 : std::vector<geombatch> geombatches;
695 : int firstbatch = -1,
696 : numbatches = 0;
697 :
698 0 : void mergetexs(const RenderState &cur, const vtxarray &va, ElementSet *texs = nullptr, int offset = 0)
699 : {
700 0 : int numtexs = 0;
701 0 : if(!texs)
702 : {
703 0 : texs = va.texelems;
704 0 : numtexs = va.texs;
705 0 : if(cur.alphaing)
706 : {
707 0 : texs += va.texs;
708 0 : offset += 3*(va.tris);
709 0 : numtexs = va.alphaback;
710 0 : if(cur.alphaing > 1)
711 : {
712 0 : numtexs += va.alphafront + va.refract;
713 : }
714 : }
715 : }
716 :
717 0 : if(firstbatch < 0)
718 : {
719 0 : firstbatch = geombatches.size();
720 0 : numbatches = numtexs;
721 0 : for(int i = 0; i < numtexs-1; ++i)
722 : {
723 0 : geombatches.emplace_back(texs[i], offset, &va);
724 0 : geombatches.back().next = i+1;
725 0 : offset += texs[i].length;
726 : }
727 0 : geombatches.emplace_back(texs[numtexs-1], offset, &va);
728 0 : return;
729 : }
730 :
731 0 : int prevbatch = -1,
732 0 : curbatch = firstbatch,
733 0 : curtex = 0;
734 : do
735 : {
736 0 : geombatches.emplace_back(texs[curtex], offset, &va);
737 0 : geombatch &b = geombatches.back();
738 0 : offset += texs[curtex].length;
739 0 : int dir = -1;
740 0 : while(curbatch >= 0)
741 : {
742 0 : dir = b.compare(geombatches[curbatch]);
743 0 : if(dir <= 0)
744 : {
745 0 : break;
746 : }
747 0 : prevbatch = curbatch;
748 0 : curbatch = geombatches[curbatch].next;
749 : }
750 0 : if(!dir)
751 : {
752 0 : int last = curbatch, next;
753 : for(;;)
754 : {
755 0 : next = geombatches[last].batch;
756 0 : if(next < 0)
757 : {
758 0 : break;
759 : }
760 0 : last = next;
761 : }
762 0 : if(last==curbatch)
763 : {
764 0 : b.batch = curbatch;
765 0 : b.next = geombatches[curbatch].next;
766 0 : if(prevbatch < 0)
767 : {
768 0 : firstbatch = geombatches.size()-1;
769 : }
770 : else
771 : {
772 0 : geombatches[prevbatch].next = geombatches.size()-1;
773 : }
774 0 : curbatch = geombatches.size()-1;
775 : }
776 : else
777 : {
778 0 : b.batch = next;
779 0 : geombatches[last].batch = geombatches.size()-1;
780 : }
781 : }
782 : else
783 : {
784 0 : numbatches++;
785 0 : b.next = curbatch;
786 0 : if(prevbatch < 0)
787 : {
788 0 : firstbatch = geombatches.size()-1;
789 : }
790 : else
791 : {
792 0 : geombatches[prevbatch].next = geombatches.size()-1;
793 : }
794 0 : prevbatch = geombatches.size()-1;
795 : }
796 0 : } while(++curtex < numtexs);
797 : }
798 :
799 0 : void RenderState::enablevattribs(bool all)
800 : {
801 0 : gle::enablevertex();
802 0 : if(all)
803 : {
804 0 : gle::enabletexcoord0();
805 0 : gle::enablenormal();
806 0 : gle::enabletangent();
807 : }
808 0 : vattribs = true;
809 0 : }
810 :
811 0 : void RenderState::disablevattribs(bool all)
812 : {
813 0 : gle::disablevertex();
814 0 : if(all)
815 : {
816 0 : gle::disabletexcoord0();
817 0 : gle::disablenormal();
818 0 : gle::disabletangent();
819 : }
820 0 : vattribs = false;
821 0 : }
822 :
823 0 : void changevbuf(RenderState &cur, int pass, const vtxarray &va)
824 : {
825 0 : gle::bindvbo(va.vbuf);
826 0 : gle::bindebo(va.ebuf);
827 0 : cur.vbuf = va.vbuf;
828 :
829 0 : vertex *vdata = nullptr;
830 0 : gle::vertexpointer(sizeof(vertex), vdata->pos.data());
831 0 : gle::vertexpointer(sizeof(vertex), vdata->pos.data());
832 :
833 0 : if(pass==RenderPass_GBuffer || pass==RenderPass_ReflectiveShadowMap)
834 : {
835 0 : gle::normalpointer(sizeof(vertex), vdata->norm.data(), GL_BYTE);
836 0 : gle::texcoord0pointer(sizeof(vertex), vdata->tc.data());
837 0 : gle::tangentpointer(sizeof(vertex), vdata->tangent.data(), GL_BYTE);
838 : }
839 0 : }
840 :
841 0 : void RenderState::changebatchtmus()
842 : {
843 0 : if(tmu != 0)
844 : {
845 0 : tmu = 0;
846 0 : glActiveTexture(GL_TEXTURE0);
847 : }
848 0 : }
849 :
850 0 : void RenderState::bindslottex(int type, const Texture *tex, GLenum target)
851 : {
852 0 : if(textures[type] != tex->id)
853 : {
854 0 : if(tmu != type)
855 : {
856 0 : tmu = type;
857 0 : glActiveTexture(GL_TEXTURE0 + type);
858 : }
859 0 : glBindTexture(target, textures[type] = tex->id);
860 : }
861 0 : }
862 :
863 0 : void RenderState::changeslottmus(int pass, Slot &newslot, VSlot &newvslot)
864 : {
865 0 : Texture *diffuse = newslot.sts.empty() ? notexture : newslot.sts[0].t;
866 0 : if(pass==RenderPass_GBuffer || pass==RenderPass_ReflectiveShadowMap)
867 : {
868 0 : bindslottex(Tex_Diffuse, diffuse);
869 :
870 0 : if(pass == RenderPass_GBuffer)
871 : {
872 0 : if(msaasamples)
873 : {
874 0 : GLOBALPARAMF(hashid, newvslot.index);
875 : }
876 0 : if(newslot.shader->type & Shader_Triplanar)
877 : {
878 0 : float scale = defaulttexscale/newvslot.scale;
879 0 : GLOBALPARAMF(texgenscale, scale/diffuse->xs, scale/diffuse->ys);
880 : }
881 : }
882 : }
883 :
884 0 : if(alphaing)
885 : {
886 0 : float alpha = alphaing > 1 ? newvslot.alphafront : newvslot.alphaback;
887 0 : if(alphascale != alpha)
888 : {
889 0 : alphascale = alpha;
890 0 : refractscale = 0;
891 0 : goto changecolorparams; //also run next if statement
892 : }
893 0 : if(colorscale != newvslot.colorscale)
894 : {
895 0 : changecolorparams:
896 0 : colorscale = newvslot.colorscale;
897 0 : GLOBALPARAMF(colorparams,
898 : alpha*newvslot.colorscale.x,
899 : alpha*newvslot.colorscale.y,
900 : alpha*newvslot.colorscale.z,
901 : alpha);
902 : }
903 0 : if(alphaing > 1 && newvslot.refractscale > 0 &&
904 0 : (refractscale != newvslot.refractscale || refractcolor != newvslot.refractcolor))
905 : {
906 0 : refractscale = newvslot.refractscale;
907 0 : refractcolor = newvslot.refractcolor;
908 0 : float refractscale = 0.5f/ldrscale*(1-alpha);
909 0 : GLOBALPARAMF(refractparams,
910 : newvslot.refractcolor.x*refractscale,
911 : newvslot.refractcolor.y*refractscale,
912 : newvslot.refractcolor.z*refractscale,
913 : newvslot.refractscale*viewh);
914 : }
915 : }
916 0 : else if(colorscale != newvslot.colorscale)
917 : {
918 0 : colorscale = newvslot.colorscale;
919 0 : GLOBALPARAMF(colorparams, newvslot.colorscale.x, newvslot.colorscale.y, newvslot.colorscale.z, 1);
920 : }
921 :
922 0 : for(const Slot::Tex &t : newslot.sts)
923 : {
924 0 : switch(t.type)
925 : {
926 0 : case Tex_Normal:
927 : case Tex_Glow:
928 : {
929 0 : bindslottex(t.type, t.t);
930 0 : break;
931 : }
932 : }
933 : }
934 0 : GLOBALPARAM(rotate, vec(newvslot.angle.y, newvslot.angle.z, diffuse->ratio()));
935 0 : if(tmu != 0)
936 : {
937 0 : tmu = 0;
938 0 : glActiveTexture(GL_TEXTURE0);
939 : }
940 0 : vslot = &newvslot;
941 0 : }
942 :
943 0 : void RenderState::changetexgen(int orient, Slot &slot, VSlot &vslot)
944 : {
945 0 : if(texgenslot != &slot || texgenvslot != &vslot)
946 : {
947 0 : const Texture *curtex = !texgenslot || texgenslot->sts.empty() ? notexture : texgenslot->sts[0].t;
948 0 : const Texture *tex = slot.sts.empty() ? notexture : slot.sts[0].t;
949 0 : if(!texgenvslot || slot.sts.empty() ||
950 0 : (curtex->xs != tex->xs || curtex->ys != tex->ys ||
951 0 : texgenvslot->rotation != vslot.rotation || texgenvslot->scale != vslot.scale ||
952 0 : texgenvslot->offset != vslot.offset || texgenvslot->scroll != vslot.scroll) ||
953 0 : texgenvslot->angle != vslot.angle)
954 : {
955 0 : const TexRotation &r = texrotations[vslot.rotation];
956 0 : float xs = r.flipx ? -tex->xs : tex->xs,
957 0 : ys = r.flipy ? -tex->ys : tex->ys;
958 0 : vec2 scroll(vslot.scroll);
959 0 : if(r.swapxy)
960 : {
961 0 : std::swap(scroll.x, scroll.y);
962 : }
963 0 : scroll.x *= texgenmillis*tex->xs/xs;
964 0 : scroll.y *= texgenmillis*tex->ys/ys;
965 0 : if(texgenscroll != scroll)
966 : {
967 0 : texgenscroll = scroll;
968 0 : texgenorient = -1;
969 : }
970 : }
971 0 : texgenslot = &slot;
972 0 : texgenvslot = &vslot;
973 : }
974 :
975 0 : if(texgenorient == orient)
976 : {
977 0 : return;
978 : }
979 0 : GLOBALPARAM(texgenscroll, texgenscroll);
980 :
981 0 : texgenorient = orient;
982 : }
983 :
984 0 : void RenderState::changeshader(int pass, const geombatch &b)
985 : {
986 0 : VSlot &vslot = b.vslot;
987 0 : Slot &slot = *vslot.slot;
988 0 : if(pass == RenderPass_ReflectiveShadowMap)
989 : {
990 0 : if(b.es.attrs.layer&BlendLayer_Bottom)
991 : {
992 0 : rsmworldshader->setvariant(0, 0, slot, vslot);
993 : }
994 : else
995 : {
996 0 : rsmworldshader->set(slot, vslot);
997 : }
998 : }
999 0 : else if(alphaing)
1000 : {
1001 0 : slot.shader->setvariant(alphaing > 1 && vslot.refractscale > 0 ? 1 : 0, 1, slot, vslot);
1002 : }
1003 0 : else if(b.es.attrs.layer&BlendLayer_Bottom)
1004 : {
1005 0 : slot.shader->setvariant(0, 0, slot, vslot);
1006 : }
1007 : else
1008 : {
1009 0 : slot.shader->set(slot, vslot);
1010 : }
1011 0 : globals = GlobalShaderParamState::nextversion;
1012 0 : }
1013 :
1014 : template<class T>
1015 0 : void updateshader(T &cur)
1016 : {
1017 0 : if(cur.globals != GlobalShaderParamState::nextversion)
1018 : {
1019 0 : if(Shader::lastshader)
1020 : {
1021 0 : Shader::lastshader->flushparams();
1022 : }
1023 0 : cur.globals = GlobalShaderParamState::nextversion;
1024 : }
1025 0 : }
1026 :
1027 0 : void geombatch::renderbatch() const
1028 : {
1029 0 : gbatches++;
1030 0 : for(const geombatch *curbatch = this;; curbatch = &geombatches[curbatch->batch])
1031 : {
1032 0 : ushort len = curbatch->es.length;
1033 0 : if(len)
1034 : {
1035 0 : drawtris(len, static_cast<ushort *>(nullptr) + curbatch->va->eoffset + curbatch->offset, curbatch->es.minvert, curbatch->es.maxvert);
1036 0 : vtris += len/3;
1037 : }
1038 0 : if(curbatch->batch < 0)
1039 : {
1040 0 : break;
1041 : }
1042 0 : }
1043 0 : }
1044 :
1045 0 : void resetbatches()
1046 : {
1047 0 : geombatches.clear();
1048 0 : firstbatch = -1;
1049 0 : numbatches = 0;
1050 0 : }
1051 :
1052 0 : void RenderState::renderbatches(int pass)
1053 : {
1054 0 : vslot = nullptr;
1055 0 : int curbatch = firstbatch;
1056 0 : if(curbatch >= 0)
1057 : {
1058 0 : if(!depthmask)
1059 : {
1060 0 : depthmask = true;
1061 0 : glDepthMask(GL_TRUE);
1062 : }
1063 0 : if(!colormask)
1064 : {
1065 0 : colormask = true;
1066 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1067 : }
1068 0 : if(!vattribs)
1069 : {
1070 0 : if(vquery)
1071 : {
1072 0 : disablevquery();
1073 : }
1074 0 : enablevattribs();
1075 : }
1076 : }
1077 0 : while(curbatch >= 0)
1078 : {
1079 0 : const geombatch &b = geombatches[curbatch];
1080 0 : curbatch = b.next;
1081 :
1082 0 : if(vbuf != b.va->vbuf)
1083 : {
1084 0 : changevbuf(*this, pass, *b.va);
1085 : }
1086 0 : if(pass == RenderPass_GBuffer || pass == RenderPass_ReflectiveShadowMap)
1087 : {
1088 0 : changebatchtmus();
1089 : }
1090 0 : if(vslot != &b.vslot)
1091 : {
1092 0 : changeslottmus(pass, *b.vslot.slot, b.vslot);
1093 0 : if(texgenorient != b.es.attrs.orient || (texgenorient < Orient_Any && texgenvslot != &b.vslot))
1094 : {
1095 0 : changetexgen(b.es.attrs.orient, *b.vslot.slot, b.vslot);
1096 : }
1097 0 : changeshader(pass, b);
1098 : }
1099 : else
1100 : {
1101 0 : if(texgenorient != b.es.attrs.orient)
1102 : {
1103 0 : changetexgen(b.es.attrs.orient, *b.vslot.slot, b.vslot);
1104 : }
1105 0 : updateshader(*this);
1106 : }
1107 :
1108 0 : b.renderbatch();
1109 : }
1110 :
1111 0 : resetbatches();
1112 0 : }
1113 :
1114 0 : void RenderState::renderzpass(const vtxarray &va)
1115 : {
1116 0 : if(!vattribs)
1117 : {
1118 0 : if(vquery)
1119 : {
1120 0 : disablevquery();
1121 : }
1122 0 : enablevattribs(false);
1123 : }
1124 0 : if(vbuf!=va.vbuf)
1125 : {
1126 0 : changevbuf(*this, RenderPass_Z, va);
1127 : }
1128 0 : if(!depthmask)
1129 : {
1130 0 : depthmask = true;
1131 0 : glDepthMask(GL_TRUE);
1132 : }
1133 0 : if(colormask)
1134 : {
1135 0 : colormask = false;
1136 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1137 : }
1138 0 : int firsttex = 0,
1139 0 : numtris = va.tris,
1140 0 : offset = 0;
1141 0 : if(alphaing)
1142 : {
1143 0 : firsttex += va.texs;
1144 0 : offset += 3*(va.tris);
1145 0 : numtris = va.alphabacktris + va.alphafronttris + va.refracttris;
1146 0 : xtravertsva += 3*numtris;
1147 : }
1148 : else
1149 : {
1150 0 : xtravertsva += va.verts;
1151 : }
1152 0 : nocolorshader->set();
1153 0 : drawvatris(va, 3*numtris, offset);
1154 0 : }
1155 :
1156 : VAR(batchgeom, 0, 1, 1);
1157 :
1158 0 : void renderva(RenderState &cur, const vtxarray &va, int pass = RenderPass_GBuffer, bool doquery = false)
1159 : {
1160 0 : switch(pass)
1161 : {
1162 0 : case RenderPass_GBuffer:
1163 0 : if(!cur.alphaing)
1164 : {
1165 0 : vverts += va.verts;
1166 : }
1167 0 : if(doquery && va.query)
1168 : {
1169 0 : if(geombatches.size())
1170 : {
1171 0 : cur.renderbatches(pass);
1172 : }
1173 0 : va.query->startquery();
1174 : }
1175 0 : mergetexs(cur, va);
1176 0 : if(doquery)
1177 : {
1178 0 : if(va.query)
1179 : {
1180 0 : if(geombatches.size())
1181 : {
1182 0 : cur.renderbatches(pass);
1183 : }
1184 0 : occlusionengine.endquery();
1185 : }
1186 : }
1187 0 : else if(!batchgeom && geombatches.size())
1188 : {
1189 0 : cur.renderbatches(pass);
1190 : }
1191 0 : break;
1192 :
1193 0 : case RenderPass_GBufferBlend:
1194 0 : if(doquery && va.query)
1195 : {
1196 0 : if(geombatches.size())
1197 : {
1198 0 : cur.renderbatches(RenderPass_GBuffer);
1199 : }
1200 0 : va.query->startquery();
1201 : }
1202 0 : mergetexs(cur, va, &va.texelems[va.texs], 3*va.tris);
1203 0 : if(doquery)
1204 : {
1205 0 : if(va.query)
1206 : {
1207 0 : if(geombatches.size())
1208 : {
1209 0 : cur.renderbatches(RenderPass_GBuffer);
1210 : }
1211 0 : occlusionengine.endquery();
1212 : }
1213 : }
1214 0 : else if(!batchgeom && geombatches.size())
1215 : {
1216 0 : cur.renderbatches(RenderPass_GBuffer);
1217 : }
1218 0 : break;
1219 :
1220 0 : case RenderPass_Caustics:
1221 0 : if(!cur.vattribs)
1222 : {
1223 0 : cur.enablevattribs(false);
1224 : }
1225 0 : if(cur.vbuf!=va.vbuf)
1226 : {
1227 0 : changevbuf(cur, pass, va);
1228 : }
1229 0 : drawvatris(va, 3*va.tris, 0);
1230 0 : xtravertsva += va.verts;
1231 0 : break;
1232 :
1233 0 : case RenderPass_Z:
1234 0 : if(doquery && va.query)
1235 : {
1236 0 : va.query->startquery();
1237 : }
1238 0 : cur.renderzpass(va);
1239 0 : if(doquery && va.query)
1240 : {
1241 0 : occlusionengine.endquery();
1242 : }
1243 0 : break;
1244 :
1245 0 : case RenderPass_ReflectiveShadowMap:
1246 0 : mergetexs(cur, va);
1247 0 : if(!batchgeom && geombatches.size())
1248 : {
1249 0 : cur.renderbatches(pass);
1250 : }
1251 0 : break;
1252 :
1253 0 : case RenderPass_ReflectiveShadowMapBlend:
1254 0 : mergetexs(cur, va, &va.texelems[va.texs], 3*va.tris);
1255 0 : if(!batchgeom && geombatches.size())
1256 : {
1257 0 : cur.renderbatches(RenderPass_ReflectiveShadowMap);
1258 : }
1259 0 : break;
1260 : }
1261 0 : }
1262 :
1263 0 : void setupgeom()
1264 : {
1265 0 : glActiveTexture(GL_TEXTURE0);
1266 0 : GLOBALPARAMF(colorparams, 1, 1, 1, 1);
1267 0 : }
1268 :
1269 0 : void RenderState::cleanupgeom()
1270 : {
1271 0 : if(vattribs)
1272 : {
1273 0 : disablevattribs();
1274 : }
1275 0 : if(vbuf)
1276 : {
1277 0 : disablevbuf();
1278 : }
1279 0 : }
1280 :
1281 : VAR(oqgeom, 0, 1, 1); //occlusion query geometry
1282 :
1283 : std::vector<const vtxarray *> alphavas;
1284 :
1285 0 : CVARP(explicitskycolor, 0x800080);
1286 :
1287 : struct decalbatch final
1288 : {
1289 : const ElementSet &es;
1290 : DecalSlot &slot;
1291 : int offset;
1292 : const vtxarray &va;
1293 : int next, batch;
1294 :
1295 0 : decalbatch(const ElementSet &es, int offset, const vtxarray &va)
1296 0 : : es(es), slot(lookupdecalslot(es.texture)), offset(offset), va(va),
1297 0 : next(-1), batch(-1)
1298 0 : {}
1299 :
1300 : void renderdecalbatch();
1301 :
1302 0 : int compare(const decalbatch &b) const
1303 : {
1304 0 : if(va.vbuf < b.va.vbuf)
1305 : {
1306 0 : return -1;
1307 : }
1308 0 : if(va.vbuf > b.va.vbuf)
1309 : {
1310 0 : return 1;
1311 : }
1312 0 : if(slot.shader < b.slot.shader)
1313 : {
1314 0 : return -1;
1315 : }
1316 0 : if(slot.shader > b.slot.shader)
1317 : {
1318 0 : return 1;
1319 : }
1320 0 : if(es.texture < b.es.texture)
1321 : {
1322 0 : return -1;
1323 : }
1324 0 : if(es.texture > b.es.texture)
1325 : {
1326 0 : return 1;
1327 : }
1328 0 : if(slot.Slot::params.size() < b.slot.Slot::params.size())
1329 : {
1330 0 : return -1;
1331 : }
1332 0 : if(slot.Slot::params.size() > b.slot.Slot::params.size())
1333 : {
1334 0 : return 1;
1335 : }
1336 0 : if(es.reuse < b.es.reuse)
1337 : {
1338 0 : return -1;
1339 : }
1340 0 : if(es.reuse > b.es.reuse)
1341 : {
1342 0 : return 1;
1343 : }
1344 0 : return 0;
1345 : }
1346 : };
1347 :
1348 : std::vector<decalbatch> decalbatches;
1349 :
1350 : class decalrenderer final
1351 : {
1352 : public:
1353 : GLuint vbuf;
1354 : int globals;
1355 :
1356 : void renderdecalbatches(int pass);
1357 :
1358 0 : decalrenderer() : vbuf(0), globals(-1), colorscale(1, 1, 1), tmu(-1), slot(nullptr)
1359 : {
1360 0 : for(int i = 0; i < 7; ++i)
1361 : {
1362 0 : textures[i] = 0;
1363 : }
1364 0 : }
1365 : private:
1366 : vec colorscale;
1367 : int tmu;
1368 : std::array<GLuint, 7> textures;
1369 : DecalSlot *slot;
1370 :
1371 : void changebatchtmus();
1372 : void bindslottex(int type, const Texture *tex, GLenum target = GL_TEXTURE_2D);
1373 : void changeslottmus(DecalSlot &slot);
1374 : void changeshader(int pass, const decalbatch &b);
1375 : };
1376 :
1377 0 : void mergedecals(const vtxarray &va)
1378 : {
1379 0 : ElementSet *texs = va.decalelems;
1380 0 : int numtexs = va.decaltexs,
1381 0 : offset = 0;
1382 :
1383 0 : if(firstbatch < 0)
1384 : {
1385 0 : firstbatch = decalbatches.size();
1386 0 : numbatches = numtexs;
1387 0 : for(int i = 0; i < numtexs-1; ++i)
1388 : {
1389 0 : decalbatches.emplace_back(decalbatch(texs[i], offset, va));
1390 0 : decalbatches.back().next = i+1;
1391 0 : offset += texs[i].length;
1392 : }
1393 0 : decalbatches.emplace_back(decalbatch(texs[numtexs-1], offset, va));
1394 0 : return;
1395 : }
1396 :
1397 0 : int prevbatch = -1,
1398 0 : curbatch = firstbatch,
1399 0 : curtex = 0;
1400 : do
1401 : {
1402 0 : decalbatch b = decalbatch(texs[curtex], offset, va);
1403 0 : offset += texs[curtex].length;
1404 0 : int dir = -1;
1405 0 : while(curbatch >= 0)
1406 : {
1407 0 : dir = b.compare(decalbatches[curbatch]);
1408 0 : if(dir <= 0)
1409 : {
1410 0 : break;
1411 : }
1412 0 : prevbatch = curbatch;
1413 0 : curbatch = decalbatches[curbatch].next;
1414 : }
1415 0 : if(!dir)
1416 : {
1417 0 : int last = curbatch, next;
1418 : for(;;)
1419 : {
1420 0 : next = decalbatches[last].batch;
1421 0 : if(next < 0)
1422 : {
1423 0 : break;
1424 : }
1425 0 : last = next;
1426 : }
1427 0 : if(last==curbatch)
1428 : {
1429 0 : b.batch = curbatch;
1430 0 : b.next = decalbatches[curbatch].next;
1431 0 : if(prevbatch < 0)
1432 : {
1433 0 : firstbatch = decalbatches.size()-1;
1434 : }
1435 : else
1436 : {
1437 0 : decalbatches[prevbatch].next = decalbatches.size()-1;
1438 : }
1439 0 : curbatch = decalbatches.size()-1;
1440 : }
1441 : else
1442 : {
1443 0 : b.batch = next;
1444 0 : decalbatches[last].batch = decalbatches.size()-1;
1445 : }
1446 : }
1447 : else
1448 : {
1449 0 : numbatches++;
1450 0 : b.next = curbatch;
1451 0 : if(prevbatch < 0)
1452 : {
1453 0 : firstbatch = decalbatches.size()-1;
1454 : }
1455 : else
1456 : {
1457 0 : decalbatches[prevbatch].next = decalbatches.size()-1;
1458 : }
1459 0 : prevbatch = decalbatches.size()-1;
1460 : }
1461 0 : decalbatches.push_back(b);
1462 0 : } while(++curtex < numtexs);
1463 : }
1464 :
1465 0 : void resetdecalbatches()
1466 : {
1467 0 : decalbatches.clear();
1468 0 : firstbatch = -1;
1469 0 : numbatches = 0;
1470 0 : }
1471 :
1472 0 : void changevbuf(decalrenderer &cur, const vtxarray &va)
1473 : {
1474 0 : gle::bindvbo(va.vbuf);
1475 0 : gle::bindebo(va.decalbuf);
1476 0 : cur.vbuf = va.vbuf;
1477 0 : const vertex *vdata = nullptr;
1478 : //note inane bikeshedding: use of offset from dereferenced null ptr (aka 0)
1479 0 : gle::vertexpointer(sizeof(vertex), vdata->pos.data());
1480 0 : gle::normalpointer(sizeof(vertex), vdata->norm.data(), GL_BYTE, 4);
1481 0 : gle::texcoord0pointer(sizeof(vertex), vdata->tc.data(), GL_FLOAT, 3);
1482 0 : gle::tangentpointer(sizeof(vertex), vdata->tangent.data(), GL_BYTE);
1483 0 : }
1484 :
1485 0 : void decalrenderer::changebatchtmus()
1486 : {
1487 0 : if(tmu != 0)
1488 : {
1489 0 : tmu = 0;
1490 0 : glActiveTexture(GL_TEXTURE0);
1491 : }
1492 0 : }
1493 :
1494 0 : void decalrenderer::bindslottex(int type, const Texture *tex, GLenum target)
1495 : {
1496 0 : if(textures[type] != tex->id)
1497 : {
1498 0 : if(tmu != type)
1499 : {
1500 0 : tmu = type;
1501 0 : glActiveTexture(GL_TEXTURE0 + type);
1502 : }
1503 0 : glBindTexture(target, textures[type] = tex->id);
1504 : }
1505 0 : }
1506 :
1507 0 : void decalrenderer::changeslottmus(DecalSlot &dslot)
1508 : {
1509 0 : const Texture *diffuse = dslot.sts.empty() ? notexture : dslot.sts[0].t;
1510 0 : bindslottex(Tex_Diffuse, diffuse);
1511 0 : for(const Slot::Tex &t : dslot.sts)
1512 : {
1513 0 : switch(t.type)
1514 : {
1515 0 : case Tex_Normal:
1516 : case Tex_Glow:
1517 : {
1518 0 : bindslottex(t.type, t.t);
1519 0 : break;
1520 : }
1521 0 : case Tex_Spec:
1522 : {
1523 0 : if(t.combined < 0)
1524 : {
1525 0 : bindslottex(Tex_Glow, t.t);
1526 : }
1527 0 : break;
1528 : }
1529 : }
1530 : }
1531 0 : if(tmu != 0)
1532 : {
1533 0 : tmu = 0;
1534 0 : glActiveTexture(GL_TEXTURE0);
1535 : }
1536 0 : if(colorscale != dslot.colorscale)
1537 : {
1538 0 : colorscale = dslot.colorscale;
1539 0 : GLOBALPARAMF(colorparams, dslot.colorscale.x, dslot.colorscale.y, dslot.colorscale.z, 1);
1540 : }
1541 0 : slot = &dslot;
1542 0 : }
1543 :
1544 0 : void decalrenderer::changeshader(int pass, const decalbatch &b)
1545 : {
1546 0 : DecalSlot &slot = b.slot;
1547 0 : if(b.es.reuse)
1548 : {
1549 0 : VSlot &reuse = lookupvslot(b.es.reuse);
1550 0 : if(pass)
1551 : {
1552 0 : slot.shader->setvariant(0, 0, slot, reuse);
1553 : }
1554 : else
1555 : {
1556 0 : slot.shader->set(slot, reuse);
1557 : }
1558 : }
1559 0 : else if(pass)
1560 : {
1561 0 : slot.shader->setvariantandslot(0, 0);
1562 : }
1563 : else
1564 : {
1565 0 : slot.shader->setslot();
1566 : }
1567 0 : globals = GlobalShaderParamState::nextversion;
1568 0 : }
1569 :
1570 0 : void decalbatch::renderdecalbatch()
1571 : {
1572 0 : gbatches++;
1573 0 : for(decalbatch *curbatch = this;; curbatch = &decalbatches[curbatch->batch])
1574 : {
1575 0 : ushort len = curbatch->es.length;
1576 0 : if(len)
1577 : {
1578 0 : drawtris(len, reinterpret_cast<ushort *>(curbatch->va.decaloffset) + curbatch->offset, curbatch->es.minvert, curbatch->es.maxvert);
1579 0 : vtris += len/3;
1580 : }
1581 0 : if(curbatch->batch < 0)
1582 : {
1583 0 : break;
1584 : }
1585 0 : }
1586 0 : }
1587 :
1588 0 : void decalrenderer::renderdecalbatches(int pass)
1589 : {
1590 0 : slot = nullptr;
1591 0 : int curbatch = firstbatch;
1592 0 : while(curbatch >= 0)
1593 : {
1594 0 : decalbatch &b = decalbatches[curbatch];
1595 0 : curbatch = b.next;
1596 :
1597 0 : if(pass && !b.slot.shader->numvariants(0))
1598 : {
1599 0 : continue;
1600 : }
1601 0 : if(vbuf != b.va.vbuf)
1602 : {
1603 0 : changevbuf(*this, b.va);
1604 : }
1605 0 : changebatchtmus();
1606 0 : if(slot != &b.slot)
1607 : {
1608 0 : changeslottmus(b.slot);
1609 0 : changeshader(pass, b);
1610 : }
1611 : else
1612 : {
1613 0 : updateshader(*this);
1614 : }
1615 :
1616 0 : b.renderdecalbatch();
1617 : }
1618 :
1619 0 : resetdecalbatches();
1620 0 : }
1621 :
1622 0 : void setupdecals()
1623 : {
1624 0 : gle::enablevertex();
1625 0 : gle::enablenormal();
1626 0 : gle::enabletexcoord0();
1627 0 : gle::enabletangent();
1628 :
1629 0 : glDepthMask(GL_FALSE);
1630 0 : glEnable(GL_BLEND);
1631 0 : enablepolygonoffset(GL_POLYGON_OFFSET_FILL);
1632 :
1633 0 : GLOBALPARAMF(colorparams, 1, 1, 1, 1);
1634 0 : }
1635 :
1636 0 : void cleanupdecals()
1637 : {
1638 0 : disablepolygonoffset(GL_POLYGON_OFFSET_FILL);
1639 0 : glDisable(GL_BLEND);
1640 0 : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1641 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1642 0 : glDepthMask(GL_TRUE);
1643 0 : maskgbuffer("cnd");
1644 :
1645 0 : gle::disablevertex();
1646 0 : gle::disablenormal();
1647 0 : gle::disabletexcoord0();
1648 0 : gle::disabletangent();
1649 :
1650 0 : gle::clearvbo();
1651 0 : gle::clearebo();
1652 0 : }
1653 :
1654 : VAR(batchdecals, 0, 1, 1);
1655 :
1656 : struct shadowdraw final
1657 : {
1658 : GLuint ebuf, vbuf;
1659 : int offset, tris, next;
1660 : GLuint minvert, maxvert;
1661 : };
1662 :
1663 : struct shadowverts final
1664 : {
1665 : static constexpr int tablesize = 1<<13;
1666 : std::array<int, tablesize> table;
1667 : std::vector<vec> verts;
1668 : std::vector<int> chain;
1669 :
1670 1 : shadowverts() { clear(); }
1671 :
1672 1 : void clear()
1673 : {
1674 1 : table.fill(-1);
1675 1 : chain.clear();
1676 1 : verts.clear();
1677 1 : }
1678 :
1679 0 : int add(const vec &v)
1680 : {
1681 : auto vechash = std::hash<vec>();
1682 0 : uint h = vechash(v)&(tablesize-1);
1683 0 : for(int i = table[h]; i>=0; i = chain[i])
1684 : {
1685 0 : if(verts[i] == v)
1686 : {
1687 0 : return i;
1688 : }
1689 : }
1690 0 : if(verts.size() >= USHRT_MAX)
1691 : {
1692 0 : return -1;
1693 : }
1694 0 : verts.push_back(v);
1695 0 : chain.emplace_back(table[h]);
1696 0 : return table[h] = verts.size()-1;
1697 : }
1698 : } shadowverts;
1699 : std::array<std::vector<GLuint>, 6> shadowtris;
1700 : std::vector<GLuint> shadowvbos;
1701 : std::unordered_map<int, shadowmesh> shadowmeshes;
1702 : std::vector<shadowdraw> shadowdraws;
1703 :
1704 : struct shadowdrawinfo final
1705 : {
1706 : int last;
1707 : GLuint minvert, maxvert;
1708 :
1709 0 : shadowdrawinfo() : last(-1)
1710 : {
1711 0 : reset();
1712 0 : }
1713 :
1714 0 : void reset()
1715 : {
1716 0 : minvert = USHRT_MAX;
1717 0 : maxvert = 0;
1718 0 : }
1719 : };
1720 :
1721 0 : void flushshadowmeshdraws(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws)
1722 : {
1723 0 : int numindexes = 0;
1724 0 : for(int i = 0; i < sides; ++i)
1725 : {
1726 0 : numindexes += shadowtris[i].size();
1727 : }
1728 0 : if(!numindexes)
1729 : {
1730 0 : return;
1731 : }
1732 :
1733 0 : GLuint ebuf = 0,
1734 0 : vbuf = 0;
1735 0 : glGenBuffers(1, &ebuf);
1736 0 : glGenBuffers(1, &vbuf);
1737 0 : ushort *indexes = new ushort[numindexes];
1738 0 : int offset = 0;
1739 0 : for(int i = 0; i < sides; ++i)
1740 : {
1741 0 : if(shadowtris[i].size())
1742 : {
1743 0 : if(draws[i].last < 0)
1744 : {
1745 0 : m.draws[i] = shadowdraws.size();
1746 : }
1747 : else
1748 : {
1749 0 : shadowdraws[draws[i].last].next = shadowdraws.size();
1750 : }
1751 0 : draws[i].last = shadowdraws.size();
1752 :
1753 : shadowdraw d;
1754 0 : d.ebuf = ebuf;
1755 0 : d.vbuf = vbuf;
1756 0 : d.offset = offset;
1757 0 : d.tris = shadowtris[i].size()/3;
1758 0 : d.minvert = draws[i].minvert;
1759 0 : d.maxvert = draws[i].maxvert;
1760 0 : d.next = -1;
1761 0 : shadowdraws.push_back(d);
1762 :
1763 0 : std::memcpy(indexes + offset, shadowtris[i].data(), shadowtris[i].size()*sizeof(ushort));
1764 0 : offset += shadowtris[i].size();
1765 :
1766 0 : shadowtris[i].clear();
1767 0 : draws[i].reset();
1768 : }
1769 : }
1770 :
1771 0 : gle::bindebo(ebuf);
1772 0 : glBufferData(GL_ELEMENT_ARRAY_BUFFER, numindexes*sizeof(ushort), indexes, GL_STATIC_DRAW);
1773 0 : gle::clearebo();
1774 0 : delete[] indexes;
1775 :
1776 0 : gle::bindvbo(vbuf);
1777 0 : glBufferData(GL_ARRAY_BUFFER, shadowverts.verts.size()*sizeof(vec), shadowverts.verts.data(), GL_STATIC_DRAW);
1778 0 : gle::clearvbo();
1779 0 : shadowverts.clear();
1780 :
1781 0 : shadowvbos.push_back(ebuf);
1782 0 : shadowvbos.push_back(vbuf);
1783 : }
1784 :
1785 0 : int calctrisidemask(const vec &p1, const vec &p2, const vec &p3, float bias)
1786 : {
1787 : // p1, p2, p3 are in the cubemap's local coordinate system
1788 : // bias = border/(size - border)
1789 0 : int mask = 0x3F;
1790 0 : float dp1 = p1.x + p1.y,
1791 0 : dn1 = p1.x - p1.y,
1792 0 : ap1 = std::fabs(dp1),
1793 0 : an1 = std::fabs(dn1),
1794 0 : dp2 = p2.x + p2.y,
1795 0 : dn2 = p2.x - p2.y,
1796 0 : ap2 = std::fabs(dp2),
1797 0 : an2 = std::fabs(dn2),
1798 0 : dp3 = p3.x + p3.y,
1799 0 : dn3 = p3.x - p3.y,
1800 0 : ap3 = std::fabs(dp3),
1801 0 : an3 = std::fabs(dn3);
1802 0 : if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1803 : {
1804 0 : mask &= (3<<4)
1805 0 : | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1806 0 : | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1807 0 : | (dp3 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1808 : }
1809 0 : if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1810 0 : mask &= (3<<4)
1811 0 : | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1812 0 : | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1813 0 : | (dn3 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1814 0 : dp1 = p1.y + p1.z,
1815 0 : dn1 = p1.y - p1.z,
1816 0 : ap1 = std::fabs(dp1),
1817 0 : an1 = std::fabs(dn1),
1818 0 : dp2 = p2.y + p2.z,
1819 0 : dn2 = p2.y - p2.z,
1820 0 : ap2 = std::fabs(dp2),
1821 0 : an2 = std::fabs(dn2),
1822 0 : dp3 = p3.y + p3.z,
1823 0 : dn3 = p3.y - p3.z,
1824 0 : ap3 = std::fabs(dp3),
1825 0 : an3 = std::fabs(dn3);
1826 0 : if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1827 : {
1828 0 : mask &= (3<<0)
1829 0 : | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1830 0 : | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1831 0 : | (dp3 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1832 : }
1833 0 : if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1834 : {
1835 0 : mask &= (3<<0)
1836 0 : | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1837 0 : | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1838 0 : | (dn3 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1839 : }
1840 0 : dp1 = p1.z + p1.x,
1841 0 : dn1 = p1.z - p1.x,
1842 0 : ap1 = std::fabs(dp1),
1843 0 : an1 = std::fabs(dn1),
1844 0 : dp2 = p2.z + p2.x,
1845 0 : dn2 = p2.z - p2.x,
1846 0 : ap2 = std::fabs(dp2),
1847 0 : an2 = std::fabs(dn2),
1848 0 : dp3 = p3.z + p3.x,
1849 0 : dn3 = p3.z - p3.x,
1850 0 : ap3 = std::fabs(dp3),
1851 0 : an3 = std::fabs(dn3);
1852 0 : if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1853 : {
1854 0 : mask &= (3<<2)
1855 0 : | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1856 0 : | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1857 0 : | (dp3 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1858 : }
1859 0 : if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1860 : {
1861 0 : mask &= (3<<2)
1862 0 : | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1863 0 : | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1864 0 : | (dn3 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1865 : }
1866 0 : return mask;
1867 : }
1868 :
1869 0 : void addshadowmeshtri(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws, const vec &v0, const vec &v1, const vec &v2)
1870 : {
1871 0 : vec l0 = vec(v0).sub(shadoworigin);
1872 0 : float side = l0.scalartriple(vec(v1).sub(v0), vec(v2).sub(v0));
1873 0 : if(smcullside ? side > 0 : side < 0)
1874 : {
1875 0 : return;
1876 : }
1877 0 : vec l1 = vec(v1).sub(shadoworigin),
1878 0 : l2 = vec(v2).sub(shadoworigin);
1879 0 : if(l0.squaredlen() > shadowradius*shadowradius && l1.squaredlen() > shadowradius*shadowradius && l2.squaredlen() > shadowradius*shadowradius)
1880 : {
1881 0 : return;
1882 : }
1883 0 : int sidemask = 0;
1884 0 : switch(m.type)
1885 : {
1886 0 : case ShadowMap_Spot:
1887 : {
1888 0 : sidemask = bbinsidespot(shadoworigin, shadowdir, shadowspot, ivec(vec(v0).min(v1).min(v2)), ivec(vec(v0).max(v1).max(v2).add(1))) ? 1 : 0;
1889 0 : break;
1890 : }
1891 0 : case ShadowMap_CubeMap:
1892 : {
1893 0 : sidemask = calctrisidemask(l0.div(shadowradius), l1.div(shadowradius), l2.div(shadowradius), shadowbias);
1894 0 : break;
1895 : }
1896 : }
1897 0 : if(!sidemask)
1898 : {
1899 0 : return;
1900 : }
1901 0 : if(shadowverts.verts.size() + 3 >= USHRT_MAX)
1902 : {
1903 0 : flushshadowmeshdraws(m, sides, draws);
1904 : }
1905 0 : int i0 = shadowverts.add(v0),
1906 0 : i1 = shadowverts.add(v1),
1907 0 : i2 = shadowverts.add(v2);
1908 0 : GLuint minvert = std::min(i0, std::min(i1, i2)),
1909 0 : maxvert = std::max(i0, std::max(i1, i2));
1910 0 : for(int k = 0; k < sides; ++k)
1911 : {
1912 0 : if(sidemask&(1<<k))
1913 : {
1914 0 : shadowdrawinfo &d = draws[k];
1915 0 : d.minvert = std::min(d.minvert, minvert);
1916 0 : d.maxvert = std::max(d.maxvert, maxvert);
1917 0 : shadowtris[k].push_back(i0);
1918 0 : shadowtris[k].push_back(i1);
1919 0 : shadowtris[k].push_back(i2);
1920 : }
1921 : }
1922 : }
1923 :
1924 0 : void genshadowmeshtris(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws, ushort *edata, int numtris, vertex *vdata)
1925 : {
1926 0 : for(int j = 0; j < 3*numtris; j += 3)
1927 : {
1928 0 : addshadowmeshtri(m, sides, draws, vdata[edata[j]].pos, vdata[edata[j+1]].pos, vdata[edata[j+2]].pos);
1929 : }
1930 0 : }
1931 :
1932 0 : void genshadowmeshmapmodels(shadowmesh &m, int sides, std::array<shadowdrawinfo, 6> &draws)
1933 : {
1934 0 : const std::vector<extentity *> &ents = entities::getents();
1935 0 : for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
1936 : {
1937 0 : for(size_t k = 0; k < oe->mapmodels.size(); k++)
1938 : {
1939 0 : extentity &e = *ents[oe->mapmodels[k]];
1940 0 : if(e.flags&(EntFlag_NoVis|EntFlag_NoShadow))
1941 : {
1942 0 : continue;
1943 : }
1944 0 : e.flags |= EntFlag_Render;
1945 : }
1946 : }
1947 0 : std::vector<triangle> tris;
1948 0 : for(octaentities *oe = shadowmms; oe; oe = oe->rnext)
1949 : {
1950 0 : for(const int &j : oe->mapmodels)
1951 : {
1952 0 : extentity &e = *ents[j];
1953 0 : if(!(e.flags&EntFlag_Render))
1954 : {
1955 0 : continue;
1956 : }
1957 0 : e.flags &= ~EntFlag_Render;
1958 0 : model *mm = loadmapmodel(e.attr1);
1959 0 : if(!mm || !mm->shadow || mm->animated() || (mm->alphashadow && mm->alphatested()))
1960 : {
1961 0 : continue;
1962 : }
1963 0 : matrix4x3 orient;
1964 0 : orient.identity();
1965 0 : if(e.attr2)
1966 : {
1967 0 : orient.rotate_around_z(sincosmod360(e.attr2));
1968 : }
1969 0 : if(e.attr3)
1970 : {
1971 0 : orient.rotate_around_x(sincosmod360(e.attr3));
1972 : }
1973 0 : if(e.attr4)
1974 : {
1975 0 : orient.rotate_around_y(sincosmod360(-e.attr4));
1976 : }
1977 0 : if(e.attr5 > 0)
1978 : {
1979 0 : orient.scale(e.attr5/100.0f);
1980 : }
1981 0 : orient.settranslation(e.o);
1982 0 : tris.clear();
1983 0 : mm->genshadowmesh(tris, orient);
1984 :
1985 0 : for(size_t i = 0; i < tris.size(); i++)
1986 : {
1987 0 : const triangle &t = tris[i];
1988 0 : addshadowmeshtri(m, sides, draws, t.a, t.b, t.c);
1989 : }
1990 :
1991 0 : e.flags |= EntFlag_ShadowMesh;
1992 : }
1993 : }
1994 0 : }
1995 :
1996 0 : void genshadowmesh(int idx, const extentity &e)
1997 : {
1998 0 : shadowmesh m;
1999 0 : m.type = calcshadowinfo(e, m.origin, m.radius, m.spotloc, m.spotangle, shadowbias);
2000 0 : if(!m.type)
2001 : {
2002 0 : return;
2003 : }
2004 0 : m.draws.fill(-1);
2005 :
2006 0 : shadowmapping = m.type;
2007 0 : shadoworigin = m.origin;
2008 0 : shadowradius = m.radius;
2009 0 : shadowdir = m.type == ShadowMap_Spot ? vec(m.spotloc).sub(m.origin).normalize() : vec(0, 0, 0);
2010 0 : shadowspot = m.spotangle;
2011 :
2012 0 : findshadowvas();
2013 0 : findshadowmms();
2014 :
2015 0 : int sides = m.type == ShadowMap_Spot ? 1 : 6;
2016 0 : std::array<shadowdrawinfo, 6> draws;
2017 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
2018 : {
2019 0 : if(va->shadowmask)
2020 : {
2021 0 : if(va->tris)
2022 : {
2023 0 : genshadowmeshtris(m, sides, draws, va->edata + va->eoffset, va->tris, va->vdata);
2024 : }
2025 0 : if(skyshadow && va->sky)
2026 : {
2027 0 : genshadowmeshtris(m, sides, draws, va->skydata + va->skyoffset, va->sky/3, va->vdata);
2028 : }
2029 : }
2030 : }
2031 0 : if(shadowmms)
2032 : {
2033 0 : genshadowmeshmapmodels(m, sides, draws);
2034 : }
2035 0 : flushshadowmeshdraws(m, sides, draws);
2036 :
2037 0 : shadowmeshes[idx] = m;
2038 :
2039 0 : shadowmapping = 0;
2040 : }
2041 :
2042 0 : VARF(smmesh, 0, 1, 1,
2043 : {
2044 : if(!smmesh)
2045 : {
2046 : clearshadowmeshes();
2047 : }
2048 : });
2049 : }
2050 :
2051 : /* externally relevant functionality */
2052 : ///////////////////////////////////////
2053 :
2054 : // vfc - view frustum culling
2055 :
2056 0 : int vfc::isfoggedcube(const ivec &o, int size) const
2057 : {
2058 0 : for(int i = 0; i < 4; ++i)
2059 : {
2060 0 : if(o.dist(vfcP[i]) < -vfcDfar[i]*size)
2061 : {
2062 0 : return true;
2063 : }
2064 : }
2065 0 : float dist = o.dist(vfcP[4]);
2066 0 : return dist < -vfcDfar[4]*size || dist > vfcDfog - vfcDnear[4]*size;
2067 : }
2068 :
2069 0 : int vfc::isvisiblecube(const ivec &o, int size) const
2070 : {
2071 0 : int v = ViewFrustumCull_FullyVisible;
2072 : float dist;
2073 :
2074 0 : for(int i = 0; i < 5; ++i)
2075 : {
2076 0 : dist = o.dist(vfcP[i]);
2077 0 : if(dist < -vfcDfar[i]*size)
2078 : {
2079 0 : return ViewFrustumCull_NotVisible;
2080 : }
2081 0 : if(dist < -vfcDnear[i]*size)
2082 : {
2083 0 : v = ViewFrustumCull_PartlyVisible;
2084 : }
2085 : }
2086 :
2087 0 : dist -= vfcDfog;
2088 0 : if(dist > -vfcDnear[4]*size)
2089 : {
2090 0 : return ViewFrustumCull_Fogged;
2091 : }
2092 0 : if(dist > -vfcDfar[4]*size)
2093 : {
2094 0 : v = ViewFrustumCull_PartlyVisible;
2095 : }
2096 :
2097 0 : return v;
2098 : }
2099 :
2100 0 : void vfc::calcvfcD()
2101 : {
2102 0 : for(int i = 0; i < 5; ++i)
2103 : {
2104 0 : const plane &p = vfcP[i];
2105 0 : vfcDnear[i] = vfcDfar[i] = 0;
2106 0 : for(int k = 0; k < 3; ++k)
2107 : {
2108 0 : if(p[k] > 0)
2109 : {
2110 0 : vfcDfar[i] += p[k];
2111 : }
2112 : else
2113 : {
2114 0 : vfcDnear[i] += p[k];
2115 : }
2116 : }
2117 : }
2118 0 : }
2119 :
2120 0 : void vfc::visiblecubes(bool cull)
2121 : {
2122 0 : if(cull)
2123 : {
2124 0 : setvfcP();
2125 0 : findvisiblevas();
2126 : }
2127 : else
2128 : {
2129 0 : for(int i = 0; i < 5; ++i)
2130 : {
2131 0 : vfcP[i].x = vfcP[i].y = vfcP[i].z = vfcP[i].offset = 0;
2132 : };
2133 0 : vfcDfog = farplane;
2134 0 : vfcDnear.fill(0);
2135 0 : vfcDfar.fill(0);
2136 0 : visibleva = nullptr;
2137 0 : for(size_t i = 0; i < valist.size(); i++)
2138 : {
2139 0 : vtxarray *va = valist[i];
2140 0 : va->distance = 0;
2141 0 : va->curvfc = ViewFrustumCull_FullyVisible;
2142 0 : va->occluded = !va->texs ? Occlude_Geom : Occlude_Nothing;
2143 0 : va->query = nullptr;
2144 0 : va->next = visibleva;
2145 0 : visibleva = va;
2146 : }
2147 : }
2148 0 : }
2149 :
2150 0 : bool vfc::isfoggedsphere(float rad, const vec &cv) const
2151 : {
2152 0 : for(int i = 0; i < 4; ++i)
2153 : {
2154 0 : if(vfcP[i].dist(cv) < -rad)
2155 : {
2156 0 : return true;
2157 : }
2158 : }
2159 0 : float dist = vfcP[4].dist(cv);
2160 0 : return dist < -rad || dist > vfcDfog + rad; // true if abs(dist) is large
2161 : }
2162 :
2163 0 : int vfc::isvisiblesphere(float rad, const vec &cv) const
2164 : {
2165 0 : int v = ViewFrustumCull_FullyVisible;
2166 : float dist;
2167 :
2168 0 : for(int i = 0; i < 5; ++i)
2169 : {
2170 0 : dist = vfcP[i].dist(cv);
2171 0 : if(dist < -rad)
2172 : {
2173 0 : return ViewFrustumCull_NotVisible;
2174 : }
2175 0 : if(dist < rad)
2176 : {
2177 0 : v = ViewFrustumCull_PartlyVisible;
2178 : }
2179 : }
2180 :
2181 0 : dist -= vfcDfog;
2182 0 : if(dist > rad)
2183 : {
2184 0 : return ViewFrustumCull_Fogged; //ViewFrustumCull_NotVisible; // culling when fog is closer than size of world results in HOM
2185 : }
2186 0 : if(dist > -rad)
2187 : {
2188 0 : v = ViewFrustumCull_PartlyVisible;
2189 : }
2190 0 : return v;
2191 : }
2192 :
2193 0 : int vfc::isvisiblebb(const ivec &bo, const ivec &br) const
2194 : {
2195 0 : int v = ViewFrustumCull_FullyVisible;
2196 : float dnear, dfar;
2197 :
2198 0 : for(int i = 0; i < 5; ++i)
2199 : {
2200 0 : const plane &p = vfcP[i];
2201 0 : dnear = dfar = bo.dist(p);
2202 0 : if(p.x > 0)
2203 : {
2204 0 : dfar += p.x*br.x;
2205 : }
2206 : else
2207 : {
2208 0 : dnear += p.x*br.x;
2209 : }
2210 0 : if(p.y > 0)
2211 : {
2212 0 : dfar += p.y*br.y;
2213 : }
2214 : else
2215 : {
2216 0 : dnear += p.y*br.y;
2217 : }
2218 0 : if(p.z > 0)
2219 : {
2220 0 : dfar += p.z*br.z;
2221 : }
2222 : else
2223 : {
2224 0 : dnear += p.z*br.z;
2225 : }
2226 0 : if(dfar < 0)
2227 : {
2228 0 : return ViewFrustumCull_NotVisible;
2229 : }
2230 0 : if(dnear < 0)
2231 : {
2232 0 : v = ViewFrustumCull_PartlyVisible;
2233 : }
2234 : }
2235 :
2236 0 : if(dnear > vfcDfog)
2237 : {
2238 0 : return ViewFrustumCull_Fogged;
2239 : }
2240 0 : if(dfar > vfcDfog)
2241 : {
2242 0 : v = ViewFrustumCull_PartlyVisible;
2243 : }
2244 0 : return v;
2245 : }
2246 :
2247 0 : bool cubeworld::bboccluded(const ivec &bo, const ivec &br) const
2248 : {
2249 0 : int diff = (bo.x^br.x) | (bo.y^br.y) | (bo.z^br.z);
2250 0 : if(diff&~((1<<worldscale)-1))
2251 : {
2252 0 : return false;
2253 : }
2254 0 : int scale = worldscale-1;
2255 0 : if(diff&(1<<scale))
2256 : {
2257 0 : return ::bboccluded(bo, br, *worldroot, ivec(0, 0, 0), 1<<scale);
2258 : }
2259 0 : const cube *c = &(*worldroot)[OCTA_STEP(bo.x, bo.y, bo.z, scale)];
2260 0 : if(c->ext && c->ext->va)
2261 : {
2262 0 : const vtxarray *va = c->ext->va;
2263 0 : if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
2264 : {
2265 0 : return true;
2266 : }
2267 : }
2268 0 : scale--;
2269 0 : while(c->children && !(diff&(1<<scale)))
2270 : {
2271 0 : c = &(*c->children)[OCTA_STEP(bo.x, bo.y, bo.z, scale)];
2272 0 : if(c->ext && c->ext->va)
2273 : {
2274 0 : const vtxarray *va = c->ext->va;
2275 0 : if(va->curvfc >= ViewFrustumCull_Fogged || (va->occluded >= Occlude_BB && bbinsideva(bo, br, *va)))
2276 : {
2277 0 : return true;
2278 : }
2279 : }
2280 0 : scale--;
2281 : }
2282 0 : if(c->children)
2283 : {
2284 0 : return ::bboccluded(bo, br, *(c->children), ivec(bo).mask(~((2<<scale)-1)), 1<<scale);
2285 : }
2286 0 : return false;
2287 : }
2288 :
2289 0 : void startbb(bool mask)
2290 : {
2291 0 : setupbb();
2292 0 : gle::bindvbo(bbvbo);
2293 0 : gle::bindebo(bbebo);
2294 0 : gle::vertexpointer(sizeof(vec), nullptr);
2295 0 : gle::enablevertex();
2296 0 : SETSHADER(bbquery);
2297 0 : if(mask)
2298 : {
2299 0 : glDepthMask(GL_FALSE);
2300 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2301 : }
2302 0 : }
2303 :
2304 0 : void endbb(bool mask)
2305 : {
2306 0 : gle::disablevertex();
2307 0 : gle::clearvbo();
2308 0 : gle::clearebo();
2309 0 : if(mask)
2310 : {
2311 0 : glDepthMask(GL_TRUE);
2312 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2313 : }
2314 0 : }
2315 :
2316 0 : void drawbb(const ivec &bo, const ivec &br)
2317 : {
2318 0 : LOCALPARAMF(bborigin, bo.x, bo.y, bo.z);
2319 0 : LOCALPARAMF(bbsize, br.x, br.y, br.z);
2320 0 : glDrawRangeElements(GL_TRIANGLES, 0, 8-1, 3*2*6, GL_UNSIGNED_SHORT, nullptr);
2321 0 : xtraverts += 8;
2322 0 : }
2323 :
2324 0 : void vfc::setvfcP(const vec &bbmin, const vec &bbmax)
2325 : {
2326 0 : vec4<float> px = camprojmatrix.rowx(),
2327 0 : py = camprojmatrix.rowy(),
2328 0 : pz = camprojmatrix.rowz(),
2329 0 : pw = camprojmatrix.roww();
2330 0 : vfcP[0] = plane(vec4<float>(pw).mul(-bbmin.x).add(px)).normalize(); // left plane
2331 0 : vfcP[1] = plane(vec4<float>(pw).mul(bbmax.x).sub(px)).normalize(); // right plane
2332 0 : vfcP[2] = plane(vec4<float>(pw).mul(-bbmin.y).add(py)).normalize(); // bottom plane
2333 0 : vfcP[3] = plane(vec4<float>(pw).mul(bbmax.y).sub(py)).normalize(); // top plane
2334 0 : vfcP[4] = plane(vec4<float>(pw).add(pz)).normalize(); // near/far planes
2335 :
2336 0 : vfcDfog = std::min(calcfogcull(), static_cast<float>(farplane));
2337 0 : view.calcvfcD();
2338 0 : }
2339 :
2340 : //oq
2341 :
2342 0 : void Occluder::clearqueries()
2343 : {
2344 0 : for(QueryFrame &i : queryframes)
2345 : {
2346 0 : i.cleanup();
2347 : }
2348 0 : }
2349 :
2350 0 : void Occluder::flipqueries()
2351 : {
2352 0 : flipquery = (flipquery + 1) % maxqueryframes;
2353 0 : queryframes[flipquery].flip();
2354 0 : }
2355 :
2356 0 : void Occluder::endquery() const
2357 : {
2358 0 : glEndQuery(querytarget());
2359 0 : }
2360 :
2361 0 : bool Occluder::checkquery(occludequery *query, bool nowait)
2362 : {
2363 0 : if(query->fragments < 0)
2364 : {
2365 0 : if(nowait || !oqwait)
2366 : {
2367 : GLint avail;
2368 0 : glGetQueryObjectiv(query->id, GL_QUERY_RESULT_AVAILABLE, &avail);
2369 0 : if(!avail)
2370 : {
2371 0 : return false;
2372 : }
2373 : }
2374 :
2375 : GLuint fragments;
2376 0 : glGetQueryObjectuiv(query->id, GL_QUERY_RESULT, &fragments);
2377 0 : query->fragments = querytarget() == GL_SAMPLES_PASSED || !fragments ? static_cast<int>(fragments) : oqfrags;
2378 : }
2379 0 : return query->fragments < oqfrags;
2380 : }
2381 :
2382 0 : void Occluder::resetqueries()
2383 : {
2384 0 : for(QueryFrame &i : queryframes)
2385 : {
2386 0 : i.reset();
2387 : }
2388 0 : }
2389 :
2390 1 : int Occluder::getnumqueries() const
2391 : {
2392 1 : return queryframes[flipquery].cur;
2393 : }
2394 :
2395 0 : void Occluder::QueryFrame::flip()
2396 : {
2397 0 : for(int i = 0; i < cur; ++i)
2398 : {
2399 0 : queries[i].owner = nullptr;
2400 : }
2401 0 : for(; defer > 0 && max < maxquery; defer--)
2402 : {
2403 0 : queries[max].owner = nullptr;
2404 0 : queries[max].fragments = -1;
2405 0 : glGenQueries(1, &queries[max++].id);
2406 : }
2407 0 : cur = defer = 0;
2408 0 : }
2409 :
2410 0 : occludequery *Occluder::QueryFrame::newquery(const void *owner)
2411 : {
2412 0 : if(cur >= max)
2413 : {
2414 0 : if(max >= maxquery)
2415 : {
2416 0 : return nullptr;
2417 : }
2418 0 : if(deferquery)
2419 : {
2420 0 : if(max + defer < maxquery)
2421 : {
2422 0 : defer++;
2423 : }
2424 0 : return nullptr;
2425 : }
2426 0 : glGenQueries(1, &queries[max++].id);
2427 : }
2428 0 : occludequery *query = &queries[cur++];
2429 0 : query->owner = owner;
2430 0 : query->fragments = -1;
2431 0 : return query;
2432 : }
2433 :
2434 0 : void Occluder::QueryFrame::reset()
2435 : {
2436 0 : for(int i = 0; i < max; ++i)
2437 : {
2438 0 : queries[i].owner = nullptr;
2439 : }
2440 0 : }
2441 :
2442 0 : void Occluder::QueryFrame::cleanup()
2443 : {
2444 0 : for(int i = 0; i < max; ++i)
2445 : {
2446 0 : glDeleteQueries(1, &queries[i].id);
2447 0 : queries[i].owner = nullptr;
2448 : }
2449 0 : cur = max = defer = 0;
2450 0 : }
2451 :
2452 0 : void occludequery::startquery() const
2453 : {
2454 0 : glBeginQuery(querytarget(), this->id);
2455 0 : }
2456 :
2457 0 : void rendermapmodels()
2458 : {
2459 : static int skipoq = 0;
2460 0 : bool doquery = !drawtex && oqfrags && oqmm;
2461 0 : const std::vector<extentity *> &ents = entities::getents();
2462 0 : findvisiblemms(ents, doquery);
2463 :
2464 0 : for(octaentities *oe = visiblemms; oe; oe = oe->next)
2465 : {
2466 0 : if(oe->distance>=0)
2467 : {
2468 0 : bool rendered = false;
2469 0 : for(size_t i = 0; i < oe->mapmodels.size(); i++)
2470 : {
2471 0 : extentity &e = *ents[oe->mapmodels[i]];
2472 0 : if(!(e.flags&EntFlag_Render))
2473 : {
2474 0 : continue;
2475 : }
2476 0 : if(!rendered)
2477 : {
2478 0 : rendered = true;
2479 0 : oe->query = doquery && oe->distance>0 && !(++skipoq%oqmm) ? occlusionengine.newquery(oe) : nullptr;
2480 0 : if(oe->query)
2481 : {
2482 0 : occlusionengine.setupmodelquery(oe->query);
2483 : }
2484 : }
2485 0 : rendermapmodel(e);
2486 0 : e.flags &= ~EntFlag_Render;
2487 : }
2488 0 : if(rendered && oe->query)
2489 : {
2490 0 : occlusionengine.endmodelquery();
2491 : }
2492 : }
2493 : }
2494 0 : batching::rendermapmodelbatches();
2495 0 : batching::clearbatchedmapmodels();
2496 :
2497 0 : bool queried = false;
2498 0 : for(octaentities *oe = visiblemms; oe; oe = oe->next)
2499 : {
2500 0 : if(oe->distance<0)
2501 : {
2502 0 : oe->query = doquery && !camera1->o.insidebb(oe->bbmin, oe->bbmax, 1) ? occlusionengine.newquery(oe) : nullptr;
2503 0 : if(!oe->query)
2504 : {
2505 0 : continue;
2506 : }
2507 0 : if(!queried)
2508 : {
2509 0 : startbb();
2510 0 : queried = true;
2511 : }
2512 0 : oe->query->startquery();
2513 0 : drawbb(oe->bbmin, ivec(oe->bbmax).sub(oe->bbmin));
2514 0 : occlusionengine.endquery();
2515 : }
2516 : }
2517 0 : if(queried)
2518 : {
2519 0 : endbb();
2520 : }
2521 0 : }
2522 :
2523 0 : void renderoutline()
2524 : {
2525 0 : ldrnotextureshader->set();
2526 :
2527 0 : gle::enablevertex();
2528 :
2529 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
2530 0 : gle::color(outlinecolor);
2531 :
2532 0 : enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
2533 :
2534 0 : if(!dtoutline)
2535 : {
2536 0 : glDisable(GL_DEPTH_TEST);
2537 : }
2538 0 : const vtxarray *prev = nullptr;
2539 0 : for(const vtxarray *va = visibleva; va; va = va->next)
2540 : {
2541 0 : if(va->occluded < Occlude_BB)
2542 : {
2543 0 : if((!va->texs || va->occluded >= Occlude_Geom) && !va->alphaback && !va->alphafront && !va->refracttris)
2544 : {
2545 0 : continue;
2546 : }
2547 0 : if(!prev || va->vbuf != prev->vbuf)
2548 : {
2549 0 : gle::bindvbo(va->vbuf);
2550 0 : gle::bindebo(va->ebuf);
2551 0 : const vertex *ptr = nullptr;
2552 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note: intentional dereference of null pointer
2553 : }
2554 0 : if(va->texs && va->occluded < Occlude_Geom)
2555 : {
2556 0 : drawvatris(*va, 3*va->tris, 0);
2557 0 : xtravertsva += va->verts;
2558 : }
2559 0 : if(va->alphaback || va->alphafront || va->refract)
2560 : {
2561 0 : drawvatris(*va, 3*(va->alphabacktris + va->alphafronttris + va->refracttris), 3*(va->tris));
2562 0 : xtravertsva += 3*(va->alphabacktris + va->alphafronttris + va->refracttris);
2563 : }
2564 0 : prev = va;
2565 : }
2566 : }
2567 0 : if(!dtoutline)
2568 : {
2569 0 : glEnable(GL_DEPTH_TEST);
2570 : }
2571 0 : disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
2572 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
2573 0 : gle::clearvbo();
2574 0 : gle::clearebo();
2575 0 : gle::disablevertex();
2576 0 : }
2577 :
2578 0 : bool renderexplicitsky(bool outline)
2579 : {
2580 0 : const vtxarray *prev = nullptr;
2581 0 : for(const vtxarray *va = visibleva; va; va = va->next)
2582 : {
2583 0 : if(va->sky && va->occluded < Occlude_BB &&
2584 0 : ((va->skymax.x >= 0 && view.isvisiblebb(va->skymin, ivec(va->skymax).sub(va->skymin)) != ViewFrustumCull_NotVisible) ||
2585 0 : !insideworld(camera1->o)))
2586 : {
2587 0 : if(!prev || va->vbuf != prev->vbuf)
2588 : {
2589 0 : if(!prev)
2590 : {
2591 0 : gle::enablevertex();
2592 0 : if(outline)
2593 : {
2594 0 : ldrnotextureshader->set();
2595 0 : gle::color(explicitskycolor);
2596 0 : glDepthMask(GL_FALSE);
2597 0 : enablepolygonoffset(GL_POLYGON_OFFSET_LINE);
2598 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
2599 : }
2600 0 : else if(editmode)
2601 : {
2602 0 : maskgbuffer("d");
2603 0 : SETSHADER(depth);
2604 : }
2605 : else
2606 : {
2607 0 : nocolorshader->set();
2608 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2609 : }
2610 : }
2611 0 : gle::bindvbo(va->vbuf);
2612 0 : gle::bindebo(va->skybuf);
2613 0 : const vertex *ptr = nullptr;
2614 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data());
2615 : }
2616 0 : drawvaskytris(*va);
2617 0 : xtraverts += va->sky;
2618 0 : prev = va;
2619 : }
2620 : }
2621 0 : if(!prev)
2622 : {
2623 0 : return false;
2624 : }
2625 0 : if(outline)
2626 : {
2627 0 : glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
2628 0 : disablepolygonoffset(GL_POLYGON_OFFSET_LINE);
2629 0 : glDepthMask(GL_TRUE);
2630 : }
2631 0 : else if(editmode)
2632 : {
2633 0 : maskgbuffer("cnd");
2634 : }
2635 : else
2636 : {
2637 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2638 : }
2639 0 : gle::disablevertex();
2640 0 : gle::clearvbo();
2641 0 : gle::clearebo();
2642 0 : return true;
2643 : }
2644 :
2645 0 : void cubeworld::cleanupva()
2646 : {
2647 0 : clearvas(*worldroot);
2648 0 : occlusionengine.clearqueries();
2649 0 : cleanupbb();
2650 0 : cleanupgrass();
2651 0 : }
2652 :
2653 0 : GBuffer::AlphaInfo GBuffer::findalphavas()
2654 : {
2655 0 : alphavas.clear();
2656 : AlphaInfo a;
2657 0 : a.alphafrontsx1 = a.alphafrontsy1 = a.alphabacksx1 = a.alphabacksy1 = a.alpharefractsx1 = a.alpharefractsy1 = 1;
2658 0 : a.alphafrontsx2 = a.alphafrontsy2 = a.alphabacksx2 = a.alphabacksy2 = a.alpharefractsx2 = a.alpharefractsy2 = -1;
2659 0 : int alphabackvas = 0,
2660 0 : alpharefractvas = 0;
2661 0 : alphatiles.fill(0);
2662 0 : for(const vtxarray *va = visibleva; va; va = va->next)
2663 : {
2664 0 : if(va->alphabacktris || va->alphafronttris || va->refracttris)
2665 : {
2666 0 : if(va->occluded >= Occlude_BB)
2667 : {
2668 0 : continue;
2669 : }
2670 0 : if(va->curvfc==ViewFrustumCull_Fogged)
2671 : {
2672 0 : continue;
2673 : }
2674 0 : float sx1 = -1,
2675 0 : sx2 = 1,
2676 0 : sy1 = -1,
2677 0 : sy2 = 1;
2678 0 : if(!calcbbscissor(va->alphamin, va->alphamax, sx1, sy1, sx2, sy2))
2679 : {
2680 0 : continue;
2681 : }
2682 0 : alphavas.push_back(va);
2683 0 : masktiles(alphatiles.data(), sx1, sy1, sx2, sy2);
2684 0 : a.alphafrontsx1 = std::min(a.alphafrontsx1, sx1);
2685 0 : a.alphafrontsy1 = std::min(a.alphafrontsy1, sy1);
2686 0 : a.alphafrontsx2 = std::max(a.alphafrontsx2, sx2);
2687 0 : a.alphafrontsy2 = std::max(a.alphafrontsy2, sy2);
2688 0 : if(va->alphabacktris)
2689 : {
2690 0 : alphabackvas++;
2691 0 : a.alphabacksx1 = std::min(a.alphabacksx1, sx1);
2692 0 : a.alphabacksy1 = std::min(a.alphabacksy1, sy1);
2693 0 : a.alphabacksx2 = std::max(a.alphabacksx2, sx2);
2694 0 : a.alphabacksy2 = std::max(a.alphabacksy2, sy2);
2695 : }
2696 0 : if(va->refracttris)
2697 : {
2698 0 : if(!calcbbscissor(va->refractmin, va->refractmax, sx1, sy1, sx2, sy2))
2699 : {
2700 0 : continue;
2701 : }
2702 0 : alpharefractvas++;
2703 0 : a.alpharefractsx1 = std::min(a.alpharefractsx1, sx1);
2704 0 : a.alpharefractsy1 = std::min(a.alpharefractsy1, sy1);
2705 0 : a.alpharefractsx2 = std::max(a.alpharefractsx2, sx2);
2706 0 : a.alpharefractsy2 = std::max(a.alpharefractsy2, sy2);
2707 : }
2708 : }
2709 : }
2710 0 : a.hasalphavas = (alpharefractvas ? 4 : 0) | (alphavas.size() ? 2 : 0) | (alphabackvas ? 1 : 0);
2711 0 : return a;
2712 : }
2713 :
2714 0 : void renderrefractmask()
2715 : {
2716 0 : gle::enablevertex();
2717 :
2718 0 : const vtxarray *prev = nullptr;
2719 0 : for(const vtxarray *va : alphavas)
2720 : {
2721 0 : if(!va->refracttris)
2722 : {
2723 0 : continue;
2724 : }
2725 0 : if(!prev || va->vbuf != prev->vbuf)
2726 : {
2727 0 : gle::bindvbo(va->vbuf);
2728 0 : gle::bindebo(va->ebuf);
2729 0 : const vertex *ptr = nullptr;
2730 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note: intentional deref of null pointer
2731 : }
2732 0 : drawvatris(*va, 3*va->refracttris, 3*(va->tris + va->alphabacktris + va->alphafronttris));
2733 0 : xtravertsva += 3*va->refracttris;
2734 0 : prev = va;
2735 : }
2736 :
2737 0 : gle::clearvbo();
2738 0 : gle::clearebo();
2739 0 : gle::disablevertex();
2740 0 : }
2741 :
2742 0 : void renderalphageom(int side)
2743 : {
2744 0 : resetbatches();
2745 :
2746 0 : RenderState cur;
2747 0 : cur.alphaing = side;
2748 0 : cur.invalidatealphascale();
2749 :
2750 0 : setupgeom();
2751 :
2752 0 : if(side == 2)
2753 : {
2754 0 : for(const vtxarray *i : alphavas)
2755 : {
2756 0 : renderva(cur, *i, RenderPass_GBuffer);
2757 : }
2758 0 : if(geombatches.size())
2759 : {
2760 0 : cur.renderbatches(RenderPass_GBuffer);
2761 : }
2762 : }
2763 : else
2764 : {
2765 0 : glCullFace(GL_FRONT);
2766 0 : for(const vtxarray *i : alphavas)
2767 : {
2768 0 : if(i->alphabacktris)
2769 : {
2770 0 : renderva(cur, *i, RenderPass_GBuffer);
2771 : }
2772 : }
2773 0 : if(geombatches.size())
2774 : {
2775 0 : cur.renderbatches(RenderPass_GBuffer);
2776 : }
2777 0 : glCullFace(GL_BACK);
2778 : }
2779 :
2780 0 : cur.cleanupgeom();
2781 0 : }
2782 :
2783 0 : void GBuffer::rendergeom()
2784 : {
2785 0 : bool doOQ = oqfrags && oqgeom && !drawtex,
2786 0 : multipassing = false;
2787 0 : RenderState cur;
2788 :
2789 0 : if(doOQ)
2790 : {
2791 0 : for(vtxarray *va = visibleva; va; va = va->next)
2792 : {
2793 0 : if(va->texs)
2794 : {
2795 0 : if(!camera1->o.insidebb(va->o, va->size, 2))
2796 : {
2797 0 : if(va->parent && va->parent->occluded >= Occlude_BB)
2798 : {
2799 0 : va->query = nullptr;
2800 0 : va->occluded = Occlude_Parent;
2801 0 : continue;
2802 : }
2803 0 : va->occluded = va->query && va->query->owner == va && occlusionengine.checkquery(va->query) ?
2804 0 : std::min(va->occluded+1, static_cast<int>(Occlude_BB)) :
2805 : Occlude_Nothing;
2806 0 : va->query = occlusionengine.newquery(va);
2807 0 : if(!va->query || !va->occluded)
2808 : {
2809 0 : va->occluded = Occlude_Nothing;
2810 : }
2811 0 : if(va->occluded >= Occlude_Geom)
2812 : {
2813 0 : if(va->query)
2814 : {
2815 0 : if(cur.vattribs)
2816 : {
2817 0 : cur.disablevattribs(false);
2818 : }
2819 0 : if(cur.vbuf)
2820 : {
2821 0 : cur.disablevbuf();
2822 : }
2823 0 : renderquery(cur, *va->query, *va);
2824 : }
2825 0 : continue;
2826 : }
2827 : }
2828 : else
2829 : {
2830 0 : va->query = nullptr;
2831 0 : va->occluded = Occlude_Nothing;
2832 0 : if(va->occluded >= Occlude_Geom)
2833 : {
2834 0 : continue;
2835 : }
2836 : }
2837 0 : renderva(cur, *va, RenderPass_Z, true);
2838 : }
2839 : }
2840 :
2841 0 : if(cur.vquery)
2842 : {
2843 0 : cur.disablevquery();
2844 : }
2845 0 : if(cur.vattribs)
2846 : {
2847 0 : cur.disablevattribs(false);
2848 : }
2849 0 : if(cur.vbuf)
2850 : {
2851 0 : cur.disablevbuf();
2852 : }
2853 0 : glFlush();
2854 0 : if(cur.colormask)
2855 : {
2856 0 : cur.colormask = false;
2857 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2858 : }
2859 0 : if(cur.depthmask)
2860 : {
2861 0 : cur.depthmask = false;
2862 0 : glDepthMask(GL_FALSE);
2863 : }
2864 0 : workinoq();
2865 0 : if(!cur.colormask)
2866 : {
2867 0 : cur.colormask = true;
2868 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2869 : }
2870 0 : if(!cur.depthmask)
2871 : {
2872 0 : cur.depthmask = true;
2873 0 : glDepthMask(GL_TRUE);
2874 : }
2875 0 : if(!multipassing)
2876 : {
2877 0 : multipassing = true;
2878 0 : glDepthFunc(GL_LEQUAL);
2879 : }
2880 0 : cur.invalidatetexgenorient();
2881 0 : setupgeom();
2882 0 : resetbatches();
2883 0 : for(const vtxarray *va = visibleva; va; va = va->next)
2884 : {
2885 0 : if(va->texs && va->occluded < Occlude_Geom)
2886 : {
2887 0 : renderva(cur, *va, RenderPass_GBuffer);
2888 : }
2889 : }
2890 0 : if(geombatches.size())
2891 : {
2892 0 : cur.renderbatches(RenderPass_GBuffer);
2893 0 : glFlush();
2894 : }
2895 0 : for(vtxarray *va = visibleva; va; va = va->next)
2896 : {
2897 0 : if(va->texs && va->occluded >= Occlude_Geom)
2898 : {
2899 0 : if((va->parent && va->parent->occluded >= Occlude_BB) || (va->query && occlusionengine.checkquery(va->query)))
2900 : {
2901 0 : va->occluded = Occlude_BB;
2902 0 : continue;
2903 : }
2904 : else
2905 : {
2906 0 : va->occluded = Occlude_Nothing;
2907 0 : if(va->occluded >= Occlude_Geom)
2908 : {
2909 0 : continue;
2910 : }
2911 : }
2912 :
2913 0 : renderva(cur, *va, RenderPass_GBuffer);
2914 : }
2915 : }
2916 0 : if(geombatches.size())
2917 : {
2918 0 : cur.renderbatches(RenderPass_GBuffer);
2919 : }
2920 : }
2921 : else
2922 : {
2923 0 : setupgeom();
2924 0 : resetbatches();
2925 0 : for(vtxarray *va = visibleva; va; va = va->next)
2926 : {
2927 0 : if(va->texs)
2928 : {
2929 0 : va->query = nullptr;
2930 0 : va->occluded = Occlude_Nothing;
2931 0 : if(va->occluded >= Occlude_Geom)
2932 : {
2933 0 : continue;
2934 : }
2935 0 : renderva(cur, *va, RenderPass_GBuffer);
2936 : }
2937 : }
2938 0 : if(geombatches.size())
2939 : {
2940 0 : cur.renderbatches(RenderPass_GBuffer);
2941 : }
2942 : }
2943 0 : if(multipassing)
2944 : {
2945 0 : glDepthFunc(GL_LESS);
2946 : }
2947 0 : cur.cleanupgeom();
2948 0 : if(!doOQ)
2949 : {
2950 0 : glFlush();
2951 0 : if(cur.colormask)
2952 : {
2953 0 : cur.colormask = false;
2954 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2955 : }
2956 0 : if(cur.depthmask)
2957 : {
2958 0 : cur.depthmask = false;
2959 0 : glDepthMask(GL_FALSE);
2960 : }
2961 0 : workinoq();
2962 0 : if(!cur.colormask)
2963 : {
2964 0 : cur.colormask = true;
2965 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2966 : }
2967 0 : if(!cur.depthmask)
2968 : {
2969 0 : cur.depthmask = true;
2970 0 : glDepthMask(GL_TRUE);
2971 : }
2972 : }
2973 0 : }
2974 :
2975 0 : void renderdecals()
2976 : {
2977 : const vtxarray *decalva;
2978 0 : for(decalva = visibleva; decalva; decalva = decalva->next)
2979 : {
2980 0 : if(decalva->decaltris && decalva->occluded < Occlude_BB)
2981 : {
2982 0 : break;
2983 : }
2984 : }
2985 0 : if(!decalva)
2986 : {
2987 0 : return;
2988 : }
2989 0 : decalrenderer cur;
2990 :
2991 0 : setupdecals();
2992 0 : resetdecalbatches();
2993 :
2994 0 : if(maxdualdrawbufs)
2995 : {
2996 0 : glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC1_ALPHA);
2997 0 : maskgbuffer("c");
2998 0 : for(const vtxarray *va = decalva; va; va = va->next)
2999 : {
3000 0 : if(va->decaltris && va->occluded < Occlude_BB)
3001 : {
3002 0 : mergedecals(*va);
3003 0 : if(!batchdecals && decalbatches.size())
3004 : {
3005 0 : cur.renderdecalbatches(0);
3006 : }
3007 : }
3008 : }
3009 0 : if(decalbatches.size())
3010 : {
3011 0 : cur.renderdecalbatches(0);
3012 : }
3013 0 : if(usepacknorm())
3014 : {
3015 0 : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3016 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
3017 : }
3018 : else
3019 : {
3020 0 : glBlendFunc(GL_SRC1_ALPHA, GL_ONE_MINUS_SRC1_ALPHA);
3021 : }
3022 0 : maskgbuffer("n");
3023 0 : cur.vbuf = 0;
3024 0 : for(const vtxarray *va = decalva; va; va = va->next)
3025 : {
3026 0 : if(va->decaltris && va->occluded < Occlude_BB)
3027 : {
3028 0 : mergedecals(*va);
3029 0 : if(!batchdecals && decalbatches.size())
3030 : {
3031 0 : cur.renderdecalbatches(1);
3032 : }
3033 : }
3034 : }
3035 0 : if(decalbatches.size())
3036 : {
3037 0 : cur.renderdecalbatches(1);
3038 : }
3039 : }
3040 : else
3041 : {
3042 0 : glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
3043 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
3044 0 : maskgbuffer("cn");
3045 0 : for(const vtxarray *va = decalva; va; va = va->next)
3046 : {
3047 0 : if(va->decaltris && va->occluded < Occlude_BB)
3048 : {
3049 0 : mergedecals(*va);
3050 0 : if(!batchdecals && decalbatches.size())
3051 : {
3052 0 : cur.renderdecalbatches(0);
3053 : }
3054 : }
3055 : }
3056 0 : if(decalbatches.size())
3057 : {
3058 0 : cur.renderdecalbatches(0);
3059 : }
3060 : }
3061 0 : cleanupdecals();
3062 : }
3063 :
3064 : //shadowmeshes
3065 :
3066 1 : void clearshadowmeshes()
3067 : {
3068 1 : if(shadowvbos.size())
3069 : {
3070 0 : glDeleteBuffers(shadowvbos.size(), shadowvbos.data());
3071 0 : shadowvbos.clear();
3072 : }
3073 1 : if(shadowmeshes.size())
3074 : {
3075 0 : std::vector<extentity *> &ents = entities::getents();
3076 0 : for(size_t i = 0; i < ents.size(); i++)
3077 : {
3078 0 : extentity &e = *ents[i];
3079 0 : if(e.flags&EntFlag_ShadowMesh)
3080 : {
3081 0 : e.flags &= ~EntFlag_ShadowMesh;
3082 : }
3083 : }
3084 : }
3085 1 : shadowmeshes.clear();
3086 1 : shadowdraws.clear();
3087 1 : }
3088 :
3089 0 : void genshadowmeshes()
3090 : {
3091 0 : clearshadowmeshes();
3092 :
3093 0 : if(!smmesh)
3094 : {
3095 0 : return;
3096 : }
3097 0 : renderprogress(0, "generating shadow meshes..");
3098 :
3099 0 : std::vector<extentity *> &ents = entities::getents();
3100 0 : for(size_t i = 0; i < ents.size(); i++)
3101 : {
3102 0 : const extentity &e = *ents[i];
3103 0 : if(e.type != EngineEnt_Light)
3104 : {
3105 0 : continue;
3106 : }
3107 0 : genshadowmesh(i, e);
3108 : }
3109 : }
3110 :
3111 0 : shadowmesh *findshadowmesh(int idx, const extentity &e)
3112 : {
3113 0 : auto itr = shadowmeshes.find(idx);
3114 0 : if(itr == shadowmeshes.end()
3115 0 : || (*itr).second.type != shadowmapping
3116 0 : || (*itr).second.origin != shadoworigin
3117 0 : || (*itr).second.radius < shadowradius)
3118 : {
3119 0 : return nullptr;
3120 : }
3121 0 : switch((*itr).second.type)
3122 : {
3123 0 : case ShadowMap_Spot:
3124 : {
3125 0 : if(!e.attached || e.attached->type != EngineEnt_Spotlight || (*itr).second.spotloc != e.attached->o || (*itr).second.spotangle < std::clamp(static_cast<int>(e.attached->attr1), 1, 89))
3126 : {
3127 0 : return nullptr;
3128 : }
3129 0 : break;
3130 : }
3131 : }
3132 0 : return &(*itr).second;
3133 : }
3134 :
3135 0 : void rendershadowmesh(const shadowmesh *m)
3136 : {
3137 0 : int draw = m->draws[shadowside];
3138 0 : if(draw < 0)
3139 : {
3140 0 : return;
3141 : }
3142 0 : SETSHADER(shadowmapworld);
3143 0 : gle::enablevertex();
3144 0 : GLuint ebuf = 0,
3145 0 : vbuf = 0;
3146 0 : while(draw >= 0)
3147 : {
3148 0 : shadowdraw &d = shadowdraws[draw];
3149 0 : if(ebuf != d.ebuf)
3150 : {
3151 0 : gle::bindebo(d.ebuf);
3152 0 : ebuf = d.ebuf;
3153 : }
3154 0 : if(vbuf != d.vbuf)
3155 : {
3156 0 : gle::bindvbo(d.vbuf);
3157 0 : vbuf = d.vbuf; gle::vertexpointer(sizeof(vec), 0);
3158 : }
3159 0 : drawtris(3*d.tris, static_cast<ushort *>(nullptr) + d.offset, d.minvert, d.maxvert);
3160 0 : xtravertsva += 3*d.tris;
3161 0 : draw = d.next;
3162 : }
3163 0 : gle::disablevertex();
3164 0 : gle::clearebo();
3165 0 : gle::clearvbo();
3166 : }
3167 :
3168 : //external api functions
3169 0 : int calcspheresidemask(const vec &p, float radius, float bias)
3170 : {
3171 : // p is in the cubemap's local coordinate system
3172 : // bias = border/(size - border)
3173 0 : float dxyp = p.x + p.y,
3174 0 : dxyn = p.x - p.y,
3175 0 : axyp = std::fabs(dxyp),
3176 0 : axyn = std::fabs(dxyn),
3177 0 : dyzp = p.y + p.z,
3178 0 : dyzn = p.y - p.z,
3179 0 : ayzp = std::fabs(dyzp),
3180 0 : ayzn = std::fabs(dyzn),
3181 0 : dzxp = p.z + p.x,
3182 0 : dzxn = p.z - p.x,
3183 0 : azxp = std::fabs(dzxp),
3184 0 : azxn = std::fabs(dzxn);
3185 0 : int mask = 0x3F;
3186 0 : radius *= SQRT2;
3187 0 : if(axyp > bias*axyn + radius)
3188 : {
3189 0 : mask &= dxyp < 0 ? ~((1<<0)|(1<<2)) : ~((2<<0)|(2<<2));
3190 : }
3191 0 : if(axyn > bias*axyp + radius)
3192 : {
3193 0 : mask &= dxyn < 0 ? ~((1<<0)|(2<<2)) : ~((2<<0)|(1<<2));
3194 : }
3195 0 : if(ayzp > bias*ayzn + radius)
3196 : {
3197 0 : mask &= dyzp < 0 ? ~((1<<2)|(1<<4)) : ~((2<<2)|(2<<4));
3198 : }
3199 0 : if(ayzn > bias*ayzp + radius)
3200 : {
3201 0 : mask &= dyzn < 0 ? ~((1<<2)|(2<<4)) : ~((2<<2)|(1<<4));
3202 : }
3203 0 : if(azxp > bias*azxn + radius)
3204 : {
3205 0 : mask &= dzxp < 0 ? ~((1<<4)|(1<<0)) : ~((2<<4)|(2<<0));
3206 : }
3207 0 : if(azxn > bias*azxp + radius)
3208 : {
3209 0 : mask &= dzxn < 0 ? ~((1<<4)|(2<<0)) : ~((2<<4)|(1<<0));
3210 : }
3211 0 : return mask;
3212 : }
3213 :
3214 0 : int vfc::cullfrustumsides(const vec &lightpos, float lightradius, float size, float border)
3215 : {
3216 0 : int sides = 0x3F,
3217 0 : masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
3218 0 : float scale = (size - 2*border)/size,
3219 0 : bias = border / static_cast<float>(size - border);
3220 : // check if cone enclosing side would cross frustum plane
3221 0 : scale = 2 / (scale*scale + 2);
3222 0 : for(int i = 0; i < 5; ++i)
3223 : {
3224 0 : if(vfcP[i].dist(lightpos) <= -0.03125f)
3225 : {
3226 0 : vec n = vec(vfcP[i]).div(lightradius);
3227 0 : float len = scale*n.squaredlen();
3228 0 : if(n.x*n.x > len)
3229 : {
3230 0 : sides &= n.x < 0 ? ~(1<<0) : ~(2 << 0);
3231 : }
3232 0 : if(n.y*n.y > len)
3233 : {
3234 0 : sides &= n.y < 0 ? ~(1<<2) : ~(2 << 2);
3235 : }
3236 0 : if(n.z*n.z > len)
3237 : {
3238 0 : sides &= n.z < 0 ? ~(1<<4) : ~(2 << 4);
3239 : }
3240 : }
3241 : }
3242 0 : if (vfcP[4].dist(lightpos) >= vfcDfog + 0.03125f)
3243 : {
3244 0 : vec n = vec(vfcP[4]).div(lightradius);
3245 0 : float len = scale*n.squaredlen();
3246 0 : if(n.x*n.x > len)
3247 : {
3248 0 : sides &= n.x >= 0 ? ~(1<<0) : ~(2 << 0);
3249 : }
3250 0 : if(n.y*n.y > len)
3251 : {
3252 0 : sides &= n.y >= 0 ? ~(1<<2) : ~(2 << 2);
3253 : }
3254 0 : if(n.z*n.z > len)
3255 : {
3256 0 : sides &= n.z >= 0 ? ~(1<<4) : ~(2 << 4);
3257 : }
3258 : }
3259 : // this next test usually clips off more sides than the former, but occasionally clips fewer/different ones, so do both and combine results
3260 : // check if frustum corners/origin cross plane sides
3261 : // infinite version, assumes frustum corners merely give direction and extend to infinite distance
3262 0 : vec p = vec(camera1->o).sub(lightpos).div(lightradius);
3263 0 : float dp = p.x + p.y,
3264 0 : dn = p.x - p.y,
3265 0 : ap = std::fabs(dp),
3266 0 : an = std::fabs(dn);
3267 0 : masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
3268 0 : masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
3269 0 : dp = p.y + p.z, dn = p.y - p.z,
3270 0 : ap = std::fabs(dp),
3271 0 : an = std::fabs(dn);
3272 0 : masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
3273 0 : masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
3274 0 : dp = p.z + p.x,
3275 0 : dn = p.z - p.x,
3276 0 : ap = std::fabs(dp),
3277 0 : an = std::fabs(dn);
3278 0 : masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
3279 0 : masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
3280 0 : for(int i = 0; i < 4; ++i)
3281 : {
3282 0 : vec n;
3283 0 : switch(i)
3284 : {
3285 0 : case 0:
3286 : {
3287 0 : n.cross(vfcP[0], vfcP[2]);
3288 0 : break;
3289 : }
3290 0 : case 1:
3291 : {
3292 0 : n.cross(vfcP[3], vfcP[0]);
3293 0 : break;
3294 : }
3295 0 : case 2:
3296 : {
3297 0 : n.cross(vfcP[2], vfcP[1]);
3298 0 : break;
3299 : }
3300 0 : case 3:
3301 : {
3302 0 : n.cross(vfcP[1], vfcP[3]);
3303 0 : break;
3304 : }
3305 : }
3306 0 : dp = n.x + n.y,
3307 0 : dn = n.x - n.y,
3308 0 : ap = std::fabs(dp),
3309 0 : an = std::fabs(dn);
3310 0 : if(ap > 0)
3311 : {
3312 0 : masks[0] |= dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2);
3313 : }
3314 0 : if(an > 0)
3315 : {
3316 0 : masks[1] |= dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2);
3317 : }
3318 0 : dp = n.y + n.z,
3319 0 : dn = n.y - n.z,
3320 0 : ap = std::fabs(dp),
3321 0 : an = std::fabs(dn);
3322 0 : if(ap > 0)
3323 : {
3324 0 : masks[2] |= dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4);
3325 : }
3326 0 : if(an > 0)
3327 : {
3328 0 : masks[3] |= dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4);
3329 : }
3330 0 : dp = n.z + n.x,
3331 0 : dn = n.z - n.x,
3332 0 : ap = std::fabs(dp),
3333 0 : an = std::fabs(dn);
3334 0 : if(ap > 0)
3335 : {
3336 0 : masks[4] |= dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0);
3337 : }
3338 0 : if(an > 0)
3339 : {
3340 0 : masks[5] |= dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0);
3341 : }
3342 : }
3343 0 : return sides & masks[0] & masks[1] & masks[2] & masks[3] & masks[4] & masks[5];
3344 : }
3345 :
3346 0 : static void findshadowvas(std::vector<vtxarray *> &vas, std::array<vtxarray *, vasortsize> &vasort)
3347 : {
3348 0 : for(vtxarray *&v : vas)
3349 : {
3350 0 : float dist = vadist(*v, shadoworigin);
3351 0 : if(dist < shadowradius || !smdistcull)
3352 : {
3353 0 : v->shadowmask = !smbbcull ? 0x3F : (v->children.size() || v->mapmodels.size() ?
3354 0 : calcbbsidemask(v->bbmin, v->bbmax, shadoworigin, shadowradius, shadowbias) :
3355 0 : calcbbsidemask(v->geommin, v->geommax, shadoworigin, shadowradius, shadowbias));
3356 0 : addshadowva(v, dist, vasort);
3357 0 : if(v->children.size())
3358 : {
3359 0 : findshadowvas(v->children, vasort);
3360 : }
3361 : }
3362 : }
3363 0 : }
3364 :
3365 0 : void renderrsmgeom(bool dyntex)
3366 : {
3367 0 : RenderState cur;
3368 0 : if(!dyntex)
3369 : {
3370 0 : cur.cleartexgenmillis();
3371 : }
3372 0 : setupgeom();
3373 0 : if(skyshadow)
3374 : {
3375 0 : cur.enablevattribs(false);
3376 0 : SETSHADER(rsmsky);
3377 0 : vtxarray *prev = nullptr;
3378 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3379 : {
3380 0 : if(va->sky)
3381 : {
3382 0 : if(!prev || va->vbuf != prev->vbuf)
3383 : {
3384 0 : gle::bindvbo(va->vbuf);
3385 0 : gle::bindebo(va->skybuf);
3386 0 : const vertex *ptr = nullptr; //note: offset of nullptr is technically UB
3387 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data());
3388 : }
3389 0 : drawvaskytris(*va);
3390 0 : xtravertsva += va->sky/3;
3391 0 : prev = va;
3392 : }
3393 : }
3394 0 : if(cur.vattribs)
3395 : {
3396 0 : cur.disablevattribs(false);
3397 : }
3398 : }
3399 0 : resetbatches();
3400 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3401 : {
3402 0 : if(va->texs)
3403 : {
3404 0 : renderva(cur, *va, RenderPass_ReflectiveShadowMap);
3405 : }
3406 : }
3407 0 : if(geombatches.size())
3408 : {
3409 0 : cur.renderbatches(RenderPass_ReflectiveShadowMap);
3410 : }
3411 0 : cur.cleanupgeom();
3412 0 : }
3413 :
3414 0 : void dynamicshadowvabounds(int mask, vec &bbmin, vec &bbmax)
3415 : {
3416 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3417 : {
3418 0 : if(va->shadowmask&mask && va->dyntexs)
3419 : {
3420 0 : bbmin.min(vec(va->geommin));
3421 0 : bbmax.max(vec(va->geommax));
3422 : }
3423 : }
3424 0 : }
3425 :
3426 0 : void findshadowmms()
3427 : {
3428 0 : shadowmms = nullptr;
3429 0 : octaentities **lastmms = &shadowmms;
3430 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3431 : {
3432 0 : for(size_t j = 0; j < va->mapmodels.size(); j++)
3433 : {
3434 0 : octaentities *oe = va->mapmodels[j];
3435 0 : switch(shadowmapping)
3436 : {
3437 0 : case ShadowMap_Reflect:
3438 : {
3439 0 : break;
3440 : }
3441 0 : case ShadowMap_Cascade:
3442 : {
3443 0 : if(!csm.calcbbsplits(oe->bbmin, oe->bbmax))
3444 : {
3445 0 : continue;
3446 : }
3447 0 : break;
3448 : }
3449 0 : case ShadowMap_CubeMap:
3450 : {
3451 0 : if(smdistcull && shadoworigin.dist_to_bb(oe->bbmin, oe->bbmax) >= shadowradius)
3452 : {
3453 0 : continue;
3454 : }
3455 0 : break;
3456 : }
3457 0 : case ShadowMap_Spot:
3458 : {
3459 0 : if(smdistcull && shadoworigin.dist_to_bb(oe->bbmin, oe->bbmax) >= shadowradius)
3460 : {
3461 0 : continue;
3462 : }
3463 0 : if(smbbcull && !bbinsidespot(shadoworigin, shadowdir, shadowspot, oe->bbmin, oe->bbmax))
3464 : {
3465 0 : continue;
3466 : }
3467 0 : break;
3468 : }
3469 : }
3470 0 : oe->rnext = nullptr;
3471 0 : *lastmms = oe;
3472 0 : lastmms = &oe->rnext;
3473 : }
3474 : }
3475 0 : }
3476 :
3477 0 : void rendershadowmapworld()
3478 : {
3479 0 : SETSHADER(shadowmapworld);
3480 :
3481 0 : gle::enablevertex();
3482 :
3483 0 : vtxarray *prev = nullptr;
3484 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3485 : {
3486 0 : if(va->tris && va->shadowmask&(1<<shadowside))
3487 : {
3488 0 : if(!prev || va->vbuf != prev->vbuf)
3489 : {
3490 0 : gle::bindvbo(va->vbuf);
3491 0 : gle::bindebo(va->ebuf);
3492 0 : const vertex *ptr = nullptr;
3493 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data());
3494 : }
3495 0 : if(!smnodraw)
3496 : {
3497 0 : drawvatris(*va, 3*va->tris, 0);
3498 : }
3499 0 : xtravertsva += va->verts;
3500 0 : prev = va;
3501 : }
3502 : }
3503 0 : if(skyshadow)
3504 : {
3505 0 : prev = nullptr;
3506 0 : for(vtxarray *va = shadowva; va; va = va->rnext)
3507 : {
3508 0 : if(va->sky && va->shadowmask&(1<<shadowside))
3509 : {
3510 0 : if(!prev || va->vbuf != prev->vbuf)
3511 : {
3512 0 : gle::bindvbo(va->vbuf);
3513 0 : gle::bindebo(va->skybuf);
3514 0 : const vertex *ptr = nullptr;
3515 0 : gle::vertexpointer(sizeof(vertex), ptr->pos.data()); //note offset from nullptr
3516 : }
3517 0 : if(!smnodraw)
3518 : {
3519 0 : drawvaskytris(*va);
3520 : }
3521 0 : xtravertsva += va->sky/3;
3522 0 : prev = va;
3523 : }
3524 : }
3525 : }
3526 :
3527 0 : gle::clearvbo();
3528 0 : gle::clearebo();
3529 0 : gle::disablevertex();
3530 0 : }
3531 :
3532 0 : void batchshadowmapmodels(bool skipmesh)
3533 : {
3534 0 : if(!shadowmms)
3535 : {
3536 0 : return;
3537 : }
3538 0 : int nflags = EntFlag_NoVis|EntFlag_NoShadow;
3539 0 : if(skipmesh)
3540 : {
3541 0 : nflags |= EntFlag_ShadowMesh;
3542 : }
3543 0 : const std::vector<extentity *> &ents = entities::getents();
3544 0 : for(const octaentities *oe = shadowmms; oe; oe = oe->rnext)
3545 : {
3546 0 : for(const int &k : oe->mapmodels)
3547 : {
3548 0 : extentity &e = *ents[k];
3549 0 : if(e.flags&nflags)
3550 : {
3551 0 : continue;
3552 : }
3553 0 : e.flags |= EntFlag_Render;
3554 : }
3555 : }
3556 0 : for(const octaentities *oe = shadowmms; oe; oe = oe->rnext)
3557 : {
3558 0 : for(const int &j : oe->mapmodels)
3559 : {
3560 0 : extentity &e = *ents[j];
3561 0 : if(!(e.flags&EntFlag_Render))
3562 : {
3563 0 : continue;
3564 : }
3565 0 : rendermapmodel(e);
3566 0 : e.flags &= ~EntFlag_Render;
3567 : }
3568 : }
3569 : }
3570 :
3571 0 : void vtxarray::findrsmshadowvas(std::array<vtxarray *, vasortsize> &vasort)
3572 : {
3573 0 : ivec bbmin, bbmax;
3574 0 : if(children.size() || mapmodels.size())
3575 : {
3576 0 : bbmin = bbmin;
3577 0 : bbmax = bbmax;
3578 : }
3579 : else
3580 : {
3581 0 : bbmin = geommin;
3582 0 : bbmax = geommax;
3583 : }
3584 0 : shadowmask = calcbbrsmsplits(bbmin, bbmax);
3585 0 : if(shadowmask)
3586 : {
3587 0 : float dist = shadowdir.project_bb(bbmin, bbmax) - shadowbias;
3588 0 : addshadowva(this, dist, vasort);
3589 0 : for(vtxarray *child : children)
3590 : {
3591 0 : child->findrsmshadowvas(vasort);
3592 : }
3593 : }
3594 0 : }
3595 :
3596 0 : void vtxarray::findcsmshadowvas(std::array<vtxarray *, vasortsize> &vasort)
3597 : {
3598 0 : ivec bbmin, bbmax;
3599 0 : if(children.size() || mapmodels.size())
3600 : {
3601 0 : bbmin = bbmin;
3602 0 : bbmax = bbmax;
3603 : }
3604 : else
3605 : {
3606 0 : bbmin = geommin;
3607 0 : bbmax = geommax;
3608 : }
3609 0 : shadowmask = csm.calcbbsplits(bbmin, bbmax);
3610 0 : if(shadowmask)
3611 : {
3612 0 : float dist = shadowdir.project_bb(bbmin, bbmax) - shadowbias;
3613 0 : addshadowva(this, dist, vasort);
3614 0 : for(vtxarray *child : children)
3615 : {
3616 0 : child->findcsmshadowvas(vasort);
3617 : }
3618 : }
3619 0 : }
3620 :
3621 0 : void vtxarray::findspotshadowvas(std::array<vtxarray *, vasortsize> &vasort)
3622 : {
3623 0 : float dist = vadist(*this, shadoworigin);
3624 0 : if(dist < shadowradius || !smdistcull)
3625 : {
3626 0 : shadowmask = !smbbcull || (children.size() || mapmodels.size() ?
3627 0 : bbinsidespot(shadoworigin, shadowdir, shadowspot, bbmin, bbmax) :
3628 0 : bbinsidespot(shadoworigin, shadowdir, shadowspot, geommin, geommax)) ? 1 : 0;
3629 0 : addshadowva(this, dist, vasort);
3630 0 : for(vtxarray *child : children)
3631 : {
3632 0 : child->findspotshadowvas(vasort);
3633 : }
3634 : }
3635 0 : }
3636 :
3637 0 : void findshadowvas()
3638 : {
3639 : std::array<vtxarray *, vasortsize> vasort;
3640 0 : vasort.fill(nullptr);
3641 0 : switch(shadowmapping)
3642 : {
3643 0 : case ShadowMap_Reflect:
3644 : {
3645 0 : for(size_t i = 0; i < varoot.size(); ++i)
3646 : {
3647 0 : varoot[i]->findrsmshadowvas(vasort);
3648 : }
3649 0 : break;
3650 : }
3651 0 : case ShadowMap_CubeMap:
3652 : {
3653 0 : findshadowvas(varoot, vasort);
3654 0 : break;
3655 : }
3656 0 : case ShadowMap_Cascade:
3657 : {
3658 0 : for(size_t i = 0; i < varoot.size(); ++i)
3659 : {
3660 0 : varoot[i]->findcsmshadowvas(vasort);
3661 : }
3662 0 : break;
3663 : }
3664 0 : case ShadowMap_Spot:
3665 : {
3666 0 : for(size_t i = 0; i < varoot.size(); ++i)
3667 : {
3668 0 : varoot[i]->findspotshadowvas(vasort);
3669 : }
3670 0 : break;
3671 : }
3672 : }
3673 0 : sortshadowvas(vasort);
3674 0 : }
|