Debian Codegraph @

editor/HTexture.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/HTexture.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * HTexture implementation
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 37 12/27/99 8:26a Gwar
30 *
31 * 36 8/30/99 12:55p Gwar
32 * added a parameter to HTexturePropagateToFace to not change the texture
33 * of the target face
34 *
35 * 35 7/30/99 1:19p Gwar
36 * don't set World_changed in NEWEDITOR
37 *
38 * 34 7/24/99 5:25a Gwar
39 * NEWEDITOR changes to HTextureApplyToRoomFace
40 *
41 * 33 7/04/99 4:56p Gwar
42 * changes for texture management in NEWEDITOR
43 *
44 * 32 5/19/99 12:19a Gwar
45 * #define Curroomp, Curface, etc as members of prim struct in NEWEDITOR
46 * -- this allows the main room frame and level window to share these
47 * variables and makes them non-global (i.e. no more sync problem between
48 * world and room views)
49 *
50 * 31 5/03/99 2:06a Gwar
51 * added HTexture.cpp to project
52 *
53 * 30 1/20/99 10:50a Jason
54 * added new terrain
55 *
56 * 29 10/21/98 1:27p Jason
57 * fixed texture u2,v2 sliding problems
58 *
59 * 28 8/16/98 4:24p Matt
60 * When copying UVs from one face to another, only copy u & v, and not
61 * other fields in the uv structure.
62 *
63 * 27 8/15/98 5:04p Matt
64 * Added code to copy a texture & UVs from one face to another
65 *
66 * 26 6/15/98 4:00p Jason
67 * replaced monochromatic polymodel lighting with rgb lighting
68 *
69 * 25 4/16/98 10:42a Matt
70 * Made texture propagation work across rooms.
71 *
72 * 24 4/02/98 12:23p Jason
73 * trimmed some fat from our structures
74 *
75 * 23 9/23/97 12:20p Jason
76 * fixed bug where rotating a texture would cause alphas and u2,v2s to
77 * become corrupt
78 *
79 * 22 9/17/97 1:36p Samir
80 * Fixed some new prototypes.
81 *
82 * 21 9/17/97 11:57a Matt
83 * Ripped out segment code
84 *
85 * 19 8/21/97 6:01p Matt
86 * Use groovy new FindSharedEdge() function
87 *
88 * 18 8/13/97 9:52a Matt
89 * Combined slide left/right/up/down into one function, and the same with
90 * rotate left/right
91 *
92 * 17 8/07/97 5:06p Jason
93 * added new tiling system for terrain
94 *
95 * 16 8/07/97 3:19p Jason
96 * replaced old terrain tmap system with new one
97 *
98 * 15 8/01/97 4:53p Jason
99 * made a more general PropagateTextureToFace function call
100 *
101 * 14 7/22/97 7:08p Matt
102 * Cleaned up D3EditState, moving some vars in and some out, and renaming
103 * and changing a few others
104 *
105 * 13 7/18/97 7:38p Jason
106 * finished room/terrain texture modifications
107 *
108 * 12 7/16/97 6:31p Jason
109 * changed defaut texture uvs for terrain
110 *
111 * 15 6/24/97 12:41p Jason
112 * checked in for safety
113 *
114 * 14 6/18/97 11:14a Mark
115 * FROM JASON:Fixed dumb terrain rotational bug
116 *
117 * 13 6/17/97 4:16p Jason
118 * added some terrain features
119 *
120 * 12 6/17/97 11:35a Jason
121 * checked in for safety
122 *
123 * 11 6/06/97 4:57p Jason
124 * more nifty megacell/tmap2 features
125 *
126 * 10 6/04/97 1:32p Matt
127 * Changed Side_to_verts[] and Side_opposite[] to be arrays of ints, which
128 * should be faster than bytes.
129 *
130 * 9 5/30/97 3:32p Samir
131 * When rotating textures, lighting preserved
132 *
133 * 8 5/02/97 7:21p Matt
134 * Added code to rotate tmap2's
135 *
136 * 7 4/17/97 4:03p Samir
137 * Apply Tmap 1 and Tmap2 implemented, but no Tmap2 rendering is occurring
138 * yet.
139 *
140 * 6 3/31/97 5:58p Matt
141 * Revamped mine update flags
142 *
143 * 5 3/31/97 3:50p Samir
144 * Set default texture uvs should work for side not whole segment.
145 *
146 * 4 2/26/97 6:40p Samir
147 * changed g3s_uvl to g3UVL
148 *
149 * 3 2/26/97 3:59p Samir
150 * Most scaling, rotating, sliding and flipping work.
151 *
152 * 2 2/20/97 11:59a Samir
153 * Fixed bugs
154 *
155 * 1 2/20/97 11:50a Samir
156 *
157 * $NoKeywords: $
158 */
159
160 #include "HTexture.h"
161
162 #ifndef NEWEDITOR
163 #include "d3edit.h"
164 #endif
165
166 #include "roomuvs.h"
167 #include "gametexture.h"
168 #include "terrain.h"
169 #include "room.h"
170 #include "descent.h"
171 #include "erooms.h"
172
173 #define HTEX_CALIB_VAL ((float)(1.0 / 128.0))
174
175 // ---------------------------------------------------------------------------
176 // internal function prototypes
177 // ---------------------------------------------------------------------------
178
179 void HTextureRoomStretch(room *rp, int facenum, int edge, int direction);
180 void HTextureRotUVPoint(g3UVL *uvrot, float *rotmat, roomUVL *uv, g3UVL *uvcenter);
181 void HTextureCreate2DRotMat(float *rotmat, angle ang);
182 void HTextureRotUVTerrainPoint(g3UVL *uvrot, float *rotmat, g3UVL *uv, g3UVL *uvcenter);
183 void GetUVLForRoomPoint(int roomnum, int facenum, int vertnum, roomUVL *uvl);
184 int HTextureGetTopVertexForFace(int roomnum, int facenum);
185 int HTextureGetBottomVertexForFace(int roomnum, int facenum);
186 int HTextureGetLeftVertexForFace(int roomnum, int facenum);
187 int HTextureGetRightVertexForFace(int roomnum, int facenum);
188 void HTextureRotUVPointsOnRoomFace(room *roomp, int facenum, float *rotmat, g3UVL *uvcenter);
189 void HTextureComputeUVRoomFaceCenter(g3UVL *uvcenter, room *roomp, int facenum);
190 void HTextureTerrainStretch(int edge, int direction);
191
192 // ---------------------------------------------------------------------------
193 // Functions to manipulate textures within the mine, through segments
194 // ---------------------------------------------------------------------------
195
196 // Apply the specified texture to the specified room:face
197 void HTextureApplyToRoomFace(room *rp, int facenum, int tnum) {
198 ASSERT(rp->used);
199
200 #ifdef NEWEDITOR
201 if (tnum == -1)
202 return;
203 if (rp->faces[facenum].tmap >= 0)
204 SwitchTexture(rp, rp->faces[facenum].tmap, tnum);
205 #endif
206
207 rp->faces[facenum].tmap = tnum;
208 }
209
210 // Copy texture from current face to adjacent face, tiling the UV coordinates
211 // Parameters: destrp,destface - room:face that the propagate is based on
212 // srcrp,srcface - room:face that is changed
213 // Return: 1 if success, 0 if faces not adjacent
214 int HTexturePropagateToFace(room *destrp, int destface, room *srcrp, int srcface, bool tex) {
215 face *dfp = &destrp->faces[destface];
216 face *sfp = &srcrp->faces[srcface];
217 int v0, v1;
218
219 // Get shared edge
220 if (destrp != srcrp) { // Faces in different rooms
221 if (!FindSharedEdgeAcrossRooms(destrp, destface, srcrp, srcface, &v0, &v1))
222 return 0;
223 } else { // Faces in same room
224 if (!FindSharedEdge(dfp, sfp, &v0, &v1))
225 return 0;
226 }
227
228 if (tex) {
229 #ifdef NEWEDITOR
230 SwitchTexture(destrp, dfp->tmap, sfp->tmap);
231 #endif
232
233 // Copy texture from Curface
234 dfp->tmap = sfp->tmap;
235 }
236
237 // Assign uvs from shared edge to rest of verts in face
238 AssignUVsToFace(destrp, destface, &sfp->face_uvls[(v1 + 1) % sfp->num_verts], &sfp->face_uvls[v1], v0,
239 (v0 + 1) % dfp->num_verts);
240
241 // Success
242 return 1;
243 }
244
245 // Copy texture UVs from one face to another
246 // Parameters: destrp,destface - room:face that is changed
247 // srcrp,srcface - room:face to copy from
248 // offset - vert 0 on source is assigned to vert offset on dest
249 // Return: 1 if success, 0 if faces don't have the same number of verts
250 int HTextureCopyUVsToFace(room *destrp, int destface, room *srcrp, int srcface, int offset) {
251 face *dfp = &destrp->faces[destface];
252 face *sfp = &srcrp->faces[srcface];
253
254 // Make sure faces have the same number of vertices
255 if (dfp->num_verts != sfp->num_verts)
256 return 0;
257
258 // Copy UV values
259 for (int i = 0; i < dfp->num_verts; i++) {
260 dfp->face_uvls[(i + offset) % dfp->num_verts].u = sfp->face_uvls[i].u;
261 dfp->face_uvls[(i + offset) % dfp->num_verts].v = sfp->face_uvls[i].v;
262 }
263
264 // Success!
265 #ifndef NEWEDITOR
266 World_changed = 1;
267 #endif
268 return 1;
269 }
270
271 // texture flipping
272
273 void HTextureFlipX() {
274 int i;
275
276 if (Editor_view_mode == VM_TERRAIN) {
277 // terrain_segment oldseg;
278
279 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
280 if (TerrainSelected[i]) {
281 /*oldseg=Terrain_seg[i];
282 Terrain_seg[i].u[0]=oldseg.u[1];
283 Terrain_seg[i].u[1]=oldseg.u[0];
284 Terrain_seg[i].u[2]=oldseg.u[3];
285 Terrain_seg[i].u[3]=oldseg.u[2];
286
287 Terrain_seg[i].v[0]=oldseg.v[1];
288 Terrain_seg[i].v[1]=oldseg.v[0];
289 Terrain_seg[i].v[2]=oldseg.v[3];
290 Terrain_seg[i].v[3]=oldseg.v[2];*/
291 }
292 }
293 } else {
294 ASSERT(Curroomp->used);
295
296 for (int i = 0; i < Curroomp->faces[Curface].num_verts; i++)
297 Curroomp->faces[Curface].face_uvls[i].u = 1 - Curroomp->faces[Curface].face_uvls[i].u;
298 }
299
300 #ifndef NEWEDITOR
301 World_changed = 1;
302 #endif
303 }
304
305 void HTextureFlipY() {
306 int i;
307
308 if (Editor_view_mode == VM_TERRAIN) {
309 terrain_segment oldseg;
310
311 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
312 if (TerrainSelected[i]) {
313 oldseg = Terrain_seg[i];
314 /*Terrain_seg[i].v[0]=oldseg.v[3];
315 Terrain_seg[i].v[1]=oldseg.v[2];
316 Terrain_seg[i].v[2]=oldseg.v[1];
317 Terrain_seg[i].v[3]=oldseg.v[0];
318
319 Terrain_seg[i].u[0]=oldseg.u[3];
320 Terrain_seg[i].u[1]=oldseg.u[2];
321 Terrain_seg[i].u[2]=oldseg.u[1];
322 Terrain_seg[i].u[3]=oldseg.u[0];*/
323 }
324 }
325 } else {
326 ASSERT(Curroomp->used);
327
328 for (int i = 0; i < Curroomp->faces[Curface].num_verts; i++)
329 Curroomp->faces[Curface].face_uvls[i].v = 1 - Curroomp->faces[Curface].face_uvls[i].v;
330 }
331
332 #ifndef NEWEDITOR
333 World_changed = 1;
334 #endif
335 }
336
337 void HTextureSlide(room *rp, int facenum, float right, float up) {
338 ASSERT(rp != NULL);
339 ASSERT(rp->used);
340
341 if (Editor_view_mode == VM_TERRAIN) {
342 /*int i;
343 for (i=0;i<TERRAIN_DEPTH*TERRAIN_WIDTH;i++)
344 {
345 if (TerrainSelected[i])
346 {
347 for (int t=0;t<4;t++)
348 {
349 float val=Terrain_UV_lookup[Terrain_seg[i].u[t]];
350
351 val+=(1.0/32.0);
352
353 Terrain_seg[i].u[t]=TerrainLookupUV(val);
354 }
355
356 }
357
358 }
359 */
360 return;
361
362 } else { // mine
363 for (int i = 0; i < rp->faces[facenum].num_verts; i++) {
364 rp->faces[facenum].face_uvls[i].u -= right / 128.0f;
365 rp->faces[facenum].face_uvls[i].v += up / 128.0f;
366 }
367 }
368
369 #ifndef NEWEDITOR
370 World_changed = 1;
371 #endif
372 }
373
374 void HTextureRotate(room *roomp, int facenum, angle ang) {
375 g3UVL uvcenter;
376 float rotmat[4];
377
378 HTextureCreate2DRotMat(rotmat, ang);
379
380 if (Editor_view_mode == VM_TERRAIN && ang == 0x4000) {
381 for (int i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
382 if (TerrainSelected[i]) {
383 /*int f=(Terrain_seg[i].rotation_factor&0x0F);
384 f++;
385 f%=4;
386
387 Terrain_seg[i].rotation_factor&=0xF0;
388 Terrain_seg[i].rotation_factor|=(f);*/
389 }
390 }
391
392 return;
393 } else {
394 HTextureComputeUVRoomFaceCenter(&uvcenter, roomp, facenum);
395 HTextureRotUVPointsOnRoomFace(roomp, facenum, rotmat, &uvcenter);
396 }
397
398 #ifndef NEWEDITOR
399 World_changed = 1;
400 #endif
401 }
402
403 void HTextureStretchMore(room *rp, int face, int edge) {
404 if (Editor_view_mode == VM_TERRAIN) {
405 HTextureTerrainStretch(edge, 1 * D3EditState.texscale);
406 } else {
407 HTextureRoomStretch(rp, face, edge, 1 * D3EditState.texscale);
408 }
409 }
410
411 void HTextureStretchLess(room *rp, int face, int edge) {
412 if (Editor_view_mode == VM_TERRAIN) {
413 HTextureTerrainStretch(edge, (-1 * D3EditState.texscale));
414 } else {
415 HTextureRoomStretch(rp, face, edge, (-1 * D3EditState.texscale));
416 }
417 }
418
419 void HTextureSetDefault(room *rp, int face) {
420 if (Editor_view_mode == VM_TERRAIN) {
421 for (int i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
422 if (TerrainSelected[i]) {
423 int index = Terrain_seg[i].texseg_index;
424 Terrain_tex_seg[index].rotation = 0;
425 }
426 }
427 World_changed = 1;
428 } else if (Editor_view_mode == VM_ROOM) {
429 //@@Stretch_scale_x = (float)1.0;
430 //@@Stretch_scale_y = (float)1.0;
431
432 for (int i = 0; i < rp->faces[face].num_verts; i++) {
433 float saveu2 = rp->faces[face].face_uvls[i].u2;
434 float savev2 = rp->faces[face].face_uvls[i].v2;
435
436 GetUVLForRoomPoint(ROOMNUM(rp), face, i, &rp->faces[face].face_uvls[i]);
437
438 rp->faces[face].face_uvls[i].u2 = saveu2;
439 rp->faces[face].face_uvls[i].v2 = savev2;
440 }
441 } else {
442 //@@Stretch_scale_x = (float)1.0;
443 //@@Stretch_scale_y = (float)1.0;
444
445 for (int i = 0; i < rp->faces[face].num_verts; i++) {
446 float saveu2 = rp->faces[face].face_uvls[i].u2;
447 float savev2 = rp->faces[face].face_uvls[i].v2;
448
449 GetUVLForRoomPoint(ROOMNUM(rp), face, i, &rp->faces[face].face_uvls[i]);
450
451 rp->faces[face].face_uvls[i].u2 = saveu2;
452 rp->faces[face].face_uvls[i].v2 = savev2;
453 }
454 }
455
456 #ifndef NEWEDITOR
457 World_changed = 1;
458 #endif
459 }
460
461 void HTextureNextEdge() {
462 Curedge++;
463
464 if (Editor_view_mode == VM_TERRAIN) {
465 if (Curedge >= 4)
466 Curedge = 0;
467 } else // mine
468 {
469 if (Curedge >= Curroomp->faces[Curface].num_verts)
470 Curedge = 0;
471 }
472
473 State_changed = 1;
474 }
475
476 // ---------------------------------------------------------------------------
477 // functions used by HTexture operations
478 // ---------------------------------------------------------------------------
479
480 // direction = -1 or 1 depending on direction
481 // Stretches all vertices towards or away from a given edge
482 // Depending on the geometry of the face, this can cause distortions
483 void HTextureRoomStretch(room *rp, int facenum, int edge, int direction) {
484 int next_edge = (edge + 1) % rp->faces[facenum].num_verts;
485
486 // find deltas
487 float du = rp->faces[facenum].face_uvls[next_edge].u - rp->faces[facenum].face_uvls[edge].u;
488 float dv = rp->faces[facenum].face_uvls[next_edge].v - rp->faces[facenum].face_uvls[edge].v;
489
490 // rotate delta's 90 degrees
491 float nu = -dv;
492 float nv = du;
493
494 // Find a magnitude to shift by
495 float mag = sqrt((nv * nv) + (nu * nu));
496 nv /= mag;
497 nu /= mag;
498
499 nv /= 64.0;
500 nu /= 64.0;
501
502 // Apply to all verts except for our edge
503 for (int i = 0; i < rp->faces[facenum].num_verts; i++) {
504 if (i == edge || i == next_edge)
505 continue;
506
507 rp->faces[facenum].face_uvls[i].v -= (nv * direction);
508 rp->faces[facenum].face_uvls[i].u -= (nu * direction);
509 }
510
511 #ifndef NEWEDITOR
512 World_changed = 1;
513 #endif
514 }
515
516 void HTextureTerrainStretch(int edge, int direction) {
517 int i;
518
519 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
520 if (TerrainSelected[i]) {
521 }
522 }
523
524 #ifndef NEWEDITOR
525 World_changed = 1;
526 #endif
527 }
528
529 void HTextureComputeUVRoomFaceCenter(g3UVL *uvcenter, room *roomp, int facenum) {
530 int i;
531 face *facep = &roomp->faces[facenum];
532
533 uvcenter->u = 0;
534 uvcenter->v = 0;
535
536 for (i = 0; i < facep->num_verts; i++) {
537 uvcenter->u += facep->face_uvls[i].u;
538 uvcenter->v += facep->face_uvls[i].v;
539 }
540
541 uvcenter->u /= i;
542 uvcenter->v /= i;
543 }
544
545 void HTextureRotUVPoint(g3UVL *uvrot, float *rotmat, roomUVL *uv, g3UVL *uvcenter) {
546 uvrot->u = ((uv->u - uvcenter->u) * rotmat[0]) + ((uv->v - uvcenter->v) * rotmat[1]) + uvcenter->u;
547 uvrot->v = ((uv->u - uvcenter->u) * rotmat[2]) + ((uv->v - uvcenter->v) * rotmat[3]) + uvcenter->v;
548 }
549
550 void HTextureRotUVPointsOnRoomFace(room *roomp, int facenum, float *rotmat, g3UVL *uvcenter) {
551 int v;
552 face *facep = &roomp->faces[facenum];
553 g3UVL tuv;
554
555 for (v = 0; v < facep->num_verts; v++) {
556 HTextureRotUVPoint(&tuv, rotmat, &facep->face_uvls[v], uvcenter);
557 facep->face_uvls[v].u = tuv.u;
558 facep->face_uvls[v].v = tuv.v;
559 }
560 }
561
562 void HTextureCreate2DRotMat(float *rotmat, angle ang) {
563 float sinang, cosang;
564
565 sinang = FixSin(ang);
566 cosang = FixCos(ang);
567
568 rotmat[0] = cosang;
569 rotmat[1] = sinang;
570 rotmat[2] = -sinang;
571 rotmat[3] = cosang;
572 }
573
574 void HTextureRotUVTerrainPoint(g3UVL *uvrot, float *rotmat, g3UVL *uv, g3UVL *uvcenter) {
575 uvrot->u = ((uv->u - uvcenter->u) * rotmat[0]) + ((uv->v - uvcenter->v) * rotmat[1]) + uvcenter->u;
576 uvrot->v = ((uv->u - uvcenter->u) * rotmat[2]) + ((uv->v - uvcenter->v) * rotmat[3]) + uvcenter->v;
577 }
578
579 // returns the index in the face_verts array for the leftmost point on this face
580 int HTextureGetLeftVertexForFace(int roomnum, int facenum) {
581 matrix face_matrix, trans_matrix;
582 vector fvec;
583 vector avg_vert;
584 vector verts[MAX_VERTS_PER_FACE];
585
586 ASSERT(Rooms[roomnum].used);
587 ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);
588
589 // find the center point of this face
590 vm_MakeZero(&avg_vert);
591 for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
592 avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
593
594 avg_vert /= i;
595
596 // Make the orientation matrix
597 fvec = Rooms[roomnum].faces[facenum].normal;
598
599 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
600 // Make the transformation matrix
601
602 angvec avec;
603 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
604 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
605
606 // Rotate all the points
607 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
608 vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
609 vector rot_vert;
610
611 vert -= avg_vert;
612 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
613
614 verts[i] = rot_vert;
615 }
616
617 // Find left most point
618 int leftmost_point = -1;
619 float leftmost_x = 900000.00f; // a big number
620
621 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
622 if (verts[i].x < leftmost_x) {
623 leftmost_point = i;
624 leftmost_x = verts[i].x;
625 }
626 }
627
628 ASSERT(leftmost_point != -1);
629 return leftmost_point;
630 }
631
632 // returns the index in the face_verts array for the topmost point on this face
633 int HTextureGetTopVertexForFace(int roomnum, int facenum) {
634 matrix face_matrix, trans_matrix;
635 vector fvec;
636 vector avg_vert;
637 vector verts[MAX_VERTS_PER_FACE];
638
639 ASSERT(Rooms[roomnum].used);
640 ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);
641
642 // find the center point of this face
643 vm_MakeZero(&avg_vert);
644 for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
645 avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
646
647 avg_vert /= i;
648
649 // Make the orientation matrix
650 fvec = Rooms[roomnum].faces[facenum].normal;
651
652 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
653 // Make the transformation matrix
654
655 angvec avec;
656 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
657 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
658
659 // Rotate all the points
660 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
661 vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
662 vector rot_vert;
663
664 vert -= avg_vert;
665 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
666
667 verts[i] = rot_vert;
668 }
669
670 // Find top most point
671 int topmost_point = -1;
672 float topmost_y = -900000.0f; // a big number
673
674 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
675 if (verts[i].y > topmost_y) {
676 topmost_point = i;
677 topmost_y = verts[i].y;
678 }
679 }
680
681 ASSERT(topmost_point != -1);
682
683 return topmost_point;
684 }
685
686 // returns the index in the face_verts array for the rightmost point on this face
687 int HTextureGetRightVertexForFace(int roomnum, int facenum) {
688 matrix face_matrix, trans_matrix;
689 vector fvec;
690 vector avg_vert;
691 vector verts[MAX_VERTS_PER_FACE];
692
693 ASSERT(Rooms[roomnum].used);
694 ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);
695
696 // find the center point of this face
697 vm_MakeZero(&avg_vert);
698 for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
699 avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
700
701 avg_vert /= i;
702
703 // Make the orientation matrix
704 fvec = Rooms[roomnum].faces[facenum].normal;
705
706 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
707 // Make the transformation matrix
708
709 angvec avec;
710 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
711 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
712
713 // Rotate all the points
714 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
715 vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
716 vector rot_vert;
717
718 vert -= avg_vert;
719 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
720
721 verts[i] = rot_vert;
722 }
723
724 // Find right most point
725 int rightmost_point = -1;
726 float rightmost_x = -900000.00f; // a big number
727
728 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
729 if (verts[i].x > rightmost_x) {
730 rightmost_point = i;
731 rightmost_x = verts[i].x;
732 }
733 }
734
735 ASSERT(rightmost_point != -1);
736 return rightmost_point;
737 }
738
739 // returns the index in the face_verts array for the most point on this face
740 int HTextureGetBottomVertexForFace(int roomnum, int facenum) {
741 matrix face_matrix, trans_matrix;
742 vector fvec;
743 vector avg_vert;
744 vector verts[MAX_VERTS_PER_FACE];
745
746 ASSERT(Rooms[roomnum].used);
747 ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);
748
749 // find the center point of this face
750 vm_MakeZero(&avg_vert);
751 for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
752 avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
753
754 avg_vert /= i;
755
756 // Make the orientation matrix
757 fvec = Rooms[roomnum].faces[facenum].normal;
758
759 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
760 // Make the transformation matrix
761
762 angvec avec;
763 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
764 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
765
766 // Rotate all the points
767 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
768 vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
769 vector rot_vert;
770
771 vert -= avg_vert;
772 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
773
774 verts[i] = rot_vert;
775 }
776
777 // Find bottom most point
778 int bottommost_point = -1;
779 float bottommost_y = 900000.0f; // a big number
780
781 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
782 if (verts[i].y < bottommost_y) {
783 bottommost_point = i;
784 bottommost_y = verts[i].y;
785 }
786 }
787
788 ASSERT(bottommost_point != -1);
789
790 return bottommost_point;
791 }
792
793 /*void HTextureGetBoxUVLForFace (room *rp,facenum)
794 {
795 int l,r,t,b;
796 vector lpoint,rpoint,tpoint,bpoint;
797 g3UVL luvl,ruvl,tuvl,buvl,xchange,ychange;
798
799 b=HTextureGetBottomVertexForFace (roomnum,facenum);
800 t=HTextureGetTopVertexForFace (roomnum,facenum);
801 l=HTextureGetLeftVertexForFace (roomnum,facenum);
802 r=HTextureGetRightVertexForFace (roomnum,facenum);
803
804 lpoint=rp->verts[rp->faces[facenum].face_verts[l]];
805 rpoint=rp->verts[rp->faces[facenum].face_verts[r]];
806 tpoint=rp->verts[rp->faces[facenum].face_verts[t]];
807 bpoint=rp->verts[rp->faces[facenum].face_verts[b]];
808
809 luvl=rp->faces[facenum].face_uvls[l];
810 ruvl=rp->faces[facenum].face_uvls[r];
811 tuvl=rp->faces[facenum].face_uvls[t];
812 buvl=rp->faces[facenum].face_uvls[b];
813
814 xchange.u=ruvl.u-luvl.u;
815 xchange.v=ruvl.v-luvl.v;
816
817 ychange=buvl.u-tuvl.u;
818 ychange=buvl.v-tuvl.v;
819
820 }*/