File: BasicMeshDrawer.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (716 lines) | stat: -rw-r--r-- 24,070 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "BasicMeshDrawer.h"
#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/TraceRay.h"
#include "Map/Ground.h"
#include "Map/ReadMap.h"
#include "Map/SMF/SMFGroundDrawer.h"
#include "Rendering/GlobalRendering.h"
#include "System/EventHandler.h"

#define USE_TRIANGLE_STRIPS 1
#define USE_MIPMAP_BUFFERS  0

#if (defined(GLEW_ARB_buffer_storage) && defined(GL_MAP_PERSISTENT_BIT))
#define USE_MAPPED_BUFFERS 1
#else
#define USE_MAPPED_BUFFERS 0
#endif


// do not use GL_MAP_UNSYNCHRONIZED_BIT in either case; causes slow driver sync
#if (USE_MAPPED_BUFFERS == 1)
#define BUFFER_MAP_BITS (GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT)
#else
#define BUFFER_MAP_BITS (GL_MAP_WRITE_BIT)
#endif



typedef CBasicMeshDrawer::MeshPatch MeshPatch;

class MeshPatchVisTestDrawer: public CReadMap::IQuadDrawer {
public:
	void ResetState() override {}
	void ResetState(CCamera* c, MeshPatch* p, uint32_t xsize) {
		testCamera = c;
		patchArray = p;
		numPatches = xsize;
	}

	void DrawQuad(int px, int py) override {
		patchArray[py * numPatches + px].visUpdateFrames[testCamera->GetCamType()] = globalRendering->drawFrame;
	}

private:
	CCamera* testCamera;
	MeshPatch* patchArray;

	uint32_t numPatches;
};

static MeshPatchVisTestDrawer patchVisTestDrawer;



CBasicMeshDrawer::CBasicMeshDrawer(CSMFGroundDrawer* gd)
	: CEventClient("[CBasicMeshDrawer]", 717171, false)
	, smfGroundDrawer(gd)
{
	eventHandler.AddClient(this);

	numPatchesX = mapDims.mapx / PATCH_SIZE;
	numPatchesY = mapDims.mapy / PATCH_SIZE;
	drawPassLOD = 0;

	assert(numPatchesX >= 1);
	assert(numPatchesY >= 1);

	// const auto& lodDistFunc = [](uint32_t i) { return (125.0f * i * i        ); }; // f(x)=125.0*x^2
	// const auto& lodDistFunc = [](uint32_t i) { return ( 25.0f * i * i * i    ); }; // f(x)=25.0*x^3
	const auto& lodDistFunc = [](uint32_t i) { return (  5.0f * i * i * i * i); }; // f(x)=5.0*x^4

	for (uint32_t n = 0; n < LOD_LEVELS; n++) {
		lodDistTable[n] = lodDistFunc(n + 1);
	}

	meshPatches.resize(numPatchesX * numPatchesY);

	for (uint32_t y = 0; y < numPatchesY; y += 1) {
		for (uint32_t x = 0; x < numPatchesX; x += 1) {
			MeshPatch& meshPatch = meshPatches[y * numPatchesX + x];

			meshPatch.squareVertexPtrs.fill(nullptr);

			meshPatch.visUpdateFrames.fill(0);
			meshPatch.uhmUpdateFrames.fill(0);
		}
	}

	for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
		UploadPatchIndices(n);
	}

	UnsyncedHeightMapUpdate(SRectangle{0, 0, mapDims.mapx, mapDims.mapy});
}

CBasicMeshDrawer::~CBasicMeshDrawer() {
	eventHandler.RemoveClient(this);

	#if (USE_MAPPED_BUFFERS == 1)
	for (uint32_t y = 0; y < numPatchesY; y += 1) {
		for (uint32_t x = 0; x < numPatchesX; x += 1) {
			MeshPatch& meshPatch = meshPatches[y * numPatchesX + x];

			for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
				meshPatch.squareVertexBuffers[n].vbo.Release();
				meshPatch.squareVertexBuffers[n].vao.Delete();
			}
			for (uint32_t i = MAP_BORDER_L; i <= MAP_BORDER_B; i++) {
				for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
					meshPatch.borderVertexBuffers[i][n].vbo.Release();
					meshPatch.borderVertexBuffers[i][n].vao.Delete();
				}
			}

			meshPatch.squareVertexPtrs.fill(nullptr);
		}
	}
	#endif

	for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
		lodSquareIndexBuffers[n].Release();
		lodBorderIndexBuffers[n].Release();
	}
}



void CBasicMeshDrawer::Update(const DrawPass::e& drawPass) {
	CCamera* activeCam = CCameraHandler::GetActiveCamera();
	MeshPatch* meshPatch = &meshPatches[0];

	patchVisTestDrawer.ResetState(activeCam, meshPatch, numPatchesX);

	activeCam->CalcFrustumLines(readMap->GetCurrMinHeight() - 100.0f, readMap->GetCurrMaxHeight() + 100.0f, SQUARE_SIZE);
	readMap->GridVisibility(activeCam, &patchVisTestDrawer, 1e9, PATCH_SIZE);

	drawPassLOD = CalcDrawPassLOD(activeCam, drawPass);
}

void CBasicMeshDrawer::UnsyncedHeightMapUpdate(const SRectangle& rect) {
	const uint32_t minPatchX = std::max(rect.x1 / PATCH_SIZE,    (              0));
	const uint32_t minPatchY = std::max(rect.z1 / PATCH_SIZE,    (              0));
	const uint32_t maxPatchX = std::min(rect.x2 / PATCH_SIZE, int(numPatchesX - 1));
	const uint32_t maxPatchY = std::min(rect.z2 / PATCH_SIZE, int(numPatchesY - 1));
	const uint32_t lodLevels = std::max(1, LOD_LEVELS * USE_MIPMAP_BUFFERS);

	const float* heightMap = readMap->GetCornerHeightMapUnsynced();
	const float3* normalMap = readMap->GetVisVertexNormalsUnsynced();

	// TODO: update asynchronously, clip rect against patch bounds
	for (uint32_t py = minPatchY; py <= maxPatchY; py += 1) {
		for (uint32_t px = minPatchX; px <= maxPatchX; px += 1) {
			for (uint32_t n = 0; n < lodLevels; n += 1) {
				UploadPatchSquareGeometry(n, px, py, heightMap, normalMap);
			}
			// need border data at all MIP's regardless of USE_MIPMAP_BUFFERS
			for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
				UploadPatchBorderGeometry(n, px, py, heightMap, normalMap);
			}
		}
	}
}



void CBasicMeshDrawer::UploadPatchSquareGeometry(uint32_t n, uint32_t px, uint32_t py, const float* chm, const float3* cnm) {
	MeshPatch& meshPatch = meshPatches[py * numPatchesX + px];

	VAO& squareVAO = meshPatch.squareVertexBuffers[n].vao;
	VBO& squareVBO = meshPatch.squareVertexBuffers[n].vbo;

	meshPatch.uhmUpdateFrames[0] = globalRendering->drawFrame;

	const uint32_t lodStep  = 1 << n;
	const uint32_t lodVerts = (PATCH_SIZE / lodStep) + 1;

	const uint32_t bpx = px * PATCH_SIZE;
	const uint32_t bpy = py * PATCH_SIZE;

	uint32_t vertexIndx = 0;

	{
		#if (USE_MAPPED_BUFFERS == 1)
		// HACK: the VBO constructor defaults to storage=false
		squareVBO.immutableStorage = true;
		#endif

		if (squareVBO.vboId == 0)
			squareVAO.Generate();

		squareVAO.Bind();
		squareVBO.Bind(GL_ARRAY_BUFFER);
		squareVBO.New((lodVerts * lodVerts) * sizeof(float3), GL_DYNAMIC_DRAW);


		float3* verts = meshPatch.squareVertexPtrs[n];

		if (verts == nullptr)
			verts = reinterpret_cast<float3*>(squareVBO.MapBuffer(BUFFER_MAP_BITS));

		#if (USE_MAPPED_BUFFERS == 1)
		meshPatch.squareVertexPtrs[n] = verts;
		#endif

		if (verts != nullptr) {
			// TODO: geometry shader?
			for (uint32_t vy = 0; vy < lodVerts; vy += 1) {
				for (uint32_t vx = 0; vx < lodVerts; vx += 1) {
					const uint32_t lvx = vx * lodStep;
					const uint32_t lvy = vy * lodStep;

					verts[vertexIndx  ].x = (bpx + lvx) * SQUARE_SIZE;
					verts[vertexIndx  ].z = (bpy + lvy) * SQUARE_SIZE;
					verts[vertexIndx++].y = chm[(bpy + lvy) * mapDims.mapxp1 + (bpx + lvx)];
				}
			}
		}

		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(float3), VA_TYPE_OFFSET(float, 0));

		#if (USE_MAPPED_BUFFERS == 0)
		squareVBO.UnmapBuffer();
		#endif
		squareVAO.Unbind();
		squareVBO.Unbind();

		glDisableVertexAttribArray(0);
	}
}

void CBasicMeshDrawer::UploadPatchBorderGeometry(uint32_t n, uint32_t px, uint32_t py, const float* chm, const float3* cnm) {
	MeshPatch& meshPatch = meshPatches[py * numPatchesX + px];

	const uint32_t lodStep  = 1 << n;
	const uint32_t lodVerts = (PATCH_SIZE / lodStep) + 1;

	const uint32_t bpx = px * PATCH_SIZE;
	const uint32_t bpy = py * PATCH_SIZE;

	uint32_t vertexIndx = 0;

	if (px == 0) {
		vertexIndx = 0;

		VAO& borderVAO = meshPatch.borderVertexBuffers[MAP_BORDER_L][n].vao;
		VBO& borderVBO = meshPatch.borderVertexBuffers[MAP_BORDER_L][n].vbo;

		{
			if (borderVBO.vboId == 0)
				borderVAO.Generate();

			borderVAO.Bind();
			borderVBO.Bind(GL_ARRAY_BUFFER);
			borderVBO.New(lodVerts * sizeof(VA_TYPE_C) * 2, GL_DYNAMIC_DRAW);

			VA_TYPE_C* verts = reinterpret_cast<VA_TYPE_C*>(borderVBO.MapBuffer());

			if (verts != nullptr) {
				for (uint32_t vy = 0; vy < lodVerts; vy += 1) {
					verts[vertexIndx           ].p.x = 0.0f;
					verts[vertexIndx + lodVerts].p.x = 0.0f;
					verts[vertexIndx           ].p.z =     (bpy + vy * lodStep) * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.z =     (bpy + vy * lodStep) * SQUARE_SIZE;
					verts[vertexIndx           ].p.y = chm[(bpy + vy * lodStep) * mapDims.mapxp1 + 0]; // upper
					verts[vertexIndx + lodVerts].p.y = std::min(readMap->GetInitMinHeight(), -500.0f); // lower

					// terrain normals point up, borders do not
					// currently not needed for border rendering
					// verts[vertexIndx           ].n = -RgtVector;
					// verts[vertexIndx + lodVerts].n = -RgtVector;

					verts[vertexIndx           ].c = SColor(255, 255, 255, 255);
					verts[vertexIndx + lodVerts].c = SColor(255, 255, 255,   0);

					vertexIndx += 1;
				}
			}

			glEnableVertexAttribArray(0);
			glEnableVertexAttribArray(1);
			glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 0));
			glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 3));

			borderVBO.UnmapBuffer();
			borderVAO.Unbind();
			borderVBO.Unbind();

			glDisableVertexAttribArray(1);
			glDisableVertexAttribArray(0);
		}
	}
	if (px == (numPatchesX - 1)) {
		vertexIndx = 0;

		VAO& borderVAO = meshPatch.borderVertexBuffers[MAP_BORDER_R][n].vao;
		VBO& borderVBO = meshPatch.borderVertexBuffers[MAP_BORDER_R][n].vbo;

		{
			if (borderVBO.vboId == 0)
				borderVAO.Generate();

			borderVAO.Bind();
			borderVBO.Bind(GL_ARRAY_BUFFER);
			borderVBO.New(lodVerts * sizeof(VA_TYPE_C) * 2, GL_DYNAMIC_DRAW);

			VA_TYPE_C* verts = reinterpret_cast<VA_TYPE_C*>(borderVBO.MapBuffer());

			if (verts != nullptr) {
				for (uint32_t vy = 0; vy < lodVerts; vy += 1) {
					verts[vertexIndx           ].p.x = mapDims.mapx * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.x = mapDims.mapx * SQUARE_SIZE;
					verts[vertexIndx           ].p.z =     (bpy + vy * lodStep) * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.z =     (bpy + vy * lodStep) * SQUARE_SIZE;
					verts[vertexIndx           ].p.y = chm[(bpy + vy * lodStep) * mapDims.mapxp1 + mapDims.mapx];
					verts[vertexIndx + lodVerts].p.y = std::min(readMap->GetInitMinHeight(), -500.0f);

					// verts[vertexIndx           ].n = RgtVector;
					// verts[vertexIndx + lodVerts].n = RgtVector;

					verts[vertexIndx           ].c = SColor(255, 255, 255, 255);
					verts[vertexIndx + lodVerts].c = SColor(255, 255, 255,   0);

					vertexIndx += 1;
				}
			}

			glEnableVertexAttribArray(0);
			glEnableVertexAttribArray(1);
			glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 0));
			glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 3));

			borderVBO.UnmapBuffer();
			borderVAO.Unbind();
			borderVBO.Unbind();

			glDisableVertexAttribArray(1);
			glDisableVertexAttribArray(0);
		}
	}

	if (py == 0) {
		vertexIndx = 0;

		VAO& borderVAO = meshPatch.borderVertexBuffers[MAP_BORDER_T][n].vao;
		VBO& borderVBO = meshPatch.borderVertexBuffers[MAP_BORDER_T][n].vbo;

		{
			if (borderVBO.vboId == 0)
				borderVAO.Generate();

			borderVAO.Bind();
			borderVBO.Bind(GL_ARRAY_BUFFER);
			borderVBO.New(lodVerts * sizeof(VA_TYPE_C) * 2, GL_DYNAMIC_DRAW);

			VA_TYPE_C* verts = reinterpret_cast<VA_TYPE_C*>(borderVBO.MapBuffer());

			if (verts != nullptr) {
				for (uint32_t vx = 0; vx < lodVerts; vx += 1) {
					verts[vertexIndx           ].p.x = (bpx + vx * lodStep) * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.x = (bpx + vx * lodStep) * SQUARE_SIZE;
					verts[vertexIndx           ].p.z = 0.0f;
					verts[vertexIndx + lodVerts].p.z = 0.0f;
					verts[vertexIndx           ].p.y = chm[0 + (bpx + vx * lodStep)];
					verts[vertexIndx + lodVerts].p.y = std::min(readMap->GetInitMinHeight(), -500.0f);

					// verts[vertexIndx           ].n = -FwdVector;
					// verts[vertexIndx + lodVerts].n = -FwdVector;

					verts[vertexIndx           ].c = SColor(255, 255, 255, 255);
					verts[vertexIndx + lodVerts].c = SColor(255, 255, 255,   0);

					vertexIndx += 1;
				}
			}

			glEnableVertexAttribArray(0);
			glEnableVertexAttribArray(1);
			glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 0));
			glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 3));

			borderVBO.UnmapBuffer();
			borderVAO.Unbind();
			borderVBO.Unbind();

			glDisableVertexAttribArray(1);
			glDisableVertexAttribArray(0);
		}
	}
	if (py == (numPatchesY - 1)) {
		vertexIndx = 0;

		VAO& borderVAO = meshPatch.borderVertexBuffers[MAP_BORDER_B][n].vao;
		VBO& borderVBO = meshPatch.borderVertexBuffers[MAP_BORDER_B][n].vbo;

		{
			if (borderVBO.vboId == 0)
				borderVAO.Generate();

			borderVAO.Bind();
			borderVBO.Bind(GL_ARRAY_BUFFER);
			borderVBO.New(lodVerts * sizeof(VA_TYPE_C) * 2, GL_DYNAMIC_DRAW);

			VA_TYPE_C* verts = reinterpret_cast<VA_TYPE_C*>(borderVBO.MapBuffer());

			if (verts != nullptr) {
				for (uint32_t vx = 0; vx < lodVerts; vx += 1) {
					verts[vertexIndx           ].p.x = (bpx + vx * lodStep) * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.x = (bpx + vx * lodStep) * SQUARE_SIZE;
					verts[vertexIndx           ].p.z = mapDims.mapy * SQUARE_SIZE;
					verts[vertexIndx + lodVerts].p.z = mapDims.mapy * SQUARE_SIZE;
					verts[vertexIndx           ].p.y = chm[mapDims.mapy * mapDims.mapxp1 + (bpx + vx * lodStep)];
					verts[vertexIndx + lodVerts].p.y = std::min(readMap->GetInitMinHeight(), -500.0f);

					// verts[vertexIndx           ].n = FwdVector;
					// verts[vertexIndx + lodVerts].n = FwdVector;

					verts[vertexIndx           ].c = SColor(255, 255, 255, 255);
					verts[vertexIndx + lodVerts].c = SColor(255, 255, 255,   0);

					vertexIndx += 1;
				}
			}

			glEnableVertexAttribArray(0);
			glEnableVertexAttribArray(1);
			glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 0));
			glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, false, sizeof(VA_TYPE_C), VA_TYPE_OFFSET(float, 3));

			borderVBO.UnmapBuffer();
			borderVAO.Unbind();
			borderVBO.Unbind();

			glDisableVertexAttribArray(1);
			glDisableVertexAttribArray(0);
		}
	}
}


void CBasicMeshDrawer::UploadPatchIndices(uint32_t n) {
	// base-level constants
	constexpr uint32_t numVerts = PATCH_SIZE + 1;
	#if (USE_TRIANGLE_STRIPS == 0)
	constexpr uint32_t numQuads = PATCH_SIZE;
	constexpr uint32_t numPolys = (numQuads * numQuads) * 2;
	#endif

	const uint32_t lodStep  = 1 << n;
	const uint32_t lodQuads = (PATCH_SIZE / lodStep);
	const uint32_t lodVerts = (PATCH_SIZE / lodStep) + 1;

	VBO& squareIBO = lodSquareIndexBuffers[n];
	VBO& borderIBO = lodBorderIndexBuffers[n];

	{
		// NOTE:
		//   with GL_STATIC_DRAW, driver spams [OPENGL_DEBUG] id=131186 source=API type=PERFORMANCE severity=MEDIUM
		//   msg="Buffer performance warning: Buffer object 18 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is
		//   GL_STATIC_DRAW) is being copied/moved from VIDEO memory to HOST memory." unless USE_MAPPED_BUFFERS=0
		squareIBO.Bind(GL_ELEMENT_ARRAY_BUFFER);
		#if (USE_TRIANGLE_STRIPS == 0)
		squareIBO.New((numPolys / (lodStep * lodStep)) * 3 * sizeof(uint16_t), GL_DYNAMIC_DRAW);
		#else
		squareIBO.New(((lodQuads * 2 + 3) * lodQuads) * sizeof(uint16_t), GL_DYNAMIC_DRAW);
		#endif

		uint16_t* indcs = reinterpret_cast<uint16_t*>(squareIBO.MapBuffer());

		if (indcs != nullptr) {
			uint32_t indxCtr = 0;
			// uint32_t quadCtr = 0;

			// A B    B   or   A ... [B]
			// C    C D        C ... [D]
			for (uint32_t vy = 0; vy < lodQuads; vy += 1) {
				#if (USE_TRIANGLE_STRIPS == 0)
					for (uint32_t vx = 0; vx < lodQuads; vx += 1) {
						#if (USE_MIPMAP_BUFFERS == 1)
							indcs[indxCtr++] = ((vy * lodVerts) + (vx +     lodVerts)); // C
							indcs[indxCtr++] = ((vy * lodVerts) + (vx + 1           )); // B
							indcs[indxCtr++] = ((vy * lodVerts) + (vx               )); // A

							indcs[indxCtr++] = ((vy * lodVerts) + (vx     + lodVerts)); // C
							indcs[indxCtr++] = ((vy * lodVerts) + (vx + 1 + lodVerts)); // D
							indcs[indxCtr++] = ((vy * lodVerts) + (vx + 1           )); // B
						#else
							indcs[indxCtr++] = ((vy * numVerts) + (vx +     numVerts)) * lodStep; // C
							indcs[indxCtr++] = ((vy * numVerts) + (vx + 1           )) * lodStep; // B
							indcs[indxCtr++] = ((vy * numVerts) + (vx               )) * lodStep; // A

							indcs[indxCtr++] = ((vy * numVerts) + (vx     + numVerts)) * lodStep; // C
							indcs[indxCtr++] = ((vy * numVerts) + (vx + 1 + numVerts)) * lodStep; // D
							indcs[indxCtr++] = ((vy * numVerts) + (vx + 1           )) * lodStep; // B
						#endif
					}

				#else

					for (uint32_t vx = 0; vx < lodQuads; vx += 1) {
						#if (USE_MIPMAP_BUFFERS == 1)
							indcs[indxCtr++] = ((vy * lodVerts) + (vx               )); // A
							indcs[indxCtr++] = ((vy * lodVerts) + (vx +     lodVerts)); // C
						#else
							indcs[indxCtr++] = ((vy * numVerts) + (vx               )) * lodStep; // A
							indcs[indxCtr++] = ((vy * numVerts) + (vx +     numVerts)) * lodStep; // C
						#endif
					}

					#if (USE_MIPMAP_BUFFERS == 1)
						indcs[indxCtr++] = ((vy * lodVerts) + (lodQuads           )); // B
						indcs[indxCtr++] = ((vy * lodVerts) + (lodQuads + lodVerts)); // D
					#else
						indcs[indxCtr++] = ((vy * numVerts) + (lodQuads           )) * lodStep; // B
						indcs[indxCtr++] = ((vy * numVerts) + (lodQuads + numVerts)) * lodStep; // D
					#endif

					// terminator
					indcs[indxCtr++] = 0xFFFF;
				#endif
			}

			#if (USE_TRIANGLE_STRIPS == 0)
			assert(indxCtr == ((numPolys / (lodStep * lodStep)) * 3));
			#else
			assert(indxCtr == ((lodQuads * 2 + 3) * lodQuads));
			#endif
		}

		squareIBO.UnmapBuffer();
		squareIBO.Unbind();
	}
	{
		borderIBO.Bind(GL_ELEMENT_ARRAY_BUFFER);
		borderIBO.New(((lodQuads * 2 + 3) * 1) * sizeof(uint16_t), GL_DYNAMIC_DRAW);

		uint16_t* indcs = reinterpret_cast<uint16_t*>(borderIBO.MapBuffer());

		if (indcs != nullptr) {
			uint32_t indxCtr = 0;
			// uint32_t quadCtr = 0;

			for (uint32_t vi = 0; vi < lodQuads; vi += 1) {
				indcs[indxCtr++] = (vi               ); // A
				indcs[indxCtr++] = (vi +     lodVerts); // C
			}

			indcs[indxCtr++] = (lodQuads           ); // B
			indcs[indxCtr++] = (lodQuads + lodVerts); // D
			indcs[indxCtr++] = 0xFFFF;
		}

		borderIBO.UnmapBuffer();
		borderIBO.Unbind();
	}
}



uint32_t CBasicMeshDrawer::CalcDrawPassLOD(const CCamera* cam, const DrawPass::e& drawPass) const {
	// higher detail biases LOD-step toward a smaller value
	// NOTE: should perhaps prevent an insane initial bias?
	int32_t lodBias = smfGroundDrawer->GetGroundDetail(drawPass) % LOD_LEVELS;
	int32_t lodIndx = LOD_LEVELS - 1;

	// force SP and NP to equal LOD; avoids projection issues
	if (drawPass == DrawPass::Shadow || drawPass == DrawPass::WaterReflection)
		cam = CCameraHandler::GetCamera(CCamera::CAMTYPE_PLAYER);

	{
		const CUnit* hitUnit = nullptr;
		const CFeature* hitFeature = nullptr;

		float mapRayDist = 0.0f;

		if ((mapRayDist = TraceRay::GuiTraceRay(cam->GetPos(), cam->GetDir(), cam->GetFarPlaneDist(), nullptr, hitUnit, hitFeature, false, true, true)) < 0.0f)
			mapRayDist = CGround::LinePlaneCol(cam->GetPos(), cam->GetDir(), cam->GetFarPlaneDist(), readMap->GetCurrMinHeight());
		if (mapRayDist < 0.0f)
			return lodIndx;

		for (uint32_t n = 0; n < LOD_LEVELS; n += 1) {
			if (mapRayDist < lodDistTable[n]) {
				lodIndx = n;
				break;
			}
		}
	}

	switch (drawPass) {
		case DrawPass::Normal         : { return (std::max(lodIndx - lodBias, 0)); } break;
		case DrawPass::Shadow         : { return (std::max(lodIndx - lodBias, 0)); } break;
		case DrawPass::TerrainDeferred: { return (std::max(lodIndx - lodBias, 0)); } break;
		default: {} break;
	}

	// prevent reflections etc from becoming too low-res
	return (Clamp(lodIndx - lodBias, 0, LOD_LEVELS - 4));
}



void CBasicMeshDrawer::DrawSquareMeshPatch(const MeshPatch& meshPatch, const CCamera* activeCam) const {
	const VAO& squareVAO = meshPatch.squareVertexBuffers[drawPassLOD * USE_MIPMAP_BUFFERS].vao;
	const VBO& squareIBO = lodSquareIndexBuffers[drawPassLOD];

	// supply indices separately after VAO is bound
	squareVAO.Bind();
	squareIBO.Bind(GL_ELEMENT_ARRAY_BUFFER);

	#if (USE_TRIANGLE_STRIPS == 0)
	const uint32_t lodStep  = 1 << drawPassLOD;
	const uint32_t lodQuads = (PATCH_SIZE / lodStep);
	const uint32_t numIndcs = squareIBO.GetSize() / sizeof(uint16_t);

	assert(numIndcs == (lodQuads * lodQuads * 2 * 3));
	assert(squareIBO.GetPtr() == nullptr);
	#endif

	#if (USE_TRIANGLE_STRIPS == 0)
		glDrawElements(GL_TRIANGLES, (squareIBO.GetSize() / sizeof(uint16_t)), GL_UNSIGNED_SHORT, squareIBO.GetPtr());
		// glDrawRangeElements(GL_TRIANGLES, 0, (lodVerts * lodVerts) - 1, (squareIBO.GetSize() / sizeof(uint16_t)), GL_UNSIGNED_SHORT, squareIBO.GetPtr());
	#else
		glPrimitiveRestartIndex(0xFFFF);
		glDrawElements(GL_TRIANGLE_STRIP, (squareIBO.GetSize() / sizeof(uint16_t)), GL_UNSIGNED_SHORT, squareIBO.GetPtr());
	#endif

	squareIBO.Unbind();
	squareVAO.Unbind();
}

void CBasicMeshDrawer::DrawMesh(const DrawPass::e& drawPass) {
	Update(drawPass);

	#if (USE_TRIANGLE_STRIPS == 1)
	glEnable(GL_PRIMITIVE_RESTART);
	#endif

	const CCamera* activeCam = CCameraHandler::GetActiveCamera();

	for (uint32_t py = 0; py < numPatchesY; py += 1) {
		for (uint32_t px = 0; px < numPatchesX; px += 1) {
			const MeshPatch& meshPatch = meshPatches[py * numPatchesX + px];

			if (meshPatch.visUpdateFrames[activeCam->GetCamType()] < globalRendering->drawFrame)
				continue;

			if (drawPass != DrawPass::Shadow)
				smfGroundDrawer->SetupBigSquare(px, py);

			DrawSquareMeshPatch(meshPatch, activeCam);
		}
	}

	#if (USE_TRIANGLE_STRIPS == 1)
	glDisable(GL_PRIMITIVE_RESTART);
	#endif
}



void CBasicMeshDrawer::DrawBorderMeshPatch(const MeshPatch& meshPatch, const CCamera* activeCam, uint32_t borderSide) const {
	if (meshPatch.visUpdateFrames[activeCam->GetCamType()] < globalRendering->drawFrame)
		return;

	const VAO& borderVAO = meshPatch.borderVertexBuffers[borderSide][drawPassLOD].vao;
	const VBO& borderIBO = lodBorderIndexBuffers[drawPassLOD];

	borderVAO.Bind();
	borderIBO.Bind(GL_ELEMENT_ARRAY_BUFFER);

	glPrimitiveRestartIndex(0xFFFF);
	glDrawElements(GL_TRIANGLE_STRIP, (borderIBO.GetSize() / sizeof(uint16_t)), GL_UNSIGNED_SHORT, borderIBO.GetPtr());

	borderIBO.Unbind();
	borderVAO.Unbind();
}

void CBasicMeshDrawer::DrawBorderMesh(const DrawPass::e& drawPass) {
	// border is always stripped
	glEnable(GL_PRIMITIVE_RESTART);

	const uint32_t npxm1 = numPatchesX - 1;
	const uint32_t npym1 = numPatchesY - 1;

	const CCamera* activeCam = CCameraHandler::GetActiveCamera();

	{
		// invert culling; index-pattern for T and R borders is also inverted
		glAttribStatePtr->FrontFace(GL_CW);

		if (drawPass != DrawPass::Shadow) {
			for (uint32_t px = 0; px < numPatchesX; px++) { smfGroundDrawer->SetupBigSquare( px  ,  0); DrawBorderMeshPatch(meshPatches[ 0 * numPatchesX +  px  ], activeCam, MAP_BORDER_T); }
			for (uint32_t py = 0; py < numPatchesY; py++) { smfGroundDrawer->SetupBigSquare(npxm1, py); DrawBorderMeshPatch(meshPatches[py * numPatchesX + npxm1], activeCam, MAP_BORDER_R); }
		} else {
			for (uint32_t px = 0; px < numPatchesX; px++) { DrawBorderMeshPatch(meshPatches[ 0 * numPatchesX +  px  ], activeCam, MAP_BORDER_T); }
			for (uint32_t py = 0; py < numPatchesY; py++) { DrawBorderMeshPatch(meshPatches[py * numPatchesX + npxm1], activeCam, MAP_BORDER_R); }
		}
	}
	{
		glAttribStatePtr->FrontFace(GL_CCW);

		if (drawPass != DrawPass::Shadow) {
			for (uint32_t px = 0; px < numPatchesX; px++) { smfGroundDrawer->SetupBigSquare(px, npym1); DrawBorderMeshPatch(meshPatches[npym1 * numPatchesX + px], activeCam, MAP_BORDER_B); }
			for (uint32_t py = 0; py < numPatchesY; py++) { smfGroundDrawer->SetupBigSquare( 0,  py  ); DrawBorderMeshPatch(meshPatches[ py   * numPatchesX +  0], activeCam, MAP_BORDER_L); }
		} else {
			for (uint32_t px = 0; px < numPatchesX; px++) { DrawBorderMeshPatch(meshPatches[npym1 * numPatchesX + px], activeCam, MAP_BORDER_B); }
			for (uint32_t py = 0; py < numPatchesY; py++) { DrawBorderMeshPatch(meshPatches[ py   * numPatchesX +  0], activeCam, MAP_BORDER_L); }
		}
	}

	glDisable(GL_PRIMITIVE_RESTART);
}