| 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 | struct InstanceContext { |
| 24 | matrix m_viewMatrix; // matrix |
| 25 | matrix m_unscaledMatrix; // unscaled matrix |
| 26 | vector m_viewPosition; // position |
| 27 | float m_modelView[4][4]; // model/view transform |
| 28 | }; |
| 29 | |
| 30 | #define MAX_INSTANCE_DEPTH 30 |
| 31 | static InstanceContext sInstanceStack[MAX_INSTANCE_DEPTH]; |
| 32 | static int sInstanceDepth = 0; |
| 33 | |
| 34 | // instance at specified point with specified orientation |
| 35 | void g3_StartInstanceMatrix(vector *pos, matrix *orient) { |
| 36 | ASSERT(orient != NULL); |
| 37 | ASSERT(sInstanceDepth < MAX_INSTANCE_DEPTH); |
| 38 | |
| 39 | sInstanceStack[sInstanceDepth].m_viewMatrix = View_matrix; |
| 40 | sInstanceStack[sInstanceDepth].m_viewPosition = View_position; |
| 41 | sInstanceStack[sInstanceDepth].m_unscaledMatrix = Unscaled_matrix; |
| 42 | memcpy(sInstanceStack[sInstanceDepth].m_modelView, gTransformModelView, sizeof(gTransformModelView)); |
| 43 | ++sInstanceDepth; |
| 44 | |
| 45 | // step 1: subtract object position from view position |
| 46 | vector tempv = View_position - *pos; |
| 47 | |
| 48 | // step 2: rotate view vector through object matrix |
| 49 | View_position = tempv * *orient; |
| 50 | |
| 51 | // step 3: rotate object matrix through view_matrix (vm = ob * vm) |
| 52 | matrix tempm, tempm2 = ~*orient; |
| 53 | |
| 54 | tempm = tempm2 * View_matrix; |
| 55 | View_matrix = tempm; |
| 56 | |
| 57 | tempm = tempm2 * Unscaled_matrix; |
| 58 | Unscaled_matrix = tempm; |
| 59 | |
| 60 | // transform the model/view matrix |
| 61 | g3_GetModelViewMatrix(&View_position, &Unscaled_matrix, (float *)gTransformModelView); |
| 62 | g3_UpdateFullTransform(); |
| 63 | } |
| 64 | |
| 65 | // instance at specified point with specified orientation |
| 66 | void g3_StartInstanceAngles(vector *pos, angvec *angles) { |
| 67 | if (angles == NULL) { |
| 68 | matrix ident; |
| 69 | vm_MakeIdentity(&ident); |
| 70 | g3_StartInstanceMatrix(pos, &ident); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | matrix tm; |
| 75 | vm_AnglesToMatrix(&tm, angles->p, angles->h, angles->b); |
| 76 | |
| 77 | g3_StartInstanceMatrix(pos, &tm); |
| 78 | } |
| 79 | |
| 80 | // pops the old context |
| 81 | void g3_DoneInstance() { |
| 82 | --sInstanceDepth; |
| 83 | ASSERT(sInstanceDepth >= 0); |
| 84 | |
| 85 | View_position = sInstanceStack[sInstanceDepth].m_viewPosition; |
| 86 | View_matrix = sInstanceStack[sInstanceDepth].m_viewMatrix; |
| 87 | Unscaled_matrix = sInstanceStack[sInstanceDepth].m_unscaledMatrix; |
| 88 | memcpy(gTransformModelView, sInstanceStack[sInstanceDepth].m_modelView, sizeof(gTransformModelView)); |
| 89 | g3_UpdateFullTransform(); |
| 90 | } |