home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_15 / black15.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-22  |  49.5 KB  |  1,809 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. // bank 1
  676.  
  677.     les di,z_bank_1       // point es:di to z buffer bank 1
  678.     mov ax,value          // move the value into ax
  679.     mov cx,z_bank_size    // number of bytes to fill
  680.     shr cx,1              // convert to number of words
  681.     rep stosw             // move the value into z buffer
  682.  
  683. // bank 2
  684.  
  685.     les di,z_bank_2       // point es:di to z buffer bank 1
  686.     mov ax,value          // move the value into ax (redundant)
  687.     mov cx,z_bank_size    // number of bytes to fill  (so is this)
  688.     shr cx,1              // convert to number of words
  689.     rep stosw             // move the value into z buffer
  690.  
  691.     } // end asm
  692.  
  693. } // end Fill_Z_Buffer
  694.  
  695. ///////////////////////////////////////////////////////////////////////////////
  696.  
  697. void Bsp_World_To_Camera(wall_ptr root)
  698. {
  699. // this function traverses the bsp tree and converts the world coordinates
  700. // to camera coordinates using the global transformation matrix note the
  701. // function is recursive and uses and inorder traversal, other traversals
  702. // such as preorder and postorder will would just as well...
  703.  
  704. static int index; // looping variable
  705.  
  706. // test if we have hit a dead end
  707.  
  708. if (root==NULL)
  709.     return;
  710.  
  711. // transform back most sub-tree
  712.  
  713. Bsp_World_To_Camera(root->back);
  714.  
  715. // iterate thru all vertices of current wall and transform them into
  716. // camera coordinates
  717.  
  718. for (index=0; index<4; index++)
  719.      {
  720.  
  721.      // multiply the point by the viewing transformation matrix
  722.  
  723.      // x component
  724.  
  725.      root->wall_camera[index].x =
  726.  
  727.                         root->wall_world[index].x * global_view[0][0] +
  728.                         root->wall_world[index].y * global_view[1][0] +
  729.                         root->wall_world[index].z * global_view[2][0] +
  730.                                                              global_view[3][0];
  731.  
  732.      // y component
  733.  
  734.      root->wall_camera[index].y =
  735.  
  736.                         root->wall_world[index].x * global_view[0][1] +
  737.                         root->wall_world[index].y * global_view[1][1] +
  738.                         root->wall_world[index].z * global_view[2][1] +
  739.                                                              global_view[3][1];
  740.      // z component
  741.  
  742.      root->wall_camera[index].z =
  743.  
  744.                         root->wall_world[index].x * global_view[0][2] +
  745.                         root->wall_world[index].y * global_view[1][2] +
  746.                         root->wall_world[index].z * global_view[2][2] +
  747.                                                              global_view[3][2];
  748.  
  749.      } // end for index
  750.  
  751. // transform front most sub-tree
  752.  
  753. Bsp_World_To_Camera(root->front);
  754.  
  755. } // end Bsp_World_To_Camera
  756.  
  757. /////////////////////////////////////////////////////////////////////////////
  758.  
  759. void Bsp_Translate(wall_ptr root,int x_trans,int y_trans,int z_trans)
  760. {
  761. // this function translates all the walls that make up the bsp world
  762. // note function is recursive, we don't really need this function, but
  763. // it's a good example of how we might perform transformations on the BSP
  764. // tree and similar tree like structures using recursion
  765.  
  766. static int index; // looping variable
  767.  
  768. // test if we have hit a dead end
  769.  
  770. if (root==NULL)
  771.     return;
  772.  
  773. // translate back most sub-tree
  774.  
  775. Bsp_Translate(root->back,x_trans,y_trans,z_trans);
  776.  
  777. // iterate thru all vertices of current wall and translate them
  778.  
  779. for (index=0; index<4; index++)
  780.      {
  781.      // perform translation
  782.  
  783.      root->wall_world[index].x+=x_trans;
  784.      root->wall_world[index].y+=y_trans;
  785.      root->wall_world[index].z+=z_trans;
  786.  
  787.      } // end for index
  788.  
  789. // translate front most sub-tree
  790.  
  791. Bsp_Translate(root->front,x_trans,y_trans,z_trans);
  792.  
  793. } // end Bsp_Translate
  794.  
  795. ///////////////////////////////////////////////////////////////////////////////
  796.  
  797. void Bsp_Shade(wall_ptr root)
  798. {
  799. // this function shades the bsp tree and need only be called if the global
  800. // lightsource changes position
  801.  
  802. static int index; // looping variable
  803.  
  804. static float normal_length, // length of surface normal
  805.              intensity,     // intensity of light falling on surface being processed
  806.              dp;            // result of dot product
  807.  
  808. // test if we have hit a dead end
  809.  
  810. if (root==NULL)
  811.    return;
  812.  
  813. // shade the back most sub-tree
  814.  
  815. Bsp_Shade(root->back);
  816.  
  817. // compute the dot product between line of sight vector and normal to surface
  818.  
  819. dp = Dot_Product_3D((vector_3d_ptr)&root->normal,(vector_3d_ptr)&light_source);
  820.  
  821. // compute length of normal of surface normal, remember this function
  822. // doesn't need to be time critical since it is only called once at startup
  823. // or whenever the light source moves
  824.  
  825. normal_length = Vector_Mag_3D((vector_3d_ptr)&root->normal);
  826.  
  827. // cos 0 = (u.v)/|u||v| or
  828.  
  829. intensity = ambient_light + (15*dp/normal_length);
  830.  
  831. // test if intensity has overflowed
  832.  
  833. if (intensity >15)
  834.     intensity = 15;
  835.  
  836. // intensity now varies from 0-1, 0 being black or grazing and 1 being
  837. // totally illuminated. use the value to index into color table
  838.  
  839. root->color = BSP_WALL_SHADE - (int)(fabs(intensity));
  840.  
  841. // shade the front most sub-tree
  842.  
  843. Bsp_Shade(root->front);
  844.  
  845. } // end Bsp_Shade
  846.  
  847. /////////////////////////////////////////////////////////////////////////////
  848.  
  849. void Bsp_Traverse(wall_ptr root)
  850. {
  851. // this function traverses the BSP tree and generates the polygon list used
  852. // by Draw_Polys() for the current global viewpoint (note the view angle is
  853. // irrelevant), also as the polygon list is being generated, only polygons
  854. // that are within the z extents are added to the polygon, in essence, the
  855. // function is performing Z clipping also, this is to minimize the amount
  856. // of polygons in the graphics pipeline that will have to be processed during
  857. // rendering
  858.  
  859. // this function works be testing the viewpoint against the current wall
  860. // in the bsp, then depending on the side the viewpoint is the algorithm
  861. // proceeds. the search takes place as the rest in an "inorder" method
  862. // with hooks to process and add each node into the polygon list at the
  863. // right time
  864.  
  865. static vector_3d test_vector;
  866.  
  867. static float dot_wall,
  868.              z1,z2,z3,z4;
  869.  
  870. // SECTION 1  ////////////////////////////////////////////////////////////////
  871.  
  872. // is this a dead end?
  873.  
  874. if (root==NULL)
  875.     return;
  876.  
  877. // test which side viewpoint is on relative to the current wall
  878.  
  879. Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  880.                (point_3d_ptr)&view_point,
  881.                (vector_3d_ptr)&test_vector);
  882.  
  883. // now dot test vector with the surface normal and analyze signs
  884.  
  885. dot_wall = Dot_Product_3D((vector_3d_ptr)&test_vector,
  886.                           (vector_3d_ptr)&root->normal);
  887.  
  888. // SECTION 2  ////////////////////////////////////////////////////////////////
  889.  
  890. // if the sign of the dot product is positive then the viewer on on the
  891. // front side of current wall, so recursively process the walls behind then
  892. // in front of this wall, else do the opposite
  893.  
  894. if (dot_wall>0)
  895.    {
  896.    // viewer is in front of this wall
  897.  
  898.    // process the back wall sub tree
  899.  
  900.    Bsp_Traverse(root->back);
  901.  
  902.    // try to add this wall to the polygon list if it's within the Z extents
  903.  
  904.    z1=root->wall_camera[0].z;
  905.    z2=root->wall_camera[1].z;
  906.    z3=root->wall_camera[2].z;
  907.    z4=root->wall_camera[3].z;
  908.  
  909.    // perform the z extents clipping test
  910.  
  911.  
  912.    if ( (z1>clip_near_z && z1<clip_far_z ) || (z2>clip_near_z && z2<clip_far_z ) ||
  913.         (z3>clip_near_z && z3<clip_far_z ) || (z4>clip_near_z && z4<clip_far_z ))
  914.       {
  915.  
  916.       // first copy data and vertices into an open slot in storage area
  917.  
  918.       world_poly_storage[num_polys_frame].num_points = 4;
  919.       world_poly_storage[num_polys_frame].color      = BSP_WALL_COLOR;
  920.       world_poly_storage[num_polys_frame].shade      = root->color;
  921.       world_poly_storage[num_polys_frame].shading    = 0;
  922.       world_poly_storage[num_polys_frame].two_sided  = 1;
  923.         world_poly_storage[num_polys_frame].visible    = 1;
  924.         world_poly_storage[num_polys_frame].clipped    = 0;
  925.       world_poly_storage[num_polys_frame].active     = 1;
  926.  
  927.       // now copy vertices
  928.  
  929.       world_poly_storage[num_polys_frame].vertex_list[0].x = root->wall_camera[0].x;
  930.       world_poly_storage[num_polys_frame].vertex_list[0].y = root->wall_camera[0].y;
  931.       world_poly_storage[num_polys_frame].vertex_list[0].z = root->wall_camera[0].z;
  932.  
  933.       world_poly_storage[num_polys_frame].vertex_list[1].x = root->wall_camera[1].x;
  934.       world_poly_storage[num_polys_frame].vertex_list[1].y = root->wall_camera[1].y;
  935.       world_poly_storage[num_polys_frame].vertex_list[1].z = root->wall_camera[1].z;
  936.  
  937.       world_poly_storage[num_polys_frame].vertex_list[2].x = root->wall_camera[2].x;
  938.       world_poly_storage[num_polys_frame].vertex_list[2].y = root->wall_camera[2].y;
  939.       world_poly_storage[num_polys_frame].vertex_list[2].z = root->wall_camera[2].z;
  940.  
  941.         world_poly_storage[num_polys_frame].vertex_list[3].x = root->wall_camera[3].x;
  942.       world_poly_storage[num_polys_frame].vertex_list[3].y = root->wall_camera[3].y;
  943.       world_poly_storage[num_polys_frame].vertex_list[3].z = root->wall_camera[3].z;
  944.  
  945.  
  946.       // assign poly list pointer to it
  947.  
  948.       world_polys[num_polys_frame] = &world_poly_storage[num_polys_frame];
  949.  
  950.       // increment number of polys in this frame
  951.  
  952.       num_polys_frame++;
  953.  
  954.       } // end if polygon is visible
  955.  
  956.    // now process the front walls sub tree
  957.  
  958.     Bsp_Traverse(root->front);
  959.  
  960.    } // end if
  961.  
  962. // SECTION 3 ////////////////////////////////////////////////////////////////
  963.  
  964. else
  965.    {
  966.    // viewer is behind this wall
  967.  
  968.    // process the front wall sub tree
  969.  
  970.    Bsp_Traverse(root->front);
  971.  
  972.    // try to add this wall to the polygon list if it's within the Z extents
  973.  
  974.    z1=root->wall_camera[0].z;
  975.     z2=root->wall_camera[1].z;
  976.    z3=root->wall_camera[2].z;
  977.    z4=root->wall_camera[3].z;
  978.  
  979.    // perform the z extents clipping test
  980.  
  981.    if ( (z1>clip_near_z && z1<clip_far_z ) || (z2>clip_near_z && z2<clip_far_z )  ||
  982.         (z3>clip_near_z && z3<clip_far_z ) || (z4>clip_near_z && z4<clip_far_z ))
  983.       {
  984.  
  985.       // first copy data and vertices into an open slot in storage area
  986.  
  987.       world_poly_storage[num_polys_frame].num_points = 4;
  988.       world_poly_storage[num_polys_frame].color      = BSP_WALL_COLOR;
  989.       world_poly_storage[num_polys_frame].shade      = root->color;
  990.       world_poly_storage[num_polys_frame].shading    = 0;
  991.       world_poly_storage[num_polys_frame].two_sided  = 1;
  992.         world_poly_storage[num_polys_frame].visible    = 1;
  993.       world_poly_storage[num_polys_frame].clipped    = 0;
  994.       world_poly_storage[num_polys_frame].active     = 1;
  995.  
  996.       // now copy vertices, note that we don't use a structure copy, it's
  997.       // not dependable
  998.  
  999.       world_poly_storage[num_polys_frame].vertex_list[0].x = root->wall_camera[0].x;
  1000.       world_poly_storage[num_polys_frame].vertex_list[0].y = root->wall_camera[0].y;
  1001.       world_poly_storage[num_polys_frame].vertex_list[0].z = root->wall_camera[0].z;
  1002.  
  1003.       world_poly_storage[num_polys_frame].vertex_list[1].x = root->wall_camera[1].x;
  1004.       world_poly_storage[num_polys_frame].vertex_list[1].y = root->wall_camera[1].y;
  1005.       world_poly_storage[num_polys_frame].vertex_list[1].z = root->wall_camera[1].z;
  1006.  
  1007.       world_poly_storage[num_polys_frame].vertex_list[2].x = root->wall_camera[2].x;
  1008.       world_poly_storage[num_polys_frame].vertex_list[2].y = root->wall_camera[2].y;
  1009.         world_poly_storage[num_polys_frame].vertex_list[2].z = root->wall_camera[2].z;
  1010.  
  1011.       world_poly_storage[num_polys_frame].vertex_list[3].x = root->wall_camera[3].x;
  1012.       world_poly_storage[num_polys_frame].vertex_list[3].y = root->wall_camera[3].y;
  1013.       world_poly_storage[num_polys_frame].vertex_list[3].z = root->wall_camera[3].z;
  1014.  
  1015.  
  1016.       // assign poly list pointer to it
  1017.  
  1018.       world_polys[num_polys_frame] = &world_poly_storage[num_polys_frame];
  1019.  
  1020.       // increment number of polys in this frame
  1021.  
  1022.       num_polys_frame++;
  1023.  
  1024.       } // end if polygon is visible
  1025.  
  1026.     // now process the front walls sub tree
  1027.  
  1028.    Bsp_Traverse(root->back);
  1029.  
  1030.    } // end else
  1031.  
  1032. } // end Bsp_Traverse
  1033.  
  1034. /////////////////////////////////////////////////////////////////////////////
  1035.  
  1036. void Bsp_Delete(wall_ptr root)
  1037. {
  1038. // this function recursively deletes all the nodes in the bsp tree and free's
  1039. // the memory back to the OS.
  1040.  
  1041. wall_ptr temp_wall; // a temporary wall
  1042.  
  1043. // test if we have hit a dead end
  1044.  
  1045. if (root==NULL)
  1046.    return;
  1047.  
  1048. // delete back sub tree
  1049.  
  1050. Bsp_Delete(root->back);
  1051.  
  1052. // delete this node, but first save the front sub-tree
  1053.  
  1054. temp_wall = root->front;
  1055.  
  1056. // delete the memory
  1057.  
  1058. free(root);
  1059.  
  1060. // assign the root to the saved front most sub-tree
  1061.  
  1062. root = temp_wall;
  1063.  
  1064. // delete front sub tree
  1065.  
  1066. Bsp_Delete(root);
  1067.  
  1068. } // end Bsp_Delete
  1069.  
  1070. /////////////////////////////////////////////////////////////////////////////
  1071.  
  1072. void Bsp_Print(wall_ptr root)
  1073. {
  1074. // this function performs a recursive in-order traversal of the BSP tree and
  1075. // prints the results out to the file opened with fp_out as the handle
  1076.  
  1077. // test if this child is null
  1078.  
  1079. if (root==NULL)
  1080.    {
  1081.    fprintf(fp_out,"\nReached NULL node returning...");
  1082.    return;
  1083.    } // end if
  1084.  
  1085. // search left tree (back walls)
  1086.  
  1087. fprintf(fp_out,"\nTraversing back sub-tree...");
  1088.  
  1089. Bsp_Print(root->back);
  1090.  
  1091. // visit node
  1092.  
  1093. fprintf(fp_out,"\n\n\nWall ID #%d",root->id);
  1094. fprintf(fp_out,"\nVertices...");
  1095. fprintf(fp_out,"\nVertex 0: (%f,%f,%f)",root->wall_world[0].x,
  1096.                                         root->wall_world[0].y,
  1097.                                         root->wall_world[0].z);
  1098.  
  1099. fprintf(fp_out,"\nVertex 1: (%f,%f,%f)",root->wall_world[1].x,
  1100.                                         root->wall_world[1].y,
  1101.                                         root->wall_world[1].z);
  1102.  
  1103. fprintf(fp_out,"\nVertex 2: (%f,%f,%f)",root->wall_world[2].x,
  1104.                                         root->wall_world[2].y,
  1105.                                         root->wall_world[2].z);
  1106.  
  1107. fprintf(fp_out,"\nVertex 3: (%f,%f,%f)",root->wall_world[3].x,
  1108.                                         root->wall_world[3].y,
  1109.                                         root->wall_world[3].z);
  1110.  
  1111. fprintf(fp_out,"\nNormal (%f,%f,%f)",root->normal.x,
  1112.                                      root->normal.y,
  1113.                                      root->normal.z);
  1114.  
  1115. fprintf(fp_out,"\nEnd wall data\n");
  1116.  
  1117. // search right tree (front walls)
  1118.  
  1119. fprintf(fp_out,"\nTraversing front sub-tree..");
  1120.  
  1121. Bsp_Print(root->front);
  1122.  
  1123. } // end Bsp_Print
  1124.  
  1125. //////////////////////////////////////////////////////////////////////////////
  1126.  
  1127. void Intersect_Lines(float x0,float y0,float x1,float y1,
  1128.                             float x2,float y2,float x3,float y3,
  1129.                      float *xi,float *yi)
  1130. {
  1131. // this function computes the intersection of the sent lines
  1132. // and returns the intersection point, note that the function assumes
  1133. // the lines intersect. the function can handle vertical as well
  1134. // as horizontal lines. note the function isn't very clever, it simply applies
  1135. // the math, but we don't need speed since this is a pre-processing step
  1136.  
  1137. float a1,b1,c1, // constants of linear equations
  1138.       a2,b2,c2,
  1139.       det_inv,  // the inverse of the determinant of the coefficient matrix
  1140.       m1,m2;    // the slopes of each line
  1141.  
  1142. // compute slopes, note the cludge for infinity, however, this will
  1143. // be close enough
  1144.  
  1145. if ((x1-x0)!=0)
  1146.    m1 = (y1-y0)/(x1-x0);
  1147. else
  1148.    m1 = (float)1e+10;   // close enough to infinity
  1149.  
  1150. if ((x3-x2)!=0)
  1151.    m2 = (y3-y2)/(x3-x2);
  1152. else
  1153.    m2 = (float)1e+10;   // close enough to infinity
  1154.  
  1155. // compute constants
  1156.  
  1157. a1 = m1;
  1158. a2 = m2;
  1159.  
  1160. b1 = -1;
  1161. b2 = -1;
  1162.  
  1163. c1 = (y0-m1*x0);
  1164. c2 = (y2-m2*x2);
  1165.  
  1166. // compute the inverse of the determinate
  1167.  
  1168. det_inv = 1/(a1*b2 - a2*b1);
  1169.  
  1170. // use Kramers rule to compute xi and yi
  1171.  
  1172. *xi=((b1*c2 - b2*c1)*det_inv);
  1173. *yi=((a2*c1 - a1*c2)*det_inv);
  1174.  
  1175. } // end Intersect_Lines
  1176.  
  1177. /////////////////////////////////////////////////////////////////////////////
  1178.  
  1179. void Build_Bsp_Tree(wall_ptr root)
  1180. {
  1181. // this function recursively builds the bsp tree from the sent wall list
  1182. // note the function has sone calls to Draw_Line() and a Time_Delay() at
  1183. // the end, these are for illustrative purposes only for the demo interface
  1184. // and should be removed if you wish to use this function in a real
  1185. // application
  1186.  
  1187. static wall_ptr next_wall,     // pointer to next wall to be processed
  1188.                 front_wall,    // the front wall
  1189.                 back_wall,     // the back wall
  1190.                 temp_wall;     // a temporary wall
  1191.  
  1192. static float
  1193.  
  1194.       dot_wall_1,                // dot products for test wall
  1195.       dot_wall_2,
  1196.         wall_x0,wall_y0,wall_z0,   // working vars for test wall
  1197.       wall_x1,wall_y1,wall_z1,
  1198.       pp_x0,pp_y0,pp_z0,         // working vars for partitioning plane
  1199.       pp_x1,pp_y1,pp_z1,
  1200.       xi,zi;                     // points of intersection when the partioning
  1201.                                  // plane cuts a wall in two
  1202.  
  1203. static vector_3d test_vector_1,  // test vectors from the partioning plane
  1204.            test_vector_2;        // to the test wall to test the side
  1205.                                  // of the partioning plane the test wall
  1206.                                  // lies on
  1207.  
  1208. static int front_flag =0,        // flags if a wall is on the front or back
  1209.            back_flag = 0,        // of the partioning plane
  1210.            index;                // looping index
  1211.  
  1212. // SECTION 1 ////////////////////////////////////////////////////////////////
  1213.  
  1214. // test if this tree is complete
  1215.  
  1216. if (root==NULL)
  1217.    return;
  1218.  
  1219. // the root is the partitioning plane, partition the polygons using it
  1220.  
  1221. next_wall  = root->link;
  1222. root->link = NULL;
  1223.  
  1224. // extract top two vertices of partioning plane wall for ease of calculations
  1225.  
  1226. pp_x0 = root->wall_world[0].x;
  1227. pp_y0 = root->wall_world[0].y;
  1228. pp_z0 = root->wall_world[0].z;
  1229.  
  1230. pp_x1 = root->wall_world[1].x;
  1231. pp_y1 = root->wall_world[1].y;
  1232. pp_z1 = root->wall_world[1].z;
  1233.  
  1234. // highlight space partition green
  1235.  
  1236. Draw_Line(pp_x0/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1237.           pp_z0/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1238.           pp_x1/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1239.           pp_z1/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1240.           10,
  1241.           video_buffer);
  1242.  
  1243. // SECTION 2  ////////////////////////////////////////////////////////////////
  1244.  
  1245. // test if all walls have been partitioned
  1246.  
  1247. while(next_wall)
  1248.      {
  1249.  
  1250.      // test which side test wall is relative to partioning plane
  1251.      // defined by root
  1252.  
  1253.      // first compute vectors from point on partioning plane to point on
  1254.      // test wall
  1255.  
  1256.      Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  1257.                     (point_3d_ptr)&next_wall->wall_world[0],
  1258.                     (vector_3d_ptr)&test_vector_1);
  1259.  
  1260.      Make_Vector_3D((point_3d_ptr)&root->wall_world[0],
  1261.                     (point_3d_ptr)&next_wall->wall_world[1],
  1262.                     (vector_3d_ptr)&test_vector_2);
  1263.  
  1264.       // now dot each test vector with the surface normal and analyze signs
  1265.  
  1266.      dot_wall_1 = Dot_Product_3D((vector_3d_ptr)&test_vector_1,
  1267.                                  (vector_3d_ptr)&root->normal);
  1268.  
  1269.  
  1270.      dot_wall_2 = Dot_Product_3D((vector_3d_ptr)&test_vector_2,
  1271.                                  (vector_3d_ptr)&root->normal);
  1272.  
  1273.  
  1274. // SECTION 3  ////////////////////////////////////////////////////////////////
  1275.  
  1276.      // perform the tests
  1277.  
  1278.      // case 0, the partioning plane and the test wall have a point in common
  1279.      // this is a special case and must be accounted for, shorten the code
  1280.      // we will set a pait of flags and then the next case will handle
  1281.       // the actual insertion of the wall into BSP
  1282.  
  1283.      // reset flags
  1284.  
  1285.      front_flag = back_flag = 0;
  1286.  
  1287.       // determine of wall is tangent to endpoints of partitioning wall
  1288.  
  1289.       if (POINTS_EQUAL_3D(root->wall_world[0],next_wall->wall_world[0]) )
  1290.           {
  1291.           // p0 of partioning plane is the same at p0 of test wall
  1292.           // we only need to see what side p1 of test wall in on
  1293.  
  1294.           if (dot_wall_2 > 0)
  1295.               front_flag = 1;
  1296.           else
  1297.               back_flag = 1;
  1298.  
  1299.         } // end if
  1300.      else
  1301.      if (POINTS_EQUAL_3D(root->wall_world[0],next_wall->wall_world[1]) )
  1302.         {
  1303.         // p0 of partioning plane is the same at p1 of test wall
  1304.           // we only need to see what side p0 of test wall in on
  1305.  
  1306.         if (dot_wall_1 > 0)
  1307.            front_flag = 1;
  1308.         else
  1309.            back_flag = 1;
  1310.  
  1311.  
  1312.         } // end if
  1313.      else
  1314.      if (POINTS_EQUAL_3D(root->wall_world[1],next_wall->wall_world[0]) )
  1315.         {
  1316.         // p1 of partioning plane is the same at p0 of test wall
  1317.         // we only need to see what side p1 of test wall in on
  1318.  
  1319.         if (dot_wall_2 > 0)
  1320.            front_flag = 1;
  1321.           else
  1322.            back_flag = 1;
  1323.  
  1324.         } // end if
  1325.      else
  1326.      if (POINTS_EQUAL_3D(root->wall_world[1],next_wall->wall_world[1]) )
  1327.         {
  1328.         // p1 of partioning plane is the same at p1 of test wall
  1329.         // we only need to see what side p0 of test wall in on
  1330.  
  1331.         if (dot_wall_1 > 0)
  1332.            front_flag = 1;
  1333.         else
  1334.            back_flag = 1;
  1335.  
  1336.         } // end if
  1337.  
  1338. // SECTION 4  ////////////////////////////////////////////////////////////////
  1339.  
  1340.      // case 1 both signs are the same or the front or back flag has been set
  1341.  
  1342.      if ( (dot_wall_1 >= 0 && dot_wall_2 >= 0) || front_flag )
  1343.         {
  1344.  
  1345.         // highlight the wall blue
  1346.  
  1347.         Draw_Line(next_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1348.                   next_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1349.                   next_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1350.                   next_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1351.                   9,
  1352.                   video_buffer);
  1353.  
  1354.         // place this wall on the front list
  1355.  
  1356.         if (root->front==NULL)
  1357.            {
  1358.               // this is the first node
  1359.  
  1360.            root->front      = next_wall;
  1361.            next_wall        = next_wall->link;
  1362.            front_wall       = root->front;
  1363.            front_wall->link = NULL;
  1364.  
  1365.            } // end if
  1366.         else
  1367.            {
  1368.            // this is the nth node
  1369.  
  1370.            front_wall->link = next_wall;
  1371.            next_wall        = next_wall->link;
  1372.               front_wall       = front_wall->link;
  1373.            front_wall->link = NULL;
  1374.  
  1375.            } // end else
  1376.  
  1377.         } // end if both positive
  1378.  
  1379. // SECTION  5 ////////////////////////////////////////////////////////////////
  1380.      else
  1381.      if ( (dot_wall_1 < 0 && dot_wall_2 < 0) || back_flag)
  1382.         {
  1383.  
  1384.         // highlight the wall red
  1385.  
  1386.           Draw_Line(next_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1387.                   next_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1388.                   next_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1389.                   next_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1390.                   12,
  1391.                   video_buffer);
  1392.  
  1393.         // place this wall on the back list
  1394.  
  1395.         if (root->back==NULL)
  1396.            {
  1397.            // this is the first node
  1398.  
  1399.            root->back      = next_wall;
  1400.               next_wall       = next_wall->link;
  1401.            back_wall       = root->back;
  1402.            back_wall->link = NULL;
  1403.  
  1404.            } // end if
  1405.         else
  1406.            {
  1407.            // this is the nth node
  1408.  
  1409.            back_wall->link = next_wall;
  1410.            next_wall       = next_wall->link;
  1411.            back_wall       = back_wall->link;
  1412.            back_wall->link = NULL;
  1413.  
  1414.               } // end else
  1415.  
  1416.         } // end if both negative
  1417.  
  1418.      // case 2 both signs are different
  1419.  
  1420. // SECTION 6  ////////////////////////////////////////////////////////////////
  1421.  
  1422.      else
  1423.      if ( (dot_wall_1 < 0 && dot_wall_2 >= 0) ||
  1424.           (dot_wall_1 >= 0 && dot_wall_2 < 0))
  1425.  
  1426.         {
  1427.         // the partioning plane cuts the wall in half, the wall
  1428.           // must be split into two walls
  1429.  
  1430.         // extract top two vertices of test wall for ease of calculations
  1431.  
  1432.         wall_x0 = next_wall->wall_world[0].x;
  1433.         wall_y0 = next_wall->wall_world[0].y;
  1434.         wall_z0 = next_wall->wall_world[0].z;
  1435.  
  1436.         wall_x1 = next_wall->wall_world[1].x;
  1437.         wall_y1 = next_wall->wall_world[1].y;
  1438.         wall_z1 = next_wall->wall_world[1].z;
  1439.  
  1440.         // compute the point of intersection between the walls
  1441.         // note that x and z are the plane that the intersection takes place in
  1442.  
  1443.         Intersect_Lines(wall_x0,wall_z0,wall_x1,wall_z1,
  1444.                         pp_x0,pp_z0,pp_x1,pp_z1,
  1445.                         &xi,&zi);
  1446.  
  1447.         // here comes the tricky part, we need to slit the wall in half and
  1448.         // create two walls. We'll do this by creating two new walls,
  1449.         // placing them on the appropriate front and back lists and
  1450.         // then deleting the original wall
  1451.  
  1452.         // process first wall
  1453.  
  1454.         // allocate the memory for the wall
  1455.  
  1456.           temp_wall = (wall_ptr)malloc(sizeof(wall));
  1457.  
  1458.         temp_wall->front = NULL;
  1459.         temp_wall->back  = NULL;
  1460.         temp_wall->link  = NULL;
  1461.  
  1462.         temp_wall->normal = next_wall->normal;
  1463.         temp_wall->id     = next_wall->id+1000; // add 1000 to denote a split
  1464.  
  1465.         // compute wall vertices
  1466.  
  1467.         for (index=0; index<4; index++)
  1468.             {
  1469.             temp_wall->wall_world[index].x = next_wall->wall_world[index].x;
  1470.                 temp_wall->wall_world[index].y = next_wall->wall_world[index].y;
  1471.             temp_wall->wall_world[index].z = next_wall->wall_world[index].z;
  1472.             } // end for index
  1473.  
  1474.         // now modify vertices 1 and 2 to reflect intersection point
  1475.         // but leave y alone since it's invariant for the wall spliting
  1476.  
  1477.         temp_wall->wall_world[1].x = xi;
  1478.         temp_wall->wall_world[1].z = zi;
  1479.  
  1480.         temp_wall->wall_world[2].x = xi;
  1481.         temp_wall->wall_world[2].z = zi;
  1482.  
  1483. // SECTION 7  ////////////////////////////////////////////////////////////////
  1484.  
  1485.         // insert new wall into front or back of root
  1486.  
  1487.         if (dot_wall_1 >= 0)
  1488.            {
  1489.  
  1490.            // highlight the wall blue
  1491.  
  1492.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1493.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1494.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1495.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1496.                      9,
  1497.                      video_buffer);
  1498.  
  1499.  
  1500.            // place this wall on the front list
  1501.  
  1502.            if (root->front==NULL)
  1503.               {
  1504.               // this is the first node
  1505.  
  1506.               root->front      = temp_wall;
  1507.               front_wall       = root->front;
  1508.               front_wall->link = NULL;
  1509.  
  1510.               } // end if
  1511.            else
  1512.                   {
  1513.               // this is the nth node
  1514.  
  1515.               front_wall->link = temp_wall;
  1516.               front_wall       = front_wall->link;
  1517.               front_wall->link = NULL;
  1518.  
  1519.               } // end else
  1520.  
  1521.            } // end if positive
  1522.         else
  1523.         if (dot_wall_1 < 0)
  1524.            {
  1525.            // highlight the wall red
  1526.  
  1527.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1528.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1529.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1530.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1531.                      12,
  1532.                      video_buffer);
  1533.  
  1534.            // place this wall on the back list
  1535.  
  1536.            if (root->back==NULL)
  1537.               {
  1538.               // this is the first node
  1539.  
  1540.                   root->back      = temp_wall;
  1541.               back_wall       = root->back;
  1542.               back_wall->link = NULL;
  1543.  
  1544.               } // end if
  1545.            else
  1546.               {
  1547.               // this is the nth node
  1548.  
  1549.               back_wall->link = temp_wall;
  1550.               back_wall       = back_wall->link;
  1551.               back_wall->link = NULL;
  1552.  
  1553.               } // end else
  1554.  
  1555.            } // end if negative
  1556.  
  1557. // SECTION  8 ////////////////////////////////////////////////////////////////
  1558.  
  1559.         // process second wall
  1560.  
  1561.         // allocate the memory for the wall
  1562.  
  1563.         temp_wall = (wall_ptr)malloc(sizeof(wall));
  1564.  
  1565.         temp_wall->front = NULL;
  1566.         temp_wall->back  = NULL;
  1567.         temp_wall->link  = NULL;
  1568.  
  1569.         temp_wall->normal = next_wall->normal;
  1570.         temp_wall->id     = next_wall->id+1000;
  1571.  
  1572.         // compute wall vertices
  1573.  
  1574.         for (index=0; index<4; index++)
  1575.             {
  1576.             temp_wall->wall_world[index].x = next_wall->wall_world[index].x;
  1577.             temp_wall->wall_world[index].y = next_wall->wall_world[index].y;
  1578.             temp_wall->wall_world[index].z = next_wall->wall_world[index].z;
  1579.             } // end for index
  1580.  
  1581.         // now modify vertices 0 and 0 to reflect intersection point
  1582.           // but leave y alone since it's invariant for the wall spliting
  1583.  
  1584.         temp_wall->wall_world[0].x = xi;
  1585.         temp_wall->wall_world[0].z = zi;
  1586.  
  1587.         temp_wall->wall_world[3].x = xi;
  1588.         temp_wall->wall_world[3].z = zi;
  1589.  
  1590.         // insert new wall into front or back of root
  1591.  
  1592.         if (dot_wall_2 >= 0)
  1593.            {
  1594.            // highlight the wall blue
  1595.  
  1596.               Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1597.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1598.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1599.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1600.                      9,
  1601.                      video_buffer);
  1602.  
  1603.            // place this wall on the front list
  1604.  
  1605.            if (root->front==NULL)
  1606.               {
  1607.               // this is the first node
  1608.  
  1609.               root->front      = temp_wall;
  1610.                   front_wall       = root->front;
  1611.               front_wall->link = NULL;
  1612.  
  1613.               } // end if
  1614.            else
  1615.               {
  1616.               // this is the nth node
  1617.  
  1618.               front_wall->link = temp_wall;
  1619.               front_wall       = front_wall->link;
  1620.               front_wall->link = NULL;
  1621.  
  1622.               } // end else
  1623.  
  1624.  
  1625.            } // end if positive
  1626.         else
  1627.         if (dot_wall_2 < 0)
  1628.            {
  1629.            // highlight the wall red
  1630.  
  1631.            Draw_Line(temp_wall->wall_world[0].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1632.                      temp_wall->wall_world[0].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1633.                      temp_wall->wall_world[1].x/WORLD_SCALE_X-SCREEN_TO_WORLD_X,
  1634.                      temp_wall->wall_world[1].z/WORLD_SCALE_Z-SCREEN_TO_WORLD_Z,
  1635.                      12,
  1636.                      video_buffer);
  1637.  
  1638.               // place this wall on the back list
  1639.  
  1640.            if (root->back==NULL)
  1641.               {
  1642.               // this is the first node
  1643.  
  1644.               root->back      = temp_wall;
  1645.               back_wall       = root->back;
  1646.               back_wall->link = NULL;
  1647.  
  1648.               } // end if
  1649.            else
  1650.               {
  1651.               // this is the nth node
  1652.  
  1653.               back_wall->link = temp_wall;
  1654.               back_wall       = back_wall->link;
  1655.               back_wall->link = NULL;
  1656.  
  1657.               } // end else
  1658.  
  1659.            } // end if negative
  1660.  
  1661. // SECTION  9  ////////////////////////////////////////////////////////////////
  1662.  
  1663.         // we are now done splitting the wall, so we can delete it
  1664.  
  1665.         temp_wall = next_wall;
  1666.           next_wall = next_wall->link;
  1667.         free(temp_wall);
  1668.  
  1669.         } // end else
  1670.  
  1671.      } // end while
  1672.  
  1673. // SECTION  10 ////////////////////////////////////////////////////////////////
  1674.  
  1675. // delay a bit so user can see BSP being created
  1676.  
  1677. Time_Delay(5);
  1678.  
  1679. // recursively process front and back walls
  1680.  
  1681. Build_Bsp_Tree(root->front);
  1682.  
  1683. Build_Bsp_Tree(root->back);
  1684.  
  1685. } // end Build_Bsp_Tree
  1686.  
  1687. //////////////////////////////////////////////////////////////////////////////
  1688.  
  1689. void Bsp_View(wall_ptr bsp_root)
  1690. {
  1691. // this function is a self contained viewing processor that has it's own event
  1692. // loop, the display will continue to be generated until the ESC key is pressed
  1693.  
  1694. int done=0;
  1695.  
  1696. // install the isr keyboard driver
  1697.  
  1698. Keyboard_Install_Driver();
  1699.  
  1700. // change the light source direction
  1701.  
  1702. light_source.x =(float)0.398636;
  1703. light_source.y =(float)-0.374248;
  1704. light_source.z =(float)0.8372275;
  1705.  
  1706. // reset viewpoint to (0,0,0)
  1707.  
  1708. view_point.x = 0;
  1709. view_point.y = 0;
  1710. view_point.z = 0;
  1711.  
  1712. // main event loop
  1713.  
  1714. while(!done)
  1715.      {
  1716.      // compute starting time of this frame
  1717.  
  1718.      starting_time = Timer_Query();
  1719.  
  1720.      // erase all objects
  1721.  
  1722.       Fill_Double_Buffer(0);
  1723.  
  1724.      // move viewpoint
  1725.  
  1726.      if (keyboard_state[MAKE_UP])
  1727.         view_point.y+=20;
  1728.  
  1729.      if (keyboard_state[MAKE_DOWN])
  1730.         view_point.y-=20;
  1731.  
  1732.      if (keyboard_state[MAKE_RIGHT])
  1733.         view_point.x+=20;
  1734.  
  1735.      if (keyboard_state[MAKE_LEFT])
  1736.           view_point.x-=20;
  1737.  
  1738.      if (keyboard_state[MAKE_KEYPAD_PLUS])
  1739.         view_point.z+=20;
  1740.  
  1741.      if (keyboard_state[MAKE_KEYPAD_MINUS])
  1742.         view_point.z-=20;
  1743.  
  1744.      if (keyboard_state[MAKE_Z])
  1745.         if ((view_angle.ang_x+=10)>360)
  1746.            view_angle.ang_x = 0;
  1747.  
  1748.      if (keyboard_state[MAKE_A])
  1749.         if ((view_angle.ang_x-=10)<0)
  1750.               view_angle.ang_x = 360;
  1751.  
  1752.      if (keyboard_state[MAKE_X])
  1753.         if ((view_angle.ang_y+=10)>360)
  1754.            view_angle.ang_y = 0;
  1755.  
  1756.      if (keyboard_state[MAKE_S])
  1757.         if ((view_angle.ang_y-=10)<0)
  1758.            view_angle.ang_y = 360;
  1759.  
  1760.      if (keyboard_state[MAKE_C])
  1761.         if ((view_angle.ang_z+=10)>360)
  1762.            view_angle.ang_z = 0;
  1763.  
  1764.       if (keyboard_state[MAKE_D])
  1765.         if ((view_angle.ang_z-=10)<0)
  1766.            view_angle.ang_z = 360;
  1767.  
  1768.      if (keyboard_state[MAKE_ESC])
  1769.         done=1;
  1770.  
  1771.      // now that user has possible moved viewpoint, create the global
  1772.      // world to camera transformation matrix
  1773.  
  1774.      Create_World_To_Camera();
  1775.  
  1776.      // now convert the bsp tree world coordinates into camera coordinates
  1777.  
  1778.       Bsp_World_To_Camera(bsp_root);
  1779.  
  1780.      // reset number of polygons in polygon list
  1781.  
  1782.      num_polys_frame = 0;
  1783.  
  1784.      // traverse the BSP tree and generate the polygon list
  1785.  
  1786.      Bsp_Traverse(bsp_root);
  1787.  
  1788.      // draw the polygon list generated by traversing the BSP tree
  1789.  
  1790.      Draw_Poly_List();
  1791.  
  1792.       // display double buffer
  1793.  
  1794.      Display_Double_Buffer(double_buffer,0);
  1795.  
  1796.      // lock onto 18 frames per second max
  1797.  
  1798.      while((Timer_Query()-starting_time)<1);
  1799.  
  1800.      } // end while
  1801.  
  1802. // restore the old keyboard driver
  1803.  
  1804. Keyboard_Remove_Driver();
  1805.  
  1806. } // end Bsp_View
  1807.  
  1808.  
  1809.