| 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/Erooms.h $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Header for ERooms.cpp |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 50 10/14/99 4:21p Matt |
| 30 | * Added code to remove duplicate faces from a room. |
| 31 | * |
| 32 | * 49 7/14/99 9:07a Gwar |
| 33 | * added a parameter to AllocLoadRoom |
| 34 | * |
| 35 | * 48 7/02/99 2:17p Gwar |
| 36 | * added a parameter to AllocLoadRoom to disable centering on origin |
| 37 | * |
| 38 | * 47 6/17/99 7:34a Gwar |
| 39 | * added some function prototypes |
| 40 | * |
| 41 | * 46 5/05/99 3:02a Gwar |
| 42 | * added a parameter to DeleteRoomFace (by permission) |
| 43 | * |
| 44 | * 45 4/06/99 5:10p Matt |
| 45 | * Base room shell on all portals in the room, not just the first one. |
| 46 | * |
| 47 | * 44 4/01/99 9:42a Matt |
| 48 | * Added code to compute room shells, and made verify level do it. |
| 49 | * |
| 50 | * 43 3/30/99 7:57p Matt |
| 51 | * Added prototype and moved a function to be inline in the header. |
| 52 | * |
| 53 | * 42 3/30/99 7:20p Matt |
| 54 | * Deleted PointsAreColinear(), since it was never used and worked the |
| 55 | * wrong way. |
| 56 | * |
| 57 | * 41 2/03/99 1:10p Matt |
| 58 | * Changed the paletted room current faces to be stored in a seperate |
| 59 | * array, instead of in the room structure. |
| 60 | * |
| 61 | * 40 10/03/98 8:05p Matt |
| 62 | * Changed the way CheckIfFaceIsPlanar() works (& renamed it to |
| 63 | * FaceIsPlanar()) and added planar check to VerifyMine() & VerifyRoom() |
| 64 | * |
| 65 | * 39 9/09/98 4:02p Matt |
| 66 | * Added default value for parm to CreateNewRoom() |
| 67 | * |
| 68 | * 38 9/08/98 6:12p Matt |
| 69 | * Added code to check for and fix duplicate and unused points. |
| 70 | * |
| 71 | * 37 9/02/98 4:20p Matt |
| 72 | * Cleaned up some linking code now that we don't have multi-face portals. |
| 73 | * |
| 74 | * 36 9/02/98 2:28p Matt |
| 75 | * New function to add a vertex to a face. |
| 76 | * |
| 77 | * 35 8/31/98 4:37p Matt |
| 78 | * Added improved room & mine error checking. |
| 79 | * |
| 80 | * 34 6/08/98 12:27p Matt |
| 81 | * Added function to copy from one face to another the flags that should |
| 82 | * be inherited. |
| 83 | * |
| 84 | * 33 4/27/98 6:43p Matt |
| 85 | * Cleaned up epsilons some more, and moved some code here from hroom.cpp. |
| 86 | * |
| 87 | * 32 4/16/98 10:41a Matt |
| 88 | * Added FindSharedEdgeAcrossRooms() |
| 89 | * |
| 90 | * 31 4/02/98 12:23p Jason |
| 91 | * trimmed some fat from our structures |
| 92 | * |
| 93 | * 30 4/01/98 3:26p Matt |
| 94 | * Changed NORMALS_SAME_VALUE to have a more reasonable value (was |
| 95 | * 0.99999999, changed to 0.999f) |
| 96 | * |
| 97 | * 29 2/24/98 2:57p Jason |
| 98 | * adjust planar tolerance (again) |
| 99 | * |
| 100 | * 28 2/24/98 1:54p Jason |
| 101 | * added much stricter concavity testing |
| 102 | * |
| 103 | * 27 1/20/98 4:05p Matt |
| 104 | * Move BuildListOfPortalVerts() from roomkeypaddialog.cpp to erooms.cpp |
| 105 | * |
| 106 | * 26 1/15/98 7:34p Matt |
| 107 | * Revamped error checking when computing face normals |
| 108 | * |
| 109 | * 25 10/13/97 5:08p Matt |
| 110 | * Moved ComputeRoomBoundingSphere() & CreateRoomObjects() from editor to |
| 111 | * main |
| 112 | * |
| 113 | * |
| 114 | * 24 10/01/97 7:51p Matt |
| 115 | * Added code for external rooms |
| 116 | * |
| 117 | * 23 9/24/97 3:20p Matt |
| 118 | * Added ComputeRoomMinMax() |
| 119 | * |
| 120 | * 22 9/22/97 2:29p Matt |
| 121 | * Added comment |
| 122 | * |
| 123 | * 21 9/19/97 2:52p Jason |
| 124 | * changes to fix lightmap seam problem |
| 125 | * |
| 126 | * 20 9/17/97 11:50a Jason |
| 127 | * ripped out segment engine |
| 128 | * |
| 129 | * 19 9/12/97 1:25p Matt |
| 130 | * Added ifdef around body to prevent errors when included twice |
| 131 | * |
| 132 | * 18 9/02/97 5:17p Jason |
| 133 | * changes for dynamic lighting |
| 134 | * |
| 135 | * 17 9/02/97 2:28p Matt |
| 136 | * Changed PointOutsideEdge() to the more generally-useful |
| 137 | * CheckPointAgainstEdge() |
| 138 | * Made CopyFace() clear the portal number |
| 139 | * |
| 140 | * 16 8/29/97 5:44p Matt |
| 141 | * Moved a couple functions from hroom.cpp, and added a couple more useful |
| 142 | * functions. |
| 143 | * |
| 144 | * 15 8/26/97 10:24a Matt |
| 145 | * Removed debugging mprintf() |
| 146 | * |
| 147 | * 14 8/22/97 1:02p Matt |
| 148 | * Got CompressMine() working for rooms |
| 149 | * |
| 150 | * 13 8/21/97 5:56p Matt |
| 151 | * Added a bunch of useful functions, and make CheckFaceConcavity() more |
| 152 | * generally useful. |
| 153 | * |
| 154 | * 12 8/04/97 6:46p Jason |
| 155 | * added code for a lightmap system |
| 156 | * |
| 157 | * 11 8/01/97 6:11p Matt |
| 158 | * Added several functions that I needed for attach room code |
| 159 | * |
| 160 | * 10 8/01/97 12:50p Jason |
| 161 | * added code to support scaling of rooms/faces/edges |
| 162 | * |
| 163 | * 9 7/31/97 3:31p Jason |
| 164 | * added functions to rotate portal rooms |
| 165 | * |
| 166 | * 8 7/29/97 1:55p Matt |
| 167 | * Added some generally useful room/face functions |
| 168 | * |
| 169 | * 7 7/20/97 3:41p Jason |
| 170 | * got room/terrain links working (at least in the terrain) |
| 171 | * |
| 172 | * 6 7/18/97 4:37p Matt |
| 173 | * Added function CheckFaceConcavity() |
| 174 | * |
| 175 | * |
| 176 | * |
| 177 | * $NoKeywords: $ |
| 178 | */ |
| 179 | |
| 180 | #ifndef _EROOMS_H |
| 181 | #define _EROOMS_H |
| 182 | |
| 183 | #include "room.h" |
| 184 | |
| 185 | #define DEFAULT_ROOM_SIZE 20.0 |
| 186 | |
| 187 | // Values to determine if two points are the same, if a point is on a plane, & if a point is on an edge |
| 188 | #define POINT_TO_POINT_EPSILON 0.1 |
| 189 | #define POINT_TO_PLANE_EPSILON 0.1 |
| 190 | #define POINT_TO_EDGE_EPSILON 0.1 |
| 191 | |
| 192 | // List of current faces for the palette rooms |
| 193 | extern int Current_faces[]; |
| 194 | |
| 195 | // Called at startup to mark all rooms as unused |
| 196 | void InitRooms(); |
| 197 | |
| 198 | // Returns a free room number, & marks it no longer free. Returns -1 if none free. |
| 199 | // If palette_room is true, allocate out of the part of the array for the room palette |
| 200 | int GetFreeRoom(bool palette_room); |
| 201 | |
| 202 | // Allocates a room & initializes it. |
| 203 | // All fields except the 3d points and the side types are set to default values. |
| 204 | // Memory is allocated for faces & verts arrays, but the elements are *not* initialized |
| 205 | // If palette_room is true, allocate out of the part of the array for the room palette |
| 206 | // Returns: pointer to new room, or NULL if no free rooms |
| 207 | room *CreateNewRoom(int nverts, int nfaces, bool palette_room = 0); |
| 208 | |
| 209 | // Searches thru all rooms for a specific name, returns -1 if not found |
| 210 | // or index of room with name |
| 211 | int FindRoomName(char *name); |
| 212 | |
| 213 | // saves a room in our ORF (Outrage room file) format |
| 214 | void SaveRoom(int n, char *filename); |
| 215 | |
| 216 | // Allocates a room and then tries to load it |
| 217 | // Returns index into Rooms[] array on success |
| 218 | // -1 on fail |
| 219 | int AllocLoadRoom(char *filename, bool bCenter = true, bool palette_room = 1); |
| 220 | |
| 221 | // Gets next room from n that has actually been alloced |
| 222 | int GetNextRoom(int n); |
| 223 | |
| 224 | // Deterimines whether a face is concave or convex |
| 225 | // Parameters: num_verts - the number of vertices in the face to be tested |
| 226 | // face_verts - list of vertex numbers in this face |
| 227 | // normal - the surface normal of this face |
| 228 | // verts - array of vertices into which face_verts elements index |
| 229 | // Returns: If the face is concave, returns the number of the vertex that makes the concavity. |
| 230 | // If the face is convex, returns -1 |
| 231 | // NOTE: A face could have multiple concavities, and this will only find the one with the |
| 232 | // lowest-numbered vertex |
| 233 | int CheckFaceConcavity(int num_verts, int16_t *face_verts, vector *normal, vector *verts); |
| 234 | |
| 235 | // Goes through each face of the passed room and sets the default uvs |
| 236 | void AssignDefaultUVsToRoom(room *rp); |
| 237 | |
| 238 | // Computes the center point on a face by averaging the points in the face |
| 239 | void ComputeCenterPointOnFace(vector *vp, room *rp, int facenum); |
| 240 | |
| 241 | // Computes the center point on a face by averaging the points in the portal |
| 242 | void ComputeCenterPointOnPortal(vector *vp, room *rp, int portalnum); |
| 243 | |
| 244 | // Recompute the surface normals for all the faces in a room |
| 245 | // Parameters: rp - pointer to the room |
| 246 | // Returns: true if normals computed ok, false if some normals were bad |
| 247 | bool ResetRoomFaceNormals(room *rp); |
| 248 | |
| 249 | // Copies the contents of one face to another. |
| 250 | // Parameters: dfp - pointer to the destination face. This face should be uninitialized. |
| 251 | // sfp - pointer to the source face |
| 252 | void CopyFace(face *dfp, face *sfp); |
| 253 | |
| 254 | // Fixes all the concave/nonplanar faces of facelist of room rp |
| 255 | void FixConcaveFaces(room *rp, int *facelist, int facecount); |
| 256 | |
| 257 | void AssignDefaultUVsToRoomFace(room *rp, int facenum); |
| 258 | |
| 259 | // Changes the number of verts in a face. Frees and reallocates the face_verts & face_uvls arrays. |
| 260 | // Leaves all other fields the same |
| 261 | void ReInitRoomFace(face *fp, int nverts); |
| 262 | |
| 263 | // Determines if two points are close enough together to be considered the same |
| 264 | // Parameters: v0,v1 - the two points |
| 265 | // Returns: true if the points are the same or very close; else false |
| 266 | bool PointsAreSame(vector *v0, vector *v1); |
| 267 | |
| 268 | // Check to see if a point is in, in front of, or behind a plane |
| 269 | // Parameters: checkpoint - the point to check |
| 270 | // planepoint,normal - the plane we're checking against |
| 271 | // Returns: 0 if on the plane, -1 if behind, 1 if in front |
| 272 | int CheckPointToPlane(vector *checkpoint, vector *planepoint, vector *normal); |
| 273 | |
| 274 | // Check to see if all the points on a face are in front of a plane |
| 275 | // Parameters: rp,facenum - the face to check |
| 276 | // planepoint,normal - define the plane we're checking against |
| 277 | // Returns: the number of the first point found on the back of the plane, or -1 of all on front |
| 278 | int CheckFaceToPlane(room *rp, int facenum, vector *planepoint, vector *normal); |
| 279 | |
| 280 | // Check if a point is inside, outside, or on an edge of a polygon |
| 281 | // Parameters: checkv - the point to be checked |
| 282 | // v1,v0 - the edge to check against. Two sequential verts in a clockwise polygon. |
| 283 | // normal - the surface normal of the polygon |
| 284 | // Returns: 1 if the point in inside the edge |
| 285 | // 0 if the point is on the edge |
| 286 | // -1 if the point is outside the edge |
| 287 | int CheckPointAgainstEdge(vector *checkv, vector *v0, vector *v1, vector *normal); |
| 288 | |
| 289 | // Create space for additional vertices in a room. |
| 290 | // Allocates a new array of vertices, copies from the old list, and frees the old list |
| 291 | // The new vertices are at the end of the list, so none of the old vertices change number |
| 292 | // Parameters: rp - the room |
| 293 | // n_new_verts - how many vertices are being added to the room |
| 294 | // Returns: the number of the first new vertex |
| 295 | int RoomAddVertices(room *rp, int num_new_verts); |
| 296 | |
| 297 | // Create space for additional faces in a room. |
| 298 | // Allocates a new faces array, copies from the old list, and frees the old list |
| 299 | // The new faces are at the end of the list, so none of the old faces change number |
| 300 | // Parameters: rp - the room |
| 301 | // num_new_faces - how many faces are being added to the room |
| 302 | // Returns: the number of the first new face |
| 303 | int RoomAddFaces(room *rp, int num_new_faces); |
| 304 | |
| 305 | // Structure to hold vector and uvl data for clipping |
| 306 | struct vertex { |
| 307 | vector vec; |
| 308 | roomUVL uvl; |
| 309 | }; |
| 310 | |
| 311 | // Clips on edge of a polygon against another edge |
| 312 | // Parameters: normal - defines the plane in which these edgs lie |
| 313 | // v0,v1 - the edge to be clipped |
| 314 | // v2,v3 - is the edge clipped against |
| 315 | // newv - filled in with the intersection point |
| 316 | void ClipEdge(vector *normal, vertex *v0, vertex *v1, vector *v2, vector *v3, vertex *newv); |
| 317 | |
| 318 | // Finds a shared edge, if one exists, between two faces in the same room |
| 319 | // Parameters: fp0,fp1 - pointers to the two faces |
| 320 | // vn0,vn1 - filled in with the vertex numbers of the edge. These vert numbers are |
| 321 | // relative to their own faces. The shared edge is |
| 322 | //verts <vn0,vn0+1> on face 0, and <vn1+1,vn1> on face 1 Returns: true if a shared edge was found, else |
| 323 | // false |
| 324 | bool FindSharedEdge(face *fp0, face *fp1, int *vn0, int *vn1); |
| 325 | |
| 326 | // Finds a shared edge, if one exists, between two faces in different rooms |
| 327 | // Parameters: rp0,rp1 - pointers to the two rooms |
| 328 | // face0,face1 - the face numbers in rp0 & rp1, respectively |
| 329 | // vn0,vn1 - filled in with the vertex numbers of the edge. These vert numbers are |
| 330 | // relative to their own faces. The shared edge is |
| 331 | //verts <vn0,vn0+1> on face 0, and <vn1+1,vn1> on face 1 Returns: true if a shared edge was found, else |
| 332 | // false |
| 333 | bool FindSharedEdgeAcrossRooms(room *rp0, int face0, room *rp1, int face1, int *vn0, int *vn1); |
| 334 | |
| 335 | // If two surface normals have a dot product greater than or equal to this value, they are the same |
| 336 | #define NORMALS_SAME_VALUE .999f |
| 337 | |
| 338 | // Determines if two normals are the same (or very very close) |
| 339 | // Parameters: n0,n1 - the two normals |
| 340 | // Returns: true if they are more-or-less the same, else false |
| 341 | inline bool NormalsAreSame(vector *n0, vector *n1) { |
| 342 | float d = *n0 * *n1; |
| 343 | |
| 344 | return (d > NORMALS_SAME_VALUE); |
| 345 | } |
| 346 | |
| 347 | // Delete a face from a room |
| 348 | // Parameters: rp - the room the face is in |
| 349 | // facenum - the face to be deleted |
| 350 | void DeleteRoomFace(room *rp, int facenum, bool delete_unused_verts = true); |
| 351 | |
| 352 | // Deletes a portal from a room. Does not delete this portal this connects to |
| 353 | // Parameters: rp - the room the portal is in |
| 354 | // portalnum - the portal to be deleted |
| 355 | void DeleteRoomPortal(room *rp, int portalnum); |
| 356 | |
| 357 | // Remove holes in the room list |
| 358 | void CompressRooms(void); |
| 359 | |
| 360 | // Compress mine by getting rid of holes the the room array |
| 361 | void CompressMine(void); |
| 362 | |
| 363 | // Copies the data from one room into another |
| 364 | // Note: Portals are not copied, and the new room will have zero portals |
| 365 | // The destination room is assumed to be uninitialized |
| 366 | // Parameters: destp - the destination room of the copy |
| 367 | // srcp - the source room for the copy |
| 368 | void CopyRoom(room *destp, room *srcp); |
| 369 | |
| 370 | // Links two rooms, creating portals in each room |
| 371 | // Parameters: roomlist - pointer to the array of rooms |
| 372 | // room0,face0 - the room & face numbers of the first room |
| 373 | // room1,face1 - the room & face numbers of the second room |
| 374 | void LinkRooms(room *roomlist, int room0, int face0, int room1, int face1); |
| 375 | |
| 376 | // Back when we had multi-face portals, LinkRoomsSimple() created a pair of 1-face portals |
| 377 | #define LinkRoomsSimple LinkRooms |
| 378 | |
| 379 | // Finds the min and max x,y,z values of the vertices in a room |
| 380 | // Parameters: min,max - filled in with the minimum and maximum x,y, & z values, respectively |
| 381 | // rp = the room |
| 382 | void ComputeRoomMinMax(vector *min, vector *max, room *rp); |
| 383 | |
| 384 | // Builds a list of all the vertices in a room that are part of a portal |
| 385 | // Parameters: rp - the room to check |
| 386 | // list - filled in with the list of vert numbers. List should be |
| 387 | //MAX_VERTS_PER_ROOM big Returns: the number of verts in the list |
| 388 | int BuildListOfPortalVerts(room *rp, int *list); |
| 389 | |
| 390 | // Copy the flags from one face to another derrived from the first (by clipping or splitting) |
| 391 | // Only those flags which are safe to copy are copied |
| 392 | // Parameters: dfp - pointer to the destination face |
| 393 | // sfp - pointer to the source face |
| 394 | void CopyFaceFlags(face *dfp, face *sfp); |
| 395 | |
| 396 | // Test the mine for validity |
| 397 | void VerifyMine(); |
| 398 | |
| 399 | // Test the given room for validity |
| 400 | void VerifyRoom(room *rp); |
| 401 | |
| 402 | // Inserts a vertex into a face |
| 403 | // Parameters: rp - the room for this face |
| 404 | // facenum - the face to which to add the vertex |
| 405 | // new_v - the number of the vertex to add |
| 406 | // after_v - the new vert is added *after* this vert (index into face verts) |
| 407 | void AddVertToFace(room *rp, int facenum, int new_v, int after_v); |
| 408 | |
| 409 | // Removes all the duplicate points in a level |
| 410 | void RemoveAllDuplicateAndUnusedPoints(); |
| 411 | |
| 412 | // Checks to see if a face is planar. |
| 413 | // See if all the points are within a certain distance of an average point |
| 414 | // Returns 1 if face is planar, 0 if not |
| 415 | bool FaceIsPlanar(int nv, int16_t *face_verts, vector *normal, vector *verts); |
| 416 | |
| 417 | // Checks to see if a face is planar. |
| 418 | // See if all the points are within a certain distance of an average point |
| 419 | // Returns 1 if face is planar, 0 if not |
| 420 | inline bool FaceIsPlanar(room *rp, int facenum) { |
| 421 | face *fp = &rp->faces[facenum]; |
| 422 | |
| 423 | return FaceIsPlanar(fp->num_verts, fp->face_verts, &fp->normal, rp->verts); |
| 424 | } |
| 425 | |
| 426 | // Finds the shell for the specified room. If the shell is found with no errors, sets |
| 427 | // the non-shell flag for those faces not in the shell. If there are errors finding the |
| 428 | // shell, all faces have the non-shell flag cleared. |
| 429 | // Returns the number of shell errors (unconnected edges) in the room. |
| 430 | // Writes errors to the error buffer |
| 431 | int ComputeRoomShell(room *rp); |
| 432 | |
| 433 | // Finds shells for all rooms. |
| 434 | // Returns the number of rooms with bad shells |
| 435 | int ComputeAllRoomShells(); |
| 436 | |
| 437 | // Log an error in the room check process |
| 438 | void CheckError(const char *str, ...); |
| 439 | |
| 440 | // Checks the normals in a room |
| 441 | // Returns the number of bad normals |
| 442 | int CheckNormals(room *rp); |
| 443 | |
| 444 | // Checks for concave faces |
| 445 | // Returns the number of concave faces |
| 446 | int CheckConcaveFaces(room *rp); |
| 447 | |
| 448 | int CheckDegenerateFaces(room *rp); |
| 449 | |
| 450 | // Checks for bad portal connections |
| 451 | // Returns the number of bad portals |
| 452 | int CheckPortals(room *rp); |
| 453 | |
| 454 | // Checks for duplicate faces |
| 455 | // Returns the number of duplicate faces |
| 456 | int CheckDuplicateFaces(room *rp); |
| 457 | |
| 458 | // Checks for non-planar faces |
| 459 | // Returns the number of non-planar faces |
| 460 | int CheckNonPlanarFaces(room *rp); |
| 461 | |
| 462 | // Checks for duplicate points |
| 463 | // Returns the number of duplicate points |
| 464 | int CheckDuplicatePoints(room *rp); |
| 465 | |
| 466 | // Find any unused points in a room |
| 467 | int CheckUnusedPoints(room *rp); |
| 468 | |
| 469 | // Checks for duplicate points in faces |
| 470 | // Returns the number of duplicate face points |
| 471 | int CheckDuplicateFacePoints(room *rp); |
| 472 | |
| 473 | int CheckRoomPortalFaces(room *rp); |
| 474 | |
| 475 | // Find t-joints in this room |
| 476 | int FindTJoints(room *rp); |
| 477 | |
| 478 | // Counts the number of unique textures in a level, plus gives names of textures used |
| 479 | void CountUniqueTextures(); |
| 480 | |
| 481 | // Removes all the duplicate faces in a room |
| 482 | void RemoveDuplicateFaces(room *rp); |
| 483 | |
| 484 | #endif // ifndef _EROOMS_H |