File: database.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (522 lines) | stat: -rw-r--r-- 16,224 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

/*
 * database.c   $Revision: 1.2 $
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "skyfly.h"

#if defined(_WIN32)
#pragma warning (disable:4244)  /* disable bogus conversion warnings */
#pragma warning (disable:4305)  /* VC++ 5.0 version of above warning. */
#endif

#define cosf(a)		cos((float)a)
#define sinf(a)  	sin((float)a)
#define sqrtf(a)  	sqrt((float)a)
#define expf(a)  	exp((float)a)

static void create_terrain(void);
static void erode_terrain(void);
static void color_terrain(void);
static void init_cells(void);
static void put_cell(float *source, perfobj_t *pobj);
static void put_paper_plane(float *source, perfobj_t *pobj);
static void put_texture_bind(int bind, perfobj_t *pobj);

int clouds;
static float paper_plane_vertexes[] = {
/*Nx  Ny  Nz   Vx     Vy    Vz */
/* ----------------------------    Top view of plane, middle streached open  */
 0.2, 0., .98, -.10,    0,  .02,/* vertex #'s      4 (.48,0,-.06)            */
 0., 0., 1.,   -.36,  .20, -.04,/*                 .                         */
 0., 0., 1.,    .36,  .01,    0,/*                ...                        */
 0., 0.,-1.,   -.32,  .02,    0,/*                 .             +X          */
 0., 1., 0.,    .48,    0, -.06,/*               2 . 6,8          ^          */
 0., 1., 0.,   -.30,    0, -.12,/*               . . .            |          */
 0.,-1., 0.,    .36, -.01,    0,/*              .. . ..           |          */
 0.,-1., 0.,   -.32, -.02,    0,/*               . . .            |          */
 0., 0.,-1.,    .36, -.01,    0,/*             . . . . .  +Y<-----*          */
 0., 0.,-1.,   -.36, -.20, -.04,/*               . . .     for this picture  */
 -0.2, 0., .98,  -.10,  0,  .02,/*            .  . . .  .  coord system rot. */
 -0.2, 0., -.98, -.10,  0,  .02,/*               . . .     90 degrees        */
 0., 0., -1.,  -.36,  .20, -.04,/*           .   . . .   .                   */
 0., 0., -1.,   .36,  .01,    0,/*               . # .           # marks     */
 0., 0., 1.,   -.32,  .02,    0,/*          .    . . .    .   (0,0) origin   */
 0., -1., 0.,   .48,    0, -.06,/*               . . .         (z=0 at top   */
 0., -1., 0.,  -.30,    0, -.12,/*         .     0 . 10    .    of plane)    */
 0.,1., 0.,     .36, -.01,    0,/*             . . . . .                     */
 0.,1., 0.,    -.32, -.02,    0,/*        .  .   . . .   .  .                */
 0., 0.,1.,     .36, -.01,    0,/*         .     . . .     .                 */
 0., 0.,1.,    -.36, -.20, -.04,/*       1.......3.5.7.......9               */
 0.2, 0., -.98,  -.10,  0,  .02,/* (-.36,.2,-.04)                            */
};

#define SIZE    400

float *A;

void init_paper_planes(void)
{
    perfobj_t       *pobj;

    /* 
     * create various perf-objs for planes 
     */
    pobj = &(SharedData->paper_plane_obj);
    pobj->flags = SharedData->paper_plane_flags;
    pobj->vdata = (float *) SharedData->paper_plane_verts;
    put_paper_plane(paper_plane_vertexes, pobj);

    pobj = &(SharedData->paper_plane_start_obj);
    pobj->flags = SharedData->paper_plane_start_flags;
    *(pobj->flags) = PD_PAPER_PLANE_MODE;
    *(pobj->flags + 1) = PLANES_START;
    *(pobj->flags + 2) = PD_END;

    pobj = &(SharedData->paper_plane_2ndpass_obj);
    pobj->flags = SharedData->paper_plane_2ndpass_flags;
    *(pobj->flags) = PD_PAPER_PLANE_MODE;
    *(pobj->flags + 1) = PLANES_SECOND_PASS;
    *(pobj->flags + 2) = PD_END;

    pobj = &(SharedData->paper_plane_end_obj);
    pobj->flags = SharedData->paper_plane_end_flags;
    *(pobj->flags) = PD_PAPER_PLANE_MODE;
    *(pobj->flags + 1) = PLANES_END;
    *(pobj->flags + 2) = PD_END;
}


/*
 * create perfobj from static definition of plane geometry above
 */

static void put_paper_plane(float *source, perfobj_t *pobj)
{
    int             j;
    perfobj_vert_t *pdataptr =(perfobj_vert_t *) pobj->vdata;
    unsigned int  *flagsptr = pobj->flags;
    float          *sp = source;

    *flagsptr++ = PD_DRAW_PAPER_PLANE;

    for (j = 0; j < 22; j++) {
        putn3fdata(sp + 0, pdataptr);
        putv3fdata(sp + 3, pdataptr);

        sp += 6;
        pdataptr++;
    }
    *flagsptr++ = PD_END;

}

static void put_texture_bind(int bind, perfobj_t *pobj)
{
	unsigned int  *flagsptr = pobj->flags;

	*flagsptr++ = PD_TEXTURE_BIND;
	*flagsptr++ = bind;

	*flagsptr++ = PD_END;

}

static void put_clouds_vert(float s, float t, float x, float y, float z,
				perfobj_vert_t *pdataptr)
{
	float           D[5];
	D[0] = s;
	D[1] = t;
	D[2] = x;
	D[3] = y;
	D[4] = z;
	putt2fdata(D, pdataptr);
	putv3fdata(D + 2, pdataptr);
}

float S[SIZE][SIZE];
float T[SIZE][SIZE];
float C[SIZE][SIZE][3];
int   M[SIZE][SIZE];

void init_terrain(void)
{
	GridDim = CellDim * NumCells;
	XYScale = (float)GRID_RANGE / (float)GridDim;
	CellSize = (float)GRID_RANGE / (float)NumCells;

	create_terrain();
	erode_terrain();
	color_terrain();
	init_cells();
}

#define SKY                             50.

void init_clouds(void)
{
    perfobj_t *pobj;
    perfobj_vert_t *pdataptr;

	clouds = 0;
    pobj = &(SharedData->clouds_texture_obj);
    pobj->flags = SharedData->clouds_texture_flags;
    put_texture_bind(2,pobj);

    pobj = &(SharedData->clouds_obj);
    pobj->flags = SharedData->clouds_flags;
    pobj->vdata = (float *)SharedData->clouds_verts;
    *(pobj->flags+ 0) = PD_DRAW_CLOUDS;
    *(pobj->flags+ 1) = PD_END;

    pdataptr =(perfobj_vert_t *) pobj->vdata;

    put_clouds_vert(0.,0., -SKY, -SKY, SKY_HIGH,pdataptr);
    pdataptr++;
    put_clouds_vert(24.,0.,  SKY+GRID_RANGE, -SKY, SKY_HIGH,pdataptr);
    pdataptr++;
    put_clouds_vert(24.,24.,  SKY+GRID_RANGE,  SKY+GRID_RANGE, SKY_HIGH,pdataptr);
    pdataptr++;
    put_clouds_vert(0.,24., -SKY,  SKY+GRID_RANGE, SKY_HIGH,pdataptr);
}

static void create_terrain(void)
{
	int             r, c, i, x1, y1, x2, y2;
	int             hillsize;

    hillsize = GRID_RANGE / 12;

    A = (float*)calloc(GridDim * GridDim, sizeof(float));

    /*
     *  initialize elevation to zero, except band down middle
     *  where make a maximum height 'hill' that will later be
     *  inverted to make the negative elevation 'canyon'
     */

    for (r = 0; r < GridDim; r++)
        for (c = 0; c < GridDim; c++)
             if(r>=(GridDim/2-2-IRND(2)) && r<=(GridDim/2+2+IRND(2)))
                A[r * GridDim + c] = 1.0;
             else
                A[r * GridDim + c] = 0.0;

    /*
     * create random sinusoidal hills that add on top
     * of each other
     */
    for (i = 1; i <= 10*GridDim; i++) {

        /* randomly position hill */
        x1 = IRND(GridDim - hillsize);
        x2 = x1 + hillsize/ 8 + IRND(hillsize-hillsize/ 8);
        y1 = IRND(GridDim - hillsize);
        y2 = y1 + hillsize/ 8 + IRND(hillsize-hillsize/ 8);

        if((x1<=GridDim/2-4 && x2>=GridDim/2-4) || 
           (x1<=GridDim/2+4 && x2>=GridDim/2+4))
        {
            x1 = IRND(2)-2 + GridDim/2;
            x2 = x1 + IRND(GridDim/2 - x1 + 2);
        }

        /* make a sinusoidal hill */
        for (r = x1; r < x2; r++)
            for (c = y1; c < y2; c++) {
                A[r * GridDim + c] +=.35 *
                    (sinf(M_PI * (float) (r - x1) / (float) (x2 - x1)) *
					 (sinf(M_PI * (float) (c - y1) / (float) (y2 - y1))));
            }
    }

    /* clamp the elevation of the terrain */
	for (r = 1; r < GridDim; r++)
		for (c = 1; c < GridDim; c++) {
			A[r * GridDim + c] = MIN(A[r * GridDim + c], .95);
			A[r * GridDim + c] = MAX(A[r * GridDim + c], 0.);
		}

}

#define NUM_DROPS                       80

static void erode_terrain(void)
{
    float           x, y, xv, yv, dx, dy;
    float           cut, min, take;
    int             nm;
    int             t, xi, yi, xo, yo, done;
    int             ii, jj, r, c;

    for (nm = 1; nm < NUM_DROPS*GridDim; nm++) {

        /* find a random position to start the 'rain drop' */
        x = (float) (IRND(GridDim));
        y = (float) (IRND(GridDim));

        /* Clamp x and y to be inside grid */
        x = MIN(MAX(2., x), (float)GridDim-2.);
        y = MIN(MAX(2., y), (float)GridDim-2.);
        
        done = 0;
        yv = xv = 0.;

        t = 0;
        cut = .3;

        while (!done) {
            xi = (int) x;
            yi = (int) y;

            min = 90.;

			if (xi != xo || yi != yo) {
                cut *=.99;

                /* gradient */
                dx = (A[(xi + 1)*GridDim + yi] - A[(xi - 1) * GridDim + yi]);
                dy = (A[xi * GridDim + yi + 1] - A[xi * GridDim + yi - 1]);


                /* find lowest neighbor */
                for (ii = -1; ii <= 1; ii++)
                    for (jj = -1; jj <= 1; jj++)
                        if (A[(xi + ii)*GridDim + yi + jj] < min)
                            min = A[(xi + ii)*GridDim + yi + jj];

                /* evaporate drop if sitting on my old location */
                if (M[xi][yi] == nm)
					done = 1;
                M[xi][yi] = nm;

                /* cave in neighbors by .3  */
                for (ii = -1; ii <= 1; ii++)
                    for (jj = -1; jj <= 1; jj++) {
                        take =.3 * cut * (A[(xi + ii)*GridDim + yi + jj]-min);
                        A[(xi + ii)*GridDim + yi + jj] -= take;
                    }

                /* take away from this cell by .7 */
                take = (A[xi*GridDim + yi] - min) *.7 * cut;
                A[xi*GridDim + yi] -= take;

            }
            xo = xi;
            yo = yi;

            /* move drop using kinematic motion*/
            xv = xv - dx -.8 * xv;
            yv = yv - dy -.8 * yv;

            x += xv;
            y += yv;

            /*
             * make sure can't move by more that 1.0
             * in any direction 
             */

            xv = MAX(xv, -1);
            yv = MAX(yv, -1);
            xv = MIN(xv,  1);
            yv = MIN(yv,  1);

            /* check to see if need a new drop */
            /* ie ran of world, got stuck, or at 'sea level' */
            if (x < 1.|| x > GridDim - 1.|| y < 1.|| y > GridDim - 1.
                                                || t++ > 2000
                || cut <.01)
                done = 1;

            if (A[xi*GridDim + yi] < 0.0001) {
                A[xi*GridDim + yi] = 0.;
                done = 1;
            }
		}                       /* while (!done) with this drop */
	}                           /* next drop */

	/*
	 *  invert the pseudo hill int the pseudo canyon
	 */

	for (r = 0; r < GridDim; r++)
		for (c = 0; c < GridDim; c++)
			 if(r>=GridDim/2-4 && r<=GridDim/2+4)
				A[r * GridDim + c] = MAX((-3.2 * A[r * GridDim + c]), -1.8);
}

static void color_terrain(void)
{
    float           N[3], D, alt, maxelev = -1.;
    int             x, y;

    for (x = 0; x < GridDim; x++)
        for (y = 0; y < GridDim; y++)
            maxelev = MAX(maxelev, A[x * GridDim + y]);

    for (x = 1; x < GridDim - 1; x++)
        for (y = 1; y < GridDim - 1; y++) {
            alt = A[x * GridDim + y] * 1.5;
            /* randomly perterb to get a mottling effect */
            alt += IRND(100) / 400. -.125;
            alt = MIN(alt, 1.0);
            if (alt < -.11) {
                C[x][y][0] = 0.6;         /* soil/rock in canyon */
                C[x][y][1] = 0.5;
                C[x][y][2] = 0.2;
            } else if (alt < .000001) {
                C[x][y][0] = 0.0;         /* dark, jungle lowlands */
                C[x][y][1] = 0.2;
                C[x][y][2] = 0.05;
            } else if (alt <.90) {
                C[x][y][0] = alt*.25;     /* green to redish hillsides */
                C[x][y][1] = (1.0 - alt) *.4 + .1;
                C[x][y][2] = 0.1;
            } else {
                C[x][y][0] = alt;
                C[x][y][1] = alt;         /* incresingly white snow */
                C[x][y][2] = alt;
            }

            /* compute normal to terrain */

            N[0] = A[(x - 1)*GridDim + y] - A[(x + 1)*GridDim + y];
            N[1] = A[x*GridDim + y - 1] - A[x*GridDim + y + 1];
            N[2] = 2.0 / ScaleZ;

            D = 1.0 / sqrtf(N[0] * N[0] + N[1] * N[1] + N[2] * N[2]);

            N[0] *= D;
            N[1] *= D;
            N[2] *= D;

            /* perform diffuse lighting of terrain */

            D = N[0] * LX + N[1] * LY + N[2] * LZ;
            D *= 1.2;

            if(!IRND(4)) D *= .5;
        
            D = MAX(D,0);

            /* darken terrain on shaded side */
            C[x][y][0] *= D;
            C[x][y][1] *= D;
            C[x][y][2] *= D;

            S[x][y] = (float) (x) / (float)CellDim;
            T[x][y] = (float) (y) / (float)CellDim;

        }
}

/*
 * create perobj to hold a cell of terrain
 * for a 5x5 terrain cell, there will be 5
 * tmeshes each with 10 triangles (12 vertexes)
 * this means looking past my cell to neighbors
 * to stitch things together.  To keep data contigious,
 * vertexes are repeated in tmeshes, not shared.
 */

static void init_cells(void)
{
    int             x, y, xx, yy, world_x, world_y;
    int             pntr, index, sstart, tstart;
    float           *D;
    perfobj_t       *pobj;

    D = (float*)calloc(CellDim *(CellDim + 1) * 
                        2 * FLOATS_PER_VERT_PAIR, sizeof(float));

    pobj = &(SharedData->terrain_texture_obj);
    pobj->flags = SharedData->terrain_texture_flags;

    put_texture_bind(1, pobj);

    for (x = 0; x < NumCells; x++)
        for (y = 0; y < NumCells; y++) {
			index = x*NumCells+y;
            SharedData->terrain_cells[index].flags = 
                SharedData->terrain_cell_flags[index];

            SharedData->terrain_cells[index].vdata =
                (float *) SharedData->terrain_cell_verts[index];

            pntr = 0;

            /*
             * Guarantee S,T to be within range 0-8 for tmesh strip using
             * 256x256 sized texture. This avoids texture index clipping
             * in pipe. 
            */
            sstart = (int)S[x*CellDim][y*CellDim];
            tstart = (int)T[x*CellDim][y*CellDim];

            for (xx = 0; xx < CellDim; xx++)
                for (yy = 0; yy < CellDim + 1; yy++) {

                    /* init a perfobj */

                    world_x = MIN(x * CellDim + xx, GridDim-2);
                    world_y = MIN(y * CellDim + yy, GridDim-2);

                    D[pntr + 0] = S[world_x][world_y] - sstart;
                    D[pntr + 1] = T[world_x][world_y] - tstart;
                    D[pntr + 2] = C[world_x][world_y][0];
                    D[pntr + 3] = C[world_x][world_y][1];
					D[pntr + 4] = C[world_x][world_y][2];
                    D[pntr + 5] = (float)world_x * XYScale;
                    D[pntr + 6] = (float)world_y * XYScale;
					D[pntr + 7] = A[world_x*GridDim + world_y]*ScaleZ;


                    D[pntr + 8] = S[world_x + 1][world_y] - sstart;
                    D[pntr + 9] = T[world_x + 1][world_y] - tstart;
                    D[pntr + 10] = C[world_x + 1][world_y][0];
                    D[pntr + 11] = C[world_x + 1][world_y][1];
                    D[pntr + 12] = C[world_x + 1][world_y][2];
                    D[pntr + 13] = (float)(world_x+1) * XYScale;
                    D[pntr + 14] = (float)world_y * XYScale;
                    D[pntr + 15] = A[(world_x+1)*GridDim + world_y] * ScaleZ;

                    pntr += FLOATS_PER_VERT_PAIR;

                }               /* for each cell */
            put_cell(D, &(SharedData->terrain_cells[index]));

        }                       /* for all cells in world */

        free(D);
}

static void put_cell(float *source, perfobj_t *pobj)
{
	int             i, j;
	perfobj_vert_t *pdataptr =(perfobj_vert_t *) pobj->vdata;
	unsigned int  *flagsptr = pobj->flags;
	float          *sp = source;

	/* For all tmesh strips in cell */
	for (i = 0; i < CellDim; i++) {
		*flagsptr++ = PD_DRAW_TERRAIN_CELL;
		*flagsptr++ = (unsigned long) pdataptr;

		/* For all verts in tmesh strip */
		for (j = 0; j < (CellDim + 1) * 2; j++) {
			putt2fdata(sp, pdataptr);
			putc3fdata(sp + 2, pdataptr);
            putv3fdata(sp + 5, pdataptr);

            sp += 8;
            pdataptr++;
        }
    }
    *flagsptr++ = PD_END;
}