File: HTexture.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (820 lines) | stat: -rw-r--r-- 23,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/editor/HTexture.cpp $
 * $Revision: 1.1.1.1 $
 * $Date: 2003-08-26 03:57:38 $
 * $Author: kevinb $
 *
 * HTexture implementation
 *
 * $Log: not supported by cvs2svn $
 *
 * 37    12/27/99 8:26a Gwar
 *
 * 36    8/30/99 12:55p Gwar
 * added a parameter to HTexturePropagateToFace to not change the texture
 * of the target face
 *
 * 35    7/30/99 1:19p Gwar
 * don't set World_changed in NEWEDITOR
 *
 * 34    7/24/99 5:25a Gwar
 * NEWEDITOR changes to HTextureApplyToRoomFace
 *
 * 33    7/04/99 4:56p Gwar
 * changes for texture management in NEWEDITOR
 *
 * 32    5/19/99 12:19a Gwar
 * #define Curroomp, Curface, etc as members of prim struct in NEWEDITOR
 * -- this allows the main room frame and level window to share these
 * variables and makes them non-global (i.e. no more sync problem between
 * world and room views)
 *
 * 31    5/03/99 2:06a Gwar
 * added HTexture.cpp to project
 *
 * 30    1/20/99 10:50a Jason
 * added new terrain
 *
 * 29    10/21/98 1:27p Jason
 * fixed texture u2,v2 sliding problems
 *
 * 28    8/16/98 4:24p Matt
 * When copying UVs from one face to another, only copy u & v, and not
 * other fields in the uv structure.
 *
 * 27    8/15/98 5:04p Matt
 * Added code to copy a texture & UVs from one face to another
 *
 * 26    6/15/98 4:00p Jason
 * replaced monochromatic polymodel lighting with rgb lighting
 *
 * 25    4/16/98 10:42a Matt
 * Made texture propagation work across rooms.
 *
 * 24    4/02/98 12:23p Jason
 * trimmed some fat from our structures
 *
 * 23    9/23/97 12:20p Jason
 * fixed bug where rotating a texture would cause alphas and u2,v2s to
 * become corrupt
 *
 * 22    9/17/97 1:36p Samir
 * Fixed some new prototypes.
 *
 * 21    9/17/97 11:57a Matt
 * Ripped out segment code
 *
 * 19    8/21/97 6:01p Matt
 * Use groovy new FindSharedEdge() function
 *
 * 18    8/13/97 9:52a Matt
 * Combined slide left/right/up/down into one function, and the same with
 * rotate left/right
 *
 * 17    8/07/97 5:06p Jason
 * added new tiling system for terrain
 *
 * 16    8/07/97 3:19p Jason
 * replaced old terrain tmap system with new one
 *
 * 15    8/01/97 4:53p Jason
 * made a more general PropagateTextureToFace function call
 *
 * 14    7/22/97 7:08p Matt
 * Cleaned up D3EditState, moving some vars in and some out, and renaming
 * and changing a few others
 *
 * 13    7/18/97 7:38p Jason
 * finished room/terrain texture modifications
 *
 * 12    7/16/97 6:31p Jason
 * changed defaut texture uvs for terrain
 *
 * 15    6/24/97 12:41p Jason
 * checked in for safety
 *
 * 14    6/18/97 11:14a Mark
 * FROM JASON:Fixed dumb terrain rotational bug
 *
 * 13    6/17/97 4:16p Jason
 * added some terrain features
 *
 * 12    6/17/97 11:35a Jason
 * checked in for safety
 *
 * 11    6/06/97 4:57p Jason
 * more nifty megacell/tmap2 features
 *
 * 10    6/04/97 1:32p Matt
 * Changed Side_to_verts[] and Side_opposite[] to be arrays of ints, which
 * should be faster than bytes.
 *
 * 9     5/30/97 3:32p Samir
 * When rotating textures, lighting preserved
 *
 * 8     5/02/97 7:21p Matt
 * Added code to rotate tmap2's
 *
 * 7     4/17/97 4:03p Samir
 * Apply Tmap 1 and Tmap2 implemented, but no Tmap2 rendering is occurring
 * yet.
 *
 * 6     3/31/97 5:58p Matt
 * Revamped mine update flags
 *
 * 5     3/31/97 3:50p Samir
 * Set default texture uvs should work for side not whole segment.
 *
 * 4     2/26/97 6:40p Samir
 * changed g3s_uvl to g3UVL
 *
 * 3     2/26/97 3:59p Samir
 * Most scaling, rotating, sliding and flipping work.
 *
 * 2     2/20/97 11:59a Samir
 * Fixed bugs
 *
 * 1     2/20/97 11:50a Samir
 *
 * $NoKeywords: $
 */

#include "HTexture.h"

#ifndef NEWEDITOR
#include "d3edit.h"
#endif

#include "roomuvs.h"
#include "gametexture.h"
#include "terrain.h"
#include "room.h"
#include "descent.h"
#include "erooms.h"

#define HTEX_CALIB_VAL ((float)(1.0 / 128.0))

//	---------------------------------------------------------------------------
//	internal function prototypes
//	---------------------------------------------------------------------------

void HTextureRoomStretch(room *rp, int facenum, int edge, int direction);
void HTextureRotUVPoint(g3UVL *uvrot, float *rotmat, roomUVL *uv, g3UVL *uvcenter);
void HTextureCreate2DRotMat(float *rotmat, angle ang);
void HTextureRotUVTerrainPoint(g3UVL *uvrot, float *rotmat, g3UVL *uv, g3UVL *uvcenter);
void GetUVLForRoomPoint(int roomnum, int facenum, int vertnum, roomUVL *uvl);
int HTextureGetTopVertexForFace(int roomnum, int facenum);
int HTextureGetBottomVertexForFace(int roomnum, int facenum);
int HTextureGetLeftVertexForFace(int roomnum, int facenum);
int HTextureGetRightVertexForFace(int roomnum, int facenum);
void HTextureRotUVPointsOnRoomFace(room *roomp, int facenum, float *rotmat, g3UVL *uvcenter);
void HTextureComputeUVRoomFaceCenter(g3UVL *uvcenter, room *roomp, int facenum);
void HTextureTerrainStretch(int edge, int direction);

//	---------------------------------------------------------------------------
//	Functions to manipulate textures within the mine, through segments
//	---------------------------------------------------------------------------

// Apply the specified texture to the specified room:face
void HTextureApplyToRoomFace(room *rp, int facenum, int tnum) {
  ASSERT(rp->used);

#ifdef NEWEDITOR
  if (tnum == -1)
    return;
  if (rp->faces[facenum].tmap >= 0)
    SwitchTexture(rp, rp->faces[facenum].tmap, tnum);
#endif

  rp->faces[facenum].tmap = tnum;
}

// Copy texture from current face to adjacent face, tiling the UV coordinates
// Parameters:	destrp,destface - room:face that the propagate is based on
//					srcrp,srcface - room:face that is changed
// Return:	1 if success, 0 if faces not adjacent
int HTexturePropagateToFace(room *destrp, int destface, room *srcrp, int srcface, bool tex) {
  face *dfp = &destrp->faces[destface];
  face *sfp = &srcrp->faces[srcface];
  int v0, v1;

  // Get shared edge
  if (destrp != srcrp) { // Faces in different rooms
    if (!FindSharedEdgeAcrossRooms(destrp, destface, srcrp, srcface, &v0, &v1))
      return 0;
  } else { // Faces in same room
    if (!FindSharedEdge(dfp, sfp, &v0, &v1))
      return 0;
  }

  if (tex) {
#ifdef NEWEDITOR
    SwitchTexture(destrp, dfp->tmap, sfp->tmap);
#endif

    // Copy texture from Curface
    dfp->tmap = sfp->tmap;
  }

  // Assign uvs from shared edge to rest of verts in face
  AssignUVsToFace(destrp, destface, &sfp->face_uvls[(v1 + 1) % sfp->num_verts], &sfp->face_uvls[v1], v0,
                  (v0 + 1) % dfp->num_verts);

  // Success
  return 1;
}

// Copy texture UVs from one face to another
// Parameters:	destrp,destface - room:face that is changed
//					srcrp,srcface - room:face to copy from
//					offset - vert 0 on source is assigned to vert offset on dest
// Return:	1 if success, 0 if faces don't have the same number of verts
int HTextureCopyUVsToFace(room *destrp, int destface, room *srcrp, int srcface, int offset) {
  face *dfp = &destrp->faces[destface];
  face *sfp = &srcrp->faces[srcface];

  // Make sure faces have the same number of vertices
  if (dfp->num_verts != sfp->num_verts)
    return 0;

  // Copy UV values
  for (int i = 0; i < dfp->num_verts; i++) {
    dfp->face_uvls[(i + offset) % dfp->num_verts].u = sfp->face_uvls[i].u;
    dfp->face_uvls[(i + offset) % dfp->num_verts].v = sfp->face_uvls[i].v;
  }

  // Success!
#ifndef NEWEDITOR
  World_changed = 1;
#endif
  return 1;
}

//	texture flipping

void HTextureFlipX() {
  int i;

  if (Editor_view_mode == VM_TERRAIN) {
    // terrain_segment oldseg;

    for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
      if (TerrainSelected[i]) {
        /*oldseg=Terrain_seg[i];
        Terrain_seg[i].u[0]=oldseg.u[1];
        Terrain_seg[i].u[1]=oldseg.u[0];
        Terrain_seg[i].u[2]=oldseg.u[3];
        Terrain_seg[i].u[3]=oldseg.u[2];

        Terrain_seg[i].v[0]=oldseg.v[1];
        Terrain_seg[i].v[1]=oldseg.v[0];
        Terrain_seg[i].v[2]=oldseg.v[3];
        Terrain_seg[i].v[3]=oldseg.v[2];*/
      }
    }
  } else {
    ASSERT(Curroomp->used);

    for (int i = 0; i < Curroomp->faces[Curface].num_verts; i++)
      Curroomp->faces[Curface].face_uvls[i].u = 1 - Curroomp->faces[Curface].face_uvls[i].u;
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureFlipY() {
  int i;

  if (Editor_view_mode == VM_TERRAIN) {
    terrain_segment oldseg;

    for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
      if (TerrainSelected[i]) {
        oldseg = Terrain_seg[i];
        /*Terrain_seg[i].v[0]=oldseg.v[3];
        Terrain_seg[i].v[1]=oldseg.v[2];
        Terrain_seg[i].v[2]=oldseg.v[1];
        Terrain_seg[i].v[3]=oldseg.v[0];

        Terrain_seg[i].u[0]=oldseg.u[3];
        Terrain_seg[i].u[1]=oldseg.u[2];
        Terrain_seg[i].u[2]=oldseg.u[1];
        Terrain_seg[i].u[3]=oldseg.u[0];*/
      }
    }
  } else {
    ASSERT(Curroomp->used);

    for (int i = 0; i < Curroomp->faces[Curface].num_verts; i++)
      Curroomp->faces[Curface].face_uvls[i].v = 1 - Curroomp->faces[Curface].face_uvls[i].v;
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureSlide(room *rp, int facenum, float right, float up) {
  ASSERT(rp != NULL);
  ASSERT(rp->used);

  if (Editor_view_mode == VM_TERRAIN) {
    /*int i;
    for (i=0;i<TERRAIN_DEPTH*TERRAIN_WIDTH;i++)
    {
            if (TerrainSelected[i])
            {
                    for (int t=0;t<4;t++)
                    {
                            float val=Terrain_UV_lookup[Terrain_seg[i].u[t]];

                            val+=(1.0/32.0);

                            Terrain_seg[i].u[t]=TerrainLookupUV(val);
                    }

            }

    }
    */
    return;

  } else { // mine
    for (int i = 0; i < rp->faces[facenum].num_verts; i++) {
      rp->faces[facenum].face_uvls[i].u -= right / 128.0f;
      rp->faces[facenum].face_uvls[i].v += up / 128.0f;
    }
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureRotate(room *roomp, int facenum, angle ang) {
  g3UVL uvcenter;
  float rotmat[4];

  HTextureCreate2DRotMat(rotmat, ang);

  if (Editor_view_mode == VM_TERRAIN && ang == 0x4000) {
    for (int i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
      if (TerrainSelected[i]) {
        /*int f=(Terrain_seg[i].rotation_factor&0x0F);
        f++;
        f%=4;

        Terrain_seg[i].rotation_factor&=0xF0;
        Terrain_seg[i].rotation_factor|=(f);*/
      }
    }

    return;
  } else {
    HTextureComputeUVRoomFaceCenter(&uvcenter, roomp, facenum);
    HTextureRotUVPointsOnRoomFace(roomp, facenum, rotmat, &uvcenter);
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureStretchMore(room *rp, int face, int edge) {
  if (Editor_view_mode == VM_TERRAIN) {
    HTextureTerrainStretch(edge, 1 * D3EditState.texscale);
  } else {
    HTextureRoomStretch(rp, face, edge, 1 * D3EditState.texscale);
  }
}

void HTextureStretchLess(room *rp, int face, int edge) {
  if (Editor_view_mode == VM_TERRAIN) {
    HTextureTerrainStretch(edge, (-1 * D3EditState.texscale));
  } else {
    HTextureRoomStretch(rp, face, edge, (-1 * D3EditState.texscale));
  }
}

void HTextureSetDefault(room *rp, int face) {
  if (Editor_view_mode == VM_TERRAIN) {
    for (int i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
      if (TerrainSelected[i]) {
        int index = Terrain_seg[i].texseg_index;
        Terrain_tex_seg[index].rotation = 0;
      }
    }
    World_changed = 1;
  } else if (Editor_view_mode == VM_ROOM) {
    //@@Stretch_scale_x = (float)1.0;
    //@@Stretch_scale_y = (float)1.0;

    for (int i = 0; i < rp->faces[face].num_verts; i++) {
      float saveu2 = rp->faces[face].face_uvls[i].u2;
      float savev2 = rp->faces[face].face_uvls[i].v2;

      GetUVLForRoomPoint(ROOMNUM(rp), face, i, &rp->faces[face].face_uvls[i]);

      rp->faces[face].face_uvls[i].u2 = saveu2;
      rp->faces[face].face_uvls[i].v2 = savev2;
    }
  } else {
    //@@Stretch_scale_x = (float)1.0;
    //@@Stretch_scale_y = (float)1.0;

    for (int i = 0; i < rp->faces[face].num_verts; i++) {
      float saveu2 = rp->faces[face].face_uvls[i].u2;
      float savev2 = rp->faces[face].face_uvls[i].v2;

      GetUVLForRoomPoint(ROOMNUM(rp), face, i, &rp->faces[face].face_uvls[i]);

      rp->faces[face].face_uvls[i].u2 = saveu2;
      rp->faces[face].face_uvls[i].v2 = savev2;
    }
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureNextEdge() {
  Curedge++;

  if (Editor_view_mode == VM_TERRAIN) {
    if (Curedge >= 4)
      Curedge = 0;
  } else // mine
  {
    if (Curedge >= Curroomp->faces[Curface].num_verts)
      Curedge = 0;
  }

  State_changed = 1;
}

//	---------------------------------------------------------------------------
//	functions used by HTexture operations
//	---------------------------------------------------------------------------

//	direction = -1 or 1 depending on direction
// Stretches all vertices towards or away from a given edge
// Depending on the geometry of the face, this can cause distortions
void HTextureRoomStretch(room *rp, int facenum, int edge, int direction) {
  int next_edge = (edge + 1) % rp->faces[facenum].num_verts;

  // find deltas
  float du = rp->faces[facenum].face_uvls[next_edge].u - rp->faces[facenum].face_uvls[edge].u;
  float dv = rp->faces[facenum].face_uvls[next_edge].v - rp->faces[facenum].face_uvls[edge].v;

  // rotate delta's 90 degrees
  float nu = -dv;
  float nv = du;

  // Find a magnitude to shift by
  float mag = sqrt((nv * nv) + (nu * nu));
  nv /= mag;
  nu /= mag;

  nv /= 64.0;
  nu /= 64.0;

  // Apply to all verts except for our edge
  for (int i = 0; i < rp->faces[facenum].num_verts; i++) {
    if (i == edge || i == next_edge)
      continue;

    rp->faces[facenum].face_uvls[i].v -= (nv * direction);
    rp->faces[facenum].face_uvls[i].u -= (nu * direction);
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureTerrainStretch(int edge, int direction) {
  int i;

  for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
    if (TerrainSelected[i]) {
    }
  }

#ifndef NEWEDITOR
  World_changed = 1;
#endif
}

void HTextureComputeUVRoomFaceCenter(g3UVL *uvcenter, room *roomp, int facenum) {
  int i;
  face *facep = &roomp->faces[facenum];

  uvcenter->u = 0;
  uvcenter->v = 0;

  for (i = 0; i < facep->num_verts; i++) {
    uvcenter->u += facep->face_uvls[i].u;
    uvcenter->v += facep->face_uvls[i].v;
  }

  uvcenter->u /= i;
  uvcenter->v /= i;
}

void HTextureRotUVPoint(g3UVL *uvrot, float *rotmat, roomUVL *uv, g3UVL *uvcenter) {
  uvrot->u = ((uv->u - uvcenter->u) * rotmat[0]) + ((uv->v - uvcenter->v) * rotmat[1]) + uvcenter->u;
  uvrot->v = ((uv->u - uvcenter->u) * rotmat[2]) + ((uv->v - uvcenter->v) * rotmat[3]) + uvcenter->v;
}

void HTextureRotUVPointsOnRoomFace(room *roomp, int facenum, float *rotmat, g3UVL *uvcenter) {
  int v;
  face *facep = &roomp->faces[facenum];
  g3UVL tuv;

  for (v = 0; v < facep->num_verts; v++) {
    HTextureRotUVPoint(&tuv, rotmat, &facep->face_uvls[v], uvcenter);
    facep->face_uvls[v].u = tuv.u;
    facep->face_uvls[v].v = tuv.v;
  }
}

void HTextureCreate2DRotMat(float *rotmat, angle ang) {
  float sinang, cosang;

  sinang = FixSin(ang);
  cosang = FixCos(ang);

  rotmat[0] = cosang;
  rotmat[1] = sinang;
  rotmat[2] = -sinang;
  rotmat[3] = cosang;
}

void HTextureRotUVTerrainPoint(g3UVL *uvrot, float *rotmat, g3UVL *uv, g3UVL *uvcenter) {
  uvrot->u = ((uv->u - uvcenter->u) * rotmat[0]) + ((uv->v - uvcenter->v) * rotmat[1]) + uvcenter->u;
  uvrot->v = ((uv->u - uvcenter->u) * rotmat[2]) + ((uv->v - uvcenter->v) * rotmat[3]) + uvcenter->v;
}

// returns the index in the face_verts array for the leftmost point on this face
int HTextureGetLeftVertexForFace(int roomnum, int facenum) {
  matrix face_matrix, trans_matrix;
  vector fvec;
  vector avg_vert;
  vector verts[MAX_VERTS_PER_FACE];

  ASSERT(Rooms[roomnum].used);
  ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);

  // find the center point of this face
  vm_MakeZero(&avg_vert);
  for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
    avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];

  avg_vert /= i;

  // Make the orientation matrix
  fvec = Rooms[roomnum].faces[facenum].normal;

  vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
  // Make the transformation matrix

  angvec avec;
  vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
  vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);

  // Rotate all the points
  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
    vector rot_vert;

    vert -= avg_vert;
    vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);

    verts[i] = rot_vert;
  }

  // Find left most point
  int leftmost_point = -1;
  float leftmost_x = 900000.00f; // a big number

  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    if (verts[i].x < leftmost_x) {
      leftmost_point = i;
      leftmost_x = verts[i].x;
    }
  }

  ASSERT(leftmost_point != -1);
  return leftmost_point;
}

// returns the index in the face_verts array for the topmost point on this face
int HTextureGetTopVertexForFace(int roomnum, int facenum) {
  matrix face_matrix, trans_matrix;
  vector fvec;
  vector avg_vert;
  vector verts[MAX_VERTS_PER_FACE];

  ASSERT(Rooms[roomnum].used);
  ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);

  // find the center point of this face
  vm_MakeZero(&avg_vert);
  for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
    avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];

  avg_vert /= i;

  // Make the orientation matrix
  fvec = Rooms[roomnum].faces[facenum].normal;

  vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
  // Make the transformation matrix

  angvec avec;
  vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
  vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);

  // Rotate all the points
  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
    vector rot_vert;

    vert -= avg_vert;
    vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);

    verts[i] = rot_vert;
  }

  // Find top most point
  int topmost_point = -1;
  float topmost_y = -900000.0f; // a big number

  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    if (verts[i].y > topmost_y) {
      topmost_point = i;
      topmost_y = verts[i].y;
    }
  }

  ASSERT(topmost_point != -1);

  return topmost_point;
}

// returns the index in the face_verts array for the rightmost point on this face
int HTextureGetRightVertexForFace(int roomnum, int facenum) {
  matrix face_matrix, trans_matrix;
  vector fvec;
  vector avg_vert;
  vector verts[MAX_VERTS_PER_FACE];

  ASSERT(Rooms[roomnum].used);
  ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);

  // find the center point of this face
  vm_MakeZero(&avg_vert);
  for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
    avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];

  avg_vert /= i;

  // Make the orientation matrix
  fvec = Rooms[roomnum].faces[facenum].normal;

  vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
  // Make the transformation matrix

  angvec avec;
  vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
  vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);

  // Rotate all the points
  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
    vector rot_vert;

    vert -= avg_vert;
    vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);

    verts[i] = rot_vert;
  }

  // Find right most point
  int rightmost_point = -1;
  float rightmost_x = -900000.00f; // a big number

  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    if (verts[i].x > rightmost_x) {
      rightmost_point = i;
      rightmost_x = verts[i].x;
    }
  }

  ASSERT(rightmost_point != -1);
  return rightmost_point;
}

// returns the index in the face_verts array for the most point on this face
int HTextureGetBottomVertexForFace(int roomnum, int facenum) {
  matrix face_matrix, trans_matrix;
  vector fvec;
  vector avg_vert;
  vector verts[MAX_VERTS_PER_FACE];

  ASSERT(Rooms[roomnum].used);
  ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);

  // find the center point of this face
  vm_MakeZero(&avg_vert);
  for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
    avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];

  avg_vert /= i;

  // Make the orientation matrix
  fvec = Rooms[roomnum].faces[facenum].normal;

  vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
  // Make the transformation matrix

  angvec avec;
  vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
  vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);

  // Rotate all the points
  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
    vector rot_vert;

    vert -= avg_vert;
    vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);

    verts[i] = rot_vert;
  }

  // Find bottom most point
  int bottommost_point = -1;
  float bottommost_y = 900000.0f; // a big number

  for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
    if (verts[i].y < bottommost_y) {
      bottommost_point = i;
      bottommost_y = verts[i].y;
    }
  }

  ASSERT(bottommost_point != -1);

  return bottommost_point;
}

/*void HTextureGetBoxUVLForFace (room *rp,facenum)
{
        int l,r,t,b;
        vector lpoint,rpoint,tpoint,bpoint;
        g3UVL luvl,ruvl,tuvl,buvl,xchange,ychange;

        b=HTextureGetBottomVertexForFace (roomnum,facenum);
        t=HTextureGetTopVertexForFace (roomnum,facenum);
        l=HTextureGetLeftVertexForFace (roomnum,facenum);
        r=HTextureGetRightVertexForFace (roomnum,facenum);

        lpoint=rp->verts[rp->faces[facenum].face_verts[l]];
        rpoint=rp->verts[rp->faces[facenum].face_verts[r]];
        tpoint=rp->verts[rp->faces[facenum].face_verts[t]];
        bpoint=rp->verts[rp->faces[facenum].face_verts[b]];

        luvl=rp->faces[facenum].face_uvls[l];
        ruvl=rp->faces[facenum].face_uvls[r];
        tuvl=rp->faces[facenum].face_uvls[t];
        buvl=rp->faces[facenum].face_uvls[b];

        xchange.u=ruvl.u-luvl.u;
        xchange.v=ruvl.v-luvl.v;

        ychange=buvl.u-tuvl.u;
        ychange=buvl.v-tuvl.v;

}*/