Source

Frontier / Terrain / CTerrain.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
/*-----------------------------------------------------------------------------

  CTerrain.cpp

-------------------------------------------------------------------------------

  This holds the terrain object class.

-----------------------------------------------------------------------------*/

#include "stdafx.h"
#include "cache.h"
#include "console.h"
#include "cterrain.h"
#include "render.h"
#include "scene.h"
#include "sdl.h"
#include "texture.h"


//Lower values make the terrain more precise at the expense of more polygons
#define TOLERANCE         0.08f
#define LAYERS            (sizeof (layers) / sizeof (LayerAttributes))

#define COMPILE_GRID      4
#define COMPILE_SIZE      (TERRAIN_SIZE / COMPILE_GRID)

static struct LayerAttributes
{
  unsigned      texture_frame;
  float         luminance;
  float         opacity;
  float         size;
  SurfaceType   surface;
  SurfaceColor   color;
} layers [] = 
{
  {7,     0.7f,  0.3f,   1.3f,  SURFACE_SAND,       SURFACE_COLOR_SAND},
  {7,     0.8f,  0.3f,   1.2f,  SURFACE_SAND,       SURFACE_COLOR_SAND},
  {7,     1.0f,  1.0f,   1.1f,  SURFACE_SAND,       SURFACE_COLOR_SAND},

  {7,     0.6f,  1.0f,   1.5f,  SURFACE_SAND_DARK,  SURFACE_COLOR_SAND},
  {4,    1.0f,  1.0f,   1.4f,   SURFACE_DIRT,       SURFACE_COLOR_DIRT},
  {4,    0.6f,  1.0f,   1.6f,   SURFACE_DIRT_DARK,  SURFACE_COLOR_DIRT},

  {3,  1.0f,  1.0f,   1.6f,     SURFACE_FOREST,     SURFACE_COLOR_DIRT},

  {6,   0.0f,  0.3f,   2.3f,    SURFACE_GRASS_EDGE, SURFACE_COLOR_GRASS},
  {6,   0.0f,  0.5f,   2.2f,    SURFACE_GRASS_EDGE, SURFACE_COLOR_GRASS},
  {6,   0.0f,  0.5f,   2.1f,    SURFACE_GRASS_EDGE, SURFACE_COLOR_GRASS},
  {5,   0.0f,  0.3f,   1.7f,    SURFACE_GRASS,      SURFACE_COLOR_GRASS},
  {5,   0.0f,  0.5f,   1.5f,    SURFACE_GRASS,      SURFACE_COLOR_GRASS},
  {5,   1.0f,  1.0f,   1.4f,    SURFACE_GRASS,      SURFACE_COLOR_GRASS},
  {6,   1.0f,  1.0f,   2.0f,    SURFACE_GRASS_EDGE, SURFACE_COLOR_GRASS},
  
  {2,    0.0f,  0.3f,   1.9f,   SURFACE_SNOW,       SURFACE_COLOR_SNOW},
  {2,    0.6f,  0.8f,   1.6f,   SURFACE_SNOW,       SURFACE_COLOR_SNOW},
  {2,    0.8f,  0.8f,   1.55f,  SURFACE_SNOW,       SURFACE_COLOR_SNOW},
  {2,    1.0f,  1.0f,   1.5f,   SURFACE_SNOW,       SURFACE_COLOR_SNOW}
};

static bool   bound_ready;
static int    boundary[TERRAIN_SIZE];

/*-----------------------------------------------------------------------------
  //This finds the largest power-of-two denominator for the given number.  This 
  //is used to determine what level of the quadtree a grid position occupies.  
-----------------------------------------------------------------------------*/

static int Boundary (int val)
{

  if (!bound_ready) {
    for (int n = 0; n < TERRAIN_SIZE; n++) {
      boundary[n] = -1;
      if (n == 0)
        boundary[n] = TERRAIN_SIZE;
      else {
        for (int level = TERRAIN_SIZE; level > 1; level /= 2) {
          if (!(n % level)) {
            boundary[n] =  level;
            break;
          }
        }
        if (boundary[n] == -1)
          boundary[n] = 1;
      }
    }
    bound_ready = true;
  }
  return boundary[val];

}

/*-----------------------------------------------------------------------------

-----------------------------------------------------------------------------*/

CTerrain::CTerrain ()
{

  //Call parent constructor
  GridData ();

}


void CTerrain::DoPatch (int patch_z, int patch_y)
{

  float       tile;
  int         x, y;
  int         world_x, world_y;
  GLvector    pos;
  int         stage ;
  GLrgba      col;
  SurfaceType surface;
  int         angle;
  GLrgba      surface_color;
  GLcoord     start, end;
  GLuvbox     uvb;
  GLvector2   uv;

  glDisable (GL_CULL_FACE);
  glDisable (GL_FOG);
  glEnable (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);	
  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
  if (_patch_steps > 1) {
    int     texture_step = TERRAIN_SIZE / _patch_steps;
    start.x = _walk.x * texture_step - 3;
    start.y = _walk.y * texture_step - 3;
    end.x = start.x + texture_step + 5;
    end.y = start.y + texture_step + 6;
  } else {
    start.x = start.y = -2;
    end.x = end.y = TERRAIN_EDGE + 2;
  }
  glBindTexture (GL_TEXTURE_2D, TextureIdFromName ("terrain_rock.png"));
	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
  for (y = start.y; y < end.y - 1; y++) {
    glBegin (GL_QUAD_STRIP);
    for (x = start.x; x < end.x; x++) {
      world_x = _origin.x + x;
      world_y = _origin.y + y;
      glTexCoord2f ((float)x / 8, (float)y / 8);
      surface_color = CacheSurfaceColor (world_x, world_y);
      glColor3fv (&surface_color.red);
      glVertex2f ((float)x, (float)y);
      glTexCoord2f ((float)x / 8, (float)(y + 1) / 8);
      surface_color = CacheSurfaceColor (world_x, world_y + 1);
      glColor3fv (&surface_color.red);
      glVertex2f ((float)x, (float)(y + 1));
    }
    glEnd ();
  }
  for (stage = 0; stage < LAYERS; stage++) {
    //Special layer to give the sand & rock some more depth
    if (stage == 3) {
      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glColor4f (1,1,1, 0.5f);
      glColor3f (1,1,1);
      glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR);
      glBindTexture (GL_TEXTURE_2D, TextureIdFromName ("terrain_shading.png"));
	    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
      glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
      glBegin (GL_QUADS);
      glTexCoord2f (0, 0); glVertex2i (0, 0);
      glTexCoord2f (0, 2); glVertex2i (TERRAIN_SIZE, 0);
      glTexCoord2f (2, 2); glVertex2i (TERRAIN_SIZE, TERRAIN_SIZE);
      glTexCoord2f (2, 0); glVertex2i (0, TERRAIN_SIZE);
      glEnd ();
      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }
    if (!_surface_used[layers[stage].surface])
      continue;
    glBindTexture (GL_TEXTURE_2D, TextureIdFromName ("terrain.png"));
    uvb.Set (0, layers[stage].texture_frame, 1, 8);
	  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
    for (y = start.y; y < end.y - 1; y++) {
      for (x = start.x; x < end.x; x++) {
        world_x = _origin.x + x;
        world_y = _origin.y + y;
        surface = CacheSurface (world_x, world_y);
        if (surface != layers[stage].surface)
          continue;
        pos.x = (float)x;
        pos.y = (float)y;
        tile = 0.66f * layers[stage].size; 
        glPushMatrix ();
        glTranslatef (pos.x - 0.5f, pos.y - 0.5f, 0);
        angle = (world_x + world_y * 2) * 25;
        angle %= 360;
        glRotatef ((float)angle, 0.0f, 0.0f, 1.0f);
        glTranslatef (-pos.x, -pos.y, 0);
        if (layers[stage].color == SURFACE_COLOR_BLACK)
          surface_color = glRgba (0.0f);
        else
          surface_color = CacheSurfaceColor (world_x, world_y);
        col = surface_color * layers[stage].luminance;
        col.alpha = layers[stage].opacity;
        glColor4fv (&col.red);
        glBegin (GL_QUADS);
        uv = uvb.Corner (0); glTexCoord2fv (&uv.x); glVertex2f (pos.x - tile, pos.y - tile);
        uv = uvb.Corner (1); glTexCoord2fv (&uv.x); glVertex2f (pos.x + tile, pos.y - tile);
        uv = uvb.Corner (2); glTexCoord2fv (&uv.x); glVertex2f (pos.x + tile, pos.y + tile);
        uv = uvb.Corner (3); glTexCoord2fv (&uv.x); glVertex2f (pos.x - tile, pos.y + tile);
        glEnd ();
        glPopMatrix ();
      }
    }
  }




}

void CTerrain::DoTexture ()
{



  if (!_back_texture) {
    glGenTextures (1, &_back_texture); 
    glBindTexture(GL_TEXTURE_2D, _back_texture);
 	  glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	
    //We draw the terrain texture in squares called patches, but how big should they be?
    //We can't draw more than will fit in the viewport
    _patch_size = min (RenderMaxDimension (), _texture_desired_size);
    _patch_steps = _texture_desired_size / _patch_size;
    //We also don't want to do much at once. Walking a 128x128 grid in a singe frame creates stuttering. 
    while (TERRAIN_SIZE / _patch_steps > 32) {
      _patch_size /= 2;
      _patch_steps = _texture_desired_size / _patch_size;
    }
    //_patch_steps = max (_patch_steps, 1);//Avoid div by zero. Trust me, it's bad.
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, _texture_desired_size, _texture_desired_size, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  }
  RenderCanvasBegin (_walk.x * TERRAIN_PATCH, _walk.x * TERRAIN_PATCH + TERRAIN_PATCH, _walk.y * TERRAIN_PATCH, _walk.y * TERRAIN_PATCH + TERRAIN_PATCH, _patch_size);
  DoPatch (_walk.x, _walk.y);
  glBindTexture(GL_TEXTURE_2D, _back_texture);
  glCopyTexSubImage2D (GL_TEXTURE_2D, 0, _walk.x * _patch_size, _walk.y * _patch_size, 0, 0, _patch_size, _patch_size);
  RenderCanvasEnd ();
  if (_walk.Walk (_patch_steps))
    _stage++;
    
}

void CTerrain::DoHeightmap ()
{

  GLcoord         world;
  GLvector        pos;

  world.x = _origin.x + _walk.x;
  world.y = _origin.y + _walk.y;
  _surface_used[CacheSurface (world.x, world.y)] = true;
  pos.x = (float)world.x;
  pos.y = (float)world.y;
  pos.z = CacheElevation (world.x, world.y);
  _pos[_walk.x][_walk.y] = pos;
  if (_walk.Walk (TERRAIN_EDGE))
    _stage++;

}

/*-----------------------------------------------------------------------------

  In order to avoid having gaps between adjacent terrains, we have to "stitch"
  them togather.  We analyze the points used along the shared edge, and
  activate any points used by our neighbor.  

-----------------------------------------------------------------------------*/

void CTerrain::DoStitch ()
{

  int          ii;
  CTerrain*    e;
  CTerrain*    s;
  CTerrain*    w;
  CTerrain*    n;
  int          b;

  w = SceneTerrainGet (_grid_position.x - 1, _grid_position.y);
  e = SceneTerrainGet (_grid_position.x + 1, _grid_position.y);
  s = SceneTerrainGet (_grid_position.x, _grid_position.y + 1);
  n = SceneTerrainGet (_grid_position.x, _grid_position.y - 1);
  for (ii = 0; ii < TERRAIN_EDGE; ii++) {
    b = Boundary (ii);
    if (w && w->Point (TERRAIN_SIZE, ii)) {
      PointActivate (0, ii);
      PointActivate (b, ii);
    }
    if (e && e->Point (0, ii)) {
      PointActivate (TERRAIN_SIZE - b, ii);
      PointActivate (TERRAIN_SIZE, ii);
    }
    if (s && s->Point (ii, 0)) {
      PointActivate (ii, TERRAIN_SIZE);
      PointActivate (ii, TERRAIN_SIZE - b);
    }
    if (n && n->Point (ii, TERRAIN_SIZE)) {
      PointActivate (ii, b);
      PointActivate (ii, 0);
    }
  }
  //Now save a snapshot of how many points our neighbors are using. 
  //If these change, they have added detail and we'll need to re-stitch.
  for (ii = 0; ii < NEIGHBOR_COUNT; ii++)
    _neighbors[ii] = 0;
  if (w)
    _neighbors[NEIGHBOR_WEST] = w->Points ();
  if (e)
    _neighbors[NEIGHBOR_EAST] = e->Points ();
  if (n)
    _neighbors[NEIGHBOR_NORTH] = n->Points ();
  if (s)
    _neighbors[NEIGHBOR_SOUTH] = s->Points ();

}


/*-----------------------------------------------------------------------------

  Look at our neighbors and see if they have added detail since our last 
  rebuild.

-----------------------------------------------------------------------------*/

bool CTerrain::DoCheckNeighbors ()
{

  CTerrain*    e;
  CTerrain*    s;
  CTerrain*    w;
  CTerrain*    n;

  w = SceneTerrainGet (_grid_position.x - 1, _grid_position.y);
  e = SceneTerrainGet (_grid_position.x + 1, _grid_position.y);
  s = SceneTerrainGet (_grid_position.x, _grid_position.y + 1);
  n = SceneTerrainGet (_grid_position.x, _grid_position.y - 1);
  if (w && w->Points () != _neighbors[NEIGHBOR_WEST])
    return true;
  if (s && s->Points () != _neighbors[NEIGHBOR_SOUTH])
    return true;
  if (e && e->Points () != _neighbors[NEIGHBOR_EAST])
    return true;
  if (n && n->Points () != _neighbors[NEIGHBOR_NORTH])
    return true;
  return false;

}

/*-----------------------------------------------------------------------------
This is tricky stuff.  When this is called, it means the given point is needed
for the terrain we are working on.  Each point, when activated, will recusivly 
require two other points at the next lowest level of detail.  This is what 
causes the "shattering" effect that breaks the terrain into triangles.  
If you want to know more, Google for Peter Lindstrom, the inventor of this 
very clever system.  
-----------------------------------------------------------------------------*/

void CTerrain::PointActivate (int x, int y)
{

  int           xl;
  int           yl;
  int           level;

  if (x < 0 || x > TERRAIN_SIZE || y < 0 || y > TERRAIN_SIZE)
    return;
  if (Point (x,y))
    return;
  _point[x][y] = true;
  xl = Boundary (x);
  yl = Boundary (y);
  level = min (xl, yl);
  if (xl > yl) {
    PointActivate (x - level, y);
    PointActivate (x + level, y);
  } else if (xl < yl) {
    PointActivate (x, y + level);
    PointActivate (x, y - level);
  } else {
    int     x2;
    int     y2;

    x2 = x & (level * 2);
    y2 = y & (level * 2);
    if (x2 == y2) {
      PointActivate (x - level, y + level);
      PointActivate (x + level, y - level);
    } else {
      PointActivate (x + level, y + level);
      PointActivate (x - level, y - level);
    }
  }

}

/*-----------------------------------------------------------------------------

            upper         
         ul-------ur        
          |\      |      
         l| \     |r     
         e|  \    |i      
         f|   c   |g    
         t|    \  |h         
          |     \ |t         
          |      \|          
         ll-------lr         
            lower            

This considers a quad for splitting. This is done by looking to see how 
coplanar the quad is.  The elevation of the corners are averaged, and compared 
to the elevation of the center.  The geater the difference between these two 
values, the more non-coplanar this quad is.
-----------------------------------------------------------------------------*/

void CTerrain::DoQuad (int x1, int y1, int size)
{

  int       xc, yc, x2, y2;
  int       half;
  float     ul, ur, ll, lr, center;
  float     average; 
  float     delta;

  half = size / 2;
  xc = x1 + half;   x2 = x1 + size;
  yc = y1 + half;   y2 = y1 + size;
  if (x2 > TERRAIN_SIZE || y2 > TERRAIN_SIZE || x1 < 0 || y1 < 0)
    return;
  ul = _pos[x1][y1].z;
  ur = _pos[x2][y1].z;
  ll = _pos[x1][y2].z;
  lr = _pos[x2][y2].z;
  center = _pos[xc][yc].z;
  average = (ul + lr + ll + ur) / 4.0f;
  //look for a delta between the center point and the average elevation
  delta = abs (average - center);
  //scale the delta based on the size of the quad we are dealing with
  delta /= (float)size;
  if (delta > TOLERANCE)
    PointActivate (xc, yc);

}

void CTerrain::TrianglePush (int i1, int i2, int i3)
{

  _index_buffer.push_back (i1);
  _index_buffer.push_back (i2);
  _index_buffer.push_back (i3);

}

/*-----------------------------------------------------------------------------
                          North                 N    
    *-------*           *---+---*           *---*---*     *---+---*
    |\      |           |\     /|           |\Nl|Nr/|     |   |   |
    | \ Sup |           | \   / |           | \ | / |     | A | B |
    |  \    |           |  \ /  |           |Wr\|/El|     |   |   |
    |   \   |       West+   *   +East      W*---*---*E    *---+---*   
    |    \  |           |  / \  |           |Wl/|\Er|     |   |   |
    | Inf \ |           | /   \ |           | / | \ |     | C | D |
    |      \|           |/     \|           |/Sr|Sl\|     |   |   |
    *-------*           *---+---*           *---*---*     *---*---*
                          South                 S      

    Figure a            Figure b            Figure c      Figure d

This takes a single quadtree block and decides how to divide it for rendering. 
If the center point in not included in the mesh (or if there IS no center 
because we are at the lowest level of the tree), then the block is simply 
cut into two triangles. (Figure a)

If the center point is active, but none of the edges, the block is cut into
four triangles.  (Fig. b)  If the edges are active, then the block is cut 
into a combination of smaller triangles (Fig. c) and sub-blocks (Fig. d).   

-----------------------------------------------------------------------------*/

void CTerrain::CompileBlock (int x, int y, int size)
{

  int     x2;
  int     y2;
  int     xc;
  int     yc;
  int     next_size;
  int     n0, n1, n2, n3, n4, n5, n6, n7, n8;

  //Define the shape of this block.  x and y are the upper-left (Northwest)
  //origin, xc and yc define the center, and x2, y2 mark the lower-right 
  //(Southeast) corner, and next_size is half the size of this block.
  next_size = size / 2;
  x2 = x + size;
  y2 = y + size;
  xc = x + next_size;
  yc = y + next_size;
  /*    n0--n1--n2
        |        |
        n3  n4  n5
        |        |
        n6--n7--n8    */
  n0 = _index_map[x][y];
  n1 = _index_map[xc][y];
  n2 = _index_map[x2][y];
  n3 = _index_map[x][yc];
  n4 = _index_map[xc][yc];
  n5 = _index_map[x2][yc];
  n6 = _index_map[x][y2];
  n7 = _index_map[xc][y2];
  n8 = _index_map[x2][y2];
  //If this is the smallest block, or the center is inactive, then just
  //Cut into two triangles as shown in Figure a
  if (size == 1 || !Point (xc, yc)) {
    if ((x / size + y / size) % 2) {
      TrianglePush (n0, n8, n2);
      TrianglePush (n0, n6, n8);
    } else {
      TrianglePush (n0, n6, n2);
      TrianglePush (n2, n6, n8);
    }
    return;
  } 
  //if the edges are inactive, we need 4 triangles (fig b)
  if (!Point (xc, y) && !Point (xc, y2) && !Point (x, yc) && !Point (x2, yc)) {
      TrianglePush (n0, n4, n2);//North
      TrianglePush (n2, n4, n8);//East
      TrianglePush (n8, n4, n6);//South
      TrianglePush (n6, n4, n0);//West
      return;
  }
  //if the top & bottom edges are inactive, it is impossible to have 
  //sub-blocks.
  if (!Point (xc, y) && !Point (xc, y2)) {
    TrianglePush (n0, n4, n2);//North
    TrianglePush (n8, n4, n6);//South
    if (Point (x, yc)) {
      TrianglePush (n3, n4, n0);//Wr
      TrianglePush (n6, n4, n3);//Wl
    } else 
      TrianglePush (n6, n4, n0);//West
    if (Point (x2, yc)) {
      TrianglePush (n2, n4, n5);//El
      TrianglePush (n5, n4, n8);//Er
    } else 
      TrianglePush (n2, n4, n8);//East
    return;
  }
  
  //if the left & right edges are inactive, it is impossible to have 
  //sub-blocks.
  if (!Point (x, yc) && !Point (x2, yc)) {
    TrianglePush (n2, n4, n8);//East
    TrianglePush (n6, n4, n0);//West
    if (Point (xc, y)) {
      TrianglePush (n0, n4, n1);//Nl
      TrianglePush (n1, n4, n2);//Nr
    } else
      TrianglePush (n0, n4, n2);//North
    if (Point (xc, y2)) {
      TrianglePush (n7, n4, n6);//Sr
      TrianglePush (n8, n4, n7);//Sl
    } else
    TrianglePush (n8, n4, n6);//South
    return;
  }
  //none of the other tests worked, which means this block is a combination 
  //of triangles and sub-blocks. Brace yourself, this is not for the timid.
  //the first step is to find out which triangles we need
  if (!Point (xc, y)) {  //is the top edge inactive?
    TrianglePush (n0, n4, n2);//North
    if (Point (x, yc))
      TrianglePush (n3, n4, n0);//Wr
    if (Point (x2, yc))
      TrianglePush (n2, n4, n5);//El
  }
  if (!Point (xc, y2)) {//is the bottom edge inactive?
    TrianglePush (n8, n4, n6);//South
    if (Point (x, yc))
      TrianglePush (n6, n4, n3);//Wl
    if (Point (x2, yc)) 
      TrianglePush (n5, n4, n8);//Er
  }
  if (!Point (x, yc)) {//is the left edge inactive?
    TrianglePush (n6, n4, n0);//West
    if (Point (xc, y))
      TrianglePush (n0, n4, n1);//Nl
    if (Point (xc, y2)) 
      TrianglePush (n7, n4, n6);//Sr
  }
  if (!Point (x2, yc)) {//is the right edge inactive?
    TrianglePush (n2, n4, n8);//East
    if (Point (xc, y))
      TrianglePush (n1, n4, n2);//Nr
    if (Point (xc, y2)) 
      TrianglePush (n8, n4, n7);//Sl
  }
  //now that the various triangles have been added, we add the 
  //various sub-blocks.  This is recursive.
  if (Point (xc, y) && Point (x, yc)) 
    CompileBlock (x, y, next_size); //Sub-block A
  if (Point (xc, y) && Point (x2, yc)) 
    CompileBlock (x + next_size, y, next_size); //Sub-block B
  if (Point (x, yc) && Point (xc, y2)) 
    CompileBlock (x, y + next_size, next_size); //Sub-block C
  if (Point (x2, yc) && Point (xc, y2)) 
    CompileBlock (x + next_size, y + next_size, next_size); //Sub-block D

}

/*-----------------------------------------------------------------------------
  This checks the four corners of zone data that will be used by this terrain.
  Returns true if the data is ready and terrain building can proceed. This
  will also "touch" the zone, letting the zone know it's still in use.
-----------------------------------------------------------------------------*/

bool CTerrain::ZoneCheck (long stop)
{

  //If we're waiting on a zone, give it our update allotment
  if (!CachePointAvailable (_origin.x, _origin.y)) {
    CacheUpdatePage (_origin.x, _origin.y, stop);
    return false;
  }
  if (!CachePointAvailable (_origin.x + TERRAIN_EDGE, _origin.y + TERRAIN_EDGE)) {
    CacheUpdatePage (_origin.x + TERRAIN_EDGE, _origin.y + TERRAIN_EDGE, stop);
    return false;
  }
  if (!CachePointAvailable (_origin.x + TERRAIN_EDGE, _origin.y)) {
    CacheUpdatePage (_origin.x + TERRAIN_EDGE, _origin.y, stop);
    return false;
  }
  if (!CachePointAvailable (_origin.x, _origin.y + TERRAIN_EDGE)) {
    CacheUpdatePage (_origin.x, _origin.y + TERRAIN_EDGE, stop);
    return false;
  }
  return true;

}


void CTerrain::Update (long stop)
{

  while (SdlTick () < stop) {
    switch (_stage) {
    case STAGE_BEGIN: 
      if (!ZoneCheck (stop)) 
        break;
      for (int i =0; i < SURFACE_TYPES; i++)
        _surface_used[i] = false;
      for (int i =0; i < NEIGHBOR_COUNT; i++)
        _neighbors[i] = 0;
      _walk.Clear ();
      _rebuild = SdlTick ();
      _stage++;
      break;
    case STAGE_CLEAR: 
      _point[_walk.x][_walk.y] = false;
      _index_map[_walk.x][_walk.y] = -1;
      if (_walk.Walk (TERRAIN_EDGE))
        _stage++;
      break;
    case STAGE_DO_COMPILE_GRID: 
      PointActivate (_walk.x * COMPILE_SIZE, _walk.y * COMPILE_SIZE);
      if (_walk.Walk (COMPILE_GRID + 1))
        _stage++;
      break;
    case STAGE_HEIGHTMAP: 
      DoHeightmap ();
      break;
    case STAGE_QUADTREE:
      if (!Point (_walk.x, _walk.y)) {
        int   xx, yy, level;

        xx = Boundary (_walk.x);
        yy = Boundary (_walk.y);
        level = min (xx, yy);
        DoQuad (_walk.x - level, _walk.y - level, level * 2);
      }
      if (_walk.Walk (TERRAIN_SIZE))
        _stage++;
      break;  
    case STAGE_STITCH:
      DoStitch ();
      _vertex_list.clear ();
      _normal_list.clear ();
      _uv_list.clear ();
      _index_buffer.clear ();
      _stage++;
      break;  
    case STAGE_BUFFER_LOAD: 
      if (Point (_walk.x, _walk.y)) {
        GLcoord world;
        
        world.x = _origin.x + _walk.x;
        world.y = _origin.y + _walk.y;
        _index_map[_walk.x][_walk.y] = _vertex_list.size ();
        _vertex_list.push_back (_pos[_walk.x][_walk.y]);
        _normal_list.push_back (CacheNormal (world.x, world.y));
        _uv_list.push_back (glVector ((float)_walk.x / TERRAIN_SIZE, (float)_walk.y / TERRAIN_SIZE));
        //_list_pos++;
      }
      if (_walk.Walk (TERRAIN_EDGE))
        _stage++;
      break; 
    case STAGE_COMPILE:
      CompileBlock (_walk.x * COMPILE_SIZE, _walk.y * COMPILE_SIZE, COMPILE_SIZE);
      if (_walk.Walk (COMPILE_GRID))
        _stage++;
      break;
    case STAGE_VBO:
      if (_vbo.Ready ())
        _vbo.Clear ();
      _vbo.Create (GL_TRIANGLES, _index_buffer.size (), _vertex_list.size (), &_index_buffer[0], &_vertex_list[0], &_normal_list[0], NULL, &_uv_list[0]);
      _stage++;
      break;
    case STAGE_TEXTURE: 
      if (_texture_current_size == _texture_desired_size) {
        _stage = STAGE_DONE;
        break;
      }
      DoTexture ();
      break;
    case STAGE_TEXTURE_FINAL: 
      if (_front_texture) 
        glDeleteTextures (1, &_front_texture); 
      _front_texture = _back_texture;
      _back_texture = 0;
      _texture_current_size = _texture_desired_size;
      _stage++;
      break;
    case STAGE_DONE:
      _valid = true;
      if (SdlTick () < _rebuild) 
        return;
      ZoneCheck (stop);//touch the zones to keep them in memory
      _rebuild = SdlTick () + 1000;
      if (_lod == LOD_HIGH && DoCheckNeighbors ())
        _stage = STAGE_QUADTREE;
      return;
    default: //any stages not used end up here, skip it
      _stage++;
      break;

    }
  }

}


/*-----------------------------------------------------------------------------
  De-allocate and cleanup 
-----------------------------------------------------------------------------*/


void CTerrain::Clear ()
{

  if (_front_texture) 
    glDeleteTextures (1, &_front_texture); 
  if (_back_texture) 
    glDeleteTextures (1, &_back_texture); 
  _front_texture = 0;
  _back_texture = 0;
  _stage = STAGE_BEGIN;
  _texture_current_size = 0;
  _vertex_list.clear ();
  _normal_list.clear ();
  _uv_list.clear ();
  _index_buffer.clear ();
  _walk.Clear ();

}

void CTerrain::TextureSize (int size)
{

  //We can't resize in the middle of rendering the texture
  if (_stage == STAGE_TEXTURE || _stage == STAGE_TEXTURE_FINAL)
    return;
  if (size != _texture_current_size) {
    _texture_desired_size = size;
    if (_stage == STAGE_DONE)
      _stage = STAGE_TEXTURE;
  }

}

GLcoord CTerrain::Origin ()
{

  return _origin;

}


void CTerrain::TexturePurge ()
{

  if (_front_texture) 
    glDeleteTextures (1, &_front_texture); 
  if (_back_texture) 
    glDeleteTextures (1, &_back_texture); 
  _front_texture = 0;
  _back_texture = 0;
  _texture_current_size = 0;
  _texture_desired_size = 64;
  if (_stage >= STAGE_TEXTURE) {
    _stage = STAGE_TEXTURE;
    _walk.Clear ();
  }

}

void CTerrain::Set (int grid_x, int grid_y, int distance)
{

  LOD       new_lod;

  if (_stage == STAGE_TEXTURE)
    return;
  if (distance < 2) 
    new_lod = LOD_HIGH;
  else
    new_lod = LOD_LOW;
  if (grid_x == _grid_position.x && grid_y == _grid_position.y && _lod == new_lod)
    return;
  //If this terrain is now in a new location, we have to kill it entirely
  if (grid_x != _grid_position.x || grid_y != _grid_position.y) 
    Clear ();
  else //Just changed LOD, rebuild the texture
    _stage = STAGE_TEXTURE;
  _lod = new_lod;
  _grid_position.x = grid_x;
  _grid_position.y = grid_y;
  _current_distance = distance;
  _origin.x = grid_x * TERRAIN_SIZE;
  _origin.y = grid_y * TERRAIN_SIZE;
  _color = glRgbaUnique (_grid_position.x + _grid_position.y * 16);
  if (_lod == LOD_HIGH) {
    _color = glRgba (1.0f, 0.0f, 1.0f);
    _texture_desired_size = 2048;
  } else {
    _color = glRgba (0.0f, 1.0f, 1.0f);
    _texture_desired_size = 128;
  }
  //ConsoleLog ("Texture: %d, %d = %d", grid_x, grid_y, _texture_desired_size);
  _walk.Clear ();

}

void CTerrain::Render ()
{

  if (_front_texture && _valid) {
    //glColor3fv (&_color.red);
    glBindTexture (GL_TEXTURE_2D, _front_texture);
    _vbo.Render ();
  }

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.