summaryrefslogtreecommitdiff
path: root/apps/graphics/traveler/src/trv_raycntl.c
blob: f1f8b1cecf4237d7fe0fc7b7035a1fdb09266b60 (plain) (blame)
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
/*******************************************************************************
 * apps/graphics/traveler/src/trv_raycntl.c
 *  This file contains the high-level ray caster control logic
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included files
 ****************************************************************************/

#include "trv_types.h"
#include "trv_debug.h"
#include "trv_world.h"
#include "trv_plane.h"
#include "trv_bitmaps.h"
#include "trv_paltable.h"
#include "trv_trigtbl.h"
#include "trv_graphics.h"
#include "trv_pov.h"
#include "trv_rayrend.h"
#include "trv_rayprune.h"
#include "trv_raycast.h"
#include "trv_raycntl.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* These definitions simplify creation of the initial ray casting cell */

#define TOP_HEIGHT  (VGULP_SIZE/2)
#define BOT_HEIGHT  (VGULP_SIZE - TOP_HEIGHT + 1)

#define TOP_ROW     0
#define MID_ROW     (TOP_ROW + TOP_HEIGHT - 1)
#define BOT_ROW     (VGULP_SIZE-1)

#define LEFT_WIDTH  ((HGULP_SIZE+1)/2)
#define RIGHT_WIDTH (HGULP_SIZE - LEFT_WIDTH + 2)

#define LEFT_COL    0
#define MID_COL     (LEFT_COL + LEFT_WIDTH - 1)
#define RIGHT_COL   HGULP_SIZE

/* The following macro converts a g_yaw[] array index into a relative screen
 * yaw.  This is used by the ray caster to perform view corrections.
 */

#define RELYAW(i)  (((i) + WINDOW_LEFT) - (WINDOW_WIDTH/2))

/* Macro to determine if two hits "hit" the same object */

#define SAME_CELL(i1,j1,i2,j2) \
  (g_ray_hit[i1][j1].rect == g_ray_hit[i2][j2].rect)

/****************************************************************************
 * Public Data
 ****************************************************************************/

/* The following array describes the hits from X/Y/Z-ray casting for the
 * current HGULP_SIZE x VGULP_SIZE cell
 */

struct trv_raycast_s g_ray_hit[VGULP_SIZE][HGULP_SIZE + 1];

/* This array points to the screen buffer row corresponding to the
 * pitch angle
 */

FAR uint8_t *g_buffer_row[VGULP_SIZE];

/* The is the "column" offset in g_buffer_row for the current cell being 
 * operated on.  This value is updated in a loop by trv_raycaster. */

int16_t g_cell_column;

/* This structure holds the parameters used in the current ray cast */

struct trv_camera_s g_camera;

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* These are all of the yaw angles which will be used by the ray caster
 * on a given cycle
 */

static int16_t g_yaw[IMAGE_WIDTH + 1];

/* These are all of the pitch angles which will be used by the ray caster
 * on each horizontal pass of a given cycle
 */

static int16_t g_pitch[VGULP_SIZE];

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Function: trv_resolve_cell
 *
 * Description:
 *   If the hits in the corners of the input cell are not the same, then
 *   recursively resolve the cell until all four corners are the same.
 *   When the are the same, then rend the cell to the display buffer.
 *
 ***************************************************************************/

static void trv_resolve_cell(uint8_t toprow, uint8_t leftcol,
                             uint8_t height, uint8_t width)
{
  uint8_t midrow;
  uint8_t botrow;
  uint8_t midcol;
  uint8_t rightcol;
  uint8_t topheight;
  uint8_t botheight;
  uint8_t leftwidth;
  uint8_t rightwidth;

  /* Check if the cell has been reduced to a vertical line */

  if (width > 1)
    {
      /* No.. Check if the cell has been reduce to a horizontal line */

      if (height > 1)
        {
          /* No.. It is still a rectangular region.  Check if the top half hit
           * the same cell type
           */

          if (!SAME_CELL(toprow, leftcol, toprow, (leftcol + width - 1)))
            {
              /* No.. the top corners are different.  Compare the top left and
               * bottom left corners to decide how to divide this up
               */

              if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
                {
                  /* The left corners are not the same.  Check the right
                   * corners.
                   */

                  if (!SAME_CELL(toprow, (leftcol + width - 1),
                                 (toprow + height - 1), (leftcol + width - 1)))
                    {
                      /* The right corners are not the same either.  Divide the 
                       * cell into three cells, retaining the bottom half
                       * (whose corners might be the same).
                       */

                      leftwidth = ((width + 1) >> 1);
                      if (leftwidth == 1)
                        {
                          rightwidth = 1;
                          midcol = leftcol + 1;
                        }
                      else
                        {
                          /* The cell is greater than 2 columns in width */

                          rightwidth = width - leftwidth + 1;
                          midcol = leftcol + leftwidth - 1;

                          /* Get the top middle hit */

                          trv_raycast(g_pitch[toprow],
                                      g_yaw[g_cell_column + midcol],
                                      RELYAW(g_cell_column + midcol),
                                      &g_ray_hit[toprow][midcol]);
                        }

                      topheight = ((height + 1) >> 1);
                      if (topheight == 1)
                        {
                          botheight = 1;
                          midrow = toprow + 1;
                        }
                      else
                        {
                          /* The cell is greater that 2 rows in height */

                          botheight = height - topheight + 1;
                          midrow = toprow + topheight - 1;

                          /* Get the middle left hit */

                          trv_raycast(g_pitch[midrow],
                                      g_yaw[g_cell_column + leftcol],
                                      RELYAW(g_cell_column + leftcol),
                                      &g_ray_hit[midrow][leftcol]);

                          /* Get the center hit */

                          if (rightwidth > 1)
                            {
                              trv_raycast(g_pitch[midrow],
                                          g_yaw[g_cell_column + midcol],
                                          RELYAW(g_cell_column + midcol),
                                          &g_ray_hit[midrow][midcol]);
                            }

                          /* Get the middle right hit */

                          rightcol = leftcol + width - 1;
                          trv_raycast(g_pitch[midrow],
                                      g_yaw[g_cell_column + rightcol],
                                      RELYAW(g_cell_column + rightcol),
                                      &g_ray_hit[midrow][rightcol]);
                        }

                      trv_resolve_cell(toprow, leftcol, topheight, leftwidth);
                      trv_resolve_cell(toprow, midcol, topheight, rightwidth);
                      trv_resolve_cell(midrow, leftcol, botheight, width);
                    }

                  /* The left corners are not the same, but the right are.
                   * Divide the cell into three cells, retaining the right half 
                   */

                  else
                    {
                      leftwidth = ((width + 1) >> 1);
                      if (leftwidth == 1)
                        {
                          rightwidth = 1;
                          midcol = leftcol + 1;
                        }
                      else
                        {
                          /* The cell is greater than 2 columns in width */

                          rightwidth = width - leftwidth + 1;
                          midcol = leftcol + leftwidth - 1;

                          /* Get the top middle hit */

                          trv_raycast(g_pitch[toprow],
                                      g_yaw[g_cell_column + midcol],
                                      RELYAW(g_cell_column + midcol),
                                      &g_ray_hit[toprow][midcol]);

                          /* Get the bottom middle hit */

                          botrow = toprow + height - 1;
                          trv_raycast(g_pitch[botrow],
                                      g_yaw[g_cell_column + midcol],
                                      RELYAW(g_cell_column + midcol),
                                      &g_ray_hit[botrow][midcol]);
                        }

                      topheight = ((height + 1) >> 1);
                      if (topheight == 1)
                        {
                          botheight = 1;
                          midrow = toprow + 1;
                        }
                      else
                        {
                          /* The cell is greater that 2 rows in height */

                          botheight = height - topheight + 1;
                          midrow = toprow + topheight - 1;

                          /* Get the middle left hit */

                          trv_raycast(g_pitch[midrow],
                                      g_yaw[g_cell_column + leftcol],
                                      RELYAW(g_cell_column + leftcol),
                                      &g_ray_hit[midrow][leftcol]);

                          /* Get the center hit */

                          if (rightwidth > 1)
                            {
                              trv_raycast(g_pitch[midrow],
                                          g_yaw[g_cell_column + midcol],
                                          RELYAW(g_cell_column + midcol),
                                          &g_ray_hit[midrow][midcol]);
                            }
                        }

                      trv_resolve_cell(toprow, leftcol, topheight, leftwidth);
                      trv_resolve_cell(midrow, leftcol, botheight, leftwidth);
                      trv_resolve_cell(toprow, midcol, height, rightwidth);
                    }
                }

              /* The left sides are the same! Divide the cell vertically into
               * two cells */

              else
                {
                  leftwidth = ((width + 1) >> 1);
                  if (leftwidth == 1)
                    {
                      rightwidth = 1;
                      midcol = leftcol + 1;
                    }
                  else
                    {
                      /* The cell is greater than 2 columns in width */

                      rightwidth = width - leftwidth + 1;
                      midcol = leftcol + leftwidth - 1;

                      /* Get the top middle hit */

                      trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
                                  RELYAW(g_cell_column + midcol),
                                  &g_ray_hit[toprow][midcol]);

                      /* Get the bottom middle hit */

                      botrow = toprow + height - 1;
                      trv_raycast(g_pitch[botrow], g_yaw[g_cell_column + midcol],
                                  RELYAW(g_cell_column + midcol),
                                  &g_ray_hit[botrow][midcol]);
                    }

                  trv_resolve_cell(toprow, leftcol, height, leftwidth);
                  trv_resolve_cell(toprow, midcol, height, rightwidth);
                }
            }

          /* The top corners are the same.  Compare the top left and bottom
           * left corners
           */

          else if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
            {
              /* The top corners are the same, but left corners are not. Divide 
               * the cell into two cells horizontally
               */

              topheight = ((height + 1) >> 1);
              if (topheight == 1)
                {
                  botheight = 1;
                  midrow = toprow + 1;
                }
              else
                {
                  /* The cell is greater that 2 rows in height */

                  botheight = height - topheight + 1;
                  midrow = toprow + topheight - 1;

                  /* Get the middle left hit */

                  trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + leftcol],
                              RELYAW(g_cell_column + leftcol),
                              &g_ray_hit[midrow][leftcol]);

                  /* Get the middle right hit */

                  rightcol = leftcol + width - 1;
                  trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + rightcol],
                              RELYAW(g_cell_column + rightcol),
                              &g_ray_hit[midrow][rightcol]);
                }

              trv_resolve_cell(toprow, leftcol, topheight, width);
              trv_resolve_cell(midrow, leftcol, botheight, width);
            }

          /* The top and left corners are the same.  Check the lower right
           * corner
           */

          else if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), (leftcol + width - 1)))
            {
              /* The lower right corner differs from all of the others.  Divide
               * the cell into three cells, retaining the left half
               */

              leftwidth = ((width + 1) >> 1);
              if (leftwidth == 1)
                {
                  rightwidth = 1;
                  midcol = leftcol + 1;
                }
              else
                {
                  /* The cell is greater than 2 columns in width */

                  rightwidth = width - leftwidth + 1;
                  midcol = leftcol + leftwidth - 1;

                  /* Get the top middle hit */

                  trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
                              RELYAW(g_cell_column + midcol),
                              &g_ray_hit[toprow][midcol]);

                  /* Get the bottom middle hit */

                  botrow = toprow + height - 1;
                  trv_raycast(g_pitch[botrow], g_yaw[g_cell_column + midcol],
                              RELYAW(g_cell_column + midcol),
                              &g_ray_hit[botrow][midcol]);
                }

              topheight = ((height + 1) >> 1);
              if (topheight == 1)
                {
                  botheight = 1;
                  midrow = toprow + 1;
                }
              else
                {
                  /* The cell is greater that 2 rows in height */

                  botheight = height - topheight + 1;
                  midrow = toprow + topheight - 1;

                  /* Get the middle right hit */

                  rightcol = leftcol + width - 1;
                  trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + rightcol],
                              RELYAW(g_cell_column + rightcol),
                              &g_ray_hit[midrow][rightcol]);

                  /* Get the center hit */

                  if (rightwidth > 1)
                    {
                      trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + midcol],
                                  RELYAW(g_cell_column + midcol),
                                  &g_ray_hit[midrow][midcol]);
                    }
                }

              trv_resolve_cell(toprow, leftcol, height, leftwidth);
              trv_resolve_cell(toprow, midcol, topheight, rightwidth);
              trv_resolve_cell(midrow, midcol, botheight, rightwidth);
            }

          /* The four corners are the same! */

          else
            {
              /* Apply texturing */

              trv_rend_cell(toprow, leftcol, height, width);
            }
        }

      /* The cell has been reduced to a horizontal line */

      else
        {
          /* Check if the endpoints of the horizontal line are the same */

          if (!SAME_CELL(toprow, leftcol, toprow, (leftcol + width - 1)))
            {
              /* No.. they are different.  Divide the line in half */

              leftwidth = ((width + 1) >> 1);
              if (leftwidth == 1)
                {
                  rightwidth = 1;
                  midcol = leftcol + 1;
                }
              else
                {
                  /* The cell is greater than 2 columns in width */

                  rightwidth = width - leftwidth + 1;
                  midcol = leftcol + leftwidth - 1;

                  /* Get the middle hit */

                  trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
                              RELYAW(g_cell_column + midcol),
                              &g_ray_hit[toprow][midcol]);
                }

              trv_resolve_cell(toprow, leftcol, 1, leftwidth);
              trv_resolve_cell(toprow, midcol, 1, rightwidth);
            }

          /* The endpoints of the horizontal line are the same! */

          else
            {
              /* Apply texturing */

              trv_rend_row(toprow, leftcol, width);
            }
        }
    }

  /* The cell is only 1 pixel wide.  Check if it has been reduced to a single
   * pixel
   */

  else if (height > 1)
    {
      /* No.. The cell has been reduced to a vertical line.  Check if the
       * endpoints are the same.
       */

      if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
        {
          /* No.. they are different.  Divide the line in half */

          topheight = ((height + 1) >> 1);
          if (topheight == 1)
            {
              botheight = 1;
              midrow = toprow + 1;
            }
          else
            {
              /* The cell is greater that 2 rows in height */

              botheight = height - topheight + 1;
              midrow = toprow + topheight - 1;

              /* Get the middle hit */

              trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + leftcol],
                          RELYAW(g_cell_column + leftcol),
                          &g_ray_hit[midrow][leftcol]);
            }

          trv_resolve_cell(toprow, leftcol, topheight, 1);
          trv_resolve_cell(midrow, leftcol, botheight, 1);
        }

      /* The endpoints of the vertical line are the same! */

      else
        {
          /* Apply texturing */

          trv_rend_column(toprow, leftcol, height);
        }
    }

  /* The cell has been reduced to a single pixel. */

  else
    {
      /* Apply texturing */

      trv_rend_pixel(toprow, leftcol);
    }
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Function: trv_raycaster
 *
 * Description:
 *   This is the heart of the system.  it casts out 320 rays and builds the
 *   3-D image from their intersections with the walls.
 *
 ***************************************************************************/

void trv_raycaster(FAR struct trv_camera_s *player,
                   FAR struct trv_graphics_info_s *ginfo)
{
  int16_t row;         /* the current row being cast 0..IMAGE_HEIGHT */
  int16_t yaw;         /* Working yaw angle */
  int16_t pitch;       /* Working pitch angle */
  uint8_t *buffer;     /* Points the screen buffer row for this pitch */
  int i;

  trv_vdebug("\ntrv_raycaster: x=%d y=%d z=%d yaw=%d pitch=%d",
             player->x, player->x, player->z, player->yaw, player->pitch);

  /* Copy the input "player" to the working "camera */

  g_camera = *player;

  /* The horizontal field of view is determined by the width of the window
   * (centered at the yaw angle)
   */

  yaw = g_camera.yaw - (IMAGE_WIDTH / 2) + (HGULP_SIZE * VIDEO_COLUMN_ANGLE);
  if (yaw < 0)
    {
      yaw += ANGLE_360;
    }

  /* The vertical field of view is determined by the currently active screen
   * HEIGHT. NOTE: camera pitch cannot exceed 90 degrees. Not check is
   * performed here.
   */

  pitch = g_camera.pitch + (IMAGE_HEIGHT / 2);
  if (pitch >= ANGLE_360)
    {
      pitch -= ANGLE_360;
    }

  /* Loop through all columns at each yaw angle on the screen */

  for (g_cell_column = IMAGE_WIDTH; g_cell_column >= 0; g_cell_column--)
    {
      /* Save the yaw angle.  By saving all of the yaw angles, we can avoid
       * complex tests for 360 degree wraps.
       */

      g_yaw[g_cell_column] = yaw;

      /* Test if viewing yaw angle needs to wrap around */

      yaw += VIDEO_COLUMN_ANGLE;
      if (yaw >= ANGLE_360)
        {
          yaw -= ANGLE_360;
        }
    }

  /* Seed the algorithm PART I: Set up the raycaster this yaw range. */

  trv_ray_yawprune(g_yaw[IMAGE_WIDTH], g_yaw[0]);

  /* Top of Ray Casting Loops */

  /* Point to the first row of the rending buffer.  This will be bumped to
   * successive rows with each change in the pitch angle.
   */

  buffer = &ginfo->swbuffer[IMAGE_TOP];

  /* Loop through each row at each pitch angle */

  for (row = 0; (row < (IMAGE_HEIGHT - VGULP_SIZE + 1)); row += VGULP_SIZE)
    {
      /* Initialize the pitch angles that will be needed in the inner loop.
       * These are pre-calculated so that once we get started, we need not have 
       * to be concerned about zero crossing conditions.
       */

      g_pitch[0] = pitch;
      g_buffer_row[0] = &buffer[IMAGE_LEFT];

      for (i = 1; i < VGULP_SIZE; i++)
        {
          g_pitch[i] = g_pitch[i - 1] - VIDEO_ROW_ANGLE;
          if (g_pitch[i] < ANGLE_0)
            {
              g_pitch[i] += ANGLE_360;
            }

          g_buffer_row[i] = g_buffer_row[i - 1] + TRV_SCREEN_WIDTH;
        }

      /* Seed the algorithm PART II: Set up the raycaster for this horizontal
       * swathe
       */

      trv_ray_pitchprune(g_pitch[VGULP_SIZE - 1], g_pitch[0]);

      /* Seed the algorithm PART III: These initial hits will be moved to the
       * beginning the hit array on the first pass through the loop.
       */

      trv_raycast(g_pitch[TOP_ROW], g_yaw[IMAGE_WIDTH],
                  RELYAW(IMAGE_WIDTH), &g_ray_hit[TOP_ROW][LEFT_COL]);
      trv_raycast(g_pitch[BOT_ROW], g_yaw[IMAGE_WIDTH],
                  RELYAW(IMAGE_WIDTH), &g_ray_hit[BOT_ROW][LEFT_COL]);

      /* Loop through all columns at each yaw angle on the screen window */

      for (g_cell_column = (IMAGE_WIDTH - HGULP_SIZE + 1);
           g_cell_column >= 0; g_cell_column -= HGULP_SIZE)
        {
          trv_vdebug("\ng_cell_column=%d yaw=%d", g_cell_column,
                     g_yaw[g_cell_column]);

          /* Perform Ray VGULP_SIZE x HGULP_SIZE Casting */

          /* The hits at the right corners will be the same as the hits for for 
           * the left hand corners on the next pass */

          g_ray_hit[TOP_ROW][RIGHT_COL] = g_ray_hit[TOP_ROW][LEFT_COL];
          g_ray_hit[BOT_ROW][RIGHT_COL] = g_ray_hit[BOT_ROW][LEFT_COL];

          /* Now get new hits in the right corners. */

          trv_raycast(g_pitch[TOP_ROW], g_yaw[g_cell_column],
                      RELYAW(g_cell_column), &g_ray_hit[TOP_ROW][LEFT_COL]);
          trv_raycast(g_pitch[BOT_ROW], g_yaw[g_cell_column],
                      RELYAW(g_cell_column), &g_ray_hit[BOT_ROW][LEFT_COL]);

          /* Now, resolve the cell recursively until the hits are the same in
           * all four corners */

          trv_resolve_cell(TOP_ROW, LEFT_COL, VGULP_SIZE, (HGULP_SIZE + 1));
        }

      /* End of the pitch loop.  Bump up the pitch angle and the rending buffer 
       * pointer for the next time through the outer loop */

      pitch -= (VGULP_SIZE * VIDEO_ROW_ANGLE);
      if (pitch < ANGLE_0)
        {
          pitch += ANGLE_360;
        }

      buffer += (VGULP_SIZE * TRV_SCREEN_WIDTH);

      /* Inform the ray cast engine that we are done with this horizonatal
       * swathe.
       */

      trv_ray_pitchunprune();
    }

  /* Inform the ray cast engine that we are done. */

  trv_ray_yawunprune();
}

/****************************************************************************
 * Function: trv_get_texture
 *
 * Description:
 *   This function returns the cell texture at the current cell row and
 *   column.  This function is used by the texture rending functions when
 *   processing a cell on a transparent wall and a transparent pixel is
 *   encountered.  This is a very inefficient way to handle these cases!
 *   Prevention of this condition is the best approach.  However, total
 *   elimination of the condition is impossible.
 *
 ***************************************************************************/

uint8_t trv_get_texture(uint8_t row, uint8_t col)
{
  FAR struct trv_raycast_s *ptr = &g_ray_hit[row][col];
  FAR uint8_t *palptr;
  int16_t zone;

  /* Perform a ray cast to get the hit at this row & column */

  trv_raycast(g_pitch[row], g_yaw[g_cell_column + col],
              RELYAW(g_cell_column + col), ptr);

  /* Check if we hit anything */

  if (ptr->rect)
    {
      /* Get a pointer to the palette map to use on this pixel */

      if (IS_SHADED(ptr->rect))
        {
          zone = GET_ZONE(ptr->xdist, ptr->ydist);
          palptr = GET_PALPTR(zone);
        }
      else
        {
          palptr = GET_PALPTR(0);
        }

      /* We did, return the pixel at this location */
      /* PROBLEM: Need to know if the is an even or odd hit */

      return palptr[GET_FRONT_PIXEL(ptr->rect, ptr->xpos, ptr->ypos)];
    }
  else
    {
      return INVISIBLE_PIXEL;
    }
}