| 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 | |
| 19 | #include "3d.h" |
| 20 | #include "HardwareInternal.h" |
| 21 | #include <string.h> |
| 22 | |
| 23 | extern vector Clip_plane_point; |
| 24 | // code a point. fills in the p3_codes field of the point, and returns the codes |
| 25 | uint8_t g3_CodePoint(g3Point *p) { |
| 26 | uint8_t cc = 0; |
| 27 | |
| 28 | if (p->p3_x > p->p3_z) |
| 29 | cc |= CC_OFF_RIGHT; |
| 30 | |
| 31 | if (p->p3_y > p->p3_z) |
| 32 | cc |= CC_OFF_TOP; |
| 33 | |
| 34 | if (p->p3_x < -p->p3_z) |
| 35 | cc |= CC_OFF_LEFT; |
| 36 | |
| 37 | if (p->p3_y < -p->p3_z) |
| 38 | cc |= CC_OFF_BOT; |
| 39 | |
| 40 | if (p->p3_z < 0) |
| 41 | cc |= CC_BEHIND; |
| 42 | |
| 43 | if (p->p3_z > Far_clip_z) |
| 44 | cc |= CC_OFF_FAR; |
| 45 | |
| 46 | // Check to see if we should be clipped to the custom plane |
| 47 | if (Clip_custom) { |
| 48 | vector vec = p->p3_vec - Clip_plane_point; |
| 49 | vec.x /= Matrix_scale.x; |
| 50 | vec.y /= Matrix_scale.y; |
| 51 | vec.z /= Matrix_scale.z; |
| 52 | |
| 53 | float dp = vec * Clip_plane; |
| 54 | if (dp < -0.005f) { |
| 55 | cc |= CC_OFF_CUSTOM; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return p->p3_codes = cc; |
| 60 | } |
| 61 | |
| 62 | // rotates a point. returns codes. does not check if already rotated |
| 63 | uint8_t g3_RotatePoint(g3Point *dest, vector *src) { |
| 64 | // store the pre-rotated point |
| 65 | dest->p3_vecPreRot = *src; |
| 66 | |
| 67 | // find the point offset from the view/camera position |
| 68 | vector tempv = *src - View_position; |
| 69 | |
| 70 | // rotate the point by the view/camera's orientation |
| 71 | dest->p3_vec = tempv * View_matrix; |
| 72 | |
| 73 | // determine the flags for the point |
| 74 | dest->p3_flags = PF_ORIGPOINT; |
| 75 | return g3_CodePoint(dest); |
| 76 | } |
| 77 | |
| 78 | // projects a point |
| 79 | void g3_ProjectPoint(g3Point *p) { |
| 80 | if (p->p3_flags & PF_PROJECTED || p->p3_codes & CC_BEHIND) |
| 81 | return; |
| 82 | |
| 83 | float one_over_z = 1.0 / p->p3_z; |
| 84 | p->p3_sx = Window_w2 + (p->p3_x * (Window_w2 * one_over_z)); |
| 85 | p->p3_sy = Window_h2 - (p->p3_y * (Window_h2 * one_over_z)); |
| 86 | p->p3_flags |= PF_PROJECTED; |
| 87 | } |
| 88 | |
| 89 | // from a 2d point, compute the vector through that point |
| 90 | void g3_Point2Vec(vector *v, int16_t sx, int16_t sy) { |
| 91 | vector tempv; |
| 92 | matrix tempm; |
| 93 | |
| 94 | tempv.x = (((sx - Window_w2) / Window_w2) * Matrix_scale.z / Matrix_scale.x); |
| 95 | tempv.y = -(((sy - Window_h2) / Window_h2) * Matrix_scale.z / Matrix_scale.y); |
| 96 | tempv.z = 1.0f; |
| 97 | |
| 98 | vm_NormalizeVector(&tempv); |
| 99 | |
| 100 | tempm = ~Unscaled_matrix; |
| 101 | |
| 102 | *v = tempv * tempm; |
| 103 | } |
| 104 | |
| 105 | // delta rotation functions |
| 106 | vector *g3_RotateDeltaX(vector *dest, float dx) { |
| 107 | dest->x = View_matrix.rvec.x * dx; |
| 108 | dest->y = View_matrix.uvec.x * dx; |
| 109 | dest->z = View_matrix.fvec.x * dx; |
| 110 | |
| 111 | return dest; |
| 112 | } |
| 113 | |
| 114 | vector *g3_RotateDeltaY(vector *dest, float dy) { |
| 115 | dest->x = View_matrix.rvec.y * dy; |
| 116 | dest->y = View_matrix.uvec.y * dy; |
| 117 | dest->z = View_matrix.fvec.y * dy; |
| 118 | |
| 119 | return dest; |
| 120 | } |
| 121 | |
| 122 | vector *g3_RotateDeltaZ(vector *dest, float dz) { |
| 123 | dest->x = View_matrix.rvec.z * dz; |
| 124 | dest->y = View_matrix.uvec.z * dz; |
| 125 | dest->z = View_matrix.fvec.z * dz; |
| 126 | |
| 127 | return dest; |
| 128 | } |
| 129 | |
| 130 | vector *g3_RotateDeltaVec(vector *dest, vector *src) { |
| 131 | *dest = *src * View_matrix; |
| 132 | |
| 133 | return dest; |
| 134 | } |
| 135 | |
| 136 | uint8_t g3_AddDeltaVec(g3Point *dest, g3Point *src, vector *deltav) { |
| 137 | dest->p3_vec = src->p3_vec + *deltav; |
| 138 | |
| 139 | dest->p3_flags = 0; // not projected |
| 140 | |
| 141 | return g3_CodePoint(dest); |
| 142 | } |
| 143 | |
| 144 | // calculate the depth of a point - returns the z coord of the rotated point |
| 145 | float g3_CalcPointDepth(vector *pnt) { |
| 146 | return ((pnt->x - View_position.x) * View_matrix.fvec.x) + ((pnt->y - View_position.y) * View_matrix.fvec.y) + |
| 147 | ((pnt->z - View_position.z) * View_matrix.fvec.z); |
| 148 | } |