| 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/Lib/3d.h $ |
| 21 | * $Revision: 36 $ |
| 22 | * $Date: 10/21/99 9:27p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Header file for Descent 3 3d library |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/Lib/3d.h $ |
| 28 | * |
| 29 | * 36 10/21/99 9:27p Jeff |
| 30 | * B.A. Macintosh code merge |
| 31 | * |
| 32 | * 35 5/13/99 10:57p Jason |
| 33 | * added extra parameter to g3_DrawPoly |
| 34 | * |
| 35 | * 34 4/22/99 5:14p Matt |
| 36 | * Changed the and & or fields in the grCodes struct to cc_and & cc_or, |
| 37 | * since the former names didn't work with some compilers. |
| 38 | * |
| 39 | * 33 3/26/99 3:53p Jason |
| 40 | * returned clip codes to their original order |
| 41 | * |
| 42 | * 32 3/26/99 3:25p Jason |
| 43 | * fixed remaining mirror problems |
| 44 | * |
| 45 | * 31 2/19/99 4:26p Jason |
| 46 | * more work on Katmai support |
| 47 | * |
| 48 | * 30 2/18/99 5:45p Jason |
| 49 | * added custom clip plane |
| 50 | * |
| 51 | * 29 1/26/99 6:39p Jason |
| 52 | * added wall effects code |
| 53 | * |
| 54 | * 28 5/25/98 3:45p Jason |
| 55 | * made g3_DrawPoly return 1 if it drew |
| 56 | * |
| 57 | * 27 5/20/98 5:43p Jason |
| 58 | * incremental checkin for bumpmapping |
| 59 | * |
| 60 | * 26 5/19/98 12:27p Jason |
| 61 | * cleaned up some 3d stuff |
| 62 | * |
| 63 | * 25 5/14/98 12:56p Jason |
| 64 | * changes to help lower memory usage |
| 65 | * |
| 66 | * 24 4/01/98 12:02p Jason |
| 67 | * incremental checkin for rendering changes |
| 68 | * |
| 69 | * 23 2/05/98 11:14a Jason |
| 70 | * added g3_DrawPlanarRotatedBitmap function |
| 71 | * |
| 72 | * 22 1/28/98 5:36p Jason |
| 73 | * added streamer weapons |
| 74 | * |
| 75 | * 21 1/07/98 3:57p Jason |
| 76 | * fixed spelling of triangulation (doh!) |
| 77 | * |
| 78 | * 20 1/07/98 3:55p Jason |
| 79 | * made Triangulate test a user callable function |
| 80 | * |
| 81 | * |
| 82 | * 19 12/29/97 5:49p Samir |
| 83 | * 3d.h doesn't include gr.h anymore. |
| 84 | * |
| 85 | * 18 12/23/97 10:59a Samir |
| 86 | * Temporarily put gr.h back in. |
| 87 | * |
| 88 | * 17 12/22/97 7:21p Samir |
| 89 | * replaced ddvid.h with grdefs.h |
| 90 | * |
| 91 | * 16 12/22/97 6:57p Samir |
| 92 | * Removed instances of gr.h |
| 93 | * |
| 94 | * 15 12/19/97 12:20p Jason |
| 95 | * changes for better 2d/3d system integration |
| 96 | * |
| 97 | * 14 11/10/97 12:10p Jason |
| 98 | * implemented g3_GetMatrixScale |
| 99 | * |
| 100 | * 13 10/22/97 4:18p Jason |
| 101 | * added g3_DrawRotatedBitmap |
| 102 | * |
| 103 | * 12 10/17/97 5:09p Jason |
| 104 | * added more fireball stuff |
| 105 | * |
| 106 | * 11 10/15/97 2:27p Jason |
| 107 | * changes for prelim fireball stuff |
| 108 | * |
| 109 | * 10 10/13/97 3:56p Jason |
| 110 | * made a better 3d bitmap system |
| 111 | * |
| 112 | * 9 9/16/97 4:23p Matt |
| 113 | * Made ClipPolygon() a public function (g3_ClipPolygon()), and added |
| 114 | * g3_FreeTempPoints(). |
| 115 | * |
| 116 | * 8 9/10/97 3:50p Jason |
| 117 | * added code for instancing the unscaled view matrix |
| 118 | * |
| 119 | * 7 9/04/97 11:59a Matt |
| 120 | * Added new function g3_ResetFarClipZ() |
| 121 | * |
| 122 | * 6 9/03/97 2:20p Samir |
| 123 | * Added function to get viewer matrix. |
| 124 | * |
| 125 | * 5 9/02/97 11:07a Jason |
| 126 | * added alpha component to uvl struct |
| 127 | * |
| 128 | * 4 8/20/97 4:17p Matt |
| 129 | * Added g3_GetViewPosition() |
| 130 | * |
| 131 | * 3 8/04/97 6:40p Jason |
| 132 | * added 2nd level UV floats for lightmaps |
| 133 | * |
| 134 | * 2 7/16/97 4:12p Matt |
| 135 | * Changed and cleaned up a bunch of stuff: |
| 136 | * |
| 137 | * 1. Modified clipper to not change the order of the vertices |
| 138 | * 2. Cleaned up drawer/clipper integration |
| 139 | * 3. Renamed internal functions to conform to nameing standards |
| 140 | * 4. Changed tmap drawer to not take list of uvls. UVL values must now |
| 141 | * be added to the points by the caller |
| 142 | * 5. Renamed g3_DrawTmap() to g3_DrawPoly(), and killed the old |
| 143 | * g3_DrawPoly() |
| 144 | * 6. Made line always do 3d clip if they go offscreen |
| 145 | * |
| 146 | * 14 6/25/97 5:45p Matt |
| 147 | * Made 3D draw functions void, to get rid of warnings about bool return |
| 148 | * values that were never used anyway. |
| 149 | * |
| 150 | * 13 5/08/97 8:11p Matt |
| 151 | * Added code to create subwindows that show a subsection of the 3d view |
| 152 | * |
| 153 | * 12 4/10/97 2:24p Jason |
| 154 | * added the existance of a far clip plane. |
| 155 | * |
| 156 | * 11 4/01/97 5:17p Matt |
| 157 | * Got g3_DrawSphere() working |
| 158 | * |
| 159 | * 10 2/28/97 10:43a Matt |
| 160 | * Added g3_DrawBox() to draw a wireframe box (used to show the current |
| 161 | * object in the editor). |
| 162 | * |
| 163 | * 9 2/27/97 5:01p Jason |
| 164 | * added 3d model functionality and created model library |
| 165 | * |
| 166 | * 8 2/26/97 6:00p Matt |
| 167 | * Renamed 3d lib structs for D3 naming convention |
| 168 | * |
| 169 | * 7 2/21/97 7:29p Matt |
| 170 | * Added rgb fields to g3s_uvl struct |
| 171 | * |
| 172 | * 6 2/19/97 3:32p Matt |
| 173 | * gr_DrawPoly() now takes the color |
| 174 | * |
| 175 | * 5 2/07/97 5:58p Matt |
| 176 | * Took out now-obsolete jason_point struct |
| 177 | * Renamed vector.h to vecmat.h |
| 178 | * |
| 179 | * $NoKeywords: $ |
| 180 | */ |
| 181 | |
| 182 | #ifndef _3D_H |
| 183 | #define _3D_H |
| 184 | |
| 185 | #include "pstypes.h" |
| 186 | #include "vecmat.h" //the vector/matrix library |
| 187 | #include "grdefs.h" |
| 188 | #include "float.h" |
| 189 | |
| 190 | extern int g3d_interp_outline; // if on, polygon models outlined in white |
| 191 | |
| 192 | extern vector Matrix_scale; // how the matrix is currently scaled |
| 193 | |
| 194 | // Structure for storing u,v,light values. This structure doesn't have a |
| 195 | // prefix because it was defined somewhere else before it was moved here |
| 196 | struct g3UVL { |
| 197 | // texture coordinates |
| 198 | float u, v; |
| 199 | float u2, v2; |
| 200 | |
| 201 | union { |
| 202 | float l; // intensity lighting |
| 203 | float r; |
| 204 | }; |
| 205 | float g, b, a; // rgba lighting |
| 206 | }; |
| 207 | |
| 208 | // Structure to store clipping codes in a word |
| 209 | struct g3Codes { |
| 210 | uint8_t cc_or, cc_and; |
| 211 | }; |
| 212 | |
| 213 | // flags for point structure |
| 214 | #define PF_PROJECTED 1 // has been projected, so sx,sy valid |
| 215 | #define PF_FAR_ALPHA 2 // past fog zone |
| 216 | #define PF_TEMP_POINT 4 // created during clip |
| 217 | #define PF_UV 8 // has uv values set |
| 218 | #define PF_L 16 // has lighting values set |
| 219 | #define PF_RGBA 32 // has RGBA lighting values set |
| 220 | #define PF_UV2 64 // has lightmap uvs as well |
| 221 | #define PF_ORIGPOINT 128 |
| 222 | |
| 223 | // clipping codes flags |
| 224 | #define CC_OFF_LEFT 1 |
| 225 | #define CC_OFF_RIGHT 2 |
| 226 | #define CC_OFF_BOT 4 |
| 227 | #define CC_OFF_TOP 8 |
| 228 | #define CC_OFF_FAR 16 |
| 229 | #define CC_OFF_CUSTOM 32 |
| 230 | #define CC_BEHIND 128 |
| 231 | |
| 232 | // Used to store rotated points for mines. Has frame count to indicate |
| 233 | // if rotated, and flag to indicate if projected. |
| 234 | struct g3Point { |
| 235 | float p3_sx, p3_sy; // screen x&y |
| 236 | uint8_t p3_codes; // clipping codes |
| 237 | uint8_t p3_flags; // projected? |
| 238 | int16_t p3_pad; // keep structure longword aligned |
| 239 | vector p3_vec; // x,y,z of rotated point |
| 240 | vector p3_vecPreRot; // original XYZ of the point |
| 241 | g3UVL p3_uvl; // uv & lighting values |
| 242 | }; |
| 243 | |
| 244 | // macros to reference x,y,z elements of a 3d point |
| 245 | #define p3_x p3_vec.x |
| 246 | #define p3_y p3_vec.y |
| 247 | #define p3_z p3_vec.z |
| 248 | |
| 249 | // macros to reference individual elements of the uvls struct |
| 250 | #define p3_u p3_uvl.u |
| 251 | #define p3_v p3_uvl.v |
| 252 | #define p3_l p3_uvl.l |
| 253 | #define p3_r p3_uvl.r |
| 254 | #define p3_g p3_uvl.g |
| 255 | #define p3_b p3_uvl.b |
| 256 | #define p3_a p3_uvl.a |
| 257 | #define p3_u2 p3_uvl.u2 |
| 258 | #define p3_v2 p3_uvl.v2 |
| 259 | |
| 260 | // Functions in library |
| 261 | |
| 262 | // 3d system startup and shutdown: |
| 263 | |
| 264 | // initialize the 3d system |
| 265 | void g3_Init(void); |
| 266 | |
| 267 | // close down the 3d system |
| 268 | void g3_Close(void); |
| 269 | |
| 270 | // Frame setup functions: |
| 271 | |
| 272 | // start the frame, specifying view position, matrix, & zoom |
| 273 | void g3_StartFrame(vector *view_pos, matrix *view_matrix, float zoom); |
| 274 | |
| 275 | // end the frame |
| 276 | void g3_EndFrame(void); |
| 277 | |
| 278 | // get the current view position |
| 279 | void g3_GetViewPosition(vector *vp); |
| 280 | |
| 281 | // returns the current view matrix |
| 282 | void g3_GetViewMatrix(matrix *mat); |
| 283 | |
| 284 | // returns the current unscaled view matrix |
| 285 | void g3_GetUnscaledMatrix(matrix *mat); |
| 286 | |
| 287 | // allows the user to specify an aspect ratio that overrides the renderer's |
| 288 | void g3_SetAspectRatio(float); |
| 289 | // returns the user-specified aspect ratio used to override the renderer's |
| 290 | float g3_GetAspectRatio(); |
| 291 | |
| 292 | // Instancing |
| 293 | |
| 294 | // instance at specified point with specified orientation |
| 295 | void g3_StartInstanceMatrix(vector *pos, matrix *orient); |
| 296 | |
| 297 | // instance at specified point with specified orientation |
| 298 | void g3_StartInstanceAngles(vector *pos, angvec *angles); |
| 299 | |
| 300 | // pops the old context |
| 301 | void g3_DoneInstance(); |
| 302 | |
| 303 | // Misc utility functions: |
| 304 | |
| 305 | // get current field of view. Fills in angle for x & y |
| 306 | void g3_GetFOV(float *fov_x, float *fov_y); |
| 307 | |
| 308 | // get zoom. For a given window size, return the zoom which will achieve |
| 309 | // the given FOV along the given axis. |
| 310 | float g3_GetZoom(char axis, float fov, int16_t window_width, int16_t window_height); |
| 311 | |
| 312 | // returns the normalized, unscaled view vectors |
| 313 | void g3_GetViewVectors(vector *forward, vector *up, vector *right); |
| 314 | |
| 315 | // returns true if a plane is facing the viewer. takes the unrotated surface |
| 316 | // normal of the plane, and a point on it. The normal need not be normalized |
| 317 | bool g3_CheckNormalFacing(vector *v, vector *norm); |
| 318 | |
| 319 | // Point definition and rotation functions: |
| 320 | |
| 321 | // returns codes_and & codes_or of a list of points numbers |
| 322 | g3Codes g3_CheckCodes(int nv, g3Point **pointlist); |
| 323 | |
| 324 | // rotates a point. returns codes. does not check if already rotated |
| 325 | uint8_t g3_RotatePoint(g3Point *dest, vector *src); |
| 326 | |
| 327 | // projects a point |
| 328 | void g3_ProjectPoint(g3Point *point); |
| 329 | |
| 330 | // calculate the depth of a point - returns the z coord of the rotated point |
| 331 | float g3_CalcPointDepth(vector *pnt); |
| 332 | |
| 333 | // from a 2d point, compute the vector through that point |
| 334 | void g3_Point2Vec(vector *v, int16_t sx, int16_t sy); |
| 335 | |
| 336 | // code a point. fills in the p3_codes field of the point, and returns the codes |
| 337 | uint8_t g3_CodePoint(g3Point *point); |
| 338 | |
| 339 | // delta rotation functions |
| 340 | vector *g3_RotateDeltaX(vector *dest, float dx); |
| 341 | vector *g3_RotateDeltaY(vector *dest, float dy); |
| 342 | vector *g3_RotateDeltaZ(vector *dest, float dz); |
| 343 | vector *g3_RotateDeltaVec(vector *dest, vector *src); |
| 344 | uint8_t g3_AddDeltaVec(g3Point *dest, g3Point *src, vector *deltav); |
| 345 | |
| 346 | // Drawing functions: |
| 347 | // draw a polygon |
| 348 | // Parameters: nv - the number of verts in the poly |
| 349 | // pointlist - a pointer to a list of pointers to points |
| 350 | // bm - the bitmap handle if texturing. ignored if flat shading |
| 351 | int g3_DrawPoly(int nv, g3Point **pointlist, int bm, int map_type = 0, g3Codes *clip_codes = NULL); |
| 352 | |
| 353 | // draw a sortof sphere - i.e., the 2d radius is proportional to the 3d |
| 354 | // radius, but not to the distance from the eye |
| 355 | void g3_DrawSphere(ddgr_color color, g3Point *pnt, float rad); |
| 356 | |
| 357 | // like g3_DrawPoly(), but checks to see if facing. If surface normal is |
| 358 | // NULL, this routine must compute it, which will be slow. It is better to |
| 359 | // pre-compute the normal, and pass it to this function. When the normal |
| 360 | // is passed, this function works like g3_CheckNormalFacing() plus |
| 361 | // g3_DrawPoly(). |
| 362 | void g3_CheckAndDrawPoly(int nv, g3Point **pointlist, int bm, vector *norm, vector *pnt); |
| 363 | |
| 364 | // draws a line. takes two points. |
| 365 | void g3_DrawLine(ddgr_color color, g3Point *p0, g3Point *p1); |
| 366 | |
| 367 | // draws a bitmap with the specified 3d width & height |
| 368 | // returns 1 if off screen, 0 if drew |
| 369 | void g3_DrawBitmap(vector *pos, float width, float height, int bm, int color = -1); |
| 370 | |
| 371 | // Draws a bitmap that has been rotated about its center. Angle of rotation is passed as 'rot_angle' |
| 372 | void g3_DrawRotatedBitmap(vector *pos, angle rot_angle, float width, float height, int bm, int color = -1); |
| 373 | |
| 374 | // specifies 2d drawing routines to use instead of defaults. Passing |
| 375 | // NULL for either or both restores defaults |
| 376 | void g3_SetSpecialRender(void (*tmap_drawer)(), void (*flat_drawer)(), int (*line_drawer)()); |
| 377 | |
| 378 | // Object functions: |
| 379 | |
| 380 | // init code for bitmap models |
| 381 | void g3_InitPolygonModel(void *model_ptr); |
| 382 | |
| 383 | // un-initialize, i.e., convert color entries back to RGB15 |
| 384 | void g3_UninitPolygonModel(void *model_ptr); |
| 385 | |
| 386 | // alternate interpreter for morphing object |
| 387 | void g3_DrawMorphingModel(void *model_ptr, int *model_bitmaps, angvec *anim_angles, float light, vector *new_points); |
| 388 | |
| 389 | // this remaps the 15bpp colors for the models into a new palette. It should |
| 390 | // be called whenever the palette changes |
| 391 | void g3_RemapInterpColors(void); |
| 392 | |
| 393 | // Draw a wireframe box aligned with the screen. Used for the editor. |
| 394 | // Parameters: color - the color to draw the lines |
| 395 | // pnt - the center point |
| 396 | // rad - specifies the width/2 & height/2 of the box |
| 397 | void g3_DrawBox(ddgr_color color, g3Point *pnt, float rad); |
| 398 | |
| 399 | // Sets up a custom clipping plane - g3_StartFrame must be called before this is called |
| 400 | void g3_SetCustomClipPlane(uint8_t state, vector *pnt, vector *normal); |
| 401 | |
| 402 | // sets the z distance of the far clipping plane |
| 403 | void g3_SetFarClipZ(float z); |
| 404 | |
| 405 | // Disables the far clip plane |
| 406 | static inline void g3_ResetFarClipZ() { g3_SetFarClipZ(FLT_MAX); } |
| 407 | |
| 408 | // Clips a polygon |
| 409 | // Parameters: pointlist - pointer to a list of pointers to points |
| 410 | // nv - the number of points in the polygon |
| 411 | // cc - the clip codes for this polygon |
| 412 | // Returns: a pointer to a list of pointer of points in the clipped polygon |
| 413 | // NOTE: You MUST call g3_FreeTempPoints() when you're done with the clipped polygon |
| 414 | g3Point **g3_ClipPolygon(g3Point **pointlist, int *nv, g3Codes *cc); |
| 415 | |
| 416 | // Free up any temp points (created by the clipper) in the given pointlist |
| 417 | // Parameters: pointlist - pointer to list of pointers to points, returned by g3_ClipPolygon() |
| 418 | // nv - the number of points in pointlist |
| 419 | void g3_FreeTempPoints(g3Point **pointlist, int nv); |
| 420 | |
| 421 | // Gets the matrix scale vector |
| 422 | void g3_GetMatrixScale(vector *matrix_scale); |
| 423 | |
| 424 | // Sets the triangulation test to on or off |
| 425 | void g3_SetTriangulationTest(int state); |
| 426 | |
| 427 | // draws a line based on the current setting of render states. takes two points. returns true if drew |
| 428 | void g3_DrawSpecialLine(g3Point *p0, g3Point *p1); |
| 429 | |
| 430 | // Draws a bitmap on a specific plane. Also does rotation. Angle of rotation is passed as 'rot_angle' |
| 431 | void g3_DrawPlanarRotatedBitmap(vector *pos, vector *norm, angle rot_angle, float width, float height, int bm); |
| 432 | |
| 433 | void g3_TransformVert(float res[4], float pt[4], float a[4][4]); |
| 434 | void g3_TransformMult(float res[4][4], float a[4][4], float b[4][4]); |
| 435 | void g3_TransformTrans(float res[4][4], float t[4][4]); |
| 436 | void g3_GetModelViewMatrix(const vector *viewPos, const matrix *viewMatrix, float *mvMat); |
| 437 | extern float gTransformViewPort[4][4]; |
| 438 | extern float gTransformProjection[4][4]; |
| 439 | extern float gTransformModelView[4][4]; |
| 440 | extern float gTransformFull[4][4]; |
| 441 | |
| 442 | void g3_RefreshTransforms(bool usePassthru); |
| 443 | |
| 444 | #endif |