Debian Codegraph @

editor/Read3ds.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/Read3ds.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Code to read .p3d files generated in 3DS Max
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 26 10/15/99 12:26p Matt
30 * Added error checking for too many verts & faces when importing a room.
31 * Also, now allow more than the max number of faces, as long as there is
32 * a valid number after combining faces.
33 *
34 * 25 4/13/99 11:23a Jason
35 * check for degenerate faces on import
36 *
37 * 24 2/25/99 10:35a Jason
38 * added removal of redundant verts
39 *
40 * 23 2/04/99 4:40p Matt
41 * Added some error checking
42 *
43 * 22 1/21/99 11:15p Jeff
44 * pulled out some structs and defines from header files and moved them
45 * into seperate header files so that multiplayer dlls don't require major
46 * game headers, just those new headers. Side effect is a shorter build
47 * time. Also cleaned up some header file #includes that weren't needed.
48 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
49 * manage.h and multi.h
50 *
51 * 21 12/22/98 2:03p Matt
52 * Added room names, and made rooms not compress so that room numbers are
53 * suitable for persistant uses.
54 *
55 * 20 5/01/98 5:55p Jason
56 * made combine faces much stricter
57 *
58 * 19 4/02/98 12:23p Jason
59 * trimmed some fat from our structures
60 *
61 * 18 3/31/98 3:49p Jason
62 * added memory lib
63 *
64 * 17 2/02/98 5:14p Matt
65 * Check for bad normals when importing room
66 *
67 * 16 1/22/98 2:56p Brent
68 * Define default textures to unassigned faces
69 *
70 * 15 1/19/98 2:55p Jason
71 * added the ability to have the importer keep the textures on the faces
72 * if they are already present in memory
73 *
74 * 14 12/23/97 11:06a Samir
75 * Added pserror.h
76 *
77 * 13 12/10/97 5:20p Jason
78 * set alphas to 1 when importing rooms
79 *
80 * 12 12/10/97 4:26p Jason
81 * don't assign default UVs to room
82 *
83 * 11 8/21/97 5:57p Matt
84 * Use new & modified functions from erooms.cpp
85 *
86 * 10 8/01/97 3:16p Chris
87 *
88 * 9 7/21/97 12:11p Matt
89 * Fixed stupid bug in concavity check
90 *
91 * 8 7/21/97 11:39a Jason
92 * checked in for matt to debug
93 *
94 * 7 7/17/97 11:13a Jason
95 * fixed bug with reading in rooms - somehow it got broken
96 *
97 * 7 7/17/97 10:51a Jason
98 * fixed bug with normals
99 *
100 * $NoKeywords: $
101 */
102
103 #include "read3ds.h"
104 #include "cfile.h"
105 #include "room.h"
106 #include "erooms.h"
107 #include "gametexture.h"
108 #include "ddio.h"
109 #include "pserror.h"
110 #include <string.h>
111 #include <stdlib.h>
112 #include "mem.h"
113 #include "vecmat.h"
114
115 // 3ds MAX id's
116 #define ID_3DS_MODIFIED 0xbeef
117 #define ID_OBJ_PROPS 0xdead
118 #define ID_SPLINE_PATH 0x8001
119 #define ID_3DSM 0xcfd0
120 #define ID_3DS 0x4d4d
121 #define ID_OBJECT 0x4000
122 #define ID_UNKNOWN 0x3d3d
123 #define ID_TRI_MESH 0x4100
124 #define ID_VERTLIST 0x4110
125 #define ID_FACELIST 0x4120
126 #define ID_MAT_APP 0x4130
127 #define ID_VERTEX_MAPPING 0x4140
128 #define ID_SMOOTH 0x4150
129 #define ID_MATRIX 0x4160
130 #define ID_MATERIAL 0xafff
131 #define ID_MAT_NAME 0xa000
132 #define ID_MAT_DSIDED 0xa081
133 #define ID_MAT_DIFFUSE_COLOR 0xa020
134 #define ID_MAT_TEXTURE 0xa200
135 #define ID_DIRECT_LIGHT 0x4600
136 #define OBJECT_NODE_TAG 0xB002
137
138 char Reading_properties[255];
139
140 #define MAX_MATERIALS 100
141
142 struct material {
143 char name[PAGENAME_LEN];
144 int texhandle;
145 };
146
147 int Num_materials = 0;
148 material Materials[MAX_MATERIALS];
149
150 struct reading_face {
151 uint8_t flags; // flags for this face (see above)
152 int16_t portal_num; // which portal this face is part of, or -1 if none
153 uint8_t num_verts; // how many vertices in this face
154 int16_t face_verts[MAX_VERTS_PER_FACE]; // index into list of vertices for this face
155 roomUVL face_uvls[MAX_VERTS_PER_FACE]; // index into list of uvls for this face
156 vector normal; // the surface normal of this face
157 int16_t tmap; // texture numbers for this face
158 };
159
160 struct reading_room {
161 char name[PAGENAME_LEN];
162 int flags; // various room flags
163 int num_faces; // how many poygons in this room
164 int num_portals; // how many connections in this room
165 int num_verts; // how many verts in the room
166 reading_face *faces; // pointer to list of faces
167 vector *verts; // array of vertices for this room
168 int objects; // index of first object in this room
169 float static_light; // the amount of light in this room
170 };
171
172 reading_room Reading_room; // the global that we use to keep a temp copy of the room while
173 // we're reading it in
174
175 int CombineFaces(reading_face *, reading_face *, reading_face *);
176
177 // Our nest level
178 int Nest_level = 0;
179
180 extern void AssignDefaultUVsToRoom(room *rp);
181
182 int DeleteUnusedRoomVerts(room *);
183 int RemoveDuplicatePoints(room *);
184 int RemoveDuplicateFacePoints(room *);
185
186 #define MAX_READING_ROOM_FACES (MAX_FACES_PER_ROOM * 2)
187
188 // Opens and reads a 3dsmax file for our rooms. Allocs a room to carry the data
189 // Returns the index into the Rooms[] array if successful
190 // Return -1 on fail
191 int Read3DSMaxFile(char *filename) {
192 uint16_t id;
193 int len;
194 CFILE *fp;
195 int i;
196
197 fp = (CFILE *)cfopen(filename, "rb");
198 if (!fp) {
199 mprintf(0, "Couldn't open 3dsmax file %s!\n", filename);
200 return -1;
201 }
202
203 Nest_level = 0;
204
205 // Alloc space for reading stuff in
206 Reading_room.faces = (reading_face *)mem_malloc(MAX_READING_ROOM_FACES * sizeof(reading_face));
207 Reading_room.verts = (vector *)mem_malloc(MAX_VERTS_PER_ROOM * sizeof(vector));
208
209 Reading_room.num_faces = 0;
210 Reading_room.num_verts = 0;
211
212 id = cf_ReadShort(fp);
213 len = cf_ReadInt(fp);
214
215 Num_materials = 0;
216
217 if (id == ID_3DS_MODIFIED)
218 Parse3DSMaxChunk(fp, len - 6);
219 else {
220 mprintf(0, "This file is not a 3ds max file!\n");
221 cfclose(fp);
222 return -1;
223 }
224 cfclose(fp);
225
226 if ((Reading_room.num_verts == 0) || (Reading_room.num_faces == 0)) {
227 OutrageMessageBox("The imported room has %d verts and %d faces. Aborting import.", Reading_room.num_faces,
228 Reading_room.num_faces);
229 return -1;
230 }
231
232 if (Reading_room.num_verts > MAX_VERTS_PER_ROOM) {
233 OutrageMessageBox("The imported room has %d verts. The limit is %d. Aborting import.", Reading_room.num_verts,
234 MAX_VERTS_PER_ROOM);
235 return -1;
236 }
237
238 if (Reading_room.num_faces > MAX_READING_ROOM_FACES) {
239 OutrageMessageBox("The imported room has %d faces. The limit is %d. Aborting import.", Reading_room.num_faces,
240 MAX_READING_ROOM_FACES);
241 return -1;
242 }
243
244 if (Reading_room.num_faces > MAX_FACES_PER_ROOM) {
245 OutrageMessageBox("The imported room has %d faces. The limit after combining faces is %d.\n\nIf there are too "
246 "many faces after combining, this room will not be imported.",
247 Reading_room.num_faces, MAX_FACES_PER_ROOM);
248 }
249
250 // Convert our points to left handed space
251 for (i = 0; i < Reading_room.num_verts; i++)
252 ConvertHandiness(&Reading_room.verts[i]);
253
254 // calculate normals
255 int bad_normals = 0;
256 for (i = 0; i < Reading_room.num_faces; i++) {
257 reading_room *rp = &Reading_room;
258 reading_face *mfp = &rp->faces[i];
259 // vm_GetNormal(&Reading_room.faces[i].normal,&rp->verts[mfp->face_verts[0]],&rp->verts[mfp->face_verts[1]],&rp->verts[mfp->face_verts[2]]);
260 if (!ComputeNormal(&Reading_room.faces[i].normal, Reading_room.faces[i].num_verts, mfp->face_verts, rp->verts)) {
261 mprintf(1, "Warning: Low precision normal for face %d\n", i);
262 bad_normals++;
263 }
264 }
265 if (bad_normals) {
266 OutrageMessageBox("Warning: The loaded room has %d faces with bad normals -- see the mono screen for details.\n\n"
267 "Coplanar faces in this room have NOT been combined.\n\n"
268 "It is STRONGLY recommended that you fix this room before using it.",
269 bad_normals);
270 goto skip_combine;
271 }
272
273 // Now make a copy of the relevant Reading_room data into our destination
274 int t;
275 reading_face destface;
276
277 mprintf(0, "Combining faces, please wait...\n");
278
279 TryAgain:
280
281 for (i = 0; i < Reading_room.num_faces; i++) {
282 reading_face *a = &Reading_room.faces[i];
283
284 for (t = 0; t < Reading_room.num_faces; t++) {
285
286 reading_face *b = &Reading_room.faces[t];
287
288 if (a == b)
289 continue;
290
291 int retval = CombineFaces(&destface, a, b);
292 if (retval) {
293 // Copy the new face in the place of face a,
294 // remove face b and then start over!
295
296 Reading_room.faces[i] = destface;
297 int k;
298 for (k = t; k < Reading_room.num_faces - 1; k++)
299 Reading_room.faces[k] = Reading_room.faces[k + 1];
300
301 Reading_room.num_faces--;
302
303 goto TryAgain;
304 }
305 }
306 }
307
308 skip_combine:;
309
310 mprintf(0, "Total faces=%d\n", Reading_room.num_faces);
311
312 if (Reading_room.num_faces > MAX_FACES_PER_ROOM) {
313 OutrageMessageBox("The imported room has %d faces. The limit is %d. Aborting import.", Reading_room.num_faces,
314 MAX_FACES_PER_ROOM);
315 return -1;
316 }
317
318 mprintf(0, "Trying to allocate a room for %d verts, %d faces!\n", Reading_room.num_verts, Reading_room.num_faces);
319 // int n=AllocRoom (Reading_room.num_verts,Reading_room.num_faces);
320 room *rp = CreateNewRoom(Reading_room.num_verts, Reading_room.num_faces, 1);
321
322 if (rp != NULL) {
323 for (i = 0; i < Reading_room.num_verts; i++)
324 rp->verts[i] = Reading_room.verts[i];
325
326 for (i = 0; i < Reading_room.num_faces; i++) {
327 InitRoomFace(&rp->faces[i], Reading_room.faces[i].num_verts);
328
329 rp->faces[i].normal = Reading_room.faces[i].normal;
330 rp->faces[i].num_verts = Reading_room.faces[i].num_verts;
331
332 rp->faces[i].tmap = Reading_room.faces[i].tmap;
333
334 for (t = 0; t < Reading_room.faces[i].num_verts; t++) {
335 rp->faces[i].face_verts[t] = Reading_room.faces[i].face_verts[t];
336 rp->faces[i].face_uvls[t] = Reading_room.faces[i].face_uvls[t];
337 }
338 }
339
340 // Reset UV's
341
342 // AssignDefaultUVsToRoom (rp);
343
344 // Remove redundant verts
345 DeleteUnusedRoomVerts(rp);
346 RemoveDuplicatePoints(rp);
347 RemoveDuplicateFacePoints(rp);
348
349 char name[255];
350 char path[255];
351 char extension[255];
352 char roomname[255];
353
354 // Find a unique name for this room
355
356 ddio_SplitPath(filename, path, name, extension);
357 int done = 0;
358 int count = 1;
359
360 sprintf(roomname, "%s.ORF", name);
361
362 while (!done) {
363 int val = FindRoomName(roomname);
364 if (val == -1) {
365 done = 1;
366 continue;
367 }
368 count++;
369 sprintf(roomname, "%s%d.ORF", name, count);
370 }
371
372 ASSERT(rp->name == NULL);
373 rp->name = (char *)mem_malloc(strlen(roomname) + 1);
374 strcpy(rp->name, roomname);
375
376 // Save it out to disk (locally)
377 ddio_MakePath(name, LocalRoomsDir.u8string().c_str(), roomname, NULL);
378 SaveRoom(ROOMNUM(rp), name);
379 }
380
381 // Free our verts
382
383 if (Reading_room.faces)
384 mem_free(Reading_room.faces);
385 if (Reading_room.verts)
386 mem_free(Reading_room.verts);
387
388 return ROOMNUM(rp);
389 }
390
391 // Converts the 3dsmax coordinate space into our left-handed coordinate space
392 void ConvertHandiness(vector *v) {
393 vector v1 = *v;
394 v->x = -1.0f * v1.x;
395 v->y = v1.z;
396 v->z = -1.0f * v1.y;
397 }
398
399 #define skip(f, n) cfseek(f, n, SEEK_CUR)
400 // Parses a chunk of a 3dsmax file - this function calls itself
401 void Parse3DSMaxChunk(CFILE *fp, int size) {
402 uint16_t id;
403 int len;
404 int level = Nest_level;
405 int i;
406 float scale_factor = 0.0254f; // 0.0254 inches/meter
407
408 Nest_level++;
409
410 while (size && !cfeof(fp)) {
411
412 id = cf_ReadShort(fp);
413 len = cf_ReadInt(fp);
414
415 if (size < 0) {
416 mprintf(0, "%d:chunk error\n", level);
417 exit(1);
418 }
419
420 switch (id) {
421 case ID_UNKNOWN:
422 Parse3DSMaxChunk(fp, len - 6);
423 break;
424
425 case ID_MAT_NAME: {
426 char material_name[PAGENAME_LEN];
427
428 cf_ReadString(material_name, PAGENAME_LEN, fp);
429
430 strcpy(Materials[Num_materials].name, material_name);
431 Num_materials++;
432
433 ASSERT(Num_materials < MAX_MATERIALS);
434 break;
435 }
436
437 case ID_MATERIAL: {
438 Parse3DSMaxChunk(fp, len - 6);
439 break;
440 }
441
442 case ID_MAT_TEXTURE: {
443 int i;
444 char texture_name[PAGENAME_LEN];
445
446 // Read in Unknown field
447 for (i = 0; i < 6; i++)
448 cf_ReadByte(fp);
449
450 cf_ReadShort(fp);
451
452 // Read in Unknown field
453 for (i = 0; i < 6; i++)
454 cf_ReadByte(fp);
455
456 cf_ReadString(texture_name, PAGENAME_LEN, fp);
457
458 // Find the texture that has this bitmap as a name
459
460 int texlen = strlen(texture_name);
461
462 texture_name[texlen - 4] = 0;
463 strcat(texture_name, ".OGF");
464
465 int ret = FindTextureBitmapName(texture_name);
466 if (ret == -1) {
467 mprintf(0, "Couldn't find bitmap %s!\n", texture_name);
468 ret = GetNextTexture(0);
469 }
470
471 Materials[Num_materials - 1].texhandle = ret;
472
473 skip(fp, len - 6 - 6 - 2 - 6 - (strlen(texture_name) + 1));
474
475 break;
476 }
477 case ID_MAT_APP: {
478 char material_name[PAGENAME_LEN];
479 int16_t n_faces, face;
480 int done = 0;
481 int texnum;
482
483 cf_ReadString(material_name, PAGENAME_LEN, fp);
484
485 n_faces = cf_ReadShort(fp);
486
487 for (int i = 0; i < Num_materials && !done; i++) {
488 if (!stricmp(Materials[i].name, material_name)) {
489 texnum = Materials[i].texhandle;
490 done = 1;
491 }
492 }
493
494 if (!done) {
495 mprintf(0, "Couldn't find material named %s!\n", material_name);
496 texnum = GetNextTexture(0);
497 }
498
499 while (n_faces--) {
500 face = cf_ReadShort(fp);
501
502 Reading_room.faces[face].tmap = texnum;
503 }
504
505 break;
506 }
507
508 case ID_OBJECT: {
509 cf_ReadString(Reading_room.name, PAGENAME_LEN, fp);
510 Parse3DSMaxChunk(fp, len - 6 - (strlen(Reading_room.name) + 1));
511 break;
512 }
513 // Special properties for this object/room
514 case ID_OBJ_PROPS: {
515 cf_ReadString(Reading_properties, 255, fp);
516 // Skip next two ints
517 cf_ReadInt(fp);
518 cf_ReadInt(fp);
519
520 Parse3DSMaxChunk(fp, len - 6 - 4 - 4 - (strlen(Reading_properties) + 1));
521 break;
522 }
523
524 case ID_TRI_MESH:
525 mprintf(0, "Found 3dsmax TRI_MESH chunk!\n");
526
527 Parse3DSMaxChunk(fp, len - 6);
528 break;
529
530 // Vertex list
531 case ID_VERTLIST: {
532 uint16_t num_verts = cf_ReadShort(fp);
533 int i;
534
535 if (num_verts > MAX_VERTS_PER_ROOM)
536 return;
537
538 mprintf(0, "Reading in %d verts from room!\n", num_verts);
539
540 // Make room for these verts
541 Reading_room.num_verts = num_verts;
542
543 for (i = 0; i < num_verts; i++) {
544 Reading_room.verts[i].x = cf_ReadFloat(fp);
545 Reading_room.verts[i].y = cf_ReadFloat(fp);
546 Reading_room.verts[i].z = cf_ReadFloat(fp);
547
548 Reading_room.verts[i] *= scale_factor;
549 }
550
551 break;
552 }
553
554 case ID_FACELIST: {
555 uint16_t num_faces = cf_ReadShort(fp);
556 uint16_t a, b, c, flags;
557 int i, t, j, this_size;
558
559 if (num_faces > MAX_READING_ROOM_FACES)
560 return;
561
562 mprintf(0, "Reading in %d faces!\n", num_faces);
563
564 Reading_room.num_faces = num_faces;
565 ASSERT(Reading_room.faces != NULL);
566
567 for (i = 0; i < num_faces; i++) {
568 // a,b, and c are indices into the list of vertices for this room
569 Reading_room.faces[i].num_verts = 3;
570
571 a = cf_ReadShort(fp);
572 b = cf_ReadShort(fp);
573 c = cf_ReadShort(fp);
574
575 flags = cf_ReadShort(fp);
576
577 // Set our pointers accordingly
578 // We must reverse the ordering of the verts because
579 // 3ds has a right handed coordinate system
580 Reading_room.faces[i].face_verts[0] = c;
581 Reading_room.faces[i].face_verts[1] = b;
582 Reading_room.faces[i].face_verts[2] = a;
583
584 Reading_room.faces[i].tmap = GetNextTexture(0);
585
586 Reading_room.faces[i].flags = flags;
587
588 // Read UVs
589
590 float u[3], v[3];
591 for (t = 0; t < 3; t++) {
592 u[t] = cf_ReadFloat(fp);
593 v[t] = cf_ReadFloat(fp);
594 }
595
596 for (j = 0; j < 3; j++) {
597 Reading_room.faces[i].face_uvls[j].u = u[2 - j];
598 Reading_room.faces[i].face_uvls[j].v = -v[2 - j];
599 Reading_room.faces[i].face_uvls[j].alpha = 255;
600 }
601 }
602
603 // get whatever is left in this segment
604 this_size = num_faces * ((4 * sizeof(int16_t)) + (6 * sizeof(float)));
605 if (len - 6 - this_size)
606 Parse3DSMaxChunk(fp, len - 6 - 2 - this_size);
607
608 break;
609 }
610
611 case ID_MATRIX: {
612 float room_matrix[12];
613 int16_t i;
614 for (i = 0; i < 12; i++)
615 room_matrix[i] = cf_ReadFloat(fp);
616 break;
617 }
618
619 case OBJECT_NODE_TAG: {
620 // printf( "=================== OBJECT_NODE_TAG! =======================\n" );
621
622 Parse3DSMaxChunk(fp, len - 6);
623 break;
624 }
625
626 default:
627 // Skip this stuff
628 for (i = 0; i < len - 6; i++)
629 cf_ReadByte(fp);
630 break;
631 }
632 size -= len;
633 }
634
635 Nest_level--;
636 }
637
638 int next_vertex(reading_face *f, int v) { return f->face_verts[(v + 1) % f->num_verts]; }
639
640 int this_vertex(reading_face *f, int v) { return f->face_verts[v]; }
641
642 int compute_faces_mapping(reading_face *f, vector *out_norm, float *out_d) {
643 int i;
644 float ut, vt, len;
645 vector point, u, v;
646 vector normal;
647
648 vm_MakeZero(&point);
649 vm_MakeZero(&normal);
650
651 ut = vt = 0.0f;
652
653 for (i = 0; i < f->num_verts; i++) {
654 u.x = f->face_uvls[i].u;
655 u.y = f->face_uvls[i].v;
656 u.z = 1.0f;
657
658 v.x = f->face_uvls[(i + 1) % f->num_verts].u;
659 v.y = f->face_uvls[(i + 1) % f->num_verts].v;
660 v.z = 1.0f;
661
662 ut += (float)fabs(u.x - v.x);
663 vt += (float)fabs(u.y - v.y);
664
665 normal.x += (u.y - v.y) * (u.z + v.z);
666 normal.y += (u.z - v.z) * (u.x + v.x);
667 normal.z += (u.x - v.x) * (u.y + v.y);
668 point += u;
669 }
670
671 len = vm_GetMagnitude(&normal);
672
673 out_norm->x = normal.x / len;
674 out_norm->y = normal.y / len;
675 out_norm->z = normal.z / len;
676 len *= f->num_verts;
677 *out_d = (vm_DotProduct(&point, &normal) / len);
678 point.x /= f->num_verts;
679 point.y /= f->num_verts;
680 point.z /= f->num_verts;
681
682 return 0;
683 }
684
685 // Returns 1 if the uv's match
686 int uvs_match(reading_face *a, int va, reading_face *b, int vb) {
687 int f1, f2, flag;
688 vector n1, n2;
689 float d1, d2;
690 float cosTheta;
691 float u_err, v_err;
692
693 u_err = (float)fabs((a->face_uvls[va].u - b->face_uvls[vb].u) / ((a->face_uvls[va].u + b->face_uvls[vb].u) / 2.0f));
694 v_err = (float)fabs((a->face_uvls[va].v - b->face_uvls[vb].v) / ((a->face_uvls[va].v + b->face_uvls[vb].v) / 2.0f));
695
696 if (u_err + v_err > 0.00001f)
697 return 0;
698
699 f1 = compute_faces_mapping(a, &n1, &d1);
700 f2 = compute_faces_mapping(b, &n2, &d2);
701
702 if (f1 || f2)
703 return 0;
704
705 cosTheta = vm_DotProduct(&n1, &n2);
706
707 if ((cosTheta < 0.99f) || (fabs(d2 - d1) > 0.0001f)) {
708 flag = 0;
709 } else {
710 flag = 1;
711 }
712
713 return flag;
714 }
715
716 #define MAX_POINT_DISTANCE_FROM_PLANE .1
717
718 int CombineFaces(reading_face *dest, reading_face *a, reading_face *b) {
719 int starta, startb, i;
720 int va;
721
722 if (!NormalsAreSame(&b->normal, &a->normal))
723 return 0;
724
725 if (a->tmap != b->tmap)
726 return 0;
727
728 ASSERT(a->num_verts > 2);
729 ASSERT(b->num_verts > 2);
730
731 // Compare points to plane
732 vector vec = Reading_room.verts[a->face_verts[0]];
733 vector norm = a->normal;
734 float plane_dist = -(vec.x * norm.x + vec.y * norm.y + vec.z * norm.z);
735
736 for (i = 0; i < b->num_verts; i++) {
737 vec = Reading_room.verts[b->face_verts[i]];
738 float dist = vec.x * norm.x + vec.y * norm.y + vec.z * norm.z + plane_dist;
739 if (fabs(dist) > MAX_POINT_DISTANCE_FROM_PLANE)
740 return 0;
741 }
742
743 // Go through each vertex and get a match
744
745 for (starta = 0; starta < a->num_verts; starta++) {
746 for (startb = 0; startb < b->num_verts; startb++) {
747 if ((this_vertex(a, starta) == next_vertex(b, startb)) && (next_vertex(a, starta) == this_vertex(b, startb)) &&
748 uvs_match(a, starta, b, (startb + 1) % b->num_verts) &&
749 uvs_match(a, (starta + 1) % a->num_verts, b, startb)) {
750 // MATCH!!!!!!!!
751
752 dest->num_verts = 0;
753 dest->flags = a->flags;
754 dest->normal = a->normal; // normal of this face
755 dest->tmap = a->tmap;
756
757 for (i = 1; i < a->num_verts; i++) {
758 ASSERT(dest->num_verts < MAX_VERTS_PER_FACE);
759 dest->face_verts[dest->num_verts] = a->face_verts[(starta + i) % a->num_verts];
760 dest->face_uvls[dest->num_verts] = a->face_uvls[(starta + i) % a->num_verts];
761 va = dest->face_verts[dest->num_verts];
762 dest->num_verts++;
763 }
764
765 if ((va == b->face_verts[(startb + 2) % b->num_verts]))
766 mprintf(0, "WARNING!!! Faces were combined that caused the loss of a vertex!\n");
767
768 for (i = 1; i < b->num_verts; i++) {
769 ASSERT(dest->num_verts < MAX_VERTS_PER_FACE);
770 if ((i == 1) && (va == b->face_verts[(startb + i + 1) % b->num_verts]))
771 continue;
772 else if ((i == 2) && (va == b->face_verts[(startb + i) % b->num_verts]))
773 continue;
774 else {
775 dest->face_verts[dest->num_verts] = b->face_verts[(startb + i) % b->num_verts];
776 dest->face_uvls[dest->num_verts] = b->face_uvls[(startb + i) % b->num_verts];
777 dest->num_verts++;
778 }
779 }
780 ASSERT(dest->num_verts > 2);
781
782 if ((CheckFaceConcavity(dest->num_verts, dest->face_verts, &dest->normal, Reading_room.verts)) >= 0)
783 return 0;
784
785 // Now check for degenerate face
786 for (int v = 0; v < dest->num_verts; v++) {
787 if (dest->face_verts[v] == dest->face_verts[(v + 2) % dest->num_verts]) {
788 return 0;
789 }
790 }
791
792 return 1;
793 }
794 }
795 }
796 return 0;
797 }