Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | /* |
| 2 | * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
||
| 3 | * It is copyright by its individual contributors, as recorded in the |
||
| 4 | * project's Git history. See COPYING.txt at the top level for license |
||
| 5 | * terms and a link to the Git history. |
||
| 6 | */ |
||
| 7 | /* |
||
| 8 | * |
||
| 9 | * Instancing routines |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | #include <stdlib.h> |
||
| 14 | #include "dxxerror.h" |
||
| 15 | |||
| 16 | #include "3d.h" |
||
| 17 | #include "globvars.h" |
||
| 18 | |||
| 19 | namespace dcx { |
||
| 20 | |||
| 21 | namespace { |
||
| 22 | |||
| 23 | struct instance_context { |
||
| 24 | vms_matrix m; |
||
| 25 | vms_vector p; |
||
| 26 | }; |
||
| 27 | |||
| 28 | } |
||
| 29 | |||
| 30 | static std::array<instance_context, 5> instance_stack; |
||
| 31 | |||
| 32 | int instance_depth = 0; |
||
| 33 | |||
| 34 | //instance at specified point with specified orientation |
||
| 35 | //if matrix==NULL, don't modify matrix. This will be like doing an offset |
||
| 36 | void g3_start_instance_matrix() |
||
| 37 | { |
||
| 38 | auto &s = instance_stack.at(instance_depth++); |
||
| 39 | s.m = View_matrix; |
||
| 40 | s.p = View_position; |
||
| 41 | } |
||
| 42 | |||
| 43 | void g3_start_instance_matrix(const vms_vector &pos, const vms_matrix &orient) |
||
| 44 | { |
||
| 45 | g3_start_instance_matrix(); |
||
| 46 | //step 1: subtract object position from view position |
||
| 47 | |||
| 48 | const auto tempv = vm_vec_sub(View_position, pos); |
||
| 49 | //step 2: rotate view vector through object matrix |
||
| 50 | |||
| 51 | vm_vec_rotate(View_position, tempv, orient); |
||
| 52 | |||
| 53 | //step 3: rotate object matrix through view_matrix (vm = ob * vm) |
||
| 54 | View_matrix = vm_matrix_x_matrix(vm_transposed_matrix(orient), View_matrix); |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | //instance at specified point with specified orientation |
||
| 59 | //if angles==NULL, don't modify matrix. This will be like doing an offset |
||
| 60 | void g3_start_instance_angles(const vms_vector &pos, const vms_angvec &angles) |
||
| 61 | { |
||
| 62 | const auto &&tm = vm_angles_2_matrix(angles); |
||
| 63 | g3_start_instance_matrix(pos, tm); |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | //pops the old context |
||
| 68 | void g3_done_instance() |
||
| 69 | { |
||
| 70 | instance_depth--; |
||
| 71 | |||
| 72 | Assert(instance_depth >= 0); |
||
| 73 | |||
| 74 | View_position = instance_stack[instance_depth].p; |
||
| 75 | View_matrix = instance_stack[instance_depth].m; |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | } |