| 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"cxx . . $ `<file>/lib/3d.h`/ |
| 20 | #include "HardwareInternal.h"cxx . . $ `<file>/renderer/HardwareInternal.h`/ |
| 21 | #include <string.h> |
| 22 | |
| 23 | extern vectorcxx . . $ vector# Clip_plane_pointcxx . . $ Clip_plane_point.; |
| 24 | // code a point. fills in the p3_codes field of the point, and returns the codes |
| 25 | uint8_tcxx . . $ uint8_t# g3_CodePointcxx . . $ g3_CodePoint(1c2cda054d3314c5).(g3Pointcxx . . $ g3Point# *p) { |
| 26 | uint8_tcxx . . $ uint8_t# cc = 0; |
| 27 | |
| 28 | if (p->p3_xcxx . . $ `lib/3d.h:245:9`! > p->p3_zcxx . . $ `lib/3d.h:247:9`!) |
| 29 | cc |= CC_OFF_RIGHTcxx . . $ `lib/3d.h:225:9`!; |
| 30 | |
| 31 | if (p->p3_ycxx . . $ `lib/3d.h:246:9`! > p->p3_zcxx . . $ `lib/3d.h:247:9`!) |
| 32 | cc |= CC_OFF_TOPcxx . . $ `lib/3d.h:227:9`!; |
| 33 | |
| 34 | if (p->p3_xcxx . . $ `lib/3d.h:245:9`! < -p->p3_zcxx . . $ `lib/3d.h:247:9`!) |
| 35 | cc |= CC_OFF_LEFTcxx . . $ `lib/3d.h:224:9`!; |
| 36 | |
| 37 | if (p->p3_ycxx . . $ `lib/3d.h:246:9`! < -p->p3_zcxx . . $ `lib/3d.h:247:9`!) |
| 38 | cc |= CC_OFF_BOTcxx . . $ `lib/3d.h:226:9`!; |
| 39 | |
| 40 | if (p->p3_zcxx . . $ `lib/3d.h:247:9`! < 0) |
| 41 | cc |= CC_BEHINDcxx . . $ `lib/3d.h:230:9`!; |
| 42 | |
| 43 | if (p->p3_zcxx . . $ `lib/3d.h:247:9`! > Far_clip_zcxx . . $ Far_clip_z.) |
| 44 | cc |= CC_OFF_FARcxx . . $ `lib/3d.h:228:9`!; |
| 45 | |
| 46 | // Check to see if we should be clipped to the custom plane |
| 47 | if (Clip_customcxx . . $ Clip_custom.) { |
| 48 | vectorcxx . . $ vector# vec = p->p3_veccxx . . $ g3Point#p3_vec. -cxx . . $ operator-(b77b703cd1e43e33). Clip_plane_pointcxx . . $ Clip_plane_point.; |
| 49 | vec.xcxx . . $ vector#x. /= Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x.; |
| 50 | vec.ycxx . . $ vector#y. /= Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y.; |
| 51 | vec.zcxx . . $ vector#z. /= Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z.; |
| 52 | |
| 53 | float dp = vec *cxx . . $ `operator*`(4a721543d64e126a). Clip_planecxx . . $ Clip_plane.; |
| 54 | if (dp < -0.005f) { |
| 55 | cc |= CC_OFF_CUSTOMcxx . . $ `lib/3d.h:229:9`!; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return p->p3_codescxx . . $ g3Point#p3_codes. = cc; |
| 60 | } |
| 61 | |
| 62 | // rotates a point. returns codes. does not check if already rotated |
| 63 | uint8_tcxx . . $ uint8_t# g3_RotatePointcxx . . $ g3_RotatePoint(fd2e8930c920e2b9).(g3Pointcxx . . $ g3Point# *dest, vectorcxx . . $ vector# *src) { |
| 64 | // store the pre-rotated point |
| 65 | dest->p3_vecPreRotcxx . . $ g3Point#p3_vecPreRot. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *src; |
| 66 | |
| 67 | // find the point offset from the view/camera position |
| 68 | vectorcxx . . $ vector# tempv = *src -cxx . . $ operator-(b77b703cd1e43e33). View_positioncxx . . $ View_position.; |
| 69 | |
| 70 | // rotate the point by the view/camera's orientation |
| 71 | dest->p3_veccxx . . $ g3Point#p3_vec. =cxx . . $ vector#`operator=`(c0842353a20857d). tempv *cxx . . $ `operator*`(14b8f9ac5c1e7b85). View_matrixcxx . . $ View_matrix.; |
| 72 | |
| 73 | // determine the flags for the point |
| 74 | dest->p3_flagscxx . . $ g3Point#p3_flags. = PF_ORIGPOINTcxx . . $ `lib/3d.h:221:9`!; |
| 75 | return g3_CodePointcxx . . $ g3_CodePoint(1c2cda054d3314c5).(dest); |
| 76 | } |
| 77 | |
| 78 | // projects a point |
| 79 | void g3_ProjectPointcxx . . $ g3_ProjectPoint(4c7ad40c64d4fb9c).(g3Pointcxx . . $ g3Point# *p) { |
| 80 | if (p->p3_flagscxx . . $ g3Point#p3_flags. & PF_PROJECTEDcxx . . $ `lib/3d.h:214:9`! || p->p3_codescxx . . $ g3Point#p3_codes. & CC_BEHINDcxx . . $ `lib/3d.h:230:9`!) |
| 81 | return; |
| 82 | |
| 83 | float one_over_z = 1.0 / p->p3_zcxx . . $ `lib/3d.h:247:9`!; |
| 84 | p->p3_sxcxx . . $ g3Point#p3_sx. = Window_w2cxx . . $ Window_w2. + (p->p3_xcxx . . $ `lib/3d.h:245:9`! * (Window_w2cxx . . $ Window_w2. * one_over_z)); |
| 85 | p->p3_sycxx . . $ g3Point#p3_sy. = Window_h2cxx . . $ Window_h2. - (p->p3_ycxx . . $ `lib/3d.h:246:9`! * (Window_h2cxx . . $ Window_h2. * one_over_z)); |
| 86 | p->p3_flagscxx . . $ g3Point#p3_flags. |= PF_PROJECTEDcxx . . $ `lib/3d.h:214:9`!; |
| 87 | } |
| 88 | |
| 89 | // from a 2d point, compute the vector through that point |
| 90 | void g3_Point2Veccxx . . $ g3_Point2Vec(a09ee21f9967de10).(vectorcxx . . $ vector# *v, int16_t sx, int16_t sy) { |
| 91 | vectorcxx . . $ vector# tempv; |
| 92 | matrixcxx . . $ matrix# tempm; |
| 93 | |
| 94 | tempv.xcxx . . $ vector#x. = (((sx - Window_w2cxx . . $ Window_w2.) / Window_w2cxx . . $ Window_w2.) * Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z. / Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x.); |
| 95 | tempv.ycxx . . $ vector#y. = -(((sy - Window_h2cxx . . $ Window_h2.) / Window_h2cxx . . $ Window_h2.) * Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z. / Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y.); |
| 96 | tempv.zcxx . . $ vector#z. = 1.0f; |
| 97 | |
| 98 | vm_NormalizeVectorcxx . . $ vm_NormalizeVector(4939a7e530383cd4).(&tempv); |
| 99 | |
| 100 | tempm =cxx . . $ matrix#`operator=`(e859ffc26b91394f). ~cxx . . $ `operator~`(1e820934589b6814).Unscaled_matrixcxx . . $ Unscaled_matrix.; |
| 101 | |
| 102 | *v =cxx . . $ vector#`operator=`(c0842353a20857d). tempv *cxx . . $ `operator*`(14b8f9ac5c1e7b85). tempm; |
| 103 | } |
| 104 | |
| 105 | // delta rotation functions |
| 106 | vectorcxx . . $ vector# *g3_RotateDeltaXcxx . . $ g3_RotateDeltaX(d723b5782943e8b1).(vectorcxx . . $ vector# *dest, float dx) { |
| 107 | dest->xcxx . . $ vector#x. = View_matrixcxx . . $ View_matrix..rveccxx . . $ matrix#rvec..xcxx . . $ vector#x. * dx; |
| 108 | dest->ycxx . . $ vector#y. = View_matrixcxx . . $ View_matrix..uveccxx . . $ matrix#uvec..xcxx . . $ vector#x. * dx; |
| 109 | dest->zcxx . . $ vector#z. = View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..xcxx . . $ vector#x. * dx; |
| 110 | |
| 111 | return dest; |
| 112 | } |
| 113 | |
| 114 | vectorcxx . . $ vector# *g3_RotateDeltaYcxx . . $ g3_RotateDeltaY(d723b5782943e8b1).(vectorcxx . . $ vector# *dest, float dy) { |
| 115 | dest->xcxx . . $ vector#x. = View_matrixcxx . . $ View_matrix..rveccxx . . $ matrix#rvec..ycxx . . $ vector#y. * dy; |
| 116 | dest->ycxx . . $ vector#y. = View_matrixcxx . . $ View_matrix..uveccxx . . $ matrix#uvec..ycxx . . $ vector#y. * dy; |
| 117 | dest->zcxx . . $ vector#z. = View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..ycxx . . $ vector#y. * dy; |
| 118 | |
| 119 | return dest; |
| 120 | } |
| 121 | |
| 122 | vectorcxx . . $ vector# *g3_RotateDeltaZcxx . . $ g3_RotateDeltaZ(d723b5782943e8b1).(vectorcxx . . $ vector# *dest, float dz) { |
| 123 | dest->xcxx . . $ vector#x. = View_matrixcxx . . $ View_matrix..rveccxx . . $ matrix#rvec..zcxx . . $ vector#z. * dz; |
| 124 | dest->ycxx . . $ vector#y. = View_matrixcxx . . $ View_matrix..uveccxx . . $ matrix#uvec..zcxx . . $ vector#z. * dz; |
| 125 | dest->zcxx . . $ vector#z. = View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..zcxx . . $ vector#z. * dz; |
| 126 | |
| 127 | return dest; |
| 128 | } |
| 129 | |
| 130 | vectorcxx . . $ vector# *g3_RotateDeltaVeccxx . . $ g3_RotateDeltaVec(b4974fe36fbbef6).(vectorcxx . . $ vector# *dest, vectorcxx . . $ vector# *src) { |
| 131 | *dest =cxx . . $ vector#`operator=`(c0842353a20857d). *src *cxx . . $ `operator*`(14b8f9ac5c1e7b85). View_matrixcxx . . $ View_matrix.; |
| 132 | |
| 133 | return dest; |
| 134 | } |
| 135 | |
| 136 | uint8_tcxx . . $ uint8_t# g3_AddDeltaVeccxx . . $ g3_AddDeltaVec(cfbaf9beee45bcf5).(g3Pointcxx . . $ g3Point# *dest, g3Pointcxx . . $ g3Point# *src, vectorcxx . . $ vector# *deltav) { |
| 137 | dest->p3_veccxx . . $ g3Point#p3_vec. =cxx . . $ vector#`operator=`(c0842353a20857d). src->p3_veccxx . . $ g3Point#p3_vec. +cxx . . $ operator+(b77b703cd1e43e33). *deltav; |
| 138 | |
| 139 | dest->p3_flagscxx . . $ g3Point#p3_flags. = 0; // not projected |
| 140 | |
| 141 | return g3_CodePointcxx . . $ g3_CodePoint(1c2cda054d3314c5).(dest); |
| 142 | } |
| 143 | |
| 144 | // calculate the depth of a point - returns the z coord of the rotated point |
| 145 | float g3_CalcPointDepthcxx . . $ g3_CalcPointDepth(4939a7e530383cd4).(vectorcxx . . $ vector# *pnt) { |
| 146 | return ((pnt->xcxx . . $ vector#x. - View_positioncxx . . $ View_position..xcxx . . $ vector#x.) * View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..xcxx . . $ vector#x.) + ((pnt->ycxx . . $ vector#y. - View_positioncxx . . $ View_position..ycxx . . $ vector#y.) * View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..ycxx . . $ vector#y.) + |
| 147 | ((pnt->zcxx . . $ vector#z. - View_positioncxx . . $ View_position..zcxx . . $ vector#z.) * View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec..zcxx . . $ vector#z.); |
| 148 | } |