home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / seguvs.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  55KB  |  1,621 lines

  1.  /*
  2.  * $Source: f:/miner/source/main/editor/rcs/seguvs.c $
  3.  * $Revision: 2.1 $
  4.  * $Author: mike $
  5.  * $Date: 1995/05/08 10:49:34 $
  6.  * 
  7.  * u,v coordinate computation for segment faces
  8.  * 
  9.  * $Log: seguvs.c $
  10.  * Revision 2.1  1995/05/08  10:49:34  mike
  11.  * fix lighting bug: oblong segments could be very dark.
  12.  * 
  13.  * Revision 2.0  1995/02/27  11:36:37  john
  14.  * Version 2.0. Ansi-fied.
  15.  * 
  16.  * Revision 1.84  1994/11/27  23:17:18  matt
  17.  * Made changes for new mprintf calling convention
  18.  * 
  19.  * Revision 1.83  1994/11/17  14:48:02  mike
  20.  * validation functions moved from editor to game.
  21.  * 
  22.  * Revision 1.82  1994/10/15  19:08:26  mike
  23.  * Disable exhaustive search mprintfs in find_point_seg during lighting.
  24.  * 
  25.  * Revision 1.81  1994/08/25  21:55:50  mike
  26.  * IS_CHILD stuff.
  27.  * 
  28.  * Revision 1.80  1994/08/04  19:13:22  matt
  29.  * Changed a bunch of vecmat calls to use multiple-function routines, and to
  30.  * allow the use of C macros for some functions
  31.  * 
  32.  * Revision 1.79  1994/08/03  10:31:33  mike
  33.  * Texture map propagation without uv assignment.
  34.  * 
  35.  * Revision 1.78  1994/08/01  13:31:12  matt
  36.  * Made fvi() check holes in transparent walls, and changed fvi() calling
  37.  * parms to take all input data in query structure.
  38.  * 
  39.  * Revision 1.77  1994/07/08  14:31:24  matt
  40.  * New parms for FVI
  41.  * 
  42.  * Revision 1.76  1994/06/23  14:01:04  mike
  43.  * Fix cache bug which caused some vertices to not get light, mainly
  44.  * noticeable at joints which had doors.
  45.  * 
  46.  * Revision 1.75  1994/06/22  17:33:11  mike
  47.  * Make position of light (which is always towards center of segment from
  48.  * actual light panel) constant, not dependent on segment size, which fixes
  49.  * bug of dark light panels in very large segments.
  50.  * 
  51.  * Revision 1.74  1994/06/21  18:58:18  mike
  52.  * Fix stupid bug in light propagation, was using wrong vector in fvi caching.
  53.  * 
  54.  * Revision 1.73  1994/06/20  11:20:24  mike
  55.  * Fix stupid lighting bug introduced when I went to cached fvi results.
  56.  * 
  57.  * Revision 1.72  1994/06/19  16:26:37  mike
  58.  * Speed up lighting by storing and hashing fvi results.
  59.  * 
  60.  * Revision 1.71  1994/06/17  16:05:56  mike
  61.  * Support optional quick lighting propagation: no find_vector_intersection.
  62.  * 
  63.  * Revision 1.70  1994/06/15  15:42:30  mike
  64.  * Propagate static_light.
  65.  * 
  66.  * Revision 1.69  1994/06/14  16:59:37  mike
  67.  * Fix references to tmap_num2, must strip off orientation bits.
  68.  * 
  69.  * Revision 1.68  1994/06/09  09:58:58  matt
  70.  * Moved find_vector_intersection() from physics.c to new file fvi.c
  71.  * 
  72.  * 
  73.  * Revision 1.67  1994/06/08  18:14:02  mike
  74.  * mprintf a dot in light casting.
  75.  * 
  76.  * Revision 1.66  1994/06/08  14:37:45  mike
  77.  * double static light value in going from value (a short) to static_light (a fix).
  78.  * 
  79.  * Revision 1.65  1994/06/08  14:29:44  matt
  80.  * Added static_light field to segment structure, and padded side struct
  81.  * to be longword aligned.
  82.  * 
  83.  * Revision 1.64  1994/06/08  11:45:24  mike
  84.  * New, supercool, superslow lighting function.
  85.  * 
  86.  * Revision 1.63  1994/06/07  09:38:11  mike
  87.  * Make lighting function yet better by calling find_vector_intersection.
  88.  * 
  89.  * Revision 1.62  1994/06/06  13:14:33  mike
  90.  * Make illusory walls cast light.
  91.  * 
  92.  * Revision 1.61  1994/06/05  20:39:47  mike
  93.  * Add new distance and dot product based lighting function.
  94.  * 
  95.  * Revision 1.60  1994/05/31  12:31:18  mike
  96.  * fix bugs in lighting, though it's not perfect, will be changing all
  97.  * lighting to be distance based.  Bug had to do with not handling one
  98.  * of the return values from WALL_IS_DOORWAY, so assuming light couldn't
  99.  * be recursively propagated almost all the time.
  100.  * 
  101.  * Revision 1.59  1994/05/19  23:35:26  mike
  102.  * Support uv coordinates in range 0..1.0.
  103.  * 
  104.  * Revision 1.58  1994/05/19  12:10:21  matt
  105.  * Use new vecmat macros and globals
  106.  * 
  107.  * Revision 1.57  1994/05/04  19:15:53  mike
  108.  * Error checking for degenerate segments.
  109.  * 
  110.  * Revision 1.56  1994/05/03  11:02:34  mike
  111.  * Change how default texture map assignment works; now pixels are constant size.
  112.  * 
  113.  * Revision 1.55  1994/04/28  23:25:26  yuan
  114.  * Obliterated warnings.
  115.  * 
  116.  */
  117.  
  118.  
  119. #pragma off (unreferenced)
  120. static char rcsid[] = "$Id: seguvs.c 2.1 1995/05/08 10:49:34 mike Exp $";
  121. #pragma on (unreferenced)
  122.  
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. #include <stdarg.h>
  126. #include <math.h>
  127. #include <string.h>
  128.  
  129. #include "inferno.h"
  130. #include "segment.h"
  131. #include "editor.h"
  132.  
  133. #include "gameseg.h"
  134.  
  135. #include "fix.h"
  136. #include "mono.h"
  137. #include "error.h"
  138.  
  139. #include "wall.h"
  140. #include "kdefs.h"
  141. #include "bm.h"        //    Needed for TmapInfo
  142. #include    "effects.h"    //    Needed for effects_bm_num
  143. #include "fvi.h"
  144.  
  145. //--rotate_uvs-- vms_vector Rightvec;
  146.  
  147. //    ---------------------------------------------------------------------------------------------
  148. //    Returns approximate area of a side
  149. fix area_on_side(side *sidep)
  150. {
  151.     fix    du,dv,width,height;
  152.  
  153.     du = sidep->uvls[1].u - sidep->uvls[0].u;
  154.     dv = sidep->uvls[1].v - sidep->uvls[0].v;
  155.  
  156.     width = fix_sqrt(fixmul(du,du) + fixmul(dv,dv));
  157.  
  158.     du = sidep->uvls[3].u - sidep->uvls[0].u;
  159.     dv = sidep->uvls[3].v - sidep->uvls[0].v;
  160.  
  161.     height = fix_sqrt(fixmul(du,du) + fixmul(dv,dv));
  162.  
  163.     return fixmul(width, height);
  164. }
  165.  
  166. //    -------------------------------------------------------------------------------------------
  167. //    DEBUG function -- callable from debugger.
  168. //    Returns approximate area of all sides which get mapped (ie, are not a connection).
  169. //    I wrote this because I was curious how much memory would be required to texture map all
  170. //    sides individually with custom artwork.  For demo1.min on 2/18/94, it would be about 5 meg.
  171. int area_on_all_sides(void)
  172. {
  173.     int    i,s;
  174.     int    total_area = 0;
  175.  
  176.     for (i=0; i<=Highest_segment_index; i++) {
  177.         segment *segp = &Segments[i];
  178.  
  179.         for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  180.             if (!IS_CHILD(segp->children[s]))
  181.                 total_area += f2i(area_on_side(&segp->sides[s]));
  182.     }
  183.  
  184.     return total_area;
  185. }
  186.  
  187. fix average_connectivity(void)
  188. {
  189.     int    i,s;
  190.     int    total_sides = 0, total_mapped_sides = 0;
  191.  
  192.     for (i=0; i<=Highest_segment_index; i++) {
  193.         segment *segp = &Segments[i];
  194.  
  195.         for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
  196.             if (!IS_CHILD(segp->children[s]))
  197.                 total_mapped_sides++;
  198.             total_sides++;
  199.         }
  200.     }
  201.  
  202.     return 6 * fixdiv(total_mapped_sides, total_sides);
  203. }
  204.  
  205. #define    MAX_LIGHT_SEGS 16
  206.  
  207. //    ---------------------------------------------------------------------------------------------
  208. //    Scan all polys in all segments, return average light value for vnum.
  209. //    segs = output array for segments containing vertex, terminated by -1.
  210. fix get_average_light_at_vertex(int vnum, short *segs)
  211. {
  212.     int    segnum, relvnum, sidenum;
  213.     fix    total_light;
  214.     int    num_occurrences;
  215.     #ifndef NDEBUG
  216.     short    *original_segs = segs;
  217.     #endif
  218.  
  219.     num_occurrences = 0;
  220.     total_light = 0;
  221.  
  222.     for (segnum=0; segnum<=Highest_segment_index; segnum++) {
  223.         segment *segp = &Segments[segnum];
  224.         short *vp = segp->verts;
  225.  
  226.         for (relvnum=0; relvnum<MAX_VERTICES_PER_SEGMENT; relvnum++)
  227.             if (*vp++ == vnum)
  228.                 break;
  229.  
  230.         if (relvnum < MAX_VERTICES_PER_SEGMENT) {
  231.  
  232.             *segs++ = segnum;
  233.             Assert(segs - original_segs < MAX_LIGHT_SEGS);
  234.  
  235.             for (sidenum=0; sidenum < MAX_SIDES_PER_SEGMENT; sidenum++) {
  236.                 if (!IS_CHILD(segp->children[sidenum])) {
  237.                     side    *sidep = &segp->sides[sidenum];
  238.                     byte    *vp = Side_to_verts[sidenum];
  239.                     int    v;
  240.  
  241.                     for (v=0; v<4; v++)
  242.                         if (*vp++ == relvnum) {
  243.                             total_light += sidep->uvls[v].l;
  244.                             num_occurrences++;
  245.                         }
  246.                 }    // end if
  247.             }    // end sidenum
  248.         }
  249.     }    // end segnum
  250.  
  251.     *segs = -1;
  252.  
  253.     if (num_occurrences)
  254.         return total_light/num_occurrences;
  255.     else
  256.         return 0;
  257.  
  258. }
  259.  
  260. void set_average_light_at_vertex(int vnum)
  261. {
  262.     int    relvnum, sidenum;
  263.     short    Segment_indices[MAX_LIGHT_SEGS];
  264.     int    segind;
  265.  
  266.     fix average_light;
  267.  
  268.     average_light = get_average_light_at_vertex(vnum, Segment_indices);
  269.  
  270.     if (!average_light)
  271.         return;
  272.  
  273.     segind = 0;
  274.     while (Segment_indices[segind] != -1) {
  275.         int segnum = Segment_indices[segind++];
  276.  
  277.         segment *segp = &Segments[segnum];
  278.  
  279.         for (relvnum=0; relvnum<MAX_VERTICES_PER_SEGMENT; relvnum++)
  280.             if (segp->verts[relvnum] == vnum)
  281.                 break;
  282.  
  283.         if (relvnum < MAX_VERTICES_PER_SEGMENT) {
  284.             for (sidenum=0; sidenum < MAX_SIDES_PER_SEGMENT; sidenum++) {
  285.                 if (!IS_CHILD(segp->children[sidenum])) {
  286.                     side *sidep = &segp->sides[sidenum];
  287.                     byte    *vp = Side_to_verts[sidenum];
  288.                     int    v;
  289.  
  290.                     for (v=0; v<4; v++)
  291.                         if (*vp++ == relvnum)
  292.                             sidep->uvls[v].l = average_light;
  293.                 }    // end if
  294.             }    // end sidenum
  295.         }    // end if
  296.     }    // end while
  297.  
  298.     Update_flags |= UF_WORLD_CHANGED;
  299. }
  300.  
  301. void set_average_light_on_side(segment *segp, int sidenum)
  302. {
  303.     int    v;
  304.  
  305.     if (!IS_CHILD(segp->children[sidenum]))
  306.         for (v=0; v<4; v++) {
  307. //            mprintf((0,"Vertex %i\n", segp->verts[Side_to_verts[side][v]]));
  308.             set_average_light_at_vertex(segp->verts[Side_to_verts[sidenum][v]]);
  309.         }
  310.  
  311. }
  312.  
  313. void set_average_light_on_curside(void)
  314. {
  315.     set_average_light_on_side(Cursegp, Curside);
  316. }
  317.  
  318. //    -----------------------------------------------------------------------------------------
  319. void set_average_light_on_all_fast(void)
  320. {
  321.     int    s,v,relvnum;
  322.     fix    al;
  323.     int    alc;
  324.     int    seglist[MAX_LIGHT_SEGS];
  325.     int    *segptr;
  326.  
  327.     set_vertex_counts();
  328.  
  329.     //    Set total light value for all vertices in array average_light.
  330.     for (v=0; v<=Highest_vertex_index; v++) {
  331.         al = 0;
  332.         alc = 0;
  333.  
  334.         if (Vertex_active[v]) {
  335.             segptr = seglist;
  336.  
  337.             for (s=0; s<=Highest_segment_index; s++) {
  338.                 segment *segp = &Segments[s];
  339.                 for (relvnum=0; relvnum<MAX_VERTICES_PER_SEGMENT; relvnum++)
  340.                     if (segp->verts[relvnum] == v)
  341.                         break;
  342.  
  343.                     if (relvnum != MAX_VERTICES_PER_SEGMENT) {
  344.                         int        si;
  345.  
  346.                         *segptr++ = s;            // Note this segment in list, so we can process it below.
  347.                         Assert(segptr - seglist < MAX_LIGHT_SEGS);
  348.  
  349.                         for (si=0; si<MAX_SIDES_PER_SEGMENT; si++) {
  350.                             if (!IS_CHILD(segp->children[si])) {
  351.                                 side    *sidep = &segp->sides[si];
  352.                                 byte    *vp = Side_to_verts[si];
  353.                                 int    vv;
  354.  
  355.                                 for (vv=0; vv<4; vv++)
  356.                                     if (*vp++ == relvnum) {
  357.                                         al += sidep->uvls[vv].l;
  358.                                         alc++;
  359.                                     }
  360.                             }    // if (segp->children[si == -1) {
  361.                         }    // for (si=0...
  362.                     }    // if (relvnum != ...
  363.             }    // for (s=0; ...
  364.  
  365.             *segptr = -1;
  366.  
  367.             //    Now, divide average_light by number of number of occurrences for each vertex
  368.             if (alc)
  369.                 al /= alc;
  370.             else
  371.                 al = 0;
  372.  
  373.             segptr = seglist;
  374.             while (*segptr != -1) {
  375.                 int         segnum = *segptr++;
  376.                 segment    *segp = &Segments[segnum];
  377.                 int        sidenum;
  378.  
  379.                 for (relvnum=0; relvnum<MAX_VERTICES_PER_SEGMENT; relvnum++)
  380.                     if (segp->verts[relvnum] == v)
  381.                         break;
  382.  
  383.                 Assert(relvnum < MAX_VERTICES_PER_SEGMENT);    // IMPOSSIBLE! This segment is in seglist, but vertex v does not occur!
  384.                 for (sidenum=0; sidenum < MAX_SIDES_PER_SEGMENT; sidenum++) {
  385.                     int    wid_result;
  386.                     wid_result = WALL_IS_DOORWAY(segp, sidenum);
  387.                     if ((wid_result != WID_FLY_FLAG) && (wid_result != WID_NO_WALL)) {
  388.                         side *sidep = &segp->sides[sidenum];
  389.                         byte    *vp = Side_to_verts[sidenum];
  390.                         int    v;
  391.  
  392.                         for (v=0; v<4; v++)
  393.                             if (*vp++ == relvnum)
  394.                                 sidep->uvls[v].l = al;
  395.                     }    // end if
  396.                 }    // end sidenum
  397.             }    // end while
  398.  
  399.         }    // if (Vertex_active[v]...
  400.  
  401.     }    // for (v=0...
  402.  
  403. }
  404.  
  405. extern int Doing_lighting_hack_flag;    //    If set, don't mprintf warning messages in gameseg.c/find_point_seg
  406. void set_average_light_on_all(void)
  407. {
  408. //    set_average_light_on_all_fast();
  409.  
  410.     Doing_lighting_hack_flag = 1;
  411.     cast_all_light_in_mine(0);
  412.     Doing_lighting_hack_flag = 0;
  413.     Update_flags |= UF_WORLD_CHANGED;
  414.  
  415. //    int seg, side;
  416.  
  417. //    for (seg=0; seg<=Highest_segment_index; seg++)
  418. //        for (side=0; side<MAX_SIDES_PER_SEGMENT; side++)
  419. //            if (Segments[seg].segnum != -1)
  420. //                set_average_light_on_side(&Segments[seg], side);
  421. }
  422.  
  423. void set_average_light_on_all_quick(void)
  424. {
  425.     cast_all_light_in_mine(1);
  426.     Update_flags |= UF_WORLD_CHANGED;
  427.  
  428. }
  429.  
  430. //    ---------------------------------------------------------------------------------------------
  431. fix compute_uv_dist(uvl *uv0, uvl *uv1)
  432. {
  433.     vms_vector    v0,v1;
  434.  
  435.     v0.x = uv0->u;
  436.     v0.y = 0;
  437.     v0.z = uv0->v;
  438.  
  439.     v1.x = uv1->u;
  440.     v1.y = 0;
  441.     v1.z = uv1->v;
  442.  
  443.     return vm_vec_dist(&v0,&v1);
  444. }
  445.  
  446. //    ---------------------------------------------------------------------------------------------
  447. //    Given a polygon, compress the uv coordinates so that they are as close to 0 as possible.
  448. //    Do this by adding a constant u and v to each uv pair.
  449. void compress_uv_coordinates(side *sidep)
  450. {
  451.     int    v;
  452.     fix    uc, vc;
  453.  
  454.     uc = 0;
  455.     vc = 0;
  456.  
  457.     for (v=0; v<4; v++) {
  458.         uc += sidep->uvls[v].u;
  459.         vc += sidep->uvls[v].v;
  460.     }
  461.  
  462.     uc /= 4;
  463.     vc /= 4;
  464.     uc = uc & 0xffff0000;
  465.     vc = vc & 0xffff0000;
  466.  
  467.     for (v=0; v<4; v++) {
  468.         sidep->uvls[v].u -= uc;
  469.         sidep->uvls[v].v -= vc;
  470.     }
  471.  
  472. }
  473.  
  474. //    ---------------------------------------------------------------------------------------------
  475. void compress_uv_coordinates_on_side(side *sidep)
  476. {
  477.     compress_uv_coordinates(sidep);
  478. }
  479.  
  480. //    ---------------------------------------------------------------------------------------------
  481. void validate_uv_coordinates_on_side(segment *segp, int sidenum)
  482. {
  483. //    int            v;
  484. //    fix            uv_dist,threed_dist;
  485. //    vms_vector    tvec;
  486. //    fix            dist_ratios[MAX_VERTICES_PER_POLY];
  487.     side            *sidep = &segp->sides[sidenum];
  488. //    byte            *vp = Side_to_verts[sidenum];
  489.  
  490. //    This next hunk doesn't seem to affect anything. @mk, 02/13/94
  491. //    for (v=1; v<4; v++) {
  492. //        uv_dist = compute_uv_dist(&sidep->uvls[v],&sidep->uvls[0]);
  493. //        threed_dist = vm_vec_mag(vm_vec_sub(&tvec,&Vertices[segp->verts[vp[v]],&Vertices[vp[0]]));
  494. //        dist_ratios[v-1] = fixdiv(uv_dist,threed_dist);
  495. //    }
  496.  
  497.     compress_uv_coordinates_on_side(sidep);
  498. }
  499.  
  500. void compress_uv_coordinates_in_segment(segment *segp)
  501. {
  502.     int    side;
  503.  
  504.     for (side=0; side<MAX_SIDES_PER_SEGMENT; side++)
  505.         compress_uv_coordinates_on_side(&segp->sides[side]);
  506. }
  507.  
  508. void compress_uv_coordinates_all(void)
  509. {
  510.     int    seg;
  511.  
  512.     for (seg=0; seg<=Highest_segment_index; seg++)
  513.         if (Segments[seg].segnum != -1)
  514.             compress_uv_coordinates_in_segment(&Segments[seg]);
  515. }
  516.  
  517. void check_lighting_side(segment *sp, int sidenum)
  518. {
  519.     int    v;
  520.     side    *sidep = &sp->sides[sidenum];
  521.  
  522.     for (v=0; v<4; v++)
  523.         if ((sidep->uvls[v].l > F1_0*16) || (sidep->uvls[v].l < 0))
  524.             Int3(); //mprintf(0,"Bogus lighting value in segment %i, side %i, vert %i = %x\n",sp-Segments, side, v, sidep->uvls[v].l);
  525. }
  526.  
  527. void check_lighting_segment(segment *segp)
  528. {
  529.     int    side;
  530.  
  531.     for (side=0; side<MAX_SIDES_PER_SEGMENT; side++)
  532.         check_lighting_side(segp, side);
  533. }
  534.  
  535. //    Flag bogus lighting values.
  536. void check_lighting_all(void)
  537. {
  538.     int    seg;
  539.  
  540.     for (seg=0; seg<=Highest_segment_index; seg++)
  541.         if (Segments[seg].segnum != -1)
  542.             check_lighting_segment(&Segments[seg]);
  543. }
  544.  
  545. void assign_default_lighting_on_side(segment *segp, int sidenum)
  546. {
  547.     int    v;
  548.     side    *sidep = &segp->sides[sidenum];
  549.  
  550.     for (v=0; v<4; v++)
  551.         sidep->uvls[v].l = DEFAULT_LIGHTING;
  552. }
  553.  
  554. void assign_default_lighting(segment *segp)
  555. {
  556.     int    sidenum;
  557.  
  558.     for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++)
  559.         assign_default_lighting_on_side(segp, sidenum);
  560. }
  561.  
  562. void assign_default_lighting_all(void)
  563. {
  564.     int    seg;
  565.  
  566.     for (seg=0; seg<=Highest_segment_index; seg++)
  567.         if (Segments[seg].segnum != -1)
  568.             assign_default_lighting(&Segments[seg]);
  569. }
  570.  
  571. //    ---------------------------------------------------------------------------------------------
  572. void validate_uv_coordinates(segment *segp)
  573. {
  574.     int    s;
  575.  
  576.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  577.         validate_uv_coordinates_on_side(segp,s);
  578.  
  579. }
  580.  
  581. //    ---------------------------------------------------------------------------------------------
  582. //    For all faces in side, copy uv coordinates from uvs array to face.
  583. void copy_uvs_from_side_to_faces(segment *segp, int sidenum, uvl uvls[])
  584. {
  585.     int    v;
  586.     side    *sidep = &segp->sides[sidenum];
  587.  
  588.     for (v=0; v<4; v++)
  589.         sidep->uvls[v] = uvls[v];
  590.  
  591. }
  592.  
  593. fix zhypot(fix a,fix b);
  594. #pragma aux zhypot parm [eax] [ebx] value [eax] modify [eax ebx ecx edx] = \
  595.     "imul    eax" \
  596.     "xchg eax,ebx" \
  597.     "mov    ecx,edx" \
  598.     "imul eax" \
  599.     "add    eax,ebx" \
  600.     "adc    edx,ecx" \
  601.     "call    quad_sqrt";
  602.  
  603. //    ---------------------------------------------------------------------------------------------
  604. //    Assign lighting value to side, a function of the normal vector.
  605. void assign_light_to_side(segment *sp, int sidenum)
  606. {
  607.     int    v;
  608.     side    *sidep = &sp->sides[sidenum];
  609.  
  610.     for (v=0; v<4; v++)
  611.         sidep->uvls[v].l = DEFAULT_LIGHTING;
  612. }
  613.  
  614. fix    Stretch_scale_x = F1_0;
  615. fix    Stretch_scale_y = F1_0;
  616.  
  617. //    ---------------------------------------------------------------------------------------------
  618. //    Given u,v coordinates at two vertices, assign u,v coordinates to other two vertices on a side.
  619. //    (Actually, assign them to the coordinates in the faces.)
  620. //    va, vb = face-relative vertex indices corresponding to uva, uvb.  Ie, they are always in 0..3 and should be looked up in
  621. //    Side_to_verts[side] to get the segment relative index.
  622. void assign_uvs_to_side(segment *segp, int sidenum, uvl *uva, uvl *uvb, int va, int vb)
  623. {
  624.     int            vlo,vhi,v0,v1,v2,v3;
  625.     vms_vector    fvec,rvec,tvec;
  626.     vms_matrix    rotmat;
  627.     uvl            uvls[4],ruvmag,fuvmag,uvlo,uvhi;
  628.     fix            fmag,mag01;
  629.     byte            *vp;
  630.  
  631.     Assert( (va<4) && (vb<4) );
  632.     Assert((abs(va - vb) == 1) || (abs(va - vb) == 3));        // make sure the verticies specify an edge
  633.  
  634.     vp = &Side_to_verts[sidenum];
  635.  
  636.     // We want vlo precedes vhi, ie vlo < vhi, or vlo = 3, vhi = 0
  637.     if (va == ((vb + 1) % 4)) {        // va = vb + 1
  638.         vlo = vb;
  639.         vhi = va;
  640.         uvlo = *uvb;
  641.         uvhi = *uva;
  642.     } else {
  643.         vlo = va;
  644.         vhi = vb;
  645.         uvlo = *uva;
  646.         uvhi = *uvb;
  647.     }
  648.  
  649.     Assert(((vlo+1) % 4) == vhi);    // If we are on an edge, then uvhi is one more than uvlo (mod 4)
  650.     uvls[vlo] = uvlo;
  651.     uvls[vhi] = uvhi;
  652.  
  653.     // Now we have vlo precedes vhi, compute vertices ((vhi+1) % 4) and ((vhi+2) % 4)
  654.  
  655.     // Assign u,v scale to a unit length right vector.
  656.     fmag = zhypot(uvhi.v - uvlo.v,uvhi.u - uvlo.u);
  657.     if (fmag < 64) {        // this is a fix, so 64 = 1/1024
  658.         mprintf((0,"Warning: fmag = %7.3f, using approximate u,v values\n",f2fl(fmag)));
  659.         ruvmag.u = F1_0*256;
  660.         ruvmag.v = F1_0*256;
  661.         fuvmag.u = F1_0*256;
  662.         fuvmag.v = F1_0*256;
  663.     } else {
  664.         ruvmag.u = uvhi.v - uvlo.v;
  665.         ruvmag.v = uvlo.u - uvhi.u;
  666.  
  667.         fuvmag.u = uvhi.u - uvlo.u;
  668.         fuvmag.v = uvhi.v - uvlo.v;
  669.     }
  670.  
  671.     v0 = segp->verts[vp[vlo]];
  672.     v1 = segp->verts[vp[vhi]];
  673.     v2 = segp->verts[vp[(vhi+1)%4]];
  674.     v3 = segp->verts[vp[(vhi+2)%4]];
  675.  
  676.     //    Compute right vector by computing orientation matrix from:
  677.     //        forward vector = vlo:vhi
  678.     //          right vector = vlo:(vhi+2) % 4
  679.     vm_vec_sub(&fvec,&Vertices[v1],&Vertices[v0]);
  680.     vm_vec_sub(&rvec,&Vertices[v3],&Vertices[v0]);
  681.  
  682.     if (((fvec.x == 0) && (fvec.y == 0) && (fvec.z == 0)) || ((rvec.x == 0) && (rvec.y == 0) && (rvec.z == 0))) {
  683.         mprintf((1, "Trapped null vector in assign_uvs_to_side, using identity matrix.\n"));
  684.         rotmat = vmd_identity_matrix;
  685.     } else
  686.         vm_vector_2_matrix(&rotmat,&fvec,0,&rvec);
  687.  
  688.     rvec = rotmat.rvec; vm_vec_negate(&rvec);
  689.     fvec = rotmat.fvec;
  690.  
  691.     // mprintf((0, "va = %i, vb = %i\n", va, vb));
  692.     mag01 = vm_vec_dist(&Vertices[v1],&Vertices[v0]);
  693.     if ((va == 0) || (va == 2))
  694.         mag01 = fixmul(mag01, Stretch_scale_x);
  695.     else
  696.         mag01 = fixmul(mag01, Stretch_scale_y);
  697.  
  698.     if (mag01 < F1_0/1024 )
  699.         editor_status("U, V bogosity in segment #%i, probably on side #%i.  CLEAN UP YOUR MESS!", segp-Segments, sidenum);
  700.     else {
  701.         vm_vec_sub(&tvec,&Vertices[v2],&Vertices[v1]);
  702.         uvls[(vhi+1)%4].u = uvhi.u + 
  703.             fixdiv(fixmul(ruvmag.u,vm_vec_dotprod(&rvec,&tvec)),mag01) +
  704.             fixdiv(fixmul(fuvmag.u,vm_vec_dotprod(&fvec,&tvec)),mag01);
  705.  
  706.         uvls[(vhi+1)%4].v = uvhi.v + 
  707.             fixdiv(fixmul(ruvmag.v,vm_vec_dotprod(&rvec,&tvec)),mag01) +
  708.             fixdiv(fixmul(fuvmag.v,vm_vec_dotprod(&fvec,&tvec)),mag01);
  709.  
  710.  
  711.         vm_vec_sub(&tvec,&Vertices[v3],&Vertices[v0]);
  712.         uvls[(vhi+2)%4].u = uvlo.u + 
  713.             fixdiv(fixmul(ruvmag.u,vm_vec_dotprod(&rvec,&tvec)),mag01) +
  714.             fixdiv(fixmul(fuvmag.u,vm_vec_dotprod(&fvec,&tvec)),mag01);
  715.  
  716.         uvls[(vhi+2)%4].v = uvlo.v + 
  717.             fixdiv(fixmul(ruvmag.v,vm_vec_dotprod(&rvec,&tvec)),mag01) +
  718.             fixdiv(fixmul(fuvmag.v,vm_vec_dotprod(&fvec,&tvec)),mag01);
  719.  
  720.         uvls[(vhi+1)%4].l = uvhi.l;
  721.         uvls[(vhi+2)%4].l = uvlo.l;
  722.  
  723.         copy_uvs_from_side_to_faces(segp, sidenum, uvls);
  724.     }
  725. }
  726.  
  727.  
  728. int Vmag = VMAG;
  729.  
  730. // -----------------------------------------------------------------------------------------------------------
  731. //    Assign default uvs to side.
  732. //    This means:
  733. //        v0 = 0,0
  734. //        v1 = k,0 where k is 3d size dependent
  735. //    v2, v3 assigned by assign_uvs_to_side
  736. void assign_default_uvs_to_side(segment *segp,int side)
  737. {
  738.     uvl            uv0,uv1;
  739.     byte            *vp;
  740.  
  741.     uv0.u = 0;
  742.     uv0.v = 0;
  743.  
  744.     vp = Side_to_verts[side];
  745.  
  746.     uv1.u = 0;
  747.     uv1.v = Num_tilings * fixmul(Vmag, vm_vec_dist(&Vertices[segp->verts[vp[1]]],&Vertices[segp->verts[vp[0]]]));
  748.  
  749.     assign_uvs_to_side(segp, side, &uv0, &uv1, 0, 1);
  750. }
  751.  
  752. // -----------------------------------------------------------------------------------------------------------
  753. //    Assign default uvs to side.
  754. //    This means:
  755. //        v0 = 0,0
  756. //        v1 = k,0 where k is 3d size dependent
  757. //    v2, v3 assigned by assign_uvs_to_side
  758. void stretch_uvs_from_curedge(segment *segp, int side)
  759. {
  760.     uvl            uv0,uv1;
  761.     int            v0, v1;
  762.  
  763.     v0 = Curedge;
  764.     v1 = (v0 + 1) % 4;
  765.  
  766.     uv0.u = segp->sides[side].uvls[v0].u;
  767.     uv0.v = segp->sides[side].uvls[v0].v;
  768.  
  769.     uv1.u = segp->sides[side].uvls[v1].u;
  770.     uv1.v = segp->sides[side].uvls[v1].v;
  771.  
  772.     assign_uvs_to_side(segp, side, &uv0, &uv1, v0, v1);
  773. }
  774.  
  775. // --------------------------------------------------------------------------------------------------------------
  776. //    Assign default uvs to a segment.
  777. void assign_default_uvs_to_segment(segment *segp)
  778. {
  779.     int    s;
  780.  
  781.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
  782.         assign_default_uvs_to_side(segp,s);
  783.         assign_light_to_side(segp, s);
  784.     }
  785. }
  786.  
  787.  
  788. // -- mk021394 -- // --------------------------------------------------------------------------------------------------------------
  789. // -- mk021394 -- //    Find the face:poly:vertex index in base_seg:base_common_side which is segment relative vertex v1
  790. // -- mk021394 -- //    This very specific routine is subsidiary to med_assign_uvs_to_side.
  791. // -- mk021394 -- void get_face_and_vert(segment *base_seg, int base_common_side, int v1, int *ff, int *vv, int *pi)
  792. // -- mk021394 -- {
  793. // -- mk021394 --     int    p,f,v;
  794. // -- mk021394 -- 
  795. // -- mk021394 --     for (f=0; f<base_seg->sides[base_common_side].num_faces; f++) {
  796. // -- mk021394 --         face *fp = &base_seg->sides[base_common_side].faces[f];
  797. // -- mk021394 --         for (p=0; p<fp->num_polys; p++) {
  798. // -- mk021394 --             poly *pp = &fp->polys[p];
  799. // -- mk021394 --             for (v=0; v<pp->num_vertices; v++)
  800. // -- mk021394 --                 if (pp->verts[v] == v1) {
  801. // -- mk021394 --                     *ff = f;
  802. // -- mk021394 --                     *vv = v;
  803. // -- mk021394 --                     *pi = p;
  804. // -- mk021394 --                     return;
  805. // -- mk021394 --                 }
  806. // -- mk021394 --         }
  807. // -- mk021394 --     }
  808. // -- mk021394 -- 
  809. // -- mk021394 --     Assert(0);    // Error -- Couldn't find face:vertex which matched vertex v1 on base_seg:base_common_side
  810. // -- mk021394 -- }
  811.  
  812. // -- mk021394 -- // --------------------------------------------------------------------------------------------------------------
  813. // -- mk021394 -- //    Find the vertex index in base_seg:base_common_side which is segment relative vertex v1
  814. // -- mk021394 -- //    This very specific routine is subsidiary to med_assign_uvs_to_side.
  815. // -- mk021394 -- void get_side_vert(segment *base_seg,int base_common_side,int v1,int *vv)
  816. // -- mk021394 -- {
  817. // -- mk021394 --     int    p,f,v;
  818. // -- mk021394 -- 
  819. // -- mk021394 --     Assert((base_seg->sides[base_common_side].tri_edge == 0) || (base_seg->sides[base_common_side].tri_edge == 1));
  820. // -- mk021394 --     Assert(base_seg->sides[base_common_side].num_faces <= 2);
  821. // -- mk021394 -- 
  822. // -- mk021394 --     for (f=0; f<base_seg->sides[base_common_side].num_faces; f++) {
  823. // -- mk021394 --         face *fp = &base_seg->sides[base_common_side].faces[f];
  824. // -- mk021394 --         for (p=0; p<fp->num_polys; p++) {
  825. // -- mk021394 --             poly    *pp = &fp->polys[p];
  826. // -- mk021394 --             for (v=0; v<pp->num_vertices; v++)
  827. // -- mk021394 --                 if (pp->verts[v] == v1) {
  828. // -- mk021394 --                     if (pp->num_vertices == 4) {
  829. // -- mk021394 --                         *vv = v;
  830. // -- mk021394 --                         return;
  831. // -- mk021394 --                     }
  832. // -- mk021394 -- 
  833. // -- mk021394 --                     if (base_seg->sides[base_common_side].tri_edge == 0) {    // triangulated 012, 023, so if f==0, *vv = v, if f==1, *vv = v if v=0, else v+1
  834. // -- mk021394 --                         if ((f == 1) && (v > 0))
  835. // -- mk021394 --                             v++;
  836. // -- mk021394 --                         *vv = v;
  837. // -- mk021394 --                         return;
  838. // -- mk021394 --                     } else {                                // triangulated 013, 123
  839. // -- mk021394 --                         if (f == 0) {
  840. // -- mk021394 --                             if (v == 2)
  841. // -- mk021394 --                                 v++;
  842. // -- mk021394 --                         } else
  843. // -- mk021394 --                             v++;
  844. // -- mk021394 --                         *vv = v;
  845. // -- mk021394 --                         return;
  846. // -- mk021394 --                     }
  847. // -- mk021394 --                 }
  848. // -- mk021394 --         }
  849. // -- mk021394 --     }
  850. // -- mk021394 -- 
  851. // -- mk021394 --     Assert(0);    // Error -- Couldn't find face:vertex which matched vertex v1 on base_seg:base_common_side
  852. // -- mk021394 -- }
  853.  
  854. //--rotate_uvs-- // --------------------------------------------------------------------------------------------------------------
  855. //--rotate_uvs-- //    Rotate uvl coordinates uva, uvb about their center point by heading
  856. //--rotate_uvs-- void rotate_uvs(uvl *uva, uvl *uvb, vms_vector *rvec)
  857. //--rotate_uvs-- {
  858. //--rotate_uvs--     uvl    uvc, uva1, uvb1;
  859. //--rotate_uvs-- 
  860. //--rotate_uvs--     uvc.u = (uva->u + uvb->u)/2;
  861. //--rotate_uvs--     uvc.v = (uva->v + uvb->v)/2;
  862. //--rotate_uvs-- 
  863. //--rotate_uvs--     uva1.u = fixmul(uva->u - uvc.u, rvec->x) - fixmul(uva->v - uvc.v, rvec->z);
  864. //--rotate_uvs--     uva1.v = fixmul(uva->u - uvc.u, rvec->z) + fixmul(uva->v - uvc.v, rvec->x);
  865. //--rotate_uvs-- 
  866. //--rotate_uvs--     uva->u = uva1.u + uvc.u;
  867. //--rotate_uvs--     uva->v = uva1.v + uvc.v;
  868. //--rotate_uvs-- 
  869. //--rotate_uvs--     uvb1.u = fixmul(uvb->u - uvc.u, rvec->x) - fixmul(uvb->v - uvc.v, rvec->z);
  870. //--rotate_uvs--     uvb1.v = fixmul(uvb->u - uvc.u, rvec->z) + fixmul(uvb->v - uvc.v, rvec->x);
  871. //--rotate_uvs-- 
  872. //--rotate_uvs--     uvb->u = uvb1.u + uvc.u;
  873. //--rotate_uvs--     uvb->v = uvb1.v + uvc.v;
  874. //--rotate_uvs-- }
  875.  
  876.  
  877. // --------------------------------------------------------------------------------------------------------------
  878. void med_assign_uvs_to_side(segment *con_seg, int con_common_side, segment *base_seg, int base_common_side, int abs_id1, int abs_id2)
  879. {
  880.     uvl        uv1,uv2;
  881.     int        v,bv1,bv2,cv1,cv2, vv1, vv2;
  882.  
  883.     bv1 = -1;    bv2 = -1;
  884.  
  885.     // Find which vertices in segment match abs_id1, abs_id2
  886.     for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
  887.         if (base_seg->verts[v] == abs_id1)
  888.             bv1 = v;
  889.         if (base_seg->verts[v] == abs_id2)
  890.             bv2 = v;
  891.         if (con_seg->verts[v] == abs_id1)
  892.             cv1 = v;
  893.         if (con_seg->verts[v] == abs_id2)
  894.             cv2 = v;
  895.     }
  896.  
  897.     //    Now, bv1, bv2 are segment relative vertices in base segment which are the same as absolute vertices abs_id1, abs_id2
  898.     //         cv1, cv2 are segment relative vertices in conn segment which are the same as absolute vertices abs_id1, abs_id2
  899.  
  900.     Assert((bv1 != -1) && (bv2 != -1) && (cv1 != -1) && (cv2 != -1));
  901.     Assert((uv1.u != uv2.u) || (uv1.v != uv2.v));
  902.  
  903.     //    Now, scan 4 vertices in base side and 4 vertices in connected side.
  904.     //    Set uv1, uv2 to uv coordinates from base side which correspond to vertices bv1, bv2.
  905.     //    Set vv1, vv2 to relative vertex ids (in 0..3) in connecting side which correspond to cv1, cv2
  906.     vv1 = -1;    vv2 = -1;
  907.     for (v=0; v<4; v++) {
  908.         if (bv1 == Side_to_verts[base_common_side][v])
  909.             uv1 = base_seg->sides[base_common_side].uvls[v];
  910.  
  911.         if (bv2 == Side_to_verts[base_common_side][v])
  912.             uv2 = base_seg->sides[base_common_side].uvls[v];
  913.  
  914.         if (cv1 == Side_to_verts[con_common_side][v])
  915.             vv1 = v;
  916.  
  917.         if (cv2 == Side_to_verts[con_common_side][v])
  918.             vv2 = v;
  919.     }
  920.  
  921.     Assert( (vv1 != -1) && (vv2 != -1) );
  922.     assign_uvs_to_side(con_seg, con_common_side, &uv1, &uv2, vv1, vv2);
  923. }
  924.  
  925.  
  926. // -----------------------------------------------------------------------------
  927. //    Given a base and a connecting segment, a side on each of those segments and two global vertex ids,
  928. //    determine which side in each of the segments shares those two vertices.
  929. //    This is used to propagate a texture map id to a connecting segment in an expected and desired way.
  930. //    Since we can attach any side of a segment to any side of another segment, and do so in each case in
  931. //    four different rotations (for a total of 6*6*4 = 144 ways), not having this nifty function will cause
  932. //    great confusion.
  933. void get_side_ids(segment *base_seg, segment *con_seg, int base_side, int con_side, int abs_id1, int abs_id2, int *base_common_side, int *con_common_side)
  934. {
  935.     char        *base_vp,*con_vp;
  936.     int        v0,side;
  937.  
  938.     *base_common_side = -1;
  939.  
  940.     //    Find side in base segment which contains the two global vertex ids.
  941.     for (side=0; side<MAX_SIDES_PER_SEGMENT; side++) {
  942.         if (side != base_side) {
  943.             base_vp = Side_to_verts[side];
  944.             for (v0=0; v0<4; v0++)
  945.                 if (((base_seg->verts[base_vp[v0]] == abs_id1) && (base_seg->verts[base_vp[ (v0+1) % 4]] == abs_id2)) || ((base_seg->verts[base_vp[v0]] == abs_id2) && (base_seg->verts[base_vp[ (v0+1) % 4]] == abs_id1))) {
  946.                     Assert(*base_common_side == -1);        // This means two different sides shared the same edge with base_side == impossible!
  947.                     *base_common_side = side;
  948.                 }
  949.         }
  950.     }
  951.  
  952.     // Note: For connecting segment, process vertices in reversed order.
  953.     *con_common_side = -1;
  954.  
  955.     //    Find side in connecting segment which contains the two global vertex ids.
  956.     for (side=0; side<MAX_SIDES_PER_SEGMENT; side++) {
  957.         if (side != con_side) {
  958.             con_vp = Side_to_verts[side];
  959.             for (v0=0; v0<4; v0++)
  960.                 if (((con_seg->verts[con_vp[(v0 + 1) % 4]] == abs_id1) && (con_seg->verts[con_vp[v0]] == abs_id2))    || ((con_seg->verts[con_vp[(v0 + 1) % 4]] == abs_id2) && (con_seg->verts[con_vp[v0]] == abs_id1))) {
  961.                     Assert(*con_common_side == -1);        // This means two different sides shared the same edge with con_side == impossible!
  962.                     *con_common_side = side;
  963.                 }
  964.         }
  965.     }
  966.  
  967. // mprintf((0,"side %3i adjacent to side %3i\n",*base_common_side,*con_common_side));
  968.  
  969.     Assert((*base_common_side != -1) && (*con_common_side != -1));
  970. }
  971.  
  972. // -----------------------------------------------------------------------------
  973. //    Propagate texture map u,v coordinates from base_seg:base_side to con_seg:con_side.
  974. //    The two vertices abs_id1 and abs_id2 are the only two vertices common to the two sides.
  975. //    If uv_only_flag is 1, then don't assign texture map ids, only update the uv coordinates
  976. //    If uv_only_flag is -1, then ONLY assign texture map ids, don't update the uv coordinates
  977. void propagate_tmaps_to_segment_side(segment *base_seg, int base_side, segment *con_seg, int con_side, int abs_id1, int abs_id2, int uv_only_flag)
  978. {
  979.     int        base_common_side,con_common_side;
  980.     int        tmap_num;
  981.  
  982.     Assert ((uv_only_flag == -1) || (uv_only_flag == 0) || (uv_only_flag == 1));
  983.  
  984.     // Set base_common_side = side in base_seg which contains edge abs_id1:abs_id2
  985.     // Set con_common_side = side in con_seg which contains edge abs_id1:abs_id2
  986.     if (base_seg != con_seg)
  987.         get_side_ids(base_seg, con_seg, base_side, con_side, abs_id1, abs_id2, &base_common_side, &con_common_side);
  988.     else {
  989.         base_common_side = base_side;
  990.         con_common_side = con_side;
  991.     }
  992.  
  993.     // Now, all faces in con_seg which are on side con_common_side get their tmap_num set to whatever tmap is assigned
  994.     // to whatever face I find which is on side base_common_side.
  995.     // First, find tmap_num for base_common_side.  If it doesn't exist (ie, there is a connection there), look at the segment
  996.     // that is connected through it.
  997.     if (!IS_CHILD(con_seg->children[con_common_side])) {
  998.         if (!IS_CHILD(base_seg->children[base_common_side])) {
  999.             // There is at least one face here, so get the tmap_num from there.
  1000.             tmap_num = base_seg->sides[base_common_side].tmap_num;
  1001.  
  1002.             // Now assign all faces in the connecting segment on side con_common_side to tmap_num.
  1003.             if ((uv_only_flag == -1) || (uv_only_flag == 0))
  1004.                 con_seg->sides[con_common_side].tmap_num = tmap_num;
  1005.  
  1006.             if (uv_only_flag != -1)
  1007.                 med_assign_uvs_to_side(con_seg, con_common_side, base_seg, base_common_side, abs_id1, abs_id2);
  1008.  
  1009.         } else {            // There are no faces here, there is a connection, trace through the connection.
  1010.             int    cside;
  1011.  
  1012.             cside = find_connect_side(base_seg, &Segments[base_seg->children[base_common_side]]);
  1013.             propagate_tmaps_to_segment_side(&Segments[base_seg->children[base_common_side]], cside, con_seg, con_side, abs_id1, abs_id2, uv_only_flag);
  1014.         }
  1015.     }
  1016.  
  1017. }
  1018.  
  1019. byte    Edge_between_sides[MAX_SIDES_PER_SEGMENT][MAX_SIDES_PER_SEGMENT][2] = {
  1020. //        left        top        right        bottom    back        front
  1021.     { {-1,-1}, { 3, 7}, {-1,-1}, { 2, 6}, { 6, 7}, { 2, 3} },    // left
  1022.     { { 3, 7}, {-1,-1}, { 0, 4}, {-1,-1}, { 4, 7}, { 0, 3} },    // top
  1023.     { {-1,-1}, { 0, 4}, {-1,-1}, { 1, 5}, { 4, 5}, { 0, 1} },    // right
  1024.     { { 2, 6}, {-1,-1}, { 1, 5}, {-1,-1}, { 5, 6}, { 1, 2} },    // bottom
  1025.     { { 6, 7}, { 4, 7}, { 4, 5}, { 5, 6}, {-1,-1}, {-1,-1} },    // back
  1026.     { { 2, 3}, { 0, 3}, { 0, 1}, { 1, 2}, {-1,-1}, {-1,-1} }};    // front
  1027.  
  1028. // -----------------------------------------------------------------------------
  1029. //    Propagate texture map u,v coordinates to base_seg:back_side from base_seg:some-other-side
  1030. //    There is no easy way to figure out which side is adjacent to another side along some edge, so we do a bit of searching.
  1031. void med_propagate_tmaps_to_back_side(segment *base_seg, int back_side, int uv_only_flag)
  1032. {
  1033.     int    v1,v2;
  1034.     int    s,ss,tmap_num,back_side_tmap;
  1035.  
  1036.     if (IS_CHILD(base_seg->children[back_side]))
  1037.         return;        // connection, so no sides here.
  1038.  
  1039.     //    Scan all sides, look for an occupied side which is not back_side or Side_opposite[back_side]
  1040.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  1041.         if ((s != back_side) && (s != Side_opposite[back_side])) {
  1042.             v1 = Edge_between_sides[s][back_side][0];
  1043.             v2 = Edge_between_sides[s][back_side][1];
  1044.             goto found1;
  1045.         }
  1046.     Assert(0);        // Error -- couldn't find edge != back_side and Side_opposite[back_side]
  1047. found1: ;
  1048.     Assert( (v1 != -1) && (v2 != -1));        // This means there was no shared edge between the two sides.
  1049.  
  1050.     propagate_tmaps_to_segment_side(base_seg, s, base_seg, back_side, base_seg->verts[v1], base_seg->verts[v2], uv_only_flag);
  1051.  
  1052.     //    Assign an unused tmap id to the back side.
  1053.     //    Note that this can get undone by the caller if this was not part of a new attach, but a rotation or a scale (which
  1054.     //    both do attaches).
  1055.     //    First see if tmap on back side is anywhere else.
  1056.     if (!uv_only_flag) {
  1057.         back_side_tmap = base_seg->sides[back_side].tmap_num;
  1058.         for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
  1059.             if (s != back_side)
  1060.                 if (base_seg->sides[s].tmap_num == back_side_tmap) {
  1061.                     for (tmap_num=0; tmap_num < MAX_SIDES_PER_SEGMENT; tmap_num++) {
  1062.                         for (ss=0; ss<MAX_SIDES_PER_SEGMENT; ss++)
  1063.                             if (ss != back_side)
  1064.                                 if (base_seg->sides[ss].tmap_num == New_segment.sides[tmap_num].tmap_num)
  1065.                                     goto found2;        // current texture map (tmap_num) is used on current (ss) side, so try next one
  1066.                         // Current texture map (tmap_num) has not been used, assign to all faces on back_side.
  1067.                         base_seg->sides[back_side].tmap_num = New_segment.sides[tmap_num].tmap_num;
  1068.                         goto done1;
  1069.                     found2: ;
  1070.                     }
  1071.                 }
  1072.         }
  1073.     done1: ;
  1074.     }
  1075.  
  1076. }
  1077.  
  1078. void fix_bogus_uvs_on_side(void)
  1079. {
  1080.     med_propagate_tmaps_to_back_side(Cursegp, Curside, 1);
  1081. }
  1082.  
  1083. void fix_bogus_uvs_on_side1(segment *sp, int sidenum, int uvonly_flag)
  1084. {
  1085.     side    *sidep = &sp->sides[sidenum];
  1086.  
  1087.     if ((sidep->uvls[0].u == 0) && (sidep->uvls[1].u == 0) && (sidep->uvls[2].u == 0)) {
  1088.         mprintf((0,"Found bogus segment %i, side %i\n", sp-Segments, sidenum));
  1089.         med_propagate_tmaps_to_back_side(sp, sidenum, uvonly_flag);
  1090.     }
  1091. }
  1092.  
  1093. void fix_bogus_uvs_seg(segment *segp)
  1094. {
  1095.     int    s;
  1096.  
  1097.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
  1098.         if (!IS_CHILD(segp->children[s]))
  1099.             fix_bogus_uvs_on_side1(segp, s, 1);
  1100.     }
  1101. }
  1102.  
  1103. void fix_bogus_uvs_all(void)
  1104. {
  1105.     int    seg;
  1106.  
  1107.     for (seg=0; seg<=Highest_segment_index; seg++)
  1108.         if (Segments[seg].segnum != -1)
  1109.             fix_bogus_uvs_seg(&Segments[seg]);
  1110. }
  1111.  
  1112. // -----------------------------------------------------------------------------
  1113. //    Propagate texture map u,v coordinates to base_seg:back_side from base_seg:some-other-side
  1114. //    There is no easy way to figure out which side is adjacent to another side along some edge, so we do a bit of searching.
  1115. void med_propagate_tmaps_to_any_side(segment *base_seg, int back_side, int tmap_num, int uv_only_flag)
  1116. {
  1117.     int    v1,v2;
  1118.     int    s;
  1119.  
  1120.     //    Scan all sides, look for an occupied side which is not back_side or Side_opposite[back_side]
  1121.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  1122.         if ((s != back_side) && (s != Side_opposite[back_side])) {
  1123.             v1 = Edge_between_sides[s][back_side][0];
  1124.             v2 = Edge_between_sides[s][back_side][1];
  1125.             goto found1;
  1126.         }
  1127.     Assert(0);        // Error -- couldn't find edge != back_side and Side_opposite[back_side]
  1128. found1: ;
  1129.     Assert( (v1 != -1) && (v2 != -1));        // This means there was no shared edge between the two sides.
  1130.  
  1131.     propagate_tmaps_to_segment_side(base_seg, s, base_seg, back_side, base_seg->verts[v1], base_seg->verts[v2], uv_only_flag);
  1132.  
  1133.     base_seg->sides[back_side].tmap_num = tmap_num;
  1134.  
  1135. }
  1136.  
  1137. // -----------------------------------------------------------------------------
  1138. //    Segment base_seg is connected through side base_side to segment con_seg on con_side.
  1139. //    For all walls in con_seg, find the wall in base_seg which shares an edge.  Copy tmap_num
  1140. //    from that side in base_seg to the wall in con_seg.  If the wall in base_seg is not present
  1141. //    (ie, there is another segment connected through it), follow the connection through that
  1142. //    segment to get the wall in the connected segment which shares the edge, and get tmap_num from there.
  1143. void propagate_tmaps_to_segment_sides(segment *base_seg, int base_side, segment *con_seg, int con_side, int uv_only_flag)
  1144. {
  1145.     char        *base_vp,*con_vp;
  1146.     short        abs_id1,abs_id2;
  1147.     int        v;
  1148.  
  1149.     base_vp = Side_to_verts[base_side];
  1150.     con_vp = Side_to_verts[con_side];
  1151.  
  1152.     // Do for each edge on connecting face.
  1153.     for (v=0; v<4; v++) {
  1154.         abs_id1 = base_seg->verts[base_vp[v]];
  1155.         abs_id2 = base_seg->verts[base_vp[(v+1) % 4]];
  1156.         propagate_tmaps_to_segment_side(base_seg, base_side, con_seg, con_side, abs_id1, abs_id2, uv_only_flag);
  1157.     }
  1158.  
  1159. }
  1160.  
  1161. // -----------------------------------------------------------------------------
  1162. //    Propagate texture maps in base_seg to con_seg.
  1163. //    For each wall in con_seg, find the wall in base_seg which shared an edge.  Copy tmap_num from that
  1164. //    wall in base_seg to the wall in con_seg.  If the wall in base_seg is not present, then look at the
  1165. //    segment connected through base_seg through the wall.  The wall with a common edge is the new wall
  1166. //    of interest.  Continue searching in this way until a wall of interest is present.
  1167. void med_propagate_tmaps_to_segments(segment *base_seg,segment *con_seg, int uv_only_flag)
  1168. {
  1169.     int        s;
  1170.  
  1171. // mprintf((0,"Propagating segments from %i to %i\n",base_seg-Segments,con_seg-Segments));
  1172.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  1173.         if (base_seg->children[s] == con_seg-Segments)
  1174.             propagate_tmaps_to_segment_sides(base_seg, s, con_seg, find_connect_side(base_seg, con_seg), uv_only_flag);
  1175.  
  1176.     con_seg->static_light = base_seg->static_light;
  1177.  
  1178.     validate_uv_coordinates(con_seg);
  1179. }
  1180.  
  1181.  
  1182. // -------------------------------------------------------------------------------
  1183. //    Copy texture map uvs from srcseg to destseg.
  1184. //    If two segments have different face structure (eg, destseg has two faces on side 3, srcseg has only 1)
  1185. //    then assign uvs according to side vertex id, not face vertex id.
  1186. void copy_uvs_seg_to_seg(segment *destseg,segment *srcseg)
  1187. {
  1188.     int    s;
  1189.  
  1190.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
  1191.         destseg->sides[s].tmap_num = srcseg->sides[s].tmap_num;
  1192.         destseg->sides[s].tmap_num2 = srcseg->sides[s].tmap_num2;
  1193.     }
  1194.  
  1195.     destseg->static_light = srcseg->static_light;
  1196. }
  1197.  
  1198. //    _________________________________________________________________________________________________________________________
  1199. //    Maximum distance between a segment containing light to a segment to receive light.
  1200. #define    LIGHT_DISTANCE_THRESHOLD    (F1_0*80)
  1201. fix    Magical_light_constant = (F1_0*16);
  1202.  
  1203. // int    Seg0, Seg1;
  1204.  
  1205. //int    Bugseg = 27;
  1206.  
  1207. typedef struct {
  1208.     byte            flag, hit_type;
  1209.     vms_vector    vector;
  1210. } hash_info;
  1211.  
  1212. #define    FVI_HASH_SIZE 8
  1213. #define    FVI_HASH_AND_MASK (FVI_HASH_SIZE - 1)
  1214.  
  1215. //    Note: This should be malloced.
  1216. //            Also, the vector should not be 12 bytes, you should only care about some smaller portion of it.
  1217. hash_info    fvi_cache[FVI_HASH_SIZE];
  1218. int    Hash_hits=0, Hash_retries=0, Hash_calcs=0;
  1219.  
  1220. //    -----------------------------------------------------------------------------------------
  1221. //    Set light from a light source.
  1222. //    Light incident on a surface is defined by the light incident at its points.
  1223. //    Light at a point = K * (V . N) / d
  1224. //    where:
  1225. //        K = some magical constant to make everything look good
  1226. //        V = normalized vector from light source to point
  1227. //        N = surface normal at point
  1228. //        d = distance from light source to point
  1229. //    (Note that the above equation can be simplified to K * (VV . N) / d^2 where VV = non-normalized V)
  1230. //    Light intensity emitted from a light source is defined to be cast from four points.
  1231. //    These four points are 1/64 of the way from the corners of the light source to the center
  1232. //    of its segment.  By assuming light is cast from these points, rather than from on the
  1233. //    light surface itself, light will be properly cast on the light surface.  Otherwise, the
  1234. //    vector V would be the null vector.
  1235. //    If quick_light set, then don't use find_vector_intersection
  1236. void cast_light_from_side(segment *segp, int light_side, fix light_intensity, int quick_light)
  1237. {
  1238.     vms_vector    segment_center;
  1239.     int            segnum,sidenum,vertnum, lightnum;
  1240.  
  1241.     compute_segment_center(&segment_center, segp);
  1242.  
  1243. //mprintf((0, "From [%i %i %7.3f]:  ", segp-Segments, light_side, f2fl(light_intensity)));
  1244.  
  1245.     //    Do for four lights, one just inside each corner of side containing light.
  1246.     for (lightnum=0; lightnum<4; lightnum++) {
  1247.         int            light_vertex_num, i;
  1248.         vms_vector    vector_to_center;
  1249.         vms_vector    light_location;
  1250.         // fix            inverse_segment_magnitude;
  1251.  
  1252.         light_vertex_num = segp->verts[Side_to_verts[light_side][lightnum]];
  1253.         light_location = Vertices[light_vertex_num];
  1254.  
  1255.  
  1256.     //    New way, 5/8/95: Move towards center irrespective of size of segment.
  1257.     vm_vec_sub(&vector_to_center, &segment_center, &light_location);
  1258.     vm_vec_normalize_quick(&vector_to_center);
  1259.     vm_vec_add2(&light_location, &vector_to_center);
  1260.  
  1261. // -- Old way, before 5/8/95 --        // -- This way was kind of dumb.  In larger segments, you move LESS towards the center.
  1262. // -- Old way, before 5/8/95 --        //    Main problem, though, is vertices don't illuminate themselves well in oblong segments because the dot product is small.
  1263. // -- Old way, before 5/8/95 --        vm_vec_sub(&vector_to_center, &segment_center, &light_location);
  1264. // -- Old way, before 5/8/95 --        inverse_segment_magnitude = fixdiv(F1_0/5, vm_vec_mag(&vector_to_center));
  1265. // -- Old way, before 5/8/95 --        vm_vec_scale_add(&light_location, &light_location, &vector_to_center, inverse_segment_magnitude);
  1266.  
  1267.         for (segnum=0; segnum<=Highest_segment_index; segnum++) {
  1268.             segment        *rsegp = &Segments[segnum];
  1269.             vms_vector    r_segment_center;
  1270.             fix            dist_to_rseg;
  1271.  
  1272.             for (i=0; i<FVI_HASH_SIZE; i++)
  1273.                 fvi_cache[i].flag = 0;
  1274.  
  1275.             //    efficiency hack (I hope!), for faraway segments, don't check each point.
  1276.             compute_segment_center(&r_segment_center, rsegp);
  1277.             dist_to_rseg = vm_vec_dist_quick(&r_segment_center, &segment_center);
  1278.  
  1279.             if (dist_to_rseg <= LIGHT_DISTANCE_THRESHOLD) {
  1280.                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
  1281.                     if (WALL_IS_DOORWAY(rsegp, sidenum) != WID_NO_WALL) {
  1282.                         side            *rsidep = &rsegp->sides[sidenum];
  1283.                         vms_vector    *side_normalp = &rsidep->normals[0];    //    kinda stupid? always use vector 0.
  1284.  
  1285. //mprintf((0, "[%i %i], ", rsegp-Segments, sidenum));
  1286.                         for (vertnum=0; vertnum<4; vertnum++) {
  1287.                             fix            distance_to_point, light_at_point, light_dot;
  1288.                             vms_vector    vert_location, vector_to_light;
  1289.                             int            abs_vertnum;
  1290.  
  1291.                             abs_vertnum = rsegp->verts[Side_to_verts[sidenum][vertnum]];
  1292.                             vert_location = Vertices[abs_vertnum];
  1293.                             distance_to_point = vm_vec_dist_quick(&vert_location, &light_location);
  1294.                             vm_vec_sub(&vector_to_light, &light_location, &vert_location);
  1295.                             vm_vec_normalize(&vector_to_light);
  1296.  
  1297.                             //    Hack: In oblong segments, it's possible to get a very small dot product
  1298.                             //    but the light source is very nearby (eg, illuminating light itself!).
  1299.                             light_dot = vm_vec_dot(&vector_to_light, side_normalp);
  1300.                             if (distance_to_point < F1_0)
  1301.                                 if (light_dot > 0)
  1302.                                     light_dot = (light_dot + F1_0)/2;
  1303.  
  1304.                             if (light_dot > 0) {
  1305.                                 light_at_point = fixdiv(fixmul(light_dot, light_dot), distance_to_point);
  1306.                                 light_at_point = fixmul(light_at_point, Magical_light_constant);
  1307.                                 if (light_at_point >= 0) {
  1308.                                     fvi_info    hit_data;
  1309.                                     int        hit_type;
  1310.                                     vms_vector    vert_location_1, r_vector_to_center;
  1311.                                     fix        inverse_segment_magnitude;
  1312.  
  1313.                                     vm_vec_sub(&r_vector_to_center, &r_segment_center, &vert_location);
  1314.                                     inverse_segment_magnitude = fixdiv(F1_0/3, vm_vec_mag(&r_vector_to_center));
  1315.                                     vm_vec_scale_add(&vert_location_1, &vert_location, &r_vector_to_center, inverse_segment_magnitude);
  1316.                                     vert_location = vert_location_1;
  1317.  
  1318. //if ((segp-Segments == 199) && (rsegp-Segments==199))
  1319. //    Int3();
  1320. // Seg0 = segp-Segments;
  1321. // Seg1 = rsegp-Segments;
  1322.                                     if (!quick_light) {
  1323.                                         int hash_value = Side_to_verts[sidenum][vertnum];
  1324.                                         hash_info    *hashp = &fvi_cache[hash_value];
  1325.                                         while (1) {
  1326.                                             if (hashp->flag) {
  1327.                                                 if ((hashp->vector.x == vector_to_light.x) && (hashp->vector.y == vector_to_light.y) && (hashp->vector.z == vector_to_light.z)) {
  1328. //mprintf((0, "{CACHE %4x} ", hash_value));
  1329.                                                     hit_type = hashp->hit_type;
  1330.                                                     Hash_hits++;
  1331.                                                     break;
  1332.                                                 } else {
  1333.                                                     Int3();    // How is this possible?  Should be no hits!
  1334.                                                     Hash_retries++;
  1335.                                                     hash_value = (hash_value+1) & FVI_HASH_AND_MASK;
  1336.                                                     hashp = &fvi_cache[hash_value];
  1337.                                                 }
  1338.                                             } else {
  1339. //mprintf((0, "\nH:%04x ", hash_value));
  1340.                                                 fvi_query fq;
  1341.  
  1342.                                                 Hash_calcs++;
  1343.                                                 hashp->vector = vector_to_light;
  1344.                                                 hashp->flag = 1;
  1345.  
  1346.                                                 fq.p0                        = &light_location;
  1347.                                                 fq.startseg                = segp-Segments;
  1348.                                                 fq.p1                        = &vert_location;
  1349.                                                 fq.rad                    = 0;
  1350.                                                 fq.thisobjnum            = -1;
  1351.                                                 fq.ignore_obj_list    = NULL;
  1352.                                                 fq.flags                    = 0;
  1353.  
  1354.                                                 hit_type = find_vector_intersection(&fq,&hit_data);
  1355.                                                 hashp->hit_type = hit_type;
  1356.                                                 break;
  1357.                                             }
  1358.                                         }
  1359.                                     } else
  1360.                                         hit_type = HIT_NONE;
  1361. //mprintf((0, "hit=%i ", hit_type));
  1362.                                     switch (hit_type) {
  1363.                                         case HIT_NONE:
  1364.                                             light_at_point = fixmul(light_at_point, light_intensity);
  1365.                                             rsidep->uvls[vertnum].l += light_at_point;
  1366. //mprintf((0, "(%5.2f) ", f2fl(light_at_point)));
  1367.                                             if (rsidep->uvls[vertnum].l > F1_0)
  1368.                                                 rsidep->uvls[vertnum].l = F1_0;
  1369.                                             break;
  1370.                                         case HIT_WALL:
  1371.                                             break;
  1372.                                         case HIT_OBJECT:
  1373.                                             Int3();    // Hit object, should be ignoring objects!
  1374.                                             break;
  1375.                                         case HIT_BAD_P0:
  1376.                                             Int3();    //    Ugh, this thing again, what happened, what does it mean?
  1377.                                             break;
  1378.                                     }
  1379.                                 }    //    end if (light_at_point...
  1380.                             }    // end if (light_dot >...
  1381.                         }    //    end for (vertnum=0...
  1382.                     }    //    end if (rsegp...
  1383.                 }    //    end for (sidenum=0...
  1384.             }    //    end if (dist_to_rseg...
  1385.  
  1386.         }    //    end for (segnum=0...
  1387.  
  1388.     }    //    end for (lightnum=0...
  1389.  
  1390. //mprintf((0, "\n"));
  1391. }
  1392.  
  1393.  
  1394. //    ------------------------------------------------------------------------------------------
  1395. //    Zero all lighting values.
  1396. void calim_zero_light_values(void)
  1397. {
  1398.     int    segnum, sidenum, vertnum;
  1399.  
  1400.     for (segnum=0; segnum<=Highest_segment_index; segnum++) {
  1401.         segment *segp = &Segments[segnum];
  1402.         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
  1403.             side    *sidep = &segp->sides[sidenum];
  1404.             for (vertnum=0; vertnum<4; vertnum++)
  1405.                 sidep->uvls[vertnum].l = F1_0/64;    // Put a tiny bit of light here.
  1406.         }
  1407.         Segments[segnum].static_light = F1_0/64;
  1408.     }
  1409. }
  1410.  
  1411.  
  1412. //    ------------------------------------------------------------------------------------------
  1413. //    Used in setting average light value in a segment, cast light from a side to the center
  1414. //    of all segments.
  1415. void cast_light_from_side_to_center(segment *segp, int light_side, fix light_intensity, int quick_light)
  1416. {
  1417.     vms_vector    segment_center;
  1418.     int            segnum, lightnum;
  1419.  
  1420.     compute_segment_center(&segment_center, segp);
  1421.  
  1422.     //    Do for four lights, one just inside each corner of side containing light.
  1423.     for (lightnum=0; lightnum<4; lightnum++) {
  1424.         int            light_vertex_num;
  1425.         vms_vector    vector_to_center;
  1426.         vms_vector    light_location;
  1427.  
  1428.         light_vertex_num = segp->verts[Side_to_verts[light_side][lightnum]];
  1429.         light_location = Vertices[light_vertex_num];
  1430.         vm_vec_sub(&vector_to_center, &segment_center, &light_location);
  1431.         vm_vec_scale_add(&light_location, &light_location, &vector_to_center, F1_0/64);
  1432.  
  1433.         for (segnum=0; segnum<=Highest_segment_index; segnum++) {
  1434.             segment        *rsegp = &Segments[segnum];
  1435.             vms_vector    r_segment_center;
  1436.             fix            dist_to_rseg;
  1437. //if ((segp == &Segments[Bugseg]) && (rsegp == &Segments[Bugseg]))
  1438. //    Int3();
  1439.             compute_segment_center(&r_segment_center, rsegp);
  1440.             dist_to_rseg = vm_vec_dist_quick(&r_segment_center, &segment_center);
  1441.  
  1442.             if (dist_to_rseg <= LIGHT_DISTANCE_THRESHOLD) {
  1443.                 fix    light_at_point;
  1444.                 if (dist_to_rseg > F1_0)
  1445.                     light_at_point = fixdiv(Magical_light_constant, dist_to_rseg);
  1446.                 else
  1447.                     light_at_point = Magical_light_constant;
  1448.  
  1449.                 if (light_at_point >= 0) {
  1450.                     int        hit_type;
  1451.  
  1452.                     if (!quick_light) {
  1453.                         fvi_query fq;
  1454.                         fvi_info    hit_data;
  1455.  
  1456.                         fq.p0                        = &light_location;
  1457.                         fq.startseg                = segp-Segments;
  1458.                         fq.p1                        = &r_segment_center;
  1459.                         fq.rad                    = 0;
  1460.                         fq.thisobjnum            = -1;
  1461.                         fq.ignore_obj_list    = NULL;
  1462.                         fq.flags                    = 0;
  1463.  
  1464.                         hit_type = find_vector_intersection(&fq,&hit_data);
  1465.                     }
  1466.                     else
  1467.                         hit_type = HIT_NONE;
  1468.  
  1469.                     switch (hit_type) {
  1470.                         case HIT_NONE:
  1471.                             light_at_point = fixmul(light_at_point, light_intensity);
  1472.                             if (light_at_point >= F1_0)
  1473.                                 light_at_point = F1_0-1;
  1474.                             rsegp->static_light += light_at_point;
  1475.                             if (segp->static_light < 0)    // if it went negative, saturate
  1476.                                 segp->static_light = 0;
  1477.                             break;
  1478.                         case HIT_WALL:
  1479.                             break;
  1480.                         case HIT_OBJECT:
  1481.                             Int3();    // Hit object, should be ignoring objects!
  1482.                             break;
  1483.                         case HIT_BAD_P0:
  1484.                             Int3();    //    Ugh, this thing again, what happened, what does it mean?
  1485.                             break;
  1486.                     }
  1487.                 }    //    end if (light_at_point...
  1488.             }    //    end if (dist_to_rseg...
  1489.  
  1490.         }    //    end for (segnum=0...
  1491.  
  1492.     }    //    end for (lightnum=0...
  1493.  
  1494. }
  1495.  
  1496. //    ------------------------------------------------------------------------------------------
  1497. //    Process all lights.
  1498. void calim_process_all_lights(int quick_light)
  1499. {
  1500.     int    segnum, sidenum;
  1501.  
  1502.     for (segnum=0; segnum<=Highest_segment_index; segnum++) {
  1503.         segment    *segp = &Segments[segnum];
  1504.         mprintf((0, "."));
  1505.         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
  1506.             // if (!IS_CHILD(segp->children[sidenum])) {
  1507.             if (WALL_IS_DOORWAY(segp, sidenum) != WID_NO_WALL) {
  1508.                 side    *sidep = &segp->sides[sidenum];
  1509.                 fix    light_intensity;
  1510.  
  1511.                 light_intensity = TmapInfo[sidep->tmap_num].lighting + TmapInfo[sidep->tmap_num2 & 0x3fff].lighting;
  1512.  
  1513. //                if (segp->sides[sidenum].wall_num != -1) {
  1514. //                    int    wall_num, bitmap_num, effect_num;
  1515. //                    wall_num = segp->sides[sidenum].wall_num;
  1516. //                    effect_num = Walls[wall_num].type;
  1517. //                    bitmap_num = effects_bm_num[effect_num];
  1518. //
  1519. //                    light_intensity += TmapInfo[bitmap_num].lighting;
  1520. //                }
  1521.  
  1522.                 if (light_intensity) {
  1523.                     light_intensity /= 4;            // casting light from four spots, so divide by 4.
  1524.                     cast_light_from_side(segp, sidenum, light_intensity, quick_light);
  1525.                     cast_light_from_side_to_center(segp, sidenum, light_intensity, quick_light);
  1526.                 }
  1527.             }
  1528.         }
  1529.     }
  1530. }
  1531.  
  1532. //    ------------------------------------------------------------------------------------------
  1533. //    Apply static light in mine.
  1534. //    First, zero all light values.
  1535. //    Then, for all light sources, cast their light.
  1536. void cast_all_light_in_mine(int quick_flag)
  1537. {
  1538.  
  1539.     validate_segment_all();
  1540.  
  1541.     calim_zero_light_values();
  1542.  
  1543.     calim_process_all_lights(quick_flag);
  1544.  
  1545. }
  1546.  
  1547. // int    Fvit_num = 1000;
  1548. // 
  1549. // fix find_vector_intersection_test(void)
  1550. // {
  1551. //     int        i;
  1552. //     fvi_info    hit_data;
  1553. //     int        p0_seg, p1_seg, this_objnum, ignore_obj, check_obj_flag;
  1554. //     fix        rad;
  1555. //     int        start_time = timer_get_milliseconds();;
  1556. //     vms_vector    p0,p1;
  1557. // 
  1558. //     ignore_obj = 1;
  1559. //     check_obj_flag = 0;
  1560. //     this_objnum = -1;
  1561. //     rad = F1_0/4;
  1562. // 
  1563. //     for (i=0; i<Fvit_num; i++) {
  1564. //         p0_seg = rand()*(Highest_segment_index+1)/32768;
  1565. //         compute_segment_center(&p0, &Segments[p0_seg]);
  1566. // 
  1567. //         p1_seg = rand()*(Highest_segment_index+1)/32768;
  1568. //         compute_segment_center(&p1, &Segments[p1_seg]);
  1569. // 
  1570. //         find_vector_intersection(&hit_data, &p0, p0_seg, &p1, rad, this_objnum, ignore_obj, check_obj_flag);
  1571. //     }
  1572. // 
  1573. //     return timer_get_milliseconds() - start_time;
  1574. // }
  1575.  
  1576. vms_vector    Normals[MAX_SEGMENTS*12];
  1577.  
  1578. int    Normal_nearness = 4;
  1579.  
  1580. int normal_near(vms_vector *v1, vms_vector *v2)
  1581. {
  1582.     if (abs(v1->x - v2->x) < Normal_nearness)
  1583.         if (abs(v1->y - v2->y) < Normal_nearness)
  1584.             if (abs(v1->z - v2->z) < Normal_nearness)
  1585.                 return 1;
  1586.     return 0;
  1587. }
  1588.  
  1589. int    Total_normals=0;
  1590. int    Diff_normals=0;
  1591.  
  1592. void print_normals(void)
  1593. {
  1594.     int            i,j,s,n,nn;
  1595.     // vms_vector    *normal;
  1596.     int            num_normals=0;
  1597.  
  1598.     Total_normals = 0;
  1599.     Diff_normals = 0;
  1600.  
  1601.     for (i=0; i<=Highest_segment_index; i++)
  1602.         for (s=0; s<6; s++) {
  1603.             if (Segments[i].sides[s].type == SIDE_IS_QUAD)
  1604.                 nn=1;
  1605.             else
  1606.                 nn=2;
  1607.             for (n=0; n<nn; n++) {
  1608.                 for (j=0; j<num_normals; j++)
  1609.                     if (normal_near(&Segments[i].sides[s].normals[n],&Normals[j]))
  1610.                         break;
  1611.                 if (j == num_normals) {
  1612.                     Normals[num_normals++] = Segments[i].sides[s].normals[n];
  1613.                     Diff_normals++;
  1614.                 }
  1615.                 Total_normals++;
  1616.             }
  1617.         }
  1618.  
  1619. }
  1620.  
  1621.