home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / msc / chap_15 / black15.c next >
Encoding:
C/C++ Source or Header  |  1995-02-21  |  50.0 KB  |  1,810 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #include <search.h>             // this one is needed for qsort()
  16.  
  17. // include all of our stuff
  18.  
  19. #include "black3.h"
  20. #include "black4.h"
  21. #include "black5.h"
  22. #include "black6.h"
  23. #include "black8.h"
  24. #include "black9.h"
  25. #include "black11.h"
  26. #include "black15.h"
  27.  
  28. // G L O B A L S //////////////////////////////////////////////////////////////
  29.  
  30. int far *z_buffer,   // the current z buffer memory
  31.     far *z_bank_1,   // memory bank 1 of z buffer
  32.     far *z_bank_2;   // memory bank 2 of z buffer
  33.  
  34. unsigned int z_height    = 200,    // the height if the z buffer
  35.              z_height_2  = 100,    // the height if half the z buffer
  36.              z_bank_size = 64000L; // size of a z buffer bank in bytes
  37.  
  38. FILE *fp_out;   // general output file
  39.  
  40. ////////////////////////////////////////////////////////////////////////////////
  41.  
  42. void Draw_TB_Tri_3D_Z(int x1,int y1, int z1,
  43.                       int x2,int y2, int z2,
  44.                       int x3,int y3, int z3,
  45.                       int color)
  46.  
  47. {
  48. // this function draws a triangle that has a flat top
  49.  
  50. float dx_right,    // the dx/dy ratio of the right edge of line
  51.       dx_left,     // the dx/dy ratio of the left edge of line
  52.       xs,xe,       // the starting and ending points of the edges
  53.       height,      // the height of the triangle
  54.  
  55.       dx,          // general delta's
  56.       dy,
  57.       z_left,      // the z value of the left edge of current line
  58.       z_right,     // the z value for the right edge of current line
  59.       ay,          // interpolator constant
  60.       b1y,         // the change of z with respect to y on the left edge
  61.       b2y;         // the change of z with respect to y on the right edge
  62.  
  63. int temp_x,        // used during sorting as temps
  64.     temp_y,
  65.     temp_z,
  66.     xs_clip,       // used by clipping
  67.     xe_clip,
  68.     x,
  69.     x_index,       // used as looping vars
  70.     y_index;
  71.  
  72. // change these two back to float and remove all *32 and >>5
  73. // if you don't want to use fixed point during horizontal z interpolation
  74.  
  75. int z_middle,  // the z value of the middle between the left and right
  76.     bx;        // the change of z with respect to x
  77.  
  78. unsigned char far *dest_addr; // current image destination
  79.  
  80. // test order of x1 and x2, note y1=y2.
  81.  
  82. // test if top or bottom is flat and set constants appropriately
  83.  
  84. if (y1==y2)
  85.    {
  86.    // perform computations for a triangle with a flat top
  87.  
  88.    if (x2 < x1)
  89.       {
  90.  
  91.       temp_x = x2;
  92.       temp_z = z2;
  93.  
  94.       x2     = x1;
  95.       z2     = z1;
  96.  
  97.       x1     = temp_x;
  98.       z1     = temp_z;
  99.  
  100.       } // end if swap
  101.  
  102.    // compute delta's for scan conversion
  103.  
  104.    height = y3-y1;
  105.  
  106.    dx_left  = (x3-x1)/height;
  107.    dx_right = (x3-x2)/height;
  108.  
  109.    // compute deltas for z interpolation
  110.  
  111.    z_left  = z1;
  112.    z_right = z2;
  113.  
  114.    // vertical interpolants
  115.  
  116.    ay  = 1/height;
  117.  
  118.    b1y = ay*(z3-z1);
  119.    b2y = ay*(z3-z2);
  120.  
  121.    // set starting points
  122.  
  123.    xs = (float)x1;
  124.    xe = (float)x2;
  125.  
  126.    } // end top is flat
  127. else
  128.    { // bottom must be flat
  129.  
  130.    // test order of x3 and x2, note y2=y3.
  131.  
  132.    if (x3 < x2)
  133.       {
  134.       temp_x = x2;
  135.       temp_z = z2;
  136.  
  137.       x2     = x3;
  138.       z2     = z3;
  139.  
  140.       x3     = temp_x;
  141.       z3     = temp_z;
  142.  
  143.       } // end if swap
  144.  
  145.    // compute delta's for scan conversion
  146.  
  147.    height = y3-y1;
  148.  
  149.    dx_left  = (x2-x1)/height;
  150.    dx_right = (x3-x1)/height;
  151.  
  152.    // compute deltas for z interpolation
  153.  
  154.    z_left  = z1;
  155.    z_right = z1;
  156.  
  157.    // vertical interpolants
  158.  
  159.    ay  = 1/height;
  160.  
  161.    b1y = ay*(z2-z1);
  162.    b2y = ay*(z3-z1);
  163.  
  164.    // set starting points
  165.  
  166.    xs = (float)x1;
  167.    xe = (float)x1;
  168.  
  169.    } // end else bottom is flat
  170.  
  171. // perform y clipping
  172.  
  173. // clip top
  174.  
  175. if (y1<poly_clip_min_y)
  176.    {
  177.    // compute new xs and ys
  178.  
  179.    dy = (float)(-y1+poly_clip_min_y);
  180.  
  181.    xs = xs+dx_left*dy;
  182.    xe = xe+dx_right*dy;
  183.  
  184.    // re-compute z_left and z_right to take into consideration
  185.    // vertical shift down
  186.  
  187.    z_left  += b1y*dy;
  188.    z_right += b2y*dy;
  189.  
  190.    // reset y1
  191.  
  192.    y1=poly_clip_min_y;
  193.  
  194.    } // end if top is off screen
  195.  
  196. // clip bottom
  197.  
  198. if (y3>poly_clip_max_y)
  199.    y3=poly_clip_max_y;
  200.  
  201. // compute starting address in video memory
  202.  
  203. dest_addr = double_buffer+(y1<<8)+(y1<<6);
  204.  
  205. // start z buffer at proper bank
  206.  
  207. if (y1<z_height_2)
  208.    z_buffer = z_bank_1+(y1<<8)+(y1<<6);
  209. else
  210.    {
  211.  
  212.    temp_y = y1-z_height_2;
  213.  
  214.    z_buffer = z_bank_2+(temp_y<<8)+(temp_y<<6);
  215.  
  216.    } // end else
  217.  
  218. // test if x clipping is needed
  219.  
  220. if (x1>=poly_clip_min_x && x1<=poly_clip_max_x &&
  221.     x2>=poly_clip_min_x && x2<=poly_clip_max_x &&
  222.     x3>=poly_clip_min_x && x3<=poly_clip_max_x)
  223.     {
  224.     // draw the triangle
  225.  
  226.     for (y_index=y1; y_index<=y3; y_index++)
  227.         {
  228.  
  229.         // test if we need to switch to z buffer bank two
  230.  
  231.         if (y_index==z_height_2)
  232.            z_buffer = z_bank_2;
  233.  
  234. ///////////////////////////////////////////////////////////////////////////////
  235.  
  236. // this section uses a bit of 16 bit fixed point for the horizontal
  237. // interpolation. The format is 11:5 i.e 11 whole places and 5 decimal places
  238. // Also, notice the use of >>5 and *32 to convert to fixed point and back to
  239. // simple integer, also the old version of each line is commented out under each
  240. // fixed point calculation, so you can see what was originally done...
  241.  
  242.         // compute horizontal z interpolant
  243.  
  244.         z_middle = 32*z_left;
  245.         // z_middle = z_left;
  246.  
  247.         bx = 32*(z_right - z_left)/(1+xe-xs);
  248.         // bx = (z_right - z_left)/(1+xe-xs);
  249.  
  250.         // draw the line
  251.  
  252.         for (x_index=xs; x_index<=xe; x_index++)
  253.             {
  254.             // if current z_middle is less than z-buffer then replace
  255.             // and update image buffer
  256.  
  257.             if (z_middle>>5 < z_buffer[x_index])
  258.             // if (z_middle < z_buffer[x_index])
  259.                {
  260.                // update z buffer
  261.  
  262.                z_buffer[x_index]=(int)z_middle>>5;
  263.                // z_buffer[x_index]=(int)z_middle;
  264.  
  265.                // write to image buffer
  266.  
  267.                dest_addr[x_index] = color;
  268.  
  269.                // update video buffer
  270.  
  271.                } // end if update buffer
  272.  
  273.             // update current z value
  274.  
  275.             z_middle += bx;
  276.  
  277. // END FIXED POINT DEMO SECTION ////////////////////////////////////////////////
  278.  
  279.             } // end draw z buffered line
  280.  
  281.         // adjust starting point and ending point for scan conversion
  282.  
  283.         xs+=dx_left;
  284.         xe+=dx_right;
  285.  
  286.         // adjust vertical z interpolants
  287.  
  288.         z_left  += b1y;
  289.         z_right += b2y;
  290.  
  291.         // adjust video and z buffer offsets
  292.  
  293.         dest_addr += 320;
  294.         z_buffer  += 320;
  295.  
  296.         } // end for
  297.  
  298.     } // end if no x clipping needed
  299. else
  300.    {
  301.    // clip x axis with slower version
  302.  
  303.    // draw the triangle
  304.  
  305.    for (y_index=y1; y_index<=y3; y_index++)
  306.        {
  307.  
  308.        // test if we need to switch to z buffer bank two
  309.  
  310.        if (y_index==z_height_2)
  311.           z_buffer = z_bank_2;
  312.  
  313.        // do x clip
  314.  
  315.        xs_clip  = (int)xs;
  316.        xe_clip  = (int)xe;
  317.  
  318. ///////////////////////////////////////////////////////////////////////////////
  319.  
  320. // this section uses a bit of 16 bit fixed point for the horizontal
  321. // interpolation. The format is 11:5 i.e 11 whole places and 5 decimal places
  322. // Also, notice the use of >>5 and *32 to convert to fixed point and back to
  323. // simple integer, also the old version of each line is commented out under each
  324. // fixed point calculation.
  325.  
  326.        // compute horizontal z interpolant
  327.  
  328.        z_middle = 32*z_left;
  329.        // z_middle = z_left;
  330.  
  331.        bx = 32*(z_right - z_left)/(1+xe-xs);
  332.        // bx = 32*(z_right - z_left)/(1+xe-xs);
  333.  
  334.        // adjust starting point and ending point
  335.  
  336.        xs+=dx_left;
  337.        xe+=dx_right;
  338.  
  339.        // adjust vertical z interpolants
  340.  
  341.        z_left  += b1y;
  342.        z_right += b2y;
  343.  
  344.        // clip line
  345.  
  346.        if (xs_clip < poly_clip_min_x)
  347.           {
  348.           dx = (-xs_clip + poly_clip_min_x);
  349.  
  350.           xs_clip = poly_clip_min_x;
  351.  
  352.           // re-compute z_middle to take into consideration horizontal shift
  353.  
  354.           z_middle += 32*bx*dx;
  355.           // z_middle += bx*dx;
  356.  
  357.           } // end if line is clipped on left
  358.  
  359.        if (xe_clip > poly_clip_max_x)
  360.           {
  361.           xe_clip = poly_clip_max_x;
  362.  
  363.           } // end if line is clipped on right
  364.  
  365.        // draw the line
  366.  
  367.        for (x_index=xs_clip; x_index<=xe_clip; x_index++)
  368.            {
  369.            // if current z_middle is less than z-buffer then replace
  370.            // and update image buffer
  371.  
  372.            if (z_middle>>5 < z_buffer[x_index])
  373.            // if (z_middle < z_buffer[x_index])
  374.               {
  375.               // update z buffer
  376.  
  377.               z_buffer[x_index]=(int)z_middle>>5;
  378.            // z_buffer[x_index]=(int)z_middle;
  379.  
  380.               // write to image buffer
  381.  
  382.               dest_addr[x_index] = color;
  383.  
  384.               // update video buffer
  385.  
  386.               } // end if update z buffer
  387.  
  388.            // update current z value
  389.  
  390.            z_middle += bx;
  391.  
  392. // END FIXED POINT DEMO SECTION ////////////////////////////////////////////////
  393.  
  394.            } // end draw z buffered line
  395.  
  396.        // adjust video and z buffer offsets
  397.  
  398.        dest_addr += 320;
  399.        z_buffer  += 320;
  400.  
  401.        } // end for y_index
  402.  
  403.    } // end else x clipping needed
  404.  
  405. } // end Draw_TB_Tri_3D_Z
  406.  
  407. /////////////////////////////////////////////////////////////////////////////
  408.  
  409. void Draw_Tri_3D_Z(int x1,int y1,int z1,
  410.                    int x2,int y2,int z2,
  411.                    int x3,int y3,int z3,
  412.                    int color)
  413. {
  414.  
  415. int temp_x,    // used for sorting
  416.     temp_y,
  417.     temp_z,
  418.     new_x,     // used to compute new x and z at triangle spliting point
  419.     new_z;
  420.  
  421. // test for h lines and v lines
  422.  
  423. if ((x1==x2 && x2==x3)  ||  (y1==y2 && y2==y3))
  424.    return;
  425.  
  426. // sort p1,p2,p3 in ascending y order
  427.  
  428. if (y2<y1)
  429.    {
  430.    temp_x = x2;
  431.    temp_y = y2;
  432.    temp_z = z2;
  433.  
  434.    x2     = x1;
  435.    y2     = y1;
  436.    z2     = z1;
  437.  
  438.    x1     = temp_x;
  439.    y1     = temp_y;
  440.    z1     = temp_z;
  441.  
  442.    } // end if
  443.  
  444. // now we know that p1 and p2 are in order
  445.  
  446. if (y3<y1)
  447.    {
  448.    temp_x = x3;
  449.    temp_y = y3;
  450.    temp_z = y3;
  451.  
  452.    x3     = x1;
  453.    y3     = y1;
  454.    z3     = z1;
  455.  
  456.    x1     = temp_x;
  457.    y1     = temp_y;
  458.    z1     = temp_z;
  459.  
  460.    } // end if
  461.  
  462. // finally test y3 against y2
  463.  
  464. if (y3<y2)
  465.    {
  466.    temp_x = x3;
  467.    temp_y = y3;
  468.    temp_z = z3;
  469.  
  470.    x3     = x2;
  471.    y3     = y2;
  472.    z3     = z2;
  473.  
  474.    x2     = temp_x;
  475.    y2     = temp_y;
  476.    z2     = temp_z;
  477.  
  478.    } // end if
  479.  
  480. // do trivial rejection tests
  481.  
  482. if ( y3<poly_clip_min_y || y1>poly_clip_max_y ||
  483.     (x1<poly_clip_min_x && x2<poly_clip_min_x && x3<poly_clip_min_x) ||
  484.     (x1>poly_clip_max_x && x2>poly_clip_max_x && x3>poly_clip_max_x) )
  485.    return;
  486.  
  487. // test if top of triangle is flat
  488.  
  489. if (y1==y2 || y2==y3)
  490.    {
  491.  
  492.    Draw_TB_Tri_3D_Z(x1,y1,z1,x2,y2,z2,x3,y3,z3,color);
  493.  
  494.    } // end if
  495. else
  496.    {
  497.    // general triangle that's needs to be broken up along long edge
  498.  
  499.    // commpute new x,z at split point
  500.  
  501.    new_x = x1 + (int)((float)(y2-y1)*(float)(x3-x1)/(float)(y3-y1));
  502.    new_z = z1 + (int)((float)(y2-y1)*(float)(z3-z1)/(float)(y3-y1));
  503.  
  504.    // draw each sub-triangle
  505.  
  506.    if (y2>=poly_clip_min_y && y1<poly_clip_max_y)
  507.       Draw_TB_Tri_3D_Z(x1,y1,z1,new_x,y2,new_z,x2,y2,z2,color);
  508.  
  509.    if (y3>=poly_clip_min_y && y1<poly_clip_max_y)
  510.       Draw_TB_Tri_3D_Z(x2,y2,z2,new_x,y2,new_z,x3,y3,z3,color);
  511.  
  512.    } // end else
  513.  
  514. } // end Draw_Tri_3D_Z
  515.  
  516. ///////////////////////////////////////////////////////////////////////////////
  517.  
  518. void Draw_Poly_List_Z(void)
  519. {
  520. // this function draws the global polygon list generated by calls to
  521. // Generate_Poly_List using the z buffer triangle system
  522.  
  523. int curr_poly,          // the current polygon
  524.     is_quad=0;          // quadrilateral flag
  525.  
  526. float x1,y1,z1,         // working variables
  527.       x2,y2,z2,
  528.       x3,y3,z3,
  529.       x4,y4,z4;
  530.  
  531. // draw each polygon in list
  532.  
  533. for (curr_poly=0; curr_poly<num_polys_frame; curr_poly++)
  534.     {
  535.  
  536.     // do Z clipping first before projection
  537.  
  538.     z1=world_polys[curr_poly]->vertex_list[0].z;
  539.     z2=world_polys[curr_poly]->vertex_list[1].z;
  540.     z3=world_polys[curr_poly]->vertex_list[2].z;
  541.  
  542.     // test if this is a quad
  543.  
  544.     if (world_polys[curr_poly]->num_points==4)
  545.        {
  546.        // extract vertex number and z component for clipping and projection
  547.  
  548.        z4=world_polys[curr_poly]->vertex_list[3].z;
  549.  
  550.        // set quad flag
  551.  
  552.        is_quad=1;
  553.  
  554.        } // end if quad
  555.     else
  556.       z4=z3;
  557.  
  558. #if 0
  559.  
  560.     // perform z clipping test
  561.  
  562.     if ( (z1<clip_near_z && z2<clip_near_z && z3<clip_near_z && z4<clip_near_z) ||
  563.          (z1>clip_far_z && z2>clip_far_z && z3>clip_far_z && z4>clip_far_z) )
  564.        continue;
  565.  
  566. #endif
  567.  
  568.         // extract points of polygon
  569.  
  570.         x1 = world_polys[curr_poly]->vertex_list[0].x;
  571.         y1 = world_polys[curr_poly]->vertex_list[0].y;
  572.  
  573.         x2 = world_polys[curr_poly]->vertex_list[1].x;
  574.         y2 = world_polys[curr_poly]->vertex_list[1].y;
  575.  
  576.         x3 = world_polys[curr_poly]->vertex_list[2].x;
  577.         y3 = world_polys[curr_poly]->vertex_list[2].y;
  578.  
  579.  
  580.         // compute screen position of points
  581.  
  582.         x1=(HALF_SCREEN_WIDTH  + x1*viewing_distance/z1);
  583.         y1=(HALF_SCREEN_HEIGHT - ASPECT_RATIO*y1*viewing_distance/z1);
  584.  
  585.         x2=(HALF_SCREEN_WIDTH  + x2*viewing_distance/z2);
  586.         y2=(HALF_SCREEN_HEIGHT - ASPECT_RATIO*y2*viewing_distance/z2);
  587.  
  588.         x3=(HALF_SCREEN_WIDTH  + x3*viewing_distance/z3);
  589.         y3=(HALF_SCREEN_HEIGHT - ASPECT_RATIO*y3*viewing_distance/z3);
  590.  
  591.         // draw triangle
  592.  
  593.         Draw_Tri_3D_Z((int)x1,(int)y1,(int)z1,
  594.                       (int)x2,(int)y2,(int)z2,
  595.                       (int)x3,(int)y3,(int)z3,
  596.                       world_polys[curr_poly]->shade);
  597.  
  598.  
  599.         // draw second poly if this is a quad
  600.  
  601.         if (is_quad)
  602.            {
  603.            // extract the point
  604.  
  605.            x4 = world_polys[curr_poly]->vertex_list[3].x;
  606.            y4 = world_polys[curr_poly]->vertex_list[3].y;
  607.  
  608.            // poject to screen
  609.  
  610.            x4=(HALF_SCREEN_WIDTH  + x4*viewing_distance/z4);
  611.            y4=(HALF_SCREEN_HEIGHT - ASPECT_RATIO*y4*viewing_distance/z4);
  612.  
  613.            // draw triangle
  614.  
  615.            Draw_Tri_3D_Z((int)x1,(int)y1,(int)z1,
  616.                          (int)x3,(int)y3,(int)z3,
  617.                          (int)x4,(int)y4,(int)z4,
  618.                          world_polys[curr_poly]->shade);
  619.  
  620.            } // end if quad
  621.  
  622.     } // end for curr_poly
  623.  
  624. } // end Draw_Poly_List_Z
  625.  
  626. ////////////////////////////////////////////////////////////////////////////////
  627.  
  628. int Create_Z_Buffer(unsigned int height)
  629. {
  630. // this function allocates the z buffer in two banks
  631.  
  632. // set global z buffer values
  633.  
  634. z_height   = height;
  635. z_height_2 = height/2;
  636.  
  637. z_bank_size = (height/2)*(unsigned int)640;
  638.  
  639. // allocate the memory
  640.  
  641. z_bank_1 = (int far *)_fmalloc(z_bank_size);
  642. z_bank_2 = (int far *)_fmalloc(z_bank_size);
  643.  
  644. // return success or failure
  645.  
  646. if (z_bank_1 && z_bank_2)
  647.    return(1);
  648. else
  649.    return(0);
  650.  
  651. } // end Create_Z_Buffer
  652.  
  653. ////////////////////////////////////////////////////////////////////////////////
  654.  
  655. void Delete_Z_Buffer(void)
  656. {
  657.  
  658. // this function free's up the memory used by the z buffer memory banks
  659.  
  660. _ffree(z_bank_1);
  661. _ffree(z_bank_2);
  662.  
  663.  
  664. } // end Delete_Z_Buffer
  665.  
  666. ////////////////////////////////////////////////////////////////////////////////
  667.  
  668. void Fill_Z_Buffer(int value)
  669. {
  670.  
  671. // this function fills the entire z buffer (both banks) with the sent value
  672.  
  673. _asm
  674.    {
  675.  
  676.    ; bank 1
  677.  
  678.    les di,z_bank_1       ; point es:di to z buffer bank 1
  679.    mov ax,value          ; move the value into ax
  680.    mov cx,z_bank_size    ; number of bytes to fill
  681.    shr cx,1              ; convert to number of words
  682.    rep stosw             ; move the value into z buffer
  683.  
  684.    ; bank 2
  685.  
  686.    les di,z_bank_2       ; point es:di to z buffer bank 1
  687.    mov ax,value          ; move the value into ax (redundant)
  688.    mov cx,z_bank_size    ; number of bytes to fill  (so is this)
  689.    shr cx,1              ; convert to number of words
  690.    rep stosw             ; move the value into z buffer
  691.  
  692.    } // end asm
  693.  
  694. } // end Fill_Z_Buffer
  695.  
  696. ///////////////////////////////////////////////////////////////////////////////
  697.  
  698. void Bsp_World_To_Camera(wall_ptr root)
  699. {
  700. // this function traverses the bsp tree and converts the world coordinates
  701. // to camera coordinates using the global transformation matrix note the
  702. // function is recursive and uses and inorder traversal, other traversals
  703. // such as preorder and postorder will would just as well...
  704.  
  705. static int index; // looping variable
  706.  
  707. // test if we have hit a dead end
  708.  
  709. if (root==NULL)
  710.    return;
  711.  
  712. // transform back most sub-tree
  713.  
  714. Bsp_World_To_Camera(root->back);
  715.  
  716. // iterate thru all vertices of current wall and transform them into
  717. // camera coordinates
  718.  
  719. for (index=0; index<4; index++)
  720.     {
  721.  
  722.     // multiply the point by the viewing transformation matrix
  723.  
  724.     // x component
  725.  
  726.     root->wall_camera[index].x =
  727.  
  728.                   root->wall_world[index].x * global_view[0][0] +
  729.                   root->wall_world[index].y * global_view[1][0] +
  730.                   root->wall_world[index].z * global_view[2][0] +
  731.                                               global_view[3][0];
  732.  
  733.     // y component
  734.  
  735.     root->wall_camera[index].y =
  736.  
  737.                   root->wall_world[index].x * global_view[0][1] +
  738.                   root->wall_world[index].y * global_view[1][1] +
  739.                   root->wall_world[index].z * global_view[2][1] +
  740.                                               global_view[3][1];
  741.     // z component
  742.  
  743.     root->wall_camera[index].z =
  744.  
  745.                   root->wall_world[index].x * global_view[0][2] +
  746.                   root->wall_world[index].y * global_view[1][2] +
  747.                   root->wall_world[index].z * global_view[2][2] +
  748.                                               global_view[3][2];
  749.  
  750.     } // end for index
  751.  
  752. // transform front most sub-tree
  753.  
  754. Bsp_World_To_Camera(root->front);
  755.  
  756. } // end Bsp_World_To_Camera
  757.  
  758. /////////////////////////////////////////////////////////////////////////////
  759.  
  760. void Bsp_Translate(wall_ptr root,int x_trans,int y_trans,int z_trans)
  761. {
  762. // this function translates all the walls that make up the bsp world
  763. // note function is recursive, we don't really need this function, but
  764. // it's a good example of how we might perform transformations on the BSP
  765. // tree and similar tree like structures using recursion
  766.  
  767. static int index; // looping variable
  768.  
  769. // test if we have hit a dead end
  770.  
  771. if (root==NULL)
  772.    return;
  773.  
  774. // translate back most sub-tree
  775.  
  776. Bsp_Translate(root->back,x_trans,y_trans,z_trans);
  777.  
  778. // iterate thru all vertices of current wall and translate them
  779.  
  780. for (index=0; index<4; index++)
  781.     {
  782.     // perform translation
  783.  
  784.     root->wall_world[index].x+=x_trans;
  785.     root->wall_world[index].y+=y_trans;
  786.     root->wall_world[index].z+=z_trans;
  787.  
  788.     } // end for index
  789.  
  790. // translate front most sub-tree
  791.  
  792. Bsp_Translate(root->front,x_trans,y_trans,z_trans);
  793.  
  794. } // end Bsp_Translate
  795.  
  796. ///////////////////////////////////////////////////////////////////////////////
  797.  
  798. void Bsp_Shade(wall_ptr root)
  799. {
  800. // this function shades the bsp tree and need only be called if the global
  801. // lightsource changes position
  802.  
  803. static int index; // looping variable
  804.  
  805. static float normal_length, // length of surface normal
  806.              intensity,     // intensity of light falling on surface being processed
  807.              dp;            // result of dot product
  808.  
  809. // test if we have hit a dead end
  810.  
  811. if (root==NULL)
  812.    return;
  813.  
  814. // shade the back most sub-tree
  815.  
  816. Bsp_Shade(root->back);
  817.  
  818. // compute the dot product between line of sight vector and normal to surface
  819.  
  820. dp = Dot_Product_3D((vector_3d_ptr)&root->normal,(vector_3d_ptr)&light_source);
  821.  
  822. // compute length of normal of surface normal, remember this function
  823. // doesn't need to be time critical since it is only called once at startup
  824. // or whenever the light source moves
  825.  
  826. normal_length = Vector_Mag_3D((vector_3d_ptr)&root->normal);
  827.  
  828. // cos 0 = (u.v)/|u||v| or
  829.  
  830. intensity = ambient_light + (15*dp/normal_length);
  831.  
  832. // test if intensity has overflowed
  833.  
  834. if (intensity >15)
  835.     intensity = 15;
  836.  
  837. // intensity now varies from 0-1, 0 being black or grazing and 1 being
  838. // totally illuminated. use the value to index into color table
  839.  
  840. root->color = BSP_WALL_SHADE - (int)(fabs(intensity));
  841.  
  842. // shade the front most sub-tree
  843.  
  844. Bsp_Shade(root->front);
  845.  
  846. } // end Bsp_Shade
  847.  
  848. /////////////////////////////////////////////////////////////////////////////
  849.  
  850. void Bsp_Traverse(wall_ptr root)
  851. {
  852. // this function traverses the BSP tree and generates the polygon list used
  853. // by Draw_Polys() for the current global viewpoint (note the view angle is
  854. // irrelevant), also as the polygon list is being generated, only polygons
  855. // that are within the z extents are added to the polygon, in essence, the
  856. // function is performing Z clipping also, this is to minimize the amount
  857. // of polygons in the graphics pipeline that will have to be processed during
  858. // rendering
  859.  
  860. // this function works be testing the viewpoint against the current wall
  861. // in the bsp, then depending on the side the viewpoint is the algorithm
  862. // proceeds. the search takes place as the rest in an "inorder" method
  863. // with hooks to process and add each node into the polygon list at the
  864. // right time
  865.  
  866. static vector_3d test_vector;
  867.  
  868. static float dot_wall,
  869.              z1,z2,z3,z4;
  870.  
  871. // SECTION 1  ////////////////////////////////////////////////////////////////
  872.  
  873. // is this a dead end?
  874.  
  875. if (root==NULL)
  876.    return;
  877.  
  878. // test which side viewpoint is on relative to the current wall
  879.  
  880. Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  881.                (point_3d_ptr)&view_point,
  882.                (vector_3d_ptr)&test_vector);
  883.  
  884. // now dot test vector with the surface normal and analyze signs
  885.  
  886. dot_wall = Dot_Product_3D((vector_3d_ptr)&test_vector,
  887.                           (vector_3d_ptr)&root->normal);
  888.  
  889. // SECTION 2  ////////////////////////////////////////////////////////////////
  890.  
  891. // if the sign of the dot product is positive then the viewer on on the
  892. // front side of current wall, so recursively process the walls behind then
  893. // in front of this wall, else do the opposite
  894.  
  895. if (dot_wall>0)
  896.    {
  897.    // viewer is in front of this wall
  898.  
  899.    // process the back wall sub tree
  900.  
  901.    Bsp_Traverse(root->back);
  902.  
  903.    // try to add this wall to the polygon list if it's within the Z extents
  904.  
  905.    z1=root->wall_camera[0].z;
  906.    z2=root->wall_camera[1].z;
  907.    z3=root->wall_camera[2].z;
  908.    z4=root->wall_camera[3].z;
  909.  
  910.    // perform the z extents clipping test
  911.  
  912.  
  913.    if ( (z1>clip_near_z && z1<clip_far_z ) || (z2>clip_near_z && z2<clip_far_z ) ||
  914.         (z3>clip_near_z && z3<clip_far_z ) || (z4>clip_near_z && z4<clip_far_z ))
  915.       {
  916.  
  917.       // first copy data and vertices into an open slot in storage area
  918.  
  919.       world_poly_storage[num_polys_frame].num_points = 4;
  920.       world_poly_storage[num_polys_frame].color      = BSP_WALL_COLOR;
  921.       world_poly_storage[num_polys_frame].shade      = root->color;
  922.       world_poly_storage[num_polys_frame].shading    = 0;
  923.       world_poly_storage[num_polys_frame].two_sided  = 1;
  924.       world_poly_storage[num_polys_frame].visible    = 1;
  925.       world_poly_storage[num_polys_frame].clipped    = 0;
  926.       world_poly_storage[num_polys_frame].active     = 1;
  927.  
  928.       // now copy vertices
  929.  
  930.       world_poly_storage[num_polys_frame].vertex_list[0].x = root->wall_camera[0].x;
  931.       world_poly_storage[num_polys_frame].vertex_list[0].y = root->wall_camera[0].y;
  932.       world_poly_storage[num_polys_frame].vertex_list[0].z = root->wall_camera[0].z;
  933.  
  934.       world_poly_storage[num_polys_frame].vertex_list[1].x = root->wall_camera[1].x;
  935.       world_poly_storage[num_polys_frame].vertex_list[1].y = root->wall_camera[1].y;
  936.       world_poly_storage[num_polys_frame].vertex_list[1].z = root->wall_camera[1].z;
  937.  
  938.       world_poly_storage[num_polys_frame].vertex_list[2].x = root->wall_camera[2].x;
  939.       world_poly_storage[num_polys_frame].vertex_list[2].y = root->wall_camera[2].y;
  940.       world_poly_storage[num_polys_frame].vertex_list[2].z = root->wall_camera[2].z;
  941.  
  942.       world_poly_storage[num_polys_frame].vertex_list[3].x = root->wall_camera[3].x;
  943.       world_poly_storage[num_polys_frame].vertex_list[3].y = root->wall_camera[3].y;
  944.       world_poly_storage[num_polys_frame].vertex_list[3].z = root->wall_camera[3].z;
  945.  
  946.  
  947.       // assign poly list pointer to it
  948.  
  949.       world_polys[num_polys_frame] = &world_poly_storage[num_polys_frame];
  950.  
  951.       // increment number of polys in this frame
  952.  
  953.       num_polys_frame++;
  954.  
  955.       } // end if polygon is visible
  956.  
  957.    // now process the front walls sub tree
  958.  
  959.    Bsp_Traverse(root->front);
  960.  
  961.    } // end if
  962.  
  963. // SECTION 3 ////////////////////////////////////////////////////////////////
  964.  
  965. else
  966.    {
  967.    // viewer is behind this wall
  968.  
  969.    // process the front wall sub tree
  970.  
  971.    Bsp_Traverse(root->front);
  972.  
  973.    // try to add this wall to the polygon list if it's within the Z extents
  974.  
  975.    z1=root->wall_camera[0].z;
  976.    z2=root->wall_camera[1].z;
  977.    z3=root->wall_camera[2].z;
  978.    z4=root->wall_camera[3].z;
  979.  
  980.    // perform the z extents clipping test
  981.  
  982.    if ( (z1>clip_near_z && z1<clip_far_z ) || (z2>clip_near_z && z2<clip_far_z )  ||
  983.         (z3>clip_near_z && z3<clip_far_z ) || (z4>clip_near_z && z4<clip_far_z ))
  984.       {
  985.  
  986.       // first copy data and vertices into an open slot in storage area
  987.  
  988.       world_poly_storage[num_polys_frame].num_points = 4;
  989.       world_poly_storage[num_polys_frame].color      = BSP_WALL_COLOR;
  990.       world_poly_storage[num_polys_frame].shade      = root->color;
  991.       world_poly_storage[num_polys_frame].shading    = 0;
  992.       world_poly_storage[num_polys_frame].two_sided  = 1;
  993.       world_poly_storage[num_polys_frame].visible    = 1;
  994.       world_poly_storage[num_polys_frame].clipped    = 0;
  995.       world_poly_storage[num_polys_frame].active     = 1;
  996.  
  997.       // now copy vertices, note that we don't use a structure copy, it's
  998.       // not dependable
  999.  
  1000.       world_poly_storage[num_polys_frame].vertex_list[0].x = root->wall_camera[0].x;
  1001.       world_poly_storage[num_polys_frame].vertex_list[0].y = root->wall_camera[0].y;
  1002.       world_poly_storage[num_polys_frame].vertex_list[0].z = root->wall_camera[0].z;
  1003.  
  1004.       world_poly_storage[num_polys_frame].vertex_list[1].x = root->wall_camera[1].x;
  1005.       world_poly_storage[num_polys_frame].vertex_list[1].y = root->wall_camera[1].y;
  1006.       world_poly_storage[num_polys_frame].vertex_list[1].z = root->wall_camera[1].z;
  1007.  
  1008.       world_poly_storage[num_polys_frame].vertex_list[2].x = root->wall_camera[2].x;
  1009.       world_poly_storage[num_polys_frame].vertex_list[2].y = root->wall_camera[2].y;
  1010.       world_poly_storage[num_polys_frame].vertex_list[2].z = root->wall_camera[2].z;
  1011.  
  1012.       world_poly_storage[num_polys_frame].vertex_list[3].x = root->wall_camera[3].x;
  1013.       world_poly_storage[num_polys_frame].vertex_list[3].y = root->wall_camera[3].y;
  1014.       world_poly_storage[num_polys_frame].vertex_list[3].z = root->wall_camera[3].z;
  1015.  
  1016.  
  1017.       // assign poly list pointer to it
  1018.  
  1019.       world_polys[num_polys_frame] = &world_poly_storage[num_polys_frame];
  1020.  
  1021.       // increment number of polys in this frame
  1022.  
  1023.       num_polys_frame++;
  1024.  
  1025.       } // end if polygon is visible
  1026.  
  1027.    // now process the front walls sub tree
  1028.  
  1029.    Bsp_Traverse(root->back);
  1030.  
  1031.    } // end else
  1032.  
  1033. } // end Bsp_Traverse
  1034.  
  1035. /////////////////////////////////////////////////////////////////////////////
  1036.  
  1037. void Bsp_Delete(wall_ptr root)
  1038. {
  1039. // this function recursively deletes all the nodes in the bsp tree and free's
  1040. // the memory back to the OS.
  1041.  
  1042. wall_ptr temp_wall; // a temporary wall
  1043.  
  1044. // test if we have hit a dead end
  1045.  
  1046. if (root==NULL)
  1047.    return;
  1048.  
  1049. // delete back sub tree
  1050.  
  1051. Bsp_Delete(root->back);
  1052.  
  1053. // delete this node, but first save the front sub-tree
  1054.  
  1055. temp_wall = root->front;
  1056.  
  1057. // delete the memory
  1058.  
  1059. free(root);
  1060.  
  1061. // assign the root to the saved front most sub-tree
  1062.  
  1063. root = temp_wall;
  1064.  
  1065. // delete front sub tree
  1066.  
  1067. Bsp_Delete(root);
  1068.  
  1069. } // end Bsp_Delete
  1070.  
  1071. /////////////////////////////////////////////////////////////////////////////
  1072.  
  1073. void Bsp_Print(wall_ptr root)
  1074. {
  1075. // this function performs a recursive in-order traversal of the BSP tree and
  1076. // prints the results out to the file opened with fp_out as the handle
  1077.  
  1078. // test if this child is null
  1079.  
  1080. if (root==NULL)
  1081.    {
  1082.    fprintf(fp_out,"\nReached NULL node returning...");
  1083.    return;
  1084.    } // end if
  1085.  
  1086. // search left tree (back walls)
  1087.  
  1088. fprintf(fp_out,"\nTraversing back sub-tree...");
  1089.  
  1090. Bsp_Print(root->back);
  1091.  
  1092. // visit node
  1093.  
  1094. fprintf(fp_out,"\n\n\nWall ID #%d",root->id);
  1095. fprintf(fp_out,"\nVertices...");
  1096. fprintf(fp_out,"\nVertex 0: (%f,%f,%f)",root->wall_world[0].x,
  1097.                                         root->wall_world[0].y,
  1098.                                         root->wall_world[0].z);
  1099.  
  1100. fprintf(fp_out,"\nVertex 1: (%f,%f,%f)",root->wall_world[1].x,
  1101.                                         root->wall_world[1].y,
  1102.                                         root->wall_world[1].z);
  1103.  
  1104. fprintf(fp_out,"\nVertex 2: (%f,%f,%f)",root->wall_world[2].x,
  1105.                                         root->wall_world[2].y,
  1106.                                         root->wall_world[2].z);
  1107.  
  1108. fprintf(fp_out,"\nVertex 3: (%f,%f,%f)",root->wall_world[3].x,
  1109.                                         root->wall_world[3].y,
  1110.                                         root->wall_world[3].z);
  1111.  
  1112. fprintf(fp_out,"\nNormal (%f,%f,%f)",root->normal.x,
  1113.                                      root->normal.y,
  1114.                                      root->normal.z);
  1115.  
  1116. fprintf(fp_out,"\nEnd wall data\n");
  1117.  
  1118. // search right tree (front walls)
  1119.  
  1120. fprintf(fp_out,"\nTraversing front sub-tree..");
  1121.  
  1122. Bsp_Print(root->front);
  1123.  
  1124. } // end Bsp_Print
  1125.  
  1126. //////////////////////////////////////////////////////////////////////////////
  1127.  
  1128. void Intersect_Lines(float x0,float y0,float x1,float y1,
  1129.                      float x2,float y2,float x3,float y3,
  1130.                      float *xi,float *yi)
  1131. {
  1132. // this function computes the intersection of the sent lines
  1133. // and returns the intersection point, note that the function assumes
  1134. // the lines intersect. the function can handle vertical as well
  1135. // as horizontal lines. note the function isn't very clever, it simply applies
  1136. // the math, but we don't need speed since this is a pre-processing step
  1137.  
  1138. float a1,b1,c1, // constants of linear equations
  1139.       a2,b2,c2,
  1140.       det_inv,  // the inverse of the determinant of the coefficient matrix
  1141.       m1,m2;    // the slopes of each line
  1142.  
  1143. // compute slopes, note the cludge for infinity, however, this will
  1144. // be close enough
  1145.  
  1146. if ((x1-x0)!=0)
  1147.    m1 = (y1-y0)/(x1-x0);
  1148. else
  1149.    m1 = (float)1e+10;   // close enough to infinity
  1150.  
  1151. if ((x3-x2)!=0)
  1152.    m2 = (y3-y2)/(x3-x2);
  1153. else
  1154.    m2 = (float)1e+10;   // close enough to infinity
  1155.  
  1156. // compute constants
  1157.  
  1158. a1 = m1;
  1159. a2 = m2;
  1160.  
  1161. b1 = -1;
  1162. b2 = -1;
  1163.  
  1164. c1 = (y0-m1*x0);
  1165. c2 = (y2-m2*x2);
  1166.  
  1167. // compute the inverse of the determinate
  1168.  
  1169. det_inv = 1/(a1*b2 - a2*b1);
  1170.  
  1171. // use Kramers rule to compute xi and yi
  1172.  
  1173. *xi=((b1*c2 - b2*c1)*det_inv);
  1174. *yi=((a2*c1 - a1*c2)*det_inv);
  1175.  
  1176. } // end Intersect_Lines
  1177.  
  1178. /////////////////////////////////////////////////////////////////////////////
  1179.  
  1180. void Build_Bsp_Tree(wall_ptr root)
  1181. {
  1182. // this function recursively builds the bsp tree from the sent wall list
  1183. // note the function has sone calls to Draw_Line() and a Time_Delay() at
  1184. // the end, these are for illustrative purposes only for the demo interface
  1185. // and should be removed if you wish to use this function in a real
  1186. // application
  1187.  
  1188. static wall_ptr next_wall,     // pointer to next wall to be processed
  1189.                 front_wall,    // the front wall
  1190.                 back_wall,     // the back wall
  1191.                 temp_wall;     // a temporary wall
  1192.  
  1193. static float
  1194.  
  1195.       dot_wall_1,                // dot products for test wall
  1196.       dot_wall_2,
  1197.       wall_x0,wall_y0,wall_z0,   // working vars for test wall
  1198.       wall_x1,wall_y1,wall_z1,
  1199.       pp_x0,pp_y0,pp_z0,         // working vars for partitioning plane
  1200.       pp_x1,pp_y1,pp_z1,
  1201.       xi,zi;                     // points of intersection when the partioning
  1202.                                  // plane cuts a wall in two
  1203.  
  1204. static vector_3d test_vector_1,  // test vectors from the partioning plane
  1205.            test_vector_2;        // to the test wall to test the side
  1206.                                  // of the partioning plane the test wall
  1207.                                  // lies on
  1208.  
  1209. static int front_flag =0,        // flags if a wall is on the front or back
  1210.            back_flag = 0,        // of the partioning plane
  1211.            index;                // looping index
  1212.  
  1213. // SECTION 1 ////////////////////////////////////////////////////////////////
  1214.  
  1215. // test if this tree is complete
  1216.  
  1217. if (root==NULL)
  1218.    return;
  1219.  
  1220. // the root is the partitioning plane, partition the polygons using it
  1221.  
  1222. next_wall  = root->link;
  1223. root->link = NULL;
  1224.  
  1225. // extract top two vertices of partioning plane wall for ease of calculations
  1226.  
  1227. pp_x0 = root->wall_world[0].x;
  1228. pp_y0 = root->wall_world[0].y;
  1229. pp_z0 = root->wall_world[0].z;
  1230.  
  1231. pp_x1 = root->wall_world[1].x;
  1232. pp_y1 = root->wall_world[1].y;
  1233. pp_z1 = root->wall_world[1].z;
  1234.  
  1235. // highlight space partition green
  1236.  
  1237. Draw_Line(pp_x0/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1238.           pp_z0/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1239.           pp_x1/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1240.           pp_z1/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1241.           10,
  1242.           video_buffer);
  1243.  
  1244. // SECTION 2  ////////////////////////////////////////////////////////////////
  1245.  
  1246. // test if all walls have been partitioned
  1247.  
  1248. while(next_wall)
  1249.      {
  1250.  
  1251.      // test which side test wall is relative to partioning plane
  1252.      // defined by root
  1253.  
  1254.      // first compute vectors from point on partioning plane to point on
  1255.      // test wall
  1256.  
  1257.      Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  1258.                     (point_3d_ptr)&next_wall->wall_world[0],
  1259.                     (vector_3d_ptr)&test_vector_1);
  1260.  
  1261.      Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  1262.                     (point_3d_ptr)&next_wall->wall_world[1],
  1263.                     (vector_3d_ptr)&test_vector_2);
  1264.  
  1265.      // now dot each test vector with the surface normal and analyze signs
  1266.  
  1267.      dot_wall_1 = Dot_Product_3D((vector_3d_ptr)&test_vector_1,
  1268.                                  (vector_3d_ptr)&root->normal);
  1269.  
  1270.  
  1271.      dot_wall_2 = Dot_Product_3D((vector_3d_ptr)&test_vector_2,
  1272.                                  (vector_3d_ptr)&root->normal);
  1273.  
  1274.  
  1275. // SECTION 3  ////////////////////////////////////////////////////////////////
  1276.  
  1277.      // perform the tests
  1278.  
  1279.      // case 0, the partioning plane and the test wall have a point in common
  1280.      // this is a special case and must be accounted for, shorten the code
  1281.      // we will set a pait of flags and then the next case will handle
  1282.      // the actual insertion of the wall into BSP
  1283.  
  1284.      // reset flags
  1285.  
  1286.      front_flag = back_flag = 0;
  1287.  
  1288.      // determine of wall is tangent to endpoints of partitioning wall
  1289.  
  1290.      if (POINTS_EQUAL_3D(root->wall_world[0],next_wall->wall_world[0]) )
  1291.         {
  1292.         // p0 of partioning plane is the same at p0 of test wall
  1293.         // we only need to see what side p1 of test wall in on
  1294.  
  1295.         if (dot_wall_2 > 0)
  1296.            front_flag = 1;
  1297.         else
  1298.            back_flag = 1;
  1299.  
  1300.         } // end if
  1301.      else
  1302.      if (POINTS_EQUAL_3D(root->wall_world[0],next_wall->wall_world[1]) )
  1303.         {
  1304.         // p0 of partioning plane is the same at p1 of test wall
  1305.         // we only need to see what side p0 of test wall in on
  1306.  
  1307.         if (dot_wall_1 > 0)
  1308.            front_flag = 1;
  1309.         else
  1310.            back_flag = 1;
  1311.  
  1312.  
  1313.         } // end if
  1314.      else
  1315.      if (POINTS_EQUAL_3D(root->wall_world[1],next_wall->wall_world[0]) )
  1316.         {
  1317.         // p1 of partioning plane is the same at p0 of test wall
  1318.         // we only need to see what side p1 of test wall in on
  1319.  
  1320.         if (dot_wall_2 > 0)
  1321.            front_flag = 1;
  1322.         else
  1323.            back_flag = 1;
  1324.  
  1325.         } // end if
  1326.      else
  1327.      if (POINTS_EQUAL_3D(root->wall_world[1],next_wall->wall_world[1]) )
  1328.         {
  1329.         // p1 of partioning plane is the same at p1 of test wall
  1330.         // we only need to see what side p0 of test wall in on
  1331.  
  1332.         if (dot_wall_1 > 0)
  1333.            front_flag = 1;
  1334.         else
  1335.            back_flag = 1;
  1336.  
  1337.         } // end if
  1338.  
  1339. // SECTION 4  ////////////////////////////////////////////////////////////////
  1340.  
  1341.      // case 1 both signs are the same or the front or back flag has been set
  1342.  
  1343.      if ( (dot_wall_1 >= 0 && dot_wall_2 >= 0) || front_flag )
  1344.         {
  1345.  
  1346.         // highlight the wall blue
  1347.  
  1348.         Draw_Line(next_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1349.                   next_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1350.                   next_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1351.                   next_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1352.                   9,
  1353.                   video_buffer);
  1354.  
  1355.         // place this wall on the front list
  1356.  
  1357.         if (root->front==NULL)
  1358.            {
  1359.            // this is the first node
  1360.  
  1361.            root->front      = next_wall;
  1362.            next_wall        = next_wall->link;
  1363.            front_wall       = root->front;
  1364.            front_wall->link = NULL;
  1365.  
  1366.            } // end if
  1367.         else
  1368.            {
  1369.            // this is the nth node
  1370.  
  1371.            front_wall->link = next_wall;
  1372.            next_wall        = next_wall->link;
  1373.            front_wall       = front_wall->link;
  1374.            front_wall->link = NULL;
  1375.  
  1376.            } // end else
  1377.  
  1378.         } // end if both positive
  1379.  
  1380. // SECTION  5 ////////////////////////////////////////////////////////////////
  1381.      else
  1382.      if ( (dot_wall_1 < 0 && dot_wall_2 < 0) || back_flag)
  1383.         {
  1384.  
  1385.         // highlight the wall red
  1386.  
  1387.         Draw_Line(next_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1388.                   next_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1389.                   next_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1390.                   next_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1391.                   12,
  1392.                   video_buffer);
  1393.  
  1394.         // place this wall on the back list
  1395.  
  1396.         if (root->back==NULL)
  1397.            {
  1398.            // this is the first node
  1399.  
  1400.            root->back      = next_wall;
  1401.            next_wall       = next_wall->link;
  1402.            back_wall       = root->back;
  1403.            back_wall->link = NULL;
  1404.  
  1405.            } // end if
  1406.         else
  1407.            {
  1408.            // this is the nth node
  1409.  
  1410.            back_wall->link = next_wall;
  1411.            next_wall       = next_wall->link;
  1412.            back_wall       = back_wall->link;
  1413.            back_wall->link = NULL;
  1414.  
  1415.            } // end else
  1416.  
  1417.         } // end if both negative
  1418.  
  1419.      // case 2 both signs are different
  1420.  
  1421. // SECTION 6  ////////////////////////////////////////////////////////////////
  1422.  
  1423.      else
  1424.      if ( (dot_wall_1 < 0 && dot_wall_2 >= 0) ||
  1425.           (dot_wall_1 >= 0 && dot_wall_2 < 0))
  1426.  
  1427.         {
  1428.         // the partioning plane cuts the wall in half, the wall
  1429.         // must be split into two walls
  1430.  
  1431.         // extract top two vertices of test wall for ease of calculations
  1432.  
  1433.         wall_x0 = next_wall->wall_world[0].x;
  1434.         wall_y0 = next_wall->wall_world[0].y;
  1435.         wall_z0 = next_wall->wall_world[0].z;
  1436.  
  1437.         wall_x1 = next_wall->wall_world[1].x;
  1438.         wall_y1 = next_wall->wall_world[1].y;
  1439.         wall_z1 = next_wall->wall_world[1].z;
  1440.  
  1441.         // compute the point of intersection between the walls
  1442.         // note that x and z are the plane that the intersection takes place in
  1443.  
  1444.         Intersect_Lines(wall_x0,wall_z0,wall_x1,wall_z1,
  1445.                         pp_x0,pp_z0,pp_x1,pp_z1,
  1446.                         &xi,&zi);
  1447.  
  1448.         // here comes the tricky part, we need to slit the wall in half and
  1449.         // create two walls. We'll do this by creating two new walls,
  1450.         // placing them on the appropriate front and back lists and
  1451.         // then deleting the original wall
  1452.  
  1453.         // process first wall
  1454.  
  1455.         // allocate the memory for the wall
  1456.  
  1457.         temp_wall = (wall_ptr)malloc(sizeof(wall));
  1458.  
  1459.         temp_wall->front = NULL;
  1460.         temp_wall->back  = NULL;
  1461.         temp_wall->link  = NULL;
  1462.  
  1463.         temp_wall->normal = next_wall->normal;
  1464.         temp_wall->id     = next_wall->id+1000; // add 1000 to denote a split
  1465.  
  1466.         // compute wall vertices
  1467.  
  1468.         for (index=0; index<4; index++)
  1469.             {
  1470.             temp_wall->wall_world[index].x = next_wall->wall_world[index].x;
  1471.             temp_wall->wall_world[index].y = next_wall->wall_world[index].y;
  1472.             temp_wall->wall_world[index].z = next_wall->wall_world[index].z;
  1473.             } // end for index
  1474.  
  1475.         // now modify vertices 1 and 2 to reflect intersection point
  1476.         // but leave y alone since it's invariant for the wall spliting
  1477.  
  1478.         temp_wall->wall_world[1].x = xi;
  1479.         temp_wall->wall_world[1].z = zi;
  1480.  
  1481.         temp_wall->wall_world[2].x = xi;
  1482.         temp_wall->wall_world[2].z = zi;
  1483.  
  1484. // SECTION 7  ////////////////////////////////////////////////////////////////
  1485.  
  1486.         // insert new wall into front or back of root
  1487.  
  1488.         if (dot_wall_1 >= 0)
  1489.            {
  1490.  
  1491.            // highlight the wall blue
  1492.  
  1493.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1494.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1495.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1496.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1497.                      9,
  1498.                      video_buffer);
  1499.  
  1500.  
  1501.            // place this wall on the front list
  1502.  
  1503.            if (root->front==NULL)
  1504.               {
  1505.               // this is the first node
  1506.  
  1507.               root->front      = temp_wall;
  1508.               front_wall       = root->front;
  1509.               front_wall->link = NULL;
  1510.  
  1511.               } // end if
  1512.            else
  1513.               {
  1514.               // this is the nth node
  1515.  
  1516.               front_wall->link = temp_wall;
  1517.               front_wall       = front_wall->link;
  1518.               front_wall->link = NULL;
  1519.  
  1520.               } // end else
  1521.  
  1522.            } // end if positive
  1523.         else
  1524.         if (dot_wall_1 < 0)
  1525.            {
  1526.            // highlight the wall red
  1527.  
  1528.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1529.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1530.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1531.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1532.                      12,
  1533.                      video_buffer);
  1534.  
  1535.            // place this wall on the back list
  1536.  
  1537.            if (root->back==NULL)
  1538.               {
  1539.               // this is the first node
  1540.  
  1541.               root->back      = temp_wall;
  1542.               back_wall       = root->back;
  1543.               back_wall->link = NULL;
  1544.  
  1545.               } // end if
  1546.            else
  1547.               {
  1548.               // this is the nth node
  1549.  
  1550.               back_wall->link = temp_wall;
  1551.               back_wall       = back_wall->link;
  1552.               back_wall->link = NULL;
  1553.  
  1554.               } // end else
  1555.  
  1556.            } // end if negative
  1557.  
  1558. // SECTION  8 ////////////////////////////////////////////////////////////////
  1559.  
  1560.         // process second wall
  1561.  
  1562.         // allocate the memory for the wall
  1563.  
  1564.         temp_wall = (wall_ptr)malloc(sizeof(wall));
  1565.  
  1566.         temp_wall->front = NULL;
  1567.         temp_wall->back  = NULL;
  1568.         temp_wall->link  = NULL;
  1569.  
  1570.         temp_wall->normal = next_wall->normal;
  1571.         temp_wall->id     = next_wall->id+1000;
  1572.  
  1573.         // compute wall vertices
  1574.  
  1575.         for (index=0; index<4; index++)
  1576.             {
  1577.             temp_wall->wall_world[index].x = next_wall->wall_world[index].x;
  1578.             temp_wall->wall_world[index].y = next_wall->wall_world[index].y;
  1579.             temp_wall->wall_world[index].z = next_wall->wall_world[index].z;
  1580.             } // end for index
  1581.  
  1582.         // now modify vertices 0 and 0 to reflect intersection point
  1583.         // but leave y alone since it's invariant for the wall spliting
  1584.  
  1585.         temp_wall->wall_world[0].x = xi;
  1586.         temp_wall->wall_world[0].z = zi;
  1587.  
  1588.         temp_wall->wall_world[3].x = xi;
  1589.         temp_wall->wall_world[3].z = zi;
  1590.  
  1591.         // insert new wall into front or back of root
  1592.  
  1593.         if (dot_wall_2 >= 0)
  1594.            {
  1595.            // highlight the wall blue
  1596.  
  1597.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1598.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1599.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1600.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1601.                      9,
  1602.                      video_buffer);
  1603.  
  1604.            // place this wall on the front list
  1605.  
  1606.            if (root->front==NULL)
  1607.               {
  1608.               // this is the first node
  1609.  
  1610.               root->front      = temp_wall;
  1611.               front_wall       = root->front;
  1612.               front_wall->link = NULL;
  1613.  
  1614.               } // end if
  1615.            else
  1616.               {
  1617.               // this is the nth node
  1618.  
  1619.               front_wall->link = temp_wall;
  1620.               front_wall       = front_wall->link;
  1621.               front_wall->link = NULL;
  1622.  
  1623.               } // end else
  1624.  
  1625.  
  1626.            } // end if positive
  1627.         else
  1628.         if (dot_wall_2 < 0)
  1629.            {
  1630.            // highlight the wall red
  1631.  
  1632.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1633.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1634.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1635.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1636.                      12,
  1637.                      video_buffer);
  1638.  
  1639.            // place this wall on the back list
  1640.  
  1641.            if (root->back==NULL)
  1642.               {
  1643.               // this is the first node
  1644.  
  1645.               root->back      = temp_wall;
  1646.               back_wall       = root->back;
  1647.               back_wall->link = NULL;
  1648.  
  1649.               } // end if
  1650.            else
  1651.               {
  1652.               // this is the nth node
  1653.  
  1654.               back_wall->link = temp_wall;
  1655.               back_wall       = back_wall->link;
  1656.               back_wall->link = NULL;
  1657.  
  1658.               } // end else
  1659.  
  1660.            } // end if negative
  1661.  
  1662. // SECTION  9  ////////////////////////////////////////////////////////////////
  1663.  
  1664.         // we are now done splitting the wall, so we can delete it
  1665.  
  1666.         temp_wall = next_wall;
  1667.         next_wall = next_wall->link;
  1668.         free(temp_wall);
  1669.  
  1670.         } // end else
  1671.  
  1672.      } // end while
  1673.  
  1674. // SECTION  10 ////////////////////////////////////////////////////////////////
  1675.  
  1676. // delay a bit so user can see BSP being created
  1677.  
  1678. Time_Delay(5);
  1679.  
  1680. // recursively process front and back walls
  1681.  
  1682. Build_Bsp_Tree(root->front);
  1683.  
  1684. Build_Bsp_Tree(root->back);
  1685.  
  1686. } // end Build_Bsp_Tree
  1687.  
  1688. //////////////////////////////////////////////////////////////////////////////
  1689.  
  1690. void Bsp_View(wall_ptr bsp_root)
  1691. {
  1692. // this function is a self contained viewing processor that has it's own event
  1693. // loop, the display will continue to be generated until the ESC key is pressed
  1694.  
  1695. int done=0;
  1696.  
  1697. // install the isr keyboard driver
  1698.  
  1699. Keyboard_Install_Driver();
  1700.  
  1701. // change the light source direction
  1702.  
  1703. light_source.x =(float)0.398636;
  1704. light_source.y =(float)-0.374248;
  1705. light_source.z =(float)0.8372275;
  1706.  
  1707. // reset viewpoint to (0,0,0)
  1708.  
  1709. view_point.x = 0;
  1710. view_point.y = 0;
  1711. view_point.z = 0;
  1712.  
  1713. // main event loop
  1714.  
  1715. while(!done)
  1716.      {
  1717.      // compute starting time of this frame
  1718.  
  1719.      starting_time = Timer_Query();
  1720.  
  1721.      // erase all objects
  1722.  
  1723.      Fill_Double_Buffer(0);
  1724.  
  1725.      // move viewpoint
  1726.  
  1727.      if (keyboard_state[MAKE_UP])
  1728.         view_point.y+=20;
  1729.  
  1730.      if (keyboard_state[MAKE_DOWN])
  1731.         view_point.y-=20;
  1732.  
  1733.      if (keyboard_state[MAKE_RIGHT])
  1734.         view_point.x+=20;
  1735.  
  1736.      if (keyboard_state[MAKE_LEFT])
  1737.         view_point.x-=20;
  1738.  
  1739.      if (keyboard_state[MAKE_KEYPAD_PLUS])
  1740.         view_point.z+=20;
  1741.  
  1742.      if (keyboard_state[MAKE_KEYPAD_MINUS])
  1743.         view_point.z-=20;
  1744.  
  1745.      if (keyboard_state[MAKE_Z])
  1746.         if ((view_angle.ang_x+=10)>360)
  1747.            view_angle.ang_x = 0;
  1748.  
  1749.      if (keyboard_state[MAKE_A])
  1750.         if ((view_angle.ang_x-=10)<0)
  1751.            view_angle.ang_x = 360;
  1752.  
  1753.      if (keyboard_state[MAKE_X])
  1754.         if ((view_angle.ang_y+=10)>360)
  1755.            view_angle.ang_y = 0;
  1756.  
  1757.      if (keyboard_state[MAKE_S])
  1758.         if ((view_angle.ang_y-=10)<0)
  1759.            view_angle.ang_y = 360;
  1760.  
  1761.      if (keyboard_state[MAKE_C])
  1762.         if ((view_angle.ang_z+=10)>360)
  1763.            view_angle.ang_z = 0;
  1764.  
  1765.      if (keyboard_state[MAKE_D])
  1766.         if ((view_angle.ang_z-=10)<0)
  1767.            view_angle.ang_z = 360;
  1768.  
  1769.      if (keyboard_state[MAKE_ESC])
  1770.         done=1;
  1771.  
  1772.      // now that user has possible moved viewpoint, create the global
  1773.      // world to camera transformation matrix
  1774.  
  1775.      Create_World_To_Camera();
  1776.  
  1777.      // now convert the bsp tree world coordinates into camera coordinates
  1778.  
  1779.      Bsp_World_To_Camera(bsp_root);
  1780.  
  1781.      // reset number of polygons in polygon list
  1782.  
  1783.      num_polys_frame = 0;
  1784.  
  1785.      // traverse the BSP tree and generate the polygon list
  1786.  
  1787.      Bsp_Traverse(bsp_root);
  1788.  
  1789.      // draw the polygon list generated by traversing the BSP tree
  1790.  
  1791.      Draw_Poly_List();
  1792.  
  1793.      // display double buffer
  1794.  
  1795.      Display_Double_Buffer(double_buffer,0);
  1796.  
  1797.      // lock onto 18 frames per second max
  1798.  
  1799.      while((Timer_Query()-starting_time)<1);
  1800.  
  1801.      } // end while
  1802.  
  1803. // restore the old keyboard driver
  1804.  
  1805. Keyboard_Remove_Driver();
  1806.  
  1807. } // end Bsp_View
  1808.  
  1809.  
  1810.