File: render.c

package info (click to toggle)
xearth 1.0-13
  • links: PTS
  • area: non-free
  • in suites: hamm, potato, slink
  • size: 600 kB
  • ctags: 429
  • sloc: ansic: 9,989; makefile: 41
file content (639 lines) | stat: -rw-r--r-- 13,736 bytes parent folder | download
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
/*
 * render.c
 * kirk johnson
 * october 1993
 *
 * RCS $Id: render.c,v 1.20 1995/09/25 01:10:37 tuna Exp $
 *
 * Copyright (C) 1989, 1990, 1993, 1994, 1995 Kirk Lauritz Johnson
 *
 * Parts of the source code (as marked) are:
 *   Copyright (C) 1989, 1990, 1991 by Jim Frost
 *   Copyright (C) 1992 by Jamie Zawinski <jwz@lucid.com>
 *
 * Permission to use, copy, modify and freely distribute xearth for
 * non-commercial and not-for-profit purposes is hereby granted
 * without fee, provided that both the above copyright notice and this
 * permission notice appear in all copies and in supporting
 * documentation.
 *
 * Unisys Corporation holds worldwide patent rights on the Lempel Zev
 * Welch (LZW) compression technique employed in the CompuServe GIF
 * image file format as well as in other formats. Unisys has made it
 * clear, however, that it does not require licensing or fees to be
 * paid for freely distributed, non-commercial applications (such as
 * xearth) that employ LZW/GIF technology. Those wishing further
 * information about licensing the LZW patent should contact Unisys
 * directly at (lzw_info@unisys.com) or by writing to
 *
 *   Unisys Corporation
 *   Welch Licensing Department
 *   M/S-C1SW19
 *   P.O. Box 500
 *   Blue Bell, PA 19424
 *
 * The author makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
 * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "xearth.h"
#include "kljcpyrt.h"

static void new_stars _P((double));
static void new_grid _P((int, int));
static void new_grid_dot _P((double *, double *));
static int dot_comp _P((const void *, const void *));
static void render_rows_setup _P((void));
static void render_next_row _P((s8or32 *, int));
static void no_shade_row _P((s8or32 *, u_char *));
static void compute_sun_vector _P((double *));
static void orth_compute_inv_x _P((double *));
static void orth_shade_row _P((int, s8or32 *, double *, double *, u_char *));
static void merc_shade_row _P((int, s8or32 *, double *, u_char *));

static int      scanbitcnt;
static ScanBit *scanbit;
static s8or32   scan_to_pix[256];

static int    night_val;
static int    day_val_base;
static double day_val_delta;

static ExtArr   dots = NULL;
static int      dotcnt;
static ScanDot *dot;


static int dot_comp(a, b)
     const void *a;
     const void *b;
{
  return (((const ScanDot *) a)->y - ((const ScanDot *) b)->y);
}


static void render_rows_setup()
{
  int i;

  scanbitcnt = scanbits->count;
  scanbit    = (ScanBit *) scanbits->body;
  dotcnt     = dots->count;
  dot        = (ScanDot *) dots->body;

  /* precompute table for translating between
   * scan buffer values and pixel types
   */
  for (i=0; i<256; i++)
    if (i == 0)
      scan_to_pix[i] = PixTypeSpace;
    else if (i > 64)
      scan_to_pix[i] = PixTypeLand;
    else
      scan_to_pix[i] = PixTypeWater;
}


static void render_next_row(buf, idx)
     s8or32 *buf;
     int     idx;
{
  int      i, i_lim;
  int      tmp;
  int      _scanbitcnt;
  ScanBit *_scanbit;

  xearth_bzero((char *) buf, (unsigned) (sizeof(s8or32) * wdth));

  /* explicitly copy scanbitcnt and scanbit to local variables
   * to help compilers figure out that they can be registered
   */
  _scanbitcnt = scanbitcnt;
  _scanbit    = scanbit;

  while ((_scanbitcnt > 0) && (_scanbit->y == idx))
  {
    /* use i_lim to encourage compilers to register loop limit
     */
    i_lim = _scanbit->hi_x;
    tmp   = _scanbit->val;
    for (i=_scanbit->lo_x; i<=i_lim; i++)
      buf[i] += tmp;

    _scanbit    += 1;
    _scanbitcnt -= 1;
  }

  /* copy changes to scanbitcnt and scanbit out to memory
   */
  scanbitcnt = _scanbitcnt;
  scanbit    = _scanbit;

  /* use i_lim to encourage compilers to register loop limit
   */
  i_lim = wdth;
  for (i=0; i<i_lim; i++)
    buf[i] = scan_to_pix[(int) (buf[i] & 0xff)];

  while ((dotcnt > 0) && (dot->y == idx))
  {
    tmp = dot->x;

    if (dot->type == DotTypeStar)
    {
      if (buf[tmp] == PixTypeSpace)
        buf[tmp] = PixTypeStar;
    }
    else
    {
      switch (buf[tmp])
      {
      case PixTypeLand:
        buf[tmp] = PixTypeGridLand;
        break;

      case PixTypeWater:
        buf[tmp] = PixTypeGridWater;
        break;
      }
    }

    dot    += 1;
    dotcnt -= 1;
  }
}


static void no_shade_row(scanbuf, rslt)
     s8or32 *scanbuf;
     u_char *rslt;
{
  int i, i_lim;

  /* use i_lim to encourage compilers to register loop limit
   */
  i_lim = wdth;
  for (i=0; i<i_lim; i++)
  {
    switch (scanbuf[i])
    {
    case PixTypeSpace:        /* black */
      rslt[0] = 0;
      rslt[1] = 0;
      rslt[2] = 0;
      break;

    case PixTypeStar:         /* white */
    case PixTypeGridLand:
    case PixTypeGridWater:
      rslt[0] = 255;
      rslt[1] = 255;
      rslt[2] = 255;
      break;

    case PixTypeLand:         /* green */
      rslt[0] = 0;
      rslt[1] = 255;
      rslt[2] = 0;
      break;

    case PixTypeWater:        /* blue */
      rslt[0] = 0;
      rslt[1] = 0;
      rslt[2] = 255;
      break;

    default:
      assert(0);
    }

    rslt += 3;
  }
}


static void compute_sun_vector(rslt)
     double *rslt;
{
  rslt[0] = sin(sun_lon * (M_PI/180)) * cos(sun_lat * (M_PI/180));
  rslt[1] = sin(sun_lat * (M_PI/180));
  rslt[2] = cos(sun_lon * (M_PI/180)) * cos(sun_lat * (M_PI/180));

  XFORM_ROTATE(rslt, view_pos_info);
}


static void orth_compute_inv_x(inv_x)
     double *inv_x;
{
  int i, i_lim;

  i_lim = wdth;
  for (i=0; i<i_lim; i++)
    inv_x[i] = INV_XPROJECT(i);
}


static void orth_shade_row(idx, scanbuf, sol, inv_x, rslt)
     int     idx;
     s8or32 *scanbuf;
     double *sol;
     double *inv_x;
     u_char *rslt;
{
  int    i, i_lim;
  int    val;
  double x, y, z;
  double scale;
  double tmp;

  y = INV_YPROJECT(idx);

  /* save a little computation in the inner loop
   */
  tmp = 1 - (y*y);

  /* use i_lim to encourage compilers to register loop limit
   */
  i_lim = wdth;
  for (i=0; i<i_lim; i++)
  {
    switch (scanbuf[i])
    {
    case PixTypeSpace:        /* black */
      rslt[0] = 0;
      rslt[1] = 0;
      rslt[2] = 0;
      break;

    case PixTypeStar:         /* white */
    case PixTypeGridLand:
    case PixTypeGridWater:
      rslt[0] = 255;
      rslt[1] = 255;
      rslt[2] = 255;
      break;

    case PixTypeLand:         /* green, blue */
    case PixTypeWater:
      x = inv_x[i];
      z = tmp - (x*x);
      z = SQRT(z);
      scale = x*sol[0] + y*sol[1] + z*sol[2];
      if (scale < 0)
      {
	val = night_val;
      }
      else
      {
	val = day_val_base + (scale * day_val_delta);
	if (val > 255)
	  val = 255;
	else
	  assert(val >= 0);
      }

      if (scanbuf[i] == PixTypeLand)
      {
	/* land (green) */
	rslt[0] = 0;
	rslt[1] = val;
	rslt[2] = 0;
      }
      else
      {
	/* water (blue) */
	rslt[0] = 0;
	rslt[1] = 0;
	rslt[2] = val;
      }
      break;

    default:
      assert(0);
    }

    rslt += 3;
  }
}


static void merc_shade_row(idx, scanbuf, sol, rslt)
     int     idx;
     s8or32 *scanbuf;
     double *sol;
     u_char *rslt;
{
  int    i, i_lim;
  int    val;
  double x, y, z;
  double sin_theta;
  double cos_theta;
  double scale;
  double tmp;

  y = INV_YPROJECT(idx);
  y = INV_MERCATOR_Y(y);

  /* conceptually, on each iteration of the i loop, we want:
   *
   *   x = sin(INV_XPROJECT(i)) * sqrt(1 - (y*y));
   *   z = cos(INV_XPROJECT(i)) * sqrt(1 - (y*y));
   *
   * computing this directly is rather expensive, however, so we only
   * compute the first (i=0) pair of values directly; all other pairs
   * (i>0) are obtained through successive rotations of the original
   * pair (by inv_proj_scale radians).
   */

  /* compute initial (x, z) values
   */
  tmp = sqrt(1 - (y*y));
  x   = sin(INV_XPROJECT(0)) * tmp;
  z   = cos(INV_XPROJECT(0)) * tmp;

  /* compute rotation coefficients used
   * to find subsequent (x, z) values
   */
  tmp = inv_proj_scale;
  sin_theta = sin(tmp);
  cos_theta = cos(tmp);

  /* use i_lim to encourage compilers to register loop limit
   */
  i_lim = wdth;
  for (i=0; i<i_lim; i++)
  {
    switch (scanbuf[i])
    {
    case PixTypeSpace:        /* black */
      rslt[0] = 0;
      rslt[1] = 0;
      rslt[2] = 0;
      break;

    case PixTypeStar:         /* white */
    case PixTypeGridLand:
    case PixTypeGridWater:
      rslt[0] = 255;
      rslt[1] = 255;
      rslt[2] = 255;
      break;

    case PixTypeLand:         /* green, blue */
    case PixTypeWater:
      scale = x*sol[0] + y*sol[1] + z*sol[2];
      if (scale < 0)
      {
	val = night_val;
      }
      else
      {
	val = day_val_base + (scale * day_val_delta);
	if (val > 255)
	  val = 255;
	else
	  assert(val >= 0);
      }

      if (scanbuf[i] == PixTypeLand)
      {
	/* land (green) */
	rslt[0] = 0;
	rslt[1] = val;
	rslt[2] = 0;
      }
      else
      {
	/* water (blue) */
	rslt[0] = 0;
	rslt[1] = 0;
	rslt[2] = val;
      }
      break;

    default:
      assert(0);
    }

    /* compute next (x, z) values via 2-d rotation
     */
    tmp = (cos_theta * z) - (sin_theta * x);
    x   = (sin_theta * z) + (cos_theta * x);
    z   = tmp;

    rslt += 3;
  }
}


void render(rowfunc)
     void (*rowfunc) _P((u_char *));
{
  int     i, i_lim;
  s8or32 *scanbuf;
  u_char *row;
  double *inv_x;
  double  sol[3];
  double  tmp;

  scanbuf = (s8or32 *) malloc((unsigned) (sizeof(s8or32) * wdth));
  row = (u_char *) malloc((unsigned) wdth*3);
  assert((scanbuf != NULL) && (row != NULL));

  inv_x = NULL;
  render_rows_setup();

  if (do_shade)
  {
    /* inv_x[] only gets used with orthographic projection
     */
    if (proj_type == ProjTypeOrthographic)
    {
      inv_x = (double *) malloc((unsigned) sizeof(double) * wdth);
      assert(inv_x != NULL);
      orth_compute_inv_x(inv_x);
    }

    compute_sun_vector(sol);

    /* precompute shading parameters
     */
    night_val     = night * (255.99/100.0);
    tmp           = terminator / 100.0;
    day_val_base  = ((tmp * day) + ((1-tmp) * night))  * (255.99/100.0);
    day_val_delta = (day * (255.99/100.0)) - day_val_base;
  }

  /* main render loop
   * (use i_lim to encourage compilers to register loop limit)
   */
  i_lim = hght;
  for (i=0; i<i_lim; i++)
  {
    render_next_row(scanbuf, i);

    if (!do_shade)
      no_shade_row(scanbuf, row);
    else if (proj_type == ProjTypeOrthographic)
      orth_shade_row(i, scanbuf, sol, inv_x, row);
    else
      merc_shade_row(i, scanbuf, sol, row);

    rowfunc(row);
  }

  free(scanbuf);
  free(row);

  if (inv_x != NULL) free(inv_x);
}


void do_dots()
{
  if (dots == NULL)
    dots = extarr_alloc(sizeof(ScanDot));
  else
    dots->count = 0;

  if (do_stars) new_stars(star_freq);
  if (do_grid) new_grid(grid_big, grid_small);

  qsort(dots->body, dots->count, sizeof(ScanDot), dot_comp);
}


static void new_stars(freq)
     double freq;
{
  int      i;
  int      x, y;
  int      max_stars;
  ScanDot *newdot;

  max_stars = wdth * hght * freq;

  for (i=0; i<max_stars; i++)
  {
    x = random() % wdth;
    y = random() % hght;

    newdot = (ScanDot *) extarr_next(dots);
    newdot->x    = x;
    newdot->y    = y;
    newdot->type = DotTypeStar;

    if ((big_stars) && (x+1 < wdth) && ((random() % 100) < big_stars))
    {
      newdot = (ScanDot *) extarr_next(dots);
      newdot->x    = x+1;
      newdot->y    = y;
      newdot->type = DotTypeStar;
    }
  }
}


static void new_grid(big, small)
     int big;
     int small;
{
  int    i, j;
  int    cnt;
  double lat, lon;
  double lat_scale, lon_scale;
  double cs_lat[2];
  double cs_lon[2];

  /* lines of longitude
   */
  lon_scale = M_PI / (2 * big);
  lat_scale = M_PI / (2 * big * small);
  for (i=(-2*big); i<(2*big); i++)
  {
    lon       = i * lon_scale;
    cs_lon[0] = cos(lon);
    cs_lon[1] = sin(lon);

    for (j=(-(big*small)+1); j<(big*small); j++)
    {
      lat       = j * lat_scale;
      cs_lat[0] = cos(lat);
      cs_lat[1] = sin(lat);

      new_grid_dot(cs_lat, cs_lon);
    }
  }

  /* lines of latitude
   */
  lat_scale = M_PI / (2 * big);
  for (i=(1-big); i<big; i++)
  {
    lat       = i * lat_scale;
    cs_lat[0] = cos(lat);
    cs_lat[1] = sin(lat);
    cnt       = 2 * ((int) ((cs_lat[0] * small) + 0.5)) * big;
    lon_scale = M_PI / cnt;

    for (j=(-cnt); j<cnt; j++)
    {
      lon       = j * lon_scale;
      cs_lon[0] = cos(lon);
      cs_lon[1] = sin(lon);

      new_grid_dot(cs_lat, cs_lon);
    }
  }
}


static void new_grid_dot(cs_lat, cs_lon)
     double *cs_lat;
     double *cs_lon;
{
  int      x, y;
  double   pos[3];
  ScanDot *new;

  pos[0] = cs_lon[1] * cs_lat[0];
  pos[1] = cs_lat[1];
  pos[2] = cs_lon[0] * cs_lat[0];

  XFORM_ROTATE(pos, view_pos_info);

  if (proj_type == ProjTypeOrthographic)
  {
    /* if the grid dot isn't visible, return immediately
     */
    if (pos[2] <= 0) return;
  }
  else /* (proj_type == ProjTypeMercator) */
  {
    /* apply mercator projection
     */
    pos[0] = MERCATOR_X(pos[0], pos[2]);
    pos[1] = MERCATOR_Y(pos[1]);
  }

  x = XPROJECT(pos[0]);
  y = YPROJECT(pos[1]);

  if ((x >= 0) && (x < wdth) && (y >= 0) && (y < hght))
  {
    new = (ScanDot *) extarr_next(dots);
    new->x    = x;
    new->y    = y;
    new->type = DotTypeGrid;
  }
}