/
terrain.cpp
1525 lines (1346 loc) · 49.8 KB
/
terrain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file terrain.c
* Draws the terrain.
* It uses the ground property of every MAPTILE to divide the terrain into different layers.
* For every layer the GROUND_TYPE (from psGroundTypes) determines the texture and size.
* The technique used it called "texture splatting".
* Every layer only draws the spots where that terrain is, and additive blending is used to make transitions smooth.
* Decals are a kind of hack now, as for some tiles (where decal == true) the old tile is just drawn.
* The water is drawn using the hardcoded page-80 and page-81 textures.
*/
#include <string.h>
#include "lib/framework/frame.h"
#include "lib/framework/opengl.h"
#include "lib/ivis_opengl/ivisdef.h"
#include "lib/ivis_opengl/imd.h"
#include "lib/ivis_opengl/piefunc.h"
#include "lib/ivis_opengl/tex.h"
#include "lib/ivis_opengl/piedef.h"
#include "lib/ivis_opengl/piestate.h"
#include "lib/ivis_opengl/pieclip.h"
#include "lib/ivis_opengl/screen.h"
#include "terrain.h"
#include "map.h"
#include "texture.h"
#include "display3d.h"
#include "hci.h"
#include "loop.h"
/**
* A sector contains all information to draw a square piece of the map.
* The actual geometry and texture data is not stored in here but in large VBO's.
* The sector only stores the index and length of the pieces it's going to use.
*/
struct Sector
{
int geometryOffset; ///< The point in the geometry VBO where our geometry starts
int geometrySize; ///< The size of our geometry
int geometryIndexOffset; ///< The point in the index VBO where our triangles start
int geometryIndexSize; ///< The size of our indices
int waterOffset; ///< The point in the water VBO where the water geometry starts
int waterSize; ///< The size of the water geometry
int waterIndexOffset; ///< The point in the water index VBO where the water triangles start
int waterIndexSize; ///< The size of our water triangles
int *textureOffset; ///< An array containing the offsets into the texture VBO for each terrain layer
int *textureSize; ///< The size of the geometry for this layer for each layer
int *textureIndexOffset; ///< The offset into the index VBO for the texture for each layer
int *textureIndexSize; ///< The size of the indices for each layer
int decalOffset; ///< Index into the decal VBO
int decalSize; ///< Size of the part of the decal VBO we are going to use
bool draw; ///< Do we draw this sector this frame?
bool dirty; ///< Do we need to update the geometry for this sector?
};
/// A vertex with just a position
struct RenderVertex
{
GLfloat x, y, z; // Vertex
};
/// A vertex with a position and texture coordinates
struct DecalVertex
{
GLfloat x, y, z;
GLfloat u, v; // uv
};
/// The lightmap texture
static GLuint lightmap_tex_num;
/// When are we going to update the lightmap next?
static unsigned int lightmapLastUpdate;
/// How big is the lightmap?
static int lightmapWidth;
static int lightmapHeight;
/// Lightmap image
static GLubyte *lightmapPixmap;
/// Ticks per lightmap refresh
static const unsigned int LIGHTMAP_REFRESH = 80;
/// VBOs
static GLuint geometryVBO, geometryIndexVBO, textureVBO, textureIndexVBO, decalVBO;
/// VBOs
static GLuint waterVBO, waterIndexVBO;
/// The amount we shift the water textures so the waves appear to be moving
static float waterOffset;
/// These are properties of your videocard and hardware
static GLint GLmaxElementsVertices, GLmaxElementsIndices;
/// The sectors are stored here
static Sector *sectors;
/// The default sector size (a sector is sectorSize x sectorSize)
static int sectorSize = 15;
/// What is the distance we can see
static int terrainDistance;
/// How many sectors have we actually got?
static int xSectors, ySectors;
/// Did we initialise the terrain renderer yet?
static bool terrainInitalised = false;
#ifdef DEBUG
/// Check for OpenGL errors
#define glError() { \
GLenum err = glGetError(); \
while (err != GL_NO_ERROR) { \
debug(LOG_ERROR, "OpenGL error %i caught at %s:%u\n", (int)err, __FILE__, __LINE__); \
err = glGetError(); \
} \
}
#else
#define glError()
#endif
/// Helper to specify the offset in a VBO
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
/// Helper variables for the DrawRangeElements functions
GLuint dreStart, dreEnd, dreOffset;
GLsizei dreCount;
/// Are we actually drawing something using the DrawRangeElements functions?
bool drawRangeElementsStarted = false;
/// Pass all remaining triangles to OpenGL
static void finishDrawRangeElements(void)
{
if (drawRangeElementsStarted && dreCount > 0)
{
ASSERT(dreEnd - dreStart + 1 <= GLmaxElementsVertices, "too many vertices (%i)", (int)(dreEnd - dreStart + 1));
ASSERT(dreCount <= GLmaxElementsIndices, "too many indices (%i)", (int)dreCount);
glDrawRangeElements(GL_TRIANGLES,
dreStart,
dreEnd,
dreCount,
GL_UNSIGNED_INT,
BUFFER_OFFSET(sizeof(GLuint)*dreOffset));
}
drawRangeElementsStarted = false;
}
/**
* Either draw the elements or batch them to be sent to OpenGL later
* This improves performance by reducing the amount of OpenGL calls.
*/
static void addDrawRangeElements(GLenum mode,
GLuint start,
GLuint end,
GLsizei count,
GLenum type,
GLuint offset)
{
ASSERT(mode == GL_TRIANGLES, "not supported");
ASSERT(type == GL_UNSIGNED_INT, "not supported");
if (end - start + 1 > GLmaxElementsVertices)
{
debug(LOG_WARNING, "A single call provided too much vertices, will operate at reduced performance or crash. Decrease the sector size to fix this.");
}
if (count > GLmaxElementsIndices)
{
debug(LOG_WARNING, "A single call provided too much indices, will operate at reduced performance or crash. Decrease the sector size to fix this.");
}
if (!drawRangeElementsStarted)
{
dreStart = start;
dreEnd = end;
dreCount = count;
dreOffset = offset;
drawRangeElementsStarted = true;
return;
}
// check if we can append theoretically and
// check if this will not go over the bounds advised by the opengl implementation
if (dreOffset + dreCount != offset ||
dreCount + count > GLmaxElementsIndices ||
end - dreStart + 1 > GLmaxElementsVertices)
{
finishDrawRangeElements();
// start anew
addDrawRangeElements(mode, start, end, count, type, offset);
}
else
{
// OK to append
dreCount += count;
dreEnd = end;
}
// make sure we did everything right
ASSERT(dreEnd - dreStart + 1 <= GLmaxElementsVertices, "too many vertices (%i)", (int)(dreEnd - dreStart + 1));
ASSERT(dreCount <= GLmaxElementsIndices, "too many indices (%i)", (int)(dreCount));
}
/// Get the colour of the terrain tile at the specified position
PIELIGHT getTileColour(int x, int y)
{
return mapTile(x, y)->colour;
}
/// Set the colour of the tile at the specified position
void setTileColour(int x, int y, PIELIGHT colour)
{
MAPTILE *psTile = mapTile(x, y);
psTile->colour = colour;
}
// NOTE: The current (max) texture size of a tile is 128x128. We allow up to a user defined texture size
// of 2048. This will cause ugly seams for the decals, if user picks a texture size bigger than the tile!
#define MAX_TILE_TEXTURE_SIZE 128.0f
/// Set up the texture coordinates for a tile
static void getTileTexCoords(Vector2f *uv, unsigned int tileNumber)
{
/* unmask proper values from compressed data */
const unsigned short texture = TileNumber_texture(tileNumber);
const unsigned short tile = TileNumber_tile(tileNumber);
/* Used to calculate texture coordinates */
const float xMult = 1.0f / TILES_IN_PAGE_COLUMN;
const float yMult = 1.0f / TILES_IN_PAGE_ROW;
float texsize = (float)getTextureSize();
float centertile, shiftamount, one;
Vector2f sP1, sP2, sP3, sP4, sPTemp;
// the decals are 128x128 (at this time), we should not go above this value. See note above
if (texsize > MAX_TILE_TEXTURE_SIZE)
{
texsize = MAX_TILE_TEXTURE_SIZE;
}
centertile = 0.5f / texsize; //compute center of tile
shiftamount = (texsize -1.0) / texsize; // 1 pixel border
one = 1.0f / (TILES_IN_PAGE_COLUMN * texsize);
// bump the texture coords, for 1 pixel border, so our range is [.5,(texsize - .5)]
one += centertile * shiftamount;
/*
* Points for flipping the texture around if the tile is flipped or rotated
* Store the source rect as four points
*/
sP1.x = one;
sP1.y = one;
sP2.x = xMult - one;
sP2.y = one;
sP3.x = xMult - one;
sP3.y = yMult - one;
sP4.x = one;
sP4.y = yMult - one;
if (texture & TILE_XFLIP)
{
sPTemp = sP1;
sP1 = sP2;
sP2 = sPTemp;
sPTemp = sP3;
sP3 = sP4;
sP4 = sPTemp;
}
if (texture & TILE_YFLIP)
{
sPTemp = sP1;
sP1 = sP4;
sP4 = sPTemp;
sPTemp = sP2;
sP2 = sP3;
sP3 = sPTemp;
}
switch ((texture & TILE_ROTMASK) >> TILE_ROTSHIFT)
{
case 1:
sPTemp = sP1;
sP1 = sP4;
sP4 = sP3;
sP3 = sP2;
sP2 = sPTemp;
break;
case 2:
sPTemp = sP1;
sP1 = sP3;
sP3 = sPTemp;
sPTemp = sP4;
sP4 = sP2;
sP2 = sPTemp;
break;
case 3:
sPTemp = sP1;
sP1 = sP2;
sP2 = sP3;
sP3 = sP4;
sP4 = sPTemp;
break;
}
uv[0 + 0].x = tileTexInfo[tile].uOffset + sP1.x;
uv[0 + 0].y = tileTexInfo[tile].vOffset + sP1.y;
uv[0 + 2].x = tileTexInfo[tile].uOffset + sP2.x;
uv[0 + 2].y = tileTexInfo[tile].vOffset + sP2.y;
uv[1 + 2].x = tileTexInfo[tile].uOffset + sP3.x;
uv[1 + 2].y = tileTexInfo[tile].vOffset + sP3.y;
uv[1 + 0].x = tileTexInfo[tile].uOffset + sP4.x;
uv[1 + 0].y = tileTexInfo[tile].vOffset + sP4.y;
}
/// Average the four positions to get the center
static void averagePos(Vector3i *center, Vector3i *a, Vector3i *b, Vector3i *c, Vector3i *d)
{
center->x = (a->x + b->x + c->x + d->x)/4;
center->y = (a->y + b->y + c->y + d->y)/4;
center->z = (a->z + b->z + c->z + d->z)/4;
}
/// Is this position next to a water tile?
static bool isWater(int x, int y)
{
bool result = false;
result = result || (tileOnMap(x ,y ) && mapTile(x ,y )->ground == waterGroundType);
result = result || (tileOnMap(x+1,y ) && mapTile(x+1,y )->ground == waterGroundType);
result = result || (tileOnMap(x ,y+1) && mapTile(x ,y+1)->ground == waterGroundType);
result = result || (tileOnMap(x+1,y+1) && mapTile(x+1,y+1)->ground == waterGroundType);
return result;
}
/// Get the position of a grid point
static void getGridPos(Vector3i *result, int x, int y, bool center, bool water)
{
if (center)
{
Vector3i a,b,c,d;
getGridPos(&a, x , y , false, water);
getGridPos(&b, x+1, y , false, water);
getGridPos(&c, x , y+1, false, water);
getGridPos(&d, x+1, y+1, false, water);
averagePos(result, &a, &b, &c, &d);
return;
}
result->x = world_coord(x);
result->z = world_coord(-y);
if (x <= 0 || y <= 0 || x >= mapWidth || y >= mapHeight)
{
result->y = 0;
}
else
{
result->y = map_TileHeight(x, y);
if (water)
{
result->y = map_WaterHeight(x, y);
}
}
}
/// Calculate the average texture coordinates of 4 points
static inline void averageUV(Vector2f *center, Vector2f* uv)
{
center->x = (uv[0].x+uv[1].x+uv[2].x+uv[3].x)/4;
center->y = (uv[0].y+uv[1].y+uv[2].y+uv[3].y)/4;
}
/// Get the texture coordinates for the map position
static inline void getTexCoords(Vector2f *uv, float x, float y, int groundType)
{
uv->x = (x/psGroundTypes[groundType].textureSize);
uv->y = (y/psGroundTypes[groundType].textureSize);
}
/// Calculate the average colour of 4 points
static inline void averageColour(PIELIGHT *average, PIELIGHT a, PIELIGHT b,
PIELIGHT c, PIELIGHT d)
{
average->byte.a = (a.byte.a + b.byte.a + c.byte.a + d.byte.a)/4;
average->byte.r = (a.byte.r + b.byte.r + c.byte.r + d.byte.r)/4;
average->byte.g = (a.byte.g + b.byte.g + c.byte.g + d.byte.g)/4;
average->byte.b = (a.byte.b + b.byte.b + c.byte.b + d.byte.b)/4;
}
/// Get the tiles colour
static inline void getColour(PIELIGHT *colour, int x, int y, bool center)
{
MAPTILE *psTile;
if (center)
{
PIELIGHT a,b,c,d;
getColour(&a, x , y , false);
getColour(&b, x+1, y , false);
getColour(&c, x , y+1, false);
getColour(&d, x+1, y+1, false);
averageColour(colour, a, b, c, d);
return;
}
*colour = WZCOL_BLACK;
if (x < 0 || y < 0 || x > mapWidth - 1 || y > mapHeight - 1)
{
return;
}
psTile = mapTile(x, y);
*colour = psTile->colour;
if (psTile->tileInfoBits & BITS_GATEWAY && showGateways)
{
colour->byte.g = 255;
}
}
/**
* Set the terrain and water geometry for the specified sector
*/
static void setSectorGeometry(int x, int y,
RenderVertex *geometry, RenderVertex *water,
int *geometrySize, int *waterSize)
{
Vector3i pos;
int i,j;
for (i = 0; i < sectorSize+1; i++)
{
for (j = 0; j < sectorSize+1; j++)
{
// set up geometry
getGridPos(&pos, i+x*sectorSize, j+y*sectorSize, false, false);
geometry[*geometrySize].x = pos.x;
geometry[*geometrySize].y = pos.y;
geometry[*geometrySize].z = pos.z;
(*geometrySize)++;
getGridPos(&pos, i+x*sectorSize, j+y*sectorSize, true, false);
geometry[*geometrySize].x = pos.x;
geometry[*geometrySize].y = pos.y;
geometry[*geometrySize].z = pos.z;
(*geometrySize)++;
getGridPos(&pos, i+x*sectorSize, j+y*sectorSize, false, true);
water[*waterSize].x = pos.x;
water[*waterSize].y = pos.y;
water[*waterSize].z = pos.z;
(*waterSize)++;
getGridPos(&pos, i+x*sectorSize, j+y*sectorSize, true, true);
water[*waterSize].x = pos.x;
water[*waterSize].y = pos.y;
water[*waterSize].z = pos.z;
(*waterSize)++;
}
}
}
/**
* Set the decals for a sector. This takes care of both the geometry and the texture part.
*/
static void setSectorDecals(int x, int y,
DecalVertex *decaldata,
int *decalSize)
{
Vector3i pos;
Vector2f uv[2][2], center;
int a,b;
int i,j;
for (i = x*sectorSize; i < x*sectorSize+sectorSize; i++)
{
for (j = y*sectorSize; j < y*sectorSize+sectorSize; j++)
{
if (i < 0 || j < 0 || i >= mapWidth || j >= mapHeight)
{
continue;
}
if (TILE_HAS_DECAL(mapTile(i, j)))
{
getTileTexCoords(*uv, mapTile(i,j)->texture);
averageUV(¢er, *uv);
getGridPos(&pos, i, j, true, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = center.x;
decaldata[*decalSize].v = center.y;
(*decalSize)++;
a = 0; b = 1;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
a = 0; b = 0;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
getGridPos(&pos, i, j, true, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = center.x;
decaldata[*decalSize].v = center.y;
(*decalSize)++;
a = 1; b = 1;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
a = 0; b = 1;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
getGridPos(&pos, i, j, true, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = center.x;
decaldata[*decalSize].v = center.y;
(*decalSize)++;
a = 1; b = 0;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
a = 1; b = 1;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
getGridPos(&pos, i, j, true, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = center.x;
decaldata[*decalSize].v = center.y;
(*decalSize)++;
a = 0; b = 0;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
a = 1; b = 0;
getGridPos(&pos, i+a, j+b, false, false);
decaldata[*decalSize].x = pos.x;
decaldata[*decalSize].y = pos.y;
decaldata[*decalSize].z = pos.z;
decaldata[*decalSize].u = uv[a][b].x;
decaldata[*decalSize].v = uv[a][b].y;
(*decalSize)++;
}
}
}
}
/**
* Update the sector for when the terrain is changed.
*/
static void updateSectorGeometry(int x, int y)
{
RenderVertex *geometry;
RenderVertex *water;
DecalVertex *decaldata;
int geometrySize = 0;
int waterSize = 0;
int decalSize = 0;
geometry = (RenderVertex *)malloc(sizeof(RenderVertex)*sectors[x*ySectors + y].geometrySize);
water = (RenderVertex *)malloc(sizeof(RenderVertex)*sectors[x*ySectors + y].waterSize);
setSectorGeometry(x, y, geometry, water, &geometrySize, &waterSize);
ASSERT(geometrySize == sectors[x*ySectors + y].geometrySize, "something went seriously wrong updating the terrain");
ASSERT(waterSize == sectors[x*ySectors + y].waterSize , "something went seriously wrong updating the terrain");
glBindBuffer(GL_ARRAY_BUFFER, geometryVBO); glError();
glBufferSubData(GL_ARRAY_BUFFER, sizeof(RenderVertex)*sectors[x*ySectors + y].geometryOffset,
sizeof(RenderVertex)*sectors[x*ySectors + y].geometrySize, geometry); glError();
glBindBuffer(GL_ARRAY_BUFFER, waterVBO); glError();
glBufferSubData(GL_ARRAY_BUFFER, sizeof(RenderVertex)*sectors[x*ySectors + y].waterOffset,
sizeof(RenderVertex)*sectors[x*ySectors + y].waterSize, water); glError();
free(geometry);
free(water);
if (sectors[x*ySectors + y].decalSize <= 0)
{
// Nothing to do here, and glBufferSubData(GL_ARRAY_BUFFER, 0, 0, *) crashes in my graphics driver. Probably shouldn't crash...
return;
}
decaldata = (DecalVertex *)malloc(sizeof(DecalVertex)*sectors[x*ySectors + y].decalSize);
setSectorDecals(x, y, decaldata, &decalSize);
ASSERT(decalSize == sectors[x*ySectors + y].decalSize , "the amount of decals has changed");
glBindBuffer(GL_ARRAY_BUFFER, decalVBO); glError();
glBufferSubData(GL_ARRAY_BUFFER, sizeof(DecalVertex)*sectors[x*ySectors + y].decalOffset,
sizeof(DecalVertex)*sectors[x*ySectors + y].decalSize, decaldata); glError();
glBindBuffer(GL_ARRAY_BUFFER, 0);
free (decaldata);
glBindBuffer(GL_ARRAY_BUFFER, 0); // HACK Must unbind GL_ARRAY_BUFFER (don't know if it has to be unbound everywhere), otherwise text rendering may mysteriously crash.
}
/**
* Mark all tiles that are influenced by this grid point as dirty.
* Dirty sectors will later get updated by updateSectorGeometry.
*/
void markTileDirty(int i, int j)
{
int x, y;
if (!terrainInitalised)
{
return; // will be updated anyway
}
x = i/sectorSize;
y = j/sectorSize;
if (x < xSectors && y < ySectors) // could be on the lower or left edge of the map
{
sectors[x*ySectors + y].dirty = true;
}
// it could be on an edge, so update for all sectors it is in
if (x*sectorSize == i && x > 0)
{
if (x-1 < xSectors && y < ySectors)
{
sectors[(x-1)*ySectors + y].dirty = true;
}
}
if (y*sectorSize == j && y > 0)
{
if (x < xSectors && y-1 < ySectors)
{
sectors[x*ySectors + (y-1)].dirty = true;
}
}
if (x*sectorSize == i && x > 0 && y*sectorSize == j && y > 0)
{
if (x-1 < xSectors && y-1 < ySectors)
{
sectors[(x-1)*ySectors + (y-1)].dirty = true;
}
}
}
/**
* Check what the videocard + drivers support and divide the loaded map into sectors that can be drawn.
* It also determines the lightmap size.
*/
bool initTerrain(void)
{
int i, j, x, y, a, b, absX, absY;
PIELIGHT colour[2][2], centerColour;
int layer = 0;
RenderVertex *geometry;
RenderVertex *water;
DecalVertex *decaldata;
int geometrySize, geometryIndexSize;
int waterSize, waterIndexSize;
int textureSize, textureIndexSize;
GLuint *geometryIndex;
GLuint *waterIndex;
GLuint *textureIndex;
PIELIGHT *texture;
int decalSize;
int maxSectorSizeIndices, maxSectorSizeVertices;
bool decreasedSize = false;
// this information is useful to prevent crashes with buggy opengl implementations
glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &GLmaxElementsVertices);
glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &GLmaxElementsIndices);
// testing for crappy cards
debug(LOG_TERRAIN, "GL_MAX_ELEMENTS_VERTICES: %i", (int)GLmaxElementsVertices);
debug(LOG_TERRAIN, "GL_MAX_ELEMENTS_INDICES: %i", (int)GLmaxElementsIndices);
// now we know these values, determine the maximum sector size achievable
maxSectorSizeVertices = iSqrt(GLmaxElementsVertices/2)-1;
maxSectorSizeIndices = iSqrt(GLmaxElementsIndices/12);
debug(LOG_TERRAIN, "preferred sector size: %i", sectorSize);
debug(LOG_TERRAIN, "maximum sector size due to vertices: %i", maxSectorSizeVertices);
debug(LOG_TERRAIN, "maximum sector size due to indices: %i", maxSectorSizeIndices);
if (sectorSize > maxSectorSizeVertices)
{
sectorSize = maxSectorSizeVertices;
decreasedSize = true;
}
if (sectorSize > maxSectorSizeIndices)
{
sectorSize = maxSectorSizeIndices;
decreasedSize = true;
}
if (decreasedSize)
{
if (sectorSize < 1)
{
debug(LOG_WARNING, "GL_MAX_ELEMENTS_VERTICES: %i", (int)GLmaxElementsVertices);
debug(LOG_WARNING, "GL_MAX_ELEMENTS_INDICES: %i", (int)GLmaxElementsIndices);
debug(LOG_WARNING, "maximum sector size due to vertices: %i", maxSectorSizeVertices);
debug(LOG_WARNING, "maximum sector size due to indices: %i", maxSectorSizeIndices);
debug(LOG_ERROR, "Your graphics card and/or drivers do not seem to support glDrawRangeElements, needed for the terrain renderer.");
debug(LOG_ERROR, "- Do other 3D games work?");
debug(LOG_ERROR, "- Did you install the latest drivers correctly?");
debug(LOG_ERROR, "- Do you have a 3D window manager (Aero/Compiz) running?");
return false;
}
debug(LOG_WARNING, "decreasing sector size to %i to fit graphics card constraints", sectorSize);
}
// +4 = +1 for iHypot rounding, +1 for sector size rounding, +2 for edge of visibility
terrainDistance = iHypot(visibleTiles.x/2, visibleTiles.y/2)+4+sectorSize/2;
debug(LOG_TERRAIN, "visible tiles x:%i y: %i", visibleTiles.x, visibleTiles.y);
debug(LOG_TERRAIN, "terrain view distance: %i", terrainDistance);
/////////////////////
// Create the sectors
xSectors = (mapWidth +sectorSize-1)/sectorSize;
ySectors = (mapHeight+sectorSize-1)/sectorSize;
sectors = (Sector *)malloc(sizeof(Sector)*xSectors*ySectors);
////////////////////
// fill the geometry part of the sectors
geometry = (RenderVertex *)malloc(sizeof(RenderVertex)*xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2);
geometryIndex = (GLuint *)malloc(sizeof(GLuint)*xSectors*ySectors*sectorSize*sectorSize*12);
geometrySize = 0;
geometryIndexSize = 0;
water = (RenderVertex *)malloc(sizeof(RenderVertex)*xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2);
waterIndex = (GLuint *)malloc(sizeof(GLuint)*xSectors*ySectors*sectorSize*sectorSize*12);
waterSize = 0;
waterIndexSize = 0;
for (x = 0; x < xSectors; x++)
{
for (y = 0; y < ySectors; y++)
{
sectors[x*ySectors + y].dirty = false;
sectors[x*ySectors + y].geometryOffset = geometrySize;
sectors[x*ySectors + y].geometrySize = 0;
sectors[x*ySectors + y].waterOffset = waterSize;
sectors[x*ySectors + y].waterSize = 0;
setSectorGeometry(x, y, geometry, water, &geometrySize, &waterSize);
sectors[x*ySectors + y].geometrySize = geometrySize - sectors[x*ySectors + y].geometryOffset;
sectors[x*ySectors + y].waterSize = waterSize - sectors[x*ySectors + y].waterOffset;
// and do the index buffers
sectors[x*ySectors + y].geometryIndexOffset = geometryIndexSize;
sectors[x*ySectors + y].geometryIndexSize = 0;
sectors[x*ySectors + y].waterIndexOffset = waterIndexSize;
sectors[x*ySectors + y].waterIndexSize = 0;
for (i = 0; i < sectorSize; i++)
{
for (j = 0; j < sectorSize; j++)
{
if (x*sectorSize+i >= mapWidth || y*sectorSize+j >= mapHeight)
{
continue; // off map, so skip
}
/* One tile is composed of 4 triangles,
* we need _2_ vertices per tile (1)
* e.g. center and bottom left
* the other 3 vertices are from the adjacent tiles
* on their top and right.
* (1) The top row and right column of tiles need 4 vertices per tile
* because they do not have adjacent tiles on their top and right,
* that is why we add _1_ row and _1_ column to provide the geometry
* for these tiles.
* This is the source of the '*2' and '+1' in the index math below.
*/
#define q(i,j,center) ((x*ySectors+y)*(sectorSize+1)*(sectorSize+1)*2 + ((i)*(sectorSize+1)+(j))*2+(center))
// First triangle
geometryIndex[geometryIndexSize+0] = q(i ,j ,1); // Center vertex
geometryIndex[geometryIndexSize+1] = q(i ,j ,0); // Bottom left
geometryIndex[geometryIndexSize+2] = q(i+1,j ,0); // Bottom right
// Second triangle
geometryIndex[geometryIndexSize+3] = q(i ,j ,1); // Center vertex
geometryIndex[geometryIndexSize+4] = q(i ,j+1,0); // Top left
geometryIndex[geometryIndexSize+5] = q(i ,j ,0); // Bottom left
// Third triangle
geometryIndex[geometryIndexSize+6] = q(i ,j ,1); // Center vertex
geometryIndex[geometryIndexSize+7] = q(i+1,j+1,0); // Top right
geometryIndex[geometryIndexSize+8] = q(i ,j+1,0); // Top left
// Fourth triangle
geometryIndex[geometryIndexSize+9] = q(i ,j ,1); // Center vertex
geometryIndex[geometryIndexSize+10] = q(i+1,j ,0); // Bottom right
geometryIndex[geometryIndexSize+11] = q(i+1,j+1,0); // Top right
geometryIndexSize += 12;
if (isWater(i+x*sectorSize,j+y*sectorSize))
{
waterIndex[waterIndexSize+0] = q(i ,j ,1);
waterIndex[waterIndexSize+1] = q(i ,j ,0);
waterIndex[waterIndexSize+2] = q(i+1,j ,0);
waterIndex[waterIndexSize+3] = q(i ,j ,1);
waterIndex[waterIndexSize+4] = q(i ,j+1,0);
waterIndex[waterIndexSize+5] = q(i ,j ,0);
waterIndex[waterIndexSize+6] = q(i ,j ,1);
waterIndex[waterIndexSize+7] = q(i+1,j+1,0);
waterIndex[waterIndexSize+8] = q(i ,j+1,0);
waterIndex[waterIndexSize+9] = q(i ,j ,1);
waterIndex[waterIndexSize+10] = q(i+1,j ,0);
waterIndex[waterIndexSize+11] = q(i+1,j+1,0);
waterIndexSize += 12;
}
}
}
sectors[x*ySectors + y].geometryIndexSize = geometryIndexSize - sectors[x*ySectors + y].geometryIndexOffset;
sectors[x*ySectors + y].waterIndexSize = waterIndexSize - sectors[x*ySectors + y].waterIndexOffset;
}
}
glGenBuffers(1, &geometryVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, geometryVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(RenderVertex)*geometrySize, geometry, GL_DYNAMIC_DRAW); glError();
free(geometry);
glGenBuffers(1, &geometryIndexVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, geometryIndexVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint)*geometryIndexSize, geometryIndex, GL_STATIC_DRAW); glError();
free(geometryIndex);
glGenBuffers(1, &waterVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, waterVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(RenderVertex)*waterSize, water, GL_DYNAMIC_DRAW); glError();
free(water);
glGenBuffers(1, &waterIndexVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, waterIndexVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint)*waterIndexSize, waterIndex, GL_STATIC_DRAW); glError();
free(waterIndex);
glBindBuffer(GL_ARRAY_BUFFER, 0);
////////////////////
// fill the texture part of the sectors
texture = (PIELIGHT *)malloc(sizeof(PIELIGHT)*xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2*numGroundTypes);
textureIndex = (GLuint *)malloc(sizeof(GLuint)*xSectors*ySectors*sectorSize*sectorSize*12*numGroundTypes);
textureSize = 0;
textureIndexSize = 0;
for (layer = 0; layer < numGroundTypes; layer++)
{
for (x = 0; x < xSectors; x++)
{
for (y = 0; y < ySectors; y++)
{
if (layer == 0)
{
sectors[x*ySectors + y].textureOffset = (int *)malloc(sizeof(int)*numGroundTypes);
sectors[x*ySectors + y].textureSize = (int *)malloc(sizeof(int)*numGroundTypes);
sectors[x*ySectors + y].textureIndexOffset = (int *)malloc(sizeof(int)*numGroundTypes);
sectors[x*ySectors + y].textureIndexSize = (int *)malloc(sizeof(int)*numGroundTypes);
}
sectors[x*ySectors + y].textureOffset[layer] = textureSize;
sectors[x*ySectors + y].textureSize[layer] = 0;
sectors[x*ySectors + y].textureIndexOffset[layer] = textureIndexSize;
sectors[x*ySectors + y].textureIndexSize[layer] = 0;
//debug(LOG_WARNING, "offset when filling %i: %i", layer, xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2*layer);
for (i = 0; i < sectorSize+1; i++)
{
for (j = 0; j < sectorSize+1; j++)
{
bool draw = false;
bool off_map;
// set transparency
for (a=0;a<2;a++)
{
for(b=0;b<2;b++)
{
absX = x*sectorSize+i+a;
absY = y*sectorSize+j+b;
colour[a][b].rgba = 0x00FFFFFF; // transparent
// extend the terrain type for the bottom and left edges of the map
off_map = false;
if (absX == mapWidth)
{
off_map = true;
absX--;
}
if (absY == mapHeight)
{
off_map = true;
absY--;
}
if (absX < 0 || absY < 0 || absX >= mapWidth || absY >= mapHeight)
{
// not on the map, so don't draw
continue;
}
if (mapTile(absX,absY)->ground == layer)
{
colour[a][b].rgba = 0xFFFFFFFF;
if (!off_map)
{
// if this point lies on the edge is may not force this tile to be drawn
// otherwise this will give a bright line when fog is enabled
draw = true;
}
}
}
}
texture[xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2*layer+((x*ySectors+y)*(sectorSize+1)*(sectorSize+1)*2 + (i*(sectorSize+1)+j)*2)].rgba = colour[0][0].rgba;
averageColour(¢erColour, colour[0][0], colour[0][1], colour[1][0], colour[1][1]);
texture[xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2*layer+((x*ySectors+y)*(sectorSize+1)*(sectorSize+1)*2 + (i*(sectorSize+1)+j)*2+1)].rgba = centerColour.rgba;
textureSize += 2;
if ((draw) && i < sectorSize && j < sectorSize)
{
textureIndex[textureIndexSize+0] = q(i ,j ,1);
textureIndex[textureIndexSize+1] = q(i ,j ,0);
textureIndex[textureIndexSize+2] = q(i+1,j ,0);
textureIndex[textureIndexSize+3] = q(i ,j ,1);
textureIndex[textureIndexSize+4] = q(i ,j+1,0);
textureIndex[textureIndexSize+5] = q(i ,j ,0);
textureIndex[textureIndexSize+6] = q(i ,j ,1);
textureIndex[textureIndexSize+7] = q(i+1,j+1,0);
textureIndex[textureIndexSize+8] = q(i ,j+1,0);
textureIndex[textureIndexSize+9] = q(i ,j ,1);
textureIndex[textureIndexSize+10] = q(i+1,j ,0);
textureIndex[textureIndexSize+11] = q(i+1,j+1,0);
textureIndexSize += 12;
}
}
}
sectors[x*ySectors + y].textureSize[layer] = textureSize - sectors[x*ySectors + y].textureOffset[layer];
sectors[x*ySectors + y].textureIndexSize[layer] = textureIndexSize - sectors[x*ySectors + y].textureIndexOffset[layer];
}
}
}
glGenBuffers(1, &textureVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, textureVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(PIELIGHT)*xSectors*ySectors*(sectorSize+1)*(sectorSize+1)*2*numGroundTypes, texture, GL_STATIC_DRAW); glError();
free(texture);
glGenBuffers(1, &textureIndexVBO); glError();
glBindBuffer(GL_ARRAY_BUFFER, textureIndexVBO); glError();
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint)*textureIndexSize, textureIndex, GL_STATIC_DRAW); glError();
free(textureIndex);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// and finally the decals
decaldata = (DecalVertex *)malloc(sizeof(DecalVertex)*mapWidth*mapHeight*12);
decalSize = 0;