| 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 <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include "3d.h"cxx . . $ `<file>/lib/3d.h`/ |
| 22 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 23 | #include "HardwareInternal.h"cxx . . $ `<file>/renderer/HardwareInternal.h`/ |
| 24 | #include "renderer.h"cxx . . $ `<file>/lib/renderer.h`/ |
| 25 | |
| 26 | // User-specified aspect ratio, stored as w/h |
| 27 | static float sAspectcxx . . $ sAspect. = 0.0f; |
| 28 | |
| 29 | // initialize the 3d system |
| 30 | void g3_Initcxx . . $ g3_Init(49f6e7a06ebc5aa8).(void) { atexitcxx . . $ atexit(cc8c495369588262).(g3_Closecxx . . $ g3_Close(49f6e7a06ebc5aa8).); } |
| 31 | |
| 32 | // close down the 3d system |
| 33 | void g3_Closecxx . . $ g3_Close(49f6e7a06ebc5aa8).(void) {} |
| 34 | |
| 35 | // allows the user to specify an aspect ratio that overrides the renderer's |
| 36 | // The parameter is the w/h of the screen pixels |
| 37 | void g3_SetAspectRatiocxx . . $ g3_SetAspectRatio(9364e6e7bff7660).(float aspect) { sAspectcxx . . $ sAspect. = aspect; } |
| 38 | // returns the user-specified aspect ratio used to override the renderer's |
| 39 | float g3_GetAspectRatiocxx . . $ g3_GetAspectRatio(9b8c9e39889c762c).() { return sAspectcxx . . $ sAspect.; } |
| 40 | |
| 41 | void g3_GetViewPortMatrixcxx . . $ g3_GetViewPortMatrix(e6881f009d13a851).(float *viewMat) { |
| 42 | // extract the viewport data from the renderer |
| 43 | int viewportWidth, viewportHeight; |
| 44 | int viewportX, viewportY; |
| 45 | rend_GetProjectionScreenParameterscxx . . $ rend_GetProjectionScreenParameters(a90a5d8ba88c22b1).(viewportX, viewportY, viewportWidth, viewportHeight); |
| 46 | |
| 47 | float viewportWidthOverTwo = ((float)viewportWidth) * 0.5f; |
| 48 | float viewportHeightOverTwo = ((float)viewportHeight) * 0.5f; |
| 49 | |
| 50 | // setup the matrix |
| 51 | memset(viewMat, 0, sizeof(float) * 16); |
| 52 | viewMat[0] = viewportWidthOverTwo; |
| 53 | viewMat[5] = -viewportHeightOverTwo; |
| 54 | viewMat[12] = viewportWidthOverTwo + (float)viewportX; |
| 55 | viewMat[13] = viewportHeightOverTwo + (float)viewportY; |
| 56 | viewMat[10] = viewMat[15] = 1.0f; |
| 57 | } |
| 58 | |
| 59 | void g3_GetProjectionMatrixcxx . . $ g3_GetProjectionMatrix(bc34794c2ac62b6).(float zoom, float *projMat) { |
| 60 | // get window size |
| 61 | int viewportWidth, viewportHeight; |
| 62 | rend_GetProjectionParameterscxx . . $ rend_GetProjectionParameters(ce8f7825f8908cfa).(&viewportWidth, &viewportHeight); |
| 63 | |
| 64 | // compute aspect ratio for this ViewPort |
| 65 | float screenAspect = rend_GetAspectRatiocxx . . $ rend_GetAspectRatio(9b8c9e39889c762c).(); |
| 66 | if (sAspectcxx . . $ sAspect. != 0.0f) { |
| 67 | // check for user override |
| 68 | screenAspect = screenAspect * 4.0f / 3.0f / sAspectcxx . . $ sAspect.; |
| 69 | } |
| 70 | float s = screenAspect * ((float)viewportWidth) / ((float)viewportHeight); |
| 71 | |
| 72 | // setup the matrix |
| 73 | memset(projMat, 0, sizeof(float) * 16); |
| 74 | |
| 75 | // calculate 1/tan(fov) |
| 76 | float oOT = 1.0f / zoom; |
| 77 | |
| 78 | // fill in the matrix |
| 79 | projMat[0] = oOT; |
| 80 | projMat[5] = oOT * s; |
| 81 | projMat[10] = 1.0f; |
| 82 | projMat[11] = 1.0f; |
| 83 | projMat[14] = -1.0f; |
| 84 | } |
| 85 | |
| 86 | // start the frame |
| 87 | void g3_StartFramecxx . . $ g3_StartFrame(e35d136ceab5c8a2).(vectorcxx . . $ vector# *view_pos, matrixcxx . . $ matrix# *view_matrix, float zoom) { |
| 88 | // initialize the viewport transform |
| 89 | g3_GetViewPortMatrixcxx . . $ g3_GetViewPortMatrix(e6881f009d13a851).((float *)gTransformViewPortcxx . . $ gTransformViewPort.); |
| 90 | g3_GetProjectionMatrixcxx . . $ g3_GetProjectionMatrix(bc34794c2ac62b6).(zoom, (float *)gTransformProjectioncxx . . $ gTransformProjection.); |
| 91 | g3_GetModelViewMatrixcxx . . $ g3_GetModelViewMatrix(f698744c99c0df38).(view_pos, view_matrix, (float *)gTransformModelViewcxx . . $ gTransformModelView.); |
| 92 | g3_UpdateFullTransformcxx . . $ g3_UpdateFullTransform(49f6e7a06ebc5aa8).(); |
| 93 | |
| 94 | // get window size |
| 95 | rend_GetProjectionParameterscxx . . $ rend_GetProjectionParameters(ce8f7825f8908cfa).(&Window_widthcxx . . $ Window_width., &Window_heightcxx . . $ Window_height.); |
| 96 | |
| 97 | // Set vars for projection |
| 98 | Window_w2cxx . . $ Window_w2. = ((float)Window_widthcxx . . $ Window_width.) * 0.5f; |
| 99 | Window_h2cxx . . $ Window_h2. = ((float)Window_heightcxx . . $ Window_height.) * 0.5f; |
| 100 | |
| 101 | // Compute aspect ratio for this window |
| 102 | float screen_aspect = rend_GetAspectRatiocxx . . $ rend_GetAspectRatio(9b8c9e39889c762c).(); |
| 103 | if (sAspectcxx . . $ sAspect. != 0.0f) { |
| 104 | // check for user override |
| 105 | screen_aspect = screen_aspect * 4.0f / 3.0f / sAspectcxx . . $ sAspect.; |
| 106 | } |
| 107 | float s = screen_aspect * (float)Window_heightcxx . . $ Window_height. / (float)Window_widthcxx . . $ Window_width.; |
| 108 | |
| 109 | if (s <= 0.0f) // JEFF: Should this have been 1.0f? |
| 110 | { |
| 111 | // scale x |
| 112 | Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x. = s; |
| 113 | Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y. = 1.0f; |
| 114 | } else { |
| 115 | Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y. = 1.0f / s; |
| 116 | Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x. = 1.0f; |
| 117 | } |
| 118 | |
| 119 | Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z. = 1.0f; |
| 120 | |
| 121 | // Set the view variables |
| 122 | View_positioncxx . . $ View_position. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *view_pos; |
| 123 | View_zoomcxx . . $ View_zoom. = zoom; |
| 124 | Unscaled_matrixcxx . . $ Unscaled_matrix. =cxx . . $ matrix#`operator=`(4c6f8b29806742). *view_matrix; |
| 125 | |
| 126 | // Compute matrix scale for zoom and aspect ratio |
| 127 | if (View_zoomcxx . . $ View_zoom. <= 1.0f) { |
| 128 | // zoom in by scaling z |
| 129 | Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z. = Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z. * View_zoomcxx . . $ View_zoom.; |
| 130 | } else { |
| 131 | // zoom out by scaling x and y |
| 132 | float oOZ = 1.0f / View_zoomcxx . . $ View_zoom.; |
| 133 | Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x. = Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x. * oOZ; |
| 134 | Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y. = Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y. * oOZ; |
| 135 | } |
| 136 | |
| 137 | // Scale the matrix elements |
| 138 | View_matrixcxx . . $ View_matrix..rveccxx . . $ matrix#rvec. =cxx . . $ vector#`operator=`(c0842353a20857d). Unscaled_matrixcxx . . $ Unscaled_matrix..rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). Matrix_scalecxx . . $ Matrix_scale..xcxx . . $ vector#x.; |
| 139 | View_matrixcxx . . $ View_matrix..uveccxx . . $ matrix#uvec. =cxx . . $ vector#`operator=`(c0842353a20857d). Unscaled_matrixcxx . . $ Unscaled_matrix..uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). Matrix_scalecxx . . $ Matrix_scale..ycxx . . $ vector#y.; |
| 140 | View_matrixcxx . . $ View_matrix..fveccxx . . $ matrix#fvec. =cxx . . $ vector#`operator=`(c0842353a20857d). Unscaled_matrixcxx . . $ Unscaled_matrix..fveccxx . . $ matrix#fvec. *cxx . . $ `operator*`(252247219a31502c). Matrix_scalecxx . . $ Matrix_scale..zcxx . . $ vector#z.; |
| 141 | |
| 142 | // Reset the list of free points |
| 143 | InitFreePointscxx . . $ InitFreePoints(49f6e7a06ebc5aa8).(); |
| 144 | |
| 145 | // Reset the far clip plane |
| 146 | g3_ResetFarClipZcxx . . $ g3_ResetFarClipZ(49f6e7a06ebc5aa8).(); |
| 147 | } |
| 148 | |
| 149 | // this doesn't do anything, but is here for completeness |
| 150 | void g3_EndFramecxx . . $ g3_EndFrame(49f6e7a06ebc5aa8).(void) { |
| 151 | // make sure temp points are free |
| 152 | CheckTempPointscxx . . $ CheckTempPoints(49f6e7a06ebc5aa8).(); |
| 153 | } |
| 154 | |
| 155 | // get the current view position |
| 156 | void g3_GetViewPositioncxx . . $ g3_GetViewPosition(ec8cf6451c36d470).(vectorcxx . . $ vector# *vp) { *vp =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). View_positioncxx . . $ View_position.; } |
| 157 | |
| 158 | void g3_GetViewMatrixcxx . . $ g3_GetViewMatrix(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *mat) { *mat =cxx . . $ matrix#`operator=`(4c6f8b29806742). View_matrixcxx . . $ View_matrix.; } |
| 159 | |
| 160 | void g3_GetUnscaledMatrixcxx . . $ g3_GetUnscaledMatrix(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *mat) { *mat =cxx . . $ matrix#`operator=`(4c6f8b29806742). Unscaled_matrixcxx . . $ Unscaled_matrix.; } |
| 161 | |
| 162 | // Gets the matrix scale vector |
| 163 | void g3_GetMatrixScalecxx . . $ g3_GetMatrixScale(ec8cf6451c36d470).(vectorcxx . . $ vector# *matrix_scale) { *matrix_scale =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Matrix_scalecxx . . $ Matrix_scale.; } |