| 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 "pserror.h" |
| 21 | #include "HardwareInternal.h" |
| 22 | #include "renderer.h" |
| 23 | #include <string.h> |
| 24 | |
| 25 | // Whether or not to use T&L transforms or the pass-thru ones |
| 26 | static int sUseTransformPassthru = -1; |
| 27 | |
| 28 | extern float Z_bias; |
| 29 | void g3_GetModelViewMatrix(const vector *viewPos, const matrix *viewMatrix, float *mvMat) { |
| 30 | matrix localOrient = (*viewMatrix); |
| 31 | vector localPos = -(*viewPos); |
| 32 | mvMat[0] = localOrient.rvec.x; |
| 33 | mvMat[1] = localOrient.uvec.x; |
| 34 | mvMat[2] = localOrient.fvec.x; |
| 35 | mvMat[3] = 0.0f; |
| 36 | mvMat[4] = localOrient.rvec.y; |
| 37 | mvMat[5] = localOrient.uvec.y; |
| 38 | mvMat[6] = localOrient.fvec.y; |
| 39 | mvMat[7] = 0.0f; |
| 40 | mvMat[8] = localOrient.rvec.z; |
| 41 | mvMat[9] = localOrient.uvec.z; |
| 42 | mvMat[10] = localOrient.fvec.z; |
| 43 | mvMat[11] = 0.0f; |
| 44 | mvMat[12] = localPos * localOrient.rvec; |
| 45 | mvMat[13] = localPos * localOrient.uvec; |
| 46 | mvMat[14] = localPos * localOrient.fvec + Z_bias; |
| 47 | mvMat[15] = 1.0f; |
| 48 | } |
| 49 | |
| 50 | void g3_TransformVert(float res[4], float pt[4], float a[4][4]) { |
| 51 | int y; |
| 52 | for (y = 0; y < 4; ++y) { |
| 53 | res[y] = (pt[0] * a[0][y]) + (pt[1] * a[1][y]) + (pt[2] * a[2][y]) + (pt[3] * a[3][y]); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void g3_TransformMult(float res[4][4], float a[4][4], float b[4][4]) { |
| 58 | float temp[4][4]; |
| 59 | |
| 60 | int x, y; |
| 61 | for (y = 0; y < 4; ++y) { |
| 62 | for (x = 0; x < 4; ++x) { |
| 63 | temp[y][x] = (a[y][0] * b[0][x]) + (a[y][1] * b[1][x]) + (a[y][2] * b[2][x]) + (a[y][3] * b[3][x]); |
| 64 | } |
| 65 | } |
| 66 | memcpy(res, temp, 16 * sizeof(float)); |
| 67 | } |
| 68 | |
| 69 | void g3_TransformTrans(float res[4][4], float t[4][4]) { |
| 70 | float temp[4][4]; |
| 71 | int y; |
| 72 | for (y = 0; y < 4; ++y) { |
| 73 | int x; |
| 74 | for (x = 0; x < 4; ++x) { |
| 75 | temp[x][y] = t[y][x]; |
| 76 | } |
| 77 | } |
| 78 | memcpy(res, temp, 16 * sizeof(float)); |
| 79 | } |
| 80 | |
| 81 | void g3_UpdateFullTransform() { |
| 82 | // ModelView -> projection |
| 83 | g3_TransformMult(gTransformFull, gTransformModelView, gTransformProjection); |
| 84 | |
| 85 | // projection -> ViewPort |
| 86 | g3_TransformMult(gTransformFull, gTransformFull, gTransformViewPort); |
| 87 | } |
| 88 | |
| 89 | void g3_ForceTransformRefresh(void) { sUseTransformPassthru = -1; } |
| 90 | |
| 91 | void g3_RefreshTransforms(bool usePassthru) { |
| 92 | if (sUseTransformPassthru == 1 && usePassthru) { |
| 93 | // we don't have to do anything because we are already setup for pass-thru |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (usePassthru) { |
| 98 | // setup OpenGL to use pass-thru |
| 99 | rend_TransformSetToPassthru(); |
| 100 | } else { |
| 101 | // extract the viewport data from the renderer |
| 102 | int viewportWidth, viewportHeight, viewportX, viewportY; |
| 103 | rend_GetProjectionScreenParameters(viewportX, viewportY, viewportWidth, viewportHeight); |
| 104 | |
| 105 | // setup OpenGL to use full transform stack |
| 106 | // TODO: in the future we only need to set those that have changed |
| 107 | rend_TransformSetViewport(viewportX, viewportY, viewportWidth, viewportHeight); |
| 108 | rend_TransformSetProjection(gTransformProjection); |
| 109 | rend_TransformSetModelView(gTransformModelView); |
| 110 | } |
| 111 | |
| 112 | // store the pass-thru |
| 113 | sUseTransformPassthru = (usePassthru) ? 1 : 0; |
| 114 | } |