-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_terrain.c.old
1367 lines (1199 loc) · 29.4 KB
/
render_terrain.c.old
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <strings.h>
#include <assert.h>
#include <GL/gl.h>
#include "util.h"
#include "common.h"
#include "render.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define ALT_COLOUR 0
#define WIREFRAME 0
#define MAX_LOD 10
/*
* 0.5 = Interpolera jämt
* 1.0 = Interpolera aldrig
*/
#define THRESHOLD 0.8f
#define POP_STEPS 256
#define INTERPOLATION_SIZE 100000
/*
This structure is used for caching the calculated height for a given
position. The cache is partially filled during node_traverse, the
rest is done in find_top_usage and find_border_usage.
*/
typedef struct
{
unsigned short value;
short count;
heightmap_element_t *data;
int id;
} heightmap_cache_t;
typedef struct
{
GLfloat vertex[3];
int use;
} render_element_t;
static heightmap_element_t interpolation_arr[INTERPOLATION_SIZE];
static int interpolation_pos;
static GLfloat target_quality;
static GLfloat offset_x, offset_y;
static GLfloat coord_scale;
static t_node_t **current_node_arr;
static heightmap_element_t **heightmap;
static int heightmap_width;
static scene_t *current_scene;
static tile_t *current_tile;
typedef tile_t *tile_ptr;
static tile_ptr current_tile_arr[3][3];
static heightmap_cache_t *cache_arr;
static int cache_id=1;
void render_terrain_init( scene_t *s )
{
cache_arr=calloc( sizeof(heightmap_cache_t),
((1<<(s->lod_max+1))+1)*((1<<(s->lod_max+1))+1) );
}
/* Returns a pointer to a heightmap_element_t,
which is valid until the end of the current rendering pass
*/
static heightmap_element_t *get_temp_element()
{
assert( interpolation_pos < INTERPOLATION_SIZE );
return &interpolation_arr[interpolation_pos++];
}
static inline GLfloat get_dist( int id,
int level )
{
int node_width = 1<<level;
int node_scale = current_tile->lod-level-1;
int x_id = ((1|2*(id & (node_width-1)))<<node_scale);
int y_id = ((1|(id >> (level-1)))<<node_scale);
// GLfloat d = 0.75*(1<<(current_tile->lod-level+1));
GLfloat x = coord_scale*x_id+offset_x-current_scene->camera.pos[0];
GLfloat y = coord_scale*y_id+offset_y-current_scene->camera.pos[1];
/* GLfloat z = heightmap[current_tile->lod-1][(x_id+y_id*heightmap_width)].height -
current_scene->camera.pos[2];*/
return maxf( 1.0, sqrt(x*x + y*y));
}
/* The id:s of the children, in counterclockwise order, starting from the southeast (west?) corner */
static void get_children( int id,
int level,
int *child_id )
{
int mask = (1<<level)-1;
int x_id = (id & mask);
int y_id = id >> level;
int y_add = 2<<level;
int new_id = (x_id << 1)+(y_id<<(2+level));
child_id[0]=new_id;
child_id[1]=new_id + 1;
child_id[2]=new_id + 1 + y_add;
child_id[3]=new_id + y_add;
}
static void get_children_border( int id,
int level,
int border,
int *child_id )
{
int i;
int all_children[4];
get_children( id, level, all_children );
for( i=0; i<4; i++ )
assert( all_children[i] < 1000000 );
switch( border )
{
case NORTH:
child_id[0]=all_children[3];
child_id[1]=all_children[2];
break;
case WEST:
child_id[0]=all_children[0];
child_id[1]=all_children[3];
break;
case SOUTH:
child_id[0]=all_children[0];
child_id[1]=all_children[1];
break;
case EAST:
child_id[0]=all_children[2];
child_id[1]=all_children[1];
break;
default:
assert(0);
}
}
static inline void get_corners( int id,
int level,
int *pos )
{
int node_width = 1<<level;
int x_id = 1+2*(id & (node_width-1));
int y_id = 1+2*(id >> level);
int node_scale = current_tile->lod-level-1;
pos[0] = (x_id-1 + (y_id-1)*heightmap_width) << node_scale;
pos[1] = (x_id+1 + (y_id-1)*heightmap_width) << node_scale;
pos[2] = (x_id+1 + (y_id+1)*heightmap_width) << node_scale;
pos[3] = (x_id-1 + (y_id+1)*heightmap_width) << node_scale;
}
static void get_node_pos_internal( int id,
int level,
int *pos,
int *mid,
int target_lod )
{
int node_width = 1<<level;
int x_id = 1+2*(id & (node_width-1));
int y_id = 1+2*(id >> level);
int hm_width = (1<<(target_lod))+1;
int node_scale = target_lod-level-1;
int base = (x_id + (y_id)*hm_width)<<node_scale;
int x_diff = 1<<node_scale;
int y_diff = heightmap_width << node_scale;
pos[0] = base - x_diff;
pos[1] = base - x_diff - y_diff;
pos[2] = base - y_diff;
pos[3] = base + x_diff - y_diff;
pos[4] = base + x_diff;
pos[5] = base + x_diff + y_diff;
pos[6] = base + y_diff;
pos[7] = base - x_diff + y_diff;
pos[8] = pos[0];
*mid = base;
}
static void get_node_pos( int id,
int level,
int *pos,
int *mid )
{
get_node_pos_internal( id,
level,
pos,
mid,
current_tile->lod );
}
static void get_border_pos(int id,
int level,
int border,
int *pos )
{
int all_pos[9];
int mid_pos;
get_node_pos_internal( id,
level,
all_pos,
&mid_pos,
MAX_LOD );
switch( border )
{
case NORTH:
pos[0] = all_pos[7];
pos[1] = all_pos[6];
pos[2] = all_pos[5];
break;
case SOUTH:
pos[0] = all_pos[1];
pos[1] = all_pos[2];
pos[2] = all_pos[3];
break;
case WEST:
pos[0] = all_pos[1];
pos[1] = all_pos[0];
pos[2] = all_pos[7];
break;
case EAST:
pos[0] = all_pos[3];
pos[1] = all_pos[4];
pos[2] = all_pos[5];
break;
}
}
static inline int render_tile( int x, int y )
{
return 0;
}
static inline void interpolate_elements( heightmap_element_t *e1,
heightmap_element_t *e2,
GLfloat fraction_1,
heightmap_element_t *res )
{
int j;
GLfloat fraction_2 = 1.0f - fraction_1;
/*
for( j=0; j<3; j++ )
{
res->colour.vec[j] =
e1->colour.vec[j]*fraction_1+e2->colour.vec[j]*fraction_2;
}
*/
res->height=e1->height*fraction_1+e2->height*fraction_2;
}
static inline void interpolate_elements_2( heightmap_element_t *e1,
heightmap_element_t *e2,
heightmap_element_t *res )
{
unsigned int col1, col2;
res->height=0.5f*(e1->height+e2->height);
/*
col1 = (e1->colour.i&(~0x01010101))>>1;
col2 = (e2->colour.i&(~0x01010101))>>1;
res->colour.i=(col1 + col2);
*/
}
static inline void interpolate_r_elements_2( render_element_t *e1,
render_element_t *e2,
heightmap_element_t *res )
{
unsigned int col1, col2;
res->height=0.5f*(e1->vertex[2]+e2->vertex[2]);
/*
col1 = (e1->colour.i&(~0x01010101))>>1;
col2 = (e2->colour.i&(~0x01010101))>>1;
res->colour.i=(col1 + col2);
*/
}
static heightmap_element_t *get_element( int pos, int level, int str )
{
if( str == POP_STEPS )
{
int x,y, new_x, new_y;
int node_scale;
int lod_heightmap_width;
int new_pos;
if( level == current_tile->lod-1 )
{
return &heightmap[level][pos];
}
else
{
x = pos % heightmap_width;
y = pos / heightmap_width;
node_scale = current_tile->lod-level-1;
new_x = x>>node_scale;
new_y= y>>node_scale;
lod_heightmap_width=(1<<(level+1))+1;
new_pos = new_x+new_y*lod_heightmap_width;
return &heightmap[level][new_pos];
}
}
else
{
heightmap_element_t *high = get_element( pos,
level,
POP_STEPS);
heightmap_element_t *low = get_element( pos,
maxi(0,level-1),
POP_STEPS);
heightmap_element_t *res = get_temp_element();
GLfloat fraction_1 = (GLfloat)str*(1.0f/POP_STEPS);
interpolate_elements( high, low, fraction_1, res );
return res;
}
}
static heightmap_element_t *get_element_2( int pos,
int pos_x,
int pos_y,
int level,
int str )
{
if( str == POP_STEPS )
{
int new_x, new_y;
int node_scale;
int lod_heightmap_width;
int new_pos;
if( level == current_tile->lod-1 )
{
return &heightmap[level][pos];
}
else
{
node_scale = current_tile->lod-level-1;
new_x = pos_x>>node_scale;
new_y= pos_y>>node_scale;
lod_heightmap_width=(1<<(level+1))+1;
new_pos = new_x+new_y*lod_heightmap_width;
return &heightmap[level][new_pos];
}
}
else
{
heightmap_element_t *high = get_element( pos, level, POP_STEPS);
heightmap_element_t *low = get_element( pos, maxi(0,level-1), POP_STEPS);
heightmap_element_t *res = get_temp_element();
GLfloat fraction_1 = (GLfloat)str*(1.0f/POP_STEPS);
interpolate_elements( high, low, fraction_1, res );
return res;
}
}
static inline GLfloat get_quality( int id,
int level )
{
t_node_t *n=¤t_node_arr[level][id];
GLfloat l = n->dist;
GLfloat d = 1<<(current_scene->lod_max-level);
GLfloat c = current_scene->render_quality;
GLfloat divisor = d*c*n->d2;
GLfloat f = l/(divisor);
// printf( "%f %f %f %f\n", l, d, n->d2, f );
return f;
}
static inline int is_visible( int id,
int level )
{
int i, j;
int pos[9];
int mid_pos;
int visible;
t_node_t *n=¤t_node_arr[level][id];
n->dist = get_dist(id, level);
get_node_pos( id, level, pos, &mid_pos );
for( i=0; i<3; i++ ){
GLfloat k = current_scene->camera.k[i];
GLfloat m = current_scene->camera.m[i];
int side = current_scene->camera.side[i];
visible=0;
for( j=1; j<8; j+= 2 )
{
GLfloat node_x = coord_scale*(pos[j]%heightmap_width)+offset_x;
GLfloat node_y = coord_scale*(pos[j]/heightmap_width)+offset_y;
GLfloat proj_y = node_x*k + m;
if(i==2)
{
// printf("%f %f\n", proj_y, node_y );
}
if( side )
visible |= proj_y <= node_y ;
else
visible |= proj_y >= node_y ;
}
if( !visible )
{
return 0;
}
}
return 1;
}
static void traverse_node( int id,
int level,
float prev_f )
{
t_node_t *n = ¤t_node_arr[level][id];
int child_id[4];
float f;
int visible=is_visible( id, level );
// visible=1;
if( visible )
f = get_quality( id, level);
else
f = 2.0;
if( f>=1.0 )
{
n->render_strength = mini( POP_STEPS,
maxi( 1, (1.0f-prev_f) / THRESHOLD * POP_STEPS) );
}
else
{
int i;
get_children( id, level, child_id );
n->render_strength = 0;
for( i=0; i<4; i++ )
{
traverse_node( child_id[i], level+1, f );
}
}
}
static void cache_element( int pos,
heightmap_element_t *e,
int value,
int count )
{
heightmap_cache_t *c = &cache_arr[pos];
if( c->id != cache_id )
{
c->id=cache_id;
c->count=count;
c->value=value;
c->data=e;
}
else
{
c->count += count;
if( value < c->value )
{
c->value = value;
c->data = e;
}
}
}
static void cache_swipe()
{
cache_id++;
}
static void cache_leaf( int id,
int level,
int str)
{
int pos[9];
int mid;
int i;
int value;
value = (level<<8) + str;
get_node_pos( id, level, pos, &mid );
for( i=1; i<9; i+=2 )
{
cache_element( pos[i],
get_element( pos[i], level, str ),
value,
1 );
}
if( str == POP_STEPS )
{
for( i=0; i<8; i+=2 )
{
cache_element( pos[i],
get_element( pos[i], level, str ),
value,
2 );
}
}
else
{
for( i=0; i<8; i+=2 )
{
int id_1=(i+7)%8;
int id_2=i+1;
heightmap_element_t *org_1=get_element( pos[id_1],
maxi(level-1,0),
POP_STEPS );
heightmap_element_t *org_2=get_element( pos[id_2],
maxi(level-1,0),
POP_STEPS );
heightmap_element_t *res=get_temp_element();
interpolate_elements_2( org_1, org_2, res );
interpolate_elements( get_element( pos[i], level, POP_STEPS ),
res,
(GLfloat)str*(1.0f/POP_STEPS),
res );
cache_element( pos[i],
res,
value,
2 );
}
}
}
static void cache_node( int id,
int level )
{
t_node_t *n = ¤t_node_arr[level][id];
if( n->render_strength > 0 )
{
cache_leaf( id, level, n->render_strength );
}
else
{
int i;
int child_id[4];
get_children( id, level, child_id );
for( i=0; i<4; i++ )
{
cache_node( child_id[i], level+1 );
}
}
}
static void find_top_usage( int grid_x,
int grid_y,
int can_skip,
int insert_pos,
tile_t *search_tile )
{
static int top_usage_arr[256];
int i;
int arr_pos = 0, arr_pos_2 = 0;
int node_width, node_scale;
int x_id;
int y_id;
int child_id[4];
int id, level;
t_node_t *self;
top_usage_arr[arr_pos++]=0;
top_usage_arr[arr_pos++]=0;
// printf( "Söker efter koord %d %d\n", grid_x, grid_y );
// printf( "heightmap_width == %d\n", heightmap_width );
while( arr_pos!=arr_pos_2)
{
level = top_usage_arr[arr_pos_2++];
id = top_usage_arr[arr_pos_2++];
node_width = 1<<level;
node_scale = search_tile->lod-level-1;
x_id = (1|2*(id & (node_width-1)))<<node_scale;
y_id = (1|2*(id >> level))<<node_scale;
// printf( "Leker med nod %d %d (koord %d %d)\n", level, id, x_id, y_id );
self = ¤t_node_arr[level][id];
if( self->render_strength >0 )
{
int search_pos = grid_x + grid_y*heightmap_width;
int pos_1, pos_2;
int top, divisor;
GLfloat fraction;
int pos[9];
int mid_pos=-1;
heightmap_element_t *el;
heightmap_element_t *org_1;
heightmap_element_t *org_2;
/* if( self->render_strength == POP_STEPS )
{
cache_arr[insert_pos].count = 4;
arr_pos = arr_pos_2;
continue;
}
*/
get_node_pos( id, level, pos, &mid_pos );
if( pos[1]/heightmap_width == grid_y )
{
/* Interpolate along lower x-axis */
if( pos[2]%heightmap_width > grid_x )
{
pos_1 = pos[1];
pos_2 = pos[2];
}
else
{
pos_1 = pos[2];
pos_2 = pos[3];
}
}
else if( pos[5]/heightmap_width == grid_y )
{
/* interpolate along upper x-axis */
if( pos[6]%heightmap_width > grid_x )
{
pos_1 = pos[7];
pos_2 = pos[6];
}
else
{
pos_1 = pos[6];
pos_2 = pos[5];
}
}
else if( pos[1]%heightmap_width == grid_x )
{
/* Interpolate along left y-axis */
if( pos[0]/heightmap_width > grid_y )
{
pos_1 = pos[1];
pos_2 = pos[0];
}
else
{
pos_1 = pos[0];
pos_2 = pos[7];
}
}
else if( pos[5]%heightmap_width == grid_x )
{
/* Interpolate along right y-axis */
if( pos[4]/heightmap_width > grid_y )
{
pos_1 = pos[3];
pos_2 = pos[4];
}
else
{
pos_1 = pos[4];
pos_2 = pos[5];
}
}
else
{
printf( "Omöjlig interpolation! söker efter %d %d bland platserna:\n"
"(%d %d) (%d %d) (%d %d) (%d %d) (%d %d) (%d %d) (%d %d) (%d %d) \n",
grid_x, grid_y,
pos[0]%heightmap_width, pos[0]/heightmap_width,
pos[1]%heightmap_width, pos[1]/heightmap_width,
pos[2]%heightmap_width, pos[2]/heightmap_width,
pos[3]%heightmap_width, pos[3]/heightmap_width,
pos[4]%heightmap_width, pos[4]/heightmap_width,
pos[5]%heightmap_width, pos[5]/heightmap_width,
pos[6]%heightmap_width, pos[6]/heightmap_width,
pos[7]%heightmap_width, pos[7]/heightmap_width
);
exit(0);
}
top=search_pos-pos_1;
divisor=pos_2-pos_1;
if(( top == 0 ) ||(top==divisor))
{
continue;
}
// printf( "Löv värde %d\n", increment );
org_1 = get_element( pos_1, level, self->render_strength );
org_2 = get_element( pos_2, level, self->render_strength );
assert( org_1 != 0 );
assert( org_2 != 0 );
assert( top > 0 );
assert( top < divisor );
el = get_temp_element();
if( (top<<1)==divisor)
{
interpolate_elements_2( org_1, org_2, el );
}
else
{
fraction = ((GLfloat)top)/divisor;
interpolate_elements( org_1, org_2, fraction, el );
}
cache_element( insert_pos,
el,
(level<<8) + self->render_strength,
2 );
arr_pos = arr_pos_2;
}
else
{
int directions[4];
int dx, dy;
directions[0]=directions[1]=directions[2]=directions[3]=1;
dx = (grid_x-x_id);
dy = (grid_y-y_id);
// printf( "Inre\n" );
if( dx < 0 )
{
directions[1]=0;
directions[2]=0;
}
if( dx > 0 )
{
directions[0]=0;
directions[3]=0;
}
if( dy < 0 )
{
directions[3]=0;
directions[2]=0;
}
if( dy > 0 )
{
directions[0]=0;
directions[1]=0;
}
// printf( "dx dy = %d %d\n", dx, dy );
get_children( id, level, child_id );
for( i=0; i<4; i++ )
{
if( directions[i] )
{
// printf( "Lägger till %d %d\n", level+1, child_id[i] );
top_usage_arr[arr_pos++]=level+1;
top_usage_arr[arr_pos++]=child_id[i];
}
}
}
}
// cache_arr[insert_pos].count=4;
}
void find_border_usage( int grid_pos,
int border,
int can_skip,
int insert_pos,
tile_t *search_tile )
{
static int q[256];
int i;
int qpos = 0;
int qpos2 = 0;
int child_id[4];
int id, level;
int pos[3];
int value;
t_node_t *self;
q[qpos++]=0;
q[qpos++]=0;
/*
printf( "Searching for pos %d along border %d at tile %d %d\n",
grid_pos,
border,
search_tile->x,
search_tile->y );
*/
while( qpos!=qpos2)
{
int done = 0;
level = q[qpos2++];
id = q[qpos2++];
get_border_pos( id, level, search_tile->lod, pos );
// printf( "a %d %d\n", level, id );
self = &search_tile->node_arr[level][id];
value = (level<<8) + self->render_strength;
// printf( "Strength %d\n", self->render_strength );
if( self->render_strength > 0 )
{
// printf( "a1\n" );
for( i=0; i<3; i++ )
{
if( grid_pos == pos[i] )
{
cache_element( insert_pos,
get_element( pos[i],
level,
self->render_strength ),
value,
1 );
done = 1;
break;
}
}
if( !done )
{
// Jobbigt...
}
}
else
{
get_children_border( id, level, border, child_id );
// printf( "pushing childen with level %d, pos %d %d from parent %d %d\n", level+1, child_id[0], child_id[1], level, id );
if( grid_pos == pos[1] )
{
q[qpos++]=level+1;
q[qpos++]=child_id[0];
q[qpos++]=level+1;
q[qpos++]=child_id[1];
}
else
{
if( grid_pos > pos[1] )
{
q[qpos++]=level+1;
q[qpos++]=child_id[1];
}
else
{
q[qpos++]=level+1;
q[qpos++]=child_id[0];
}
}
}
}
// printf( "whee\n" );
}
static int calc_side( int orig_val,
int orig_lod )
{
return orig_val << (MAX_LOD-orig_lod);
}
static void get_top_usage_2( int pos,
int can_skip )
{
if( cache_arr[pos].id != cache_id || cache_arr[pos].count < 4 )
{
int x = pos % heightmap_width;
int y = pos / heightmap_width;
int max = heightmap_width-1;
if(( x==0 )&&(y==0))
{
cache_arr[pos].count =4;
return;
}
if( (x==max)&&(y==0))
{
cache_arr[pos].count =4;
return;
}
if( (x==0)&&(y==max))
{
cache_arr[pos].count =4;
return;
}
if( (x==max)&&(y==max))
{
cache_arr[pos].count =4;
return;
}
if( x==0 )
{
if( (current_tile_arr[0][1] != 0) )
{
if( (current_tile_arr[0][1]->state == TILE_READY) )
{
find_border_usage( calc_side( y,
current_tile_arr[1][1]->lod ),
EAST,
can_skip,
pos,
current_tile_arr[0][1] );
}
}
cache_arr[pos].count =4;
return;
}
if( x==max )
{
if( (current_tile_arr[2][1] != 0) )
{
if( (current_tile_arr[2][1]->state == TILE_READY) )
{
find_border_usage( calc_side( y,
current_tile_arr[1][1]->lod ),
WEST,
can_skip,
pos,
current_tile_arr[2][1] );
}
}
cache_arr[pos].count =4;
return;
}
if( y==0 )
{
if( (current_tile_arr[1][0] != 0) )
{
if( (current_tile_arr[1][0]->state == TILE_READY) )
{
find_border_usage( calc_side( x,
current_tile_arr[1][1]->lod ),
NORTH, //??
can_skip,
pos,
current_tile_arr[1][0] );
}
}
cache_arr[pos].count =4;
return;
}
if( y==max )
{
if( (current_tile_arr[1][2] != 0) )
{
if( (current_tile_arr[1][2]->state == TILE_READY) )
{
find_border_usage( calc_side( x,
current_tile_arr[1][1]->lod ),
SOUTH,//??
can_skip,
pos,