| 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/vecmat_external.h $ |
| 21 | * $Revision: 3 $ |
| 22 | * $Date: 2/19/99 4:26p $ |
| 23 | * $Author: Jason $ |
| 24 | * |
| 25 | * Contains any header information that can/should be exported to DLLs |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/vecmat_external.h $ |
| 28 | * |
| 29 | * 3 2/19/99 4:26p Jason |
| 30 | * more work on Katmai support |
| 31 | * |
| 32 | * 2 1/21/99 11:15p Jeff |
| 33 | * pulled out some structs and defines from header files and moved them |
| 34 | * into seperate header files so that multiplayer dlls don't require major |
| 35 | * game headers, just those new headers. Side effect is a shorter build |
| 36 | * time. Also cleaned up some header file #includes that weren't needed. |
| 37 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 38 | * manage.h and multi.h |
| 39 | * |
| 40 | * $NoKeywords: $ |
| 41 | */ |
| 42 | |
| 43 | #ifndef VECMAT_EXTERNAL_H |
| 44 | #define VECMAT_EXTERNAL_H |
| 45 | |
| 46 | #include <cstdint> |
| 47 | |
| 48 | // Angles are unsigned shorts |
| 49 | typedef uint16_t angle; // make sure this matches up with fix.h |
| 50 | |
| 51 | struct angvec { |
| 52 | angle p, h, b; |
| 53 | }; |
| 54 | |
| 55 | #define IDENTITY_MATRIX \ |
| 56 | { \ |
| 57 | {1.0, 0, 0}, {0, 1.0, 0}, { 0, 0, 1.0 } \ |
| 58 | } |
| 59 | |
| 60 | struct vector { |
| 61 | float x, y, z; |
| 62 | }; |
| 63 | |
| 64 | struct vector4 { |
| 65 | float x, y, z, kat_pad; |
| 66 | }; |
| 67 | |
| 68 | struct vector_array { |
| 69 | float xyz[3]; |
| 70 | }; |
| 71 | |
| 72 | struct matrix { |
| 73 | vector rvec, uvec, fvec; |
| 74 | }; |
| 75 | |
| 76 | struct matrix4 { |
| 77 | vector4 rvec, uvec, fvec; |
| 78 | }; |
| 79 | |
| 80 | // Zero's out a vector |
| 81 | static inline void vm_MakeZero(vector *v) { v->x = v->y = v->z = 0; } |
| 82 | |
| 83 | // Set an angvec to {0,0,0} |
| 84 | static inline void vm_MakeZero(angvec *a) { a->p = a->h = a->b = 0; } |
| 85 | |
| 86 | // Checks for equality |
| 87 | static inline bool operator==(vector a, vector b) { |
| 88 | bool equality = false; |
| 89 | // Adds two vectors. |
| 90 | |
| 91 | if (a.x == b.x && a.y == b.y && a.z == b.z) |
| 92 | equality = true; |
| 93 | |
| 94 | return equality; |
| 95 | } |
| 96 | |
| 97 | // Checks for inequality |
| 98 | static inline bool operator!=(vector a, vector b) { |
| 99 | bool equality = true; |
| 100 | // Adds two vectors. |
| 101 | |
| 102 | if (a.x == b.x && a.y == b.y && a.z == b.z) |
| 103 | equality = false; |
| 104 | |
| 105 | return equality; |
| 106 | } |
| 107 | |
| 108 | // Adds 2 vectors |
| 109 | static inline vector operator+(vector a, vector b) { |
| 110 | // Adds two vectors. |
| 111 | |
| 112 | a.x += b.x; |
| 113 | a.y += b.y; |
| 114 | a.z += b.z; |
| 115 | |
| 116 | return a; |
| 117 | } |
| 118 | |
| 119 | // Adds 2 vectors |
| 120 | static inline vector operator+=(vector &a, vector b) { return (a = a + b); } |
| 121 | |
| 122 | // Adds 2 matrices |
| 123 | static inline matrix operator+(matrix a, matrix b) { |
| 124 | // Adds two 3x3 matrixs. |
| 125 | |
| 126 | a.rvec += b.rvec; |
| 127 | a.uvec += b.uvec; |
| 128 | a.fvec += b.fvec; |
| 129 | |
| 130 | return a; |
| 131 | } |
| 132 | |
| 133 | // Adds 2 matrices |
| 134 | static inline matrix operator+=(matrix &a, matrix b) { return (a = a + b); } |
| 135 | |
| 136 | // Subtracts 2 vectors |
| 137 | static inline vector operator-(vector a, vector b) { |
| 138 | // subtracts two vectors |
| 139 | |
| 140 | a.x -= b.x; |
| 141 | a.y -= b.y; |
| 142 | a.z -= b.z; |
| 143 | |
| 144 | return a; |
| 145 | } |
| 146 | |
| 147 | // Subtracts 2 vectors |
| 148 | static inline vector operator-=(vector &a, vector b) { return (a = a - b); } |
| 149 | |
| 150 | // Subtracts 2 matrices |
| 151 | static inline matrix operator-(matrix a, matrix b) { |
| 152 | // subtracts two 3x3 matrices |
| 153 | |
| 154 | a.rvec = a.rvec - b.rvec; |
| 155 | a.uvec = a.uvec - b.uvec; |
| 156 | a.fvec = a.fvec - b.fvec; |
| 157 | |
| 158 | return a; |
| 159 | } |
| 160 | |
| 161 | // Subtracts 2 matrices |
| 162 | static inline matrix operator-=(matrix &a, matrix b) { return (a = a - b); } |
| 163 | |
| 164 | // Does a simple dot product calculation |
| 165 | static inline float operator*(vector u, vector v) { return (u.x * v.x) + (u.y * v.y) + (u.z * v.z); } |
| 166 | |
| 167 | // Scalar multiplication |
| 168 | static inline vector operator*(vector v, float s) { |
| 169 | v.x *= s; |
| 170 | v.y *= s; |
| 171 | v.z *= s; |
| 172 | |
| 173 | return v; |
| 174 | } |
| 175 | |
| 176 | // Scalar multiplication |
| 177 | static inline vector operator*=(vector &v, float s) { return (v = v * s); } |
| 178 | |
| 179 | // Scalar multiplication |
| 180 | static inline vector operator*(float s, vector v) { return v * s; } |
| 181 | |
| 182 | // Scalar multiplication |
| 183 | static inline matrix operator*(float s, matrix m) { |
| 184 | m.fvec = m.fvec * s; |
| 185 | m.uvec = m.uvec * s; |
| 186 | m.rvec = m.rvec * s; |
| 187 | |
| 188 | return m; |
| 189 | } |
| 190 | |
| 191 | // Scalar multiplication |
| 192 | static inline matrix operator*(matrix m, float s) { return s * m; } |
| 193 | |
| 194 | // Scalar multiplication |
| 195 | static inline matrix operator*=(matrix &m, float s) { return (m = m * s); } |
| 196 | |
| 197 | // Scalar division |
| 198 | static inline vector operator/(vector src, float n) { |
| 199 | src.x /= n; |
| 200 | src.y /= n; |
| 201 | src.z /= n; |
| 202 | |
| 203 | return src; |
| 204 | } |
| 205 | |
| 206 | // Scalar division |
| 207 | static inline vector operator/=(vector &src, float n) { return (src = src / n); } |
| 208 | |
| 209 | // Scalar division |
| 210 | static inline matrix operator/(matrix src, float n) { |
| 211 | src.fvec = src.fvec / n; |
| 212 | src.rvec = src.rvec / n; |
| 213 | src.uvec = src.uvec / n; |
| 214 | |
| 215 | return src; |
| 216 | } |
| 217 | |
| 218 | // Scalar division |
| 219 | static inline matrix operator/=(matrix &src, float n) { return (src = src / n); } |
| 220 | |
| 221 | // Computes a cross product between u and v, returns the result |
| 222 | // in Normal. |
| 223 | static inline vector operator^(vector u, vector v) { |
| 224 | vector dest; |
| 225 | |
| 226 | dest.x = (u.y * v.z) - (u.z * v.y); |
| 227 | dest.y = (u.z * v.x) - (u.x * v.z); |
| 228 | dest.z = (u.x * v.y) - (u.y * v.x); |
| 229 | |
| 230 | return dest; |
| 231 | } |
| 232 | |
| 233 | // Matrix transpose |
| 234 | static inline matrix operator~(matrix m) { |
| 235 | float t; |
| 236 | |
| 237 | t = m.uvec.x; |
| 238 | m.uvec.x = m.rvec.y; |
| 239 | m.rvec.y = t; |
| 240 | t = m.fvec.x; |
| 241 | m.fvec.x = m.rvec.z; |
| 242 | m.rvec.z = t; |
| 243 | t = m.fvec.y; |
| 244 | m.fvec.y = m.uvec.z; |
| 245 | m.uvec.z = t; |
| 246 | |
| 247 | return m; |
| 248 | } |
| 249 | |
| 250 | // Negate vector |
| 251 | static inline vector operator-(vector a) { |
| 252 | a.x *= -1; |
| 253 | a.y *= -1; |
| 254 | a.z *= -1; |
| 255 | |
| 256 | return a; |
| 257 | } |
| 258 | |
| 259 | // Apply a matrix to a vector |
| 260 | static inline vector operator*(vector v, matrix m) { |
| 261 | vector result; |
| 262 | |
| 263 | result.x = v * m.rvec; |
| 264 | result.y = v * m.uvec; |
| 265 | result.z = v * m.fvec; |
| 266 | |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | static inline float vm_Dot3Vector(float x, float y, float z, vector *v) { return (x * v->x) + (y * v->y) + (z * v->z); } |
| 271 | |
| 272 | #define vm_GetSurfaceNormal vm_GetNormal |
| 273 | |
| 274 | #endif |