Debian Codegraph @

common/3d/points.cpp

raw
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 * Routines for point definition, rotation, etc.
10 *
11 */
12
13
14 #include "3d.h"
15 #include "globvars.h"
16
17 namespace dcx {
18
19 clipping_code build_g3_clipping_code_from_viewer_relative_position(const vms_vector &viewer_relative_position)
20 {
21 clipping_code cc{};
22
23 if (viewer_relative_position.x > viewer_relative_position.z)
24 cc |= clipping_code::off_right;
25
26 if (viewer_relative_position.y > viewer_relative_position.z)
27 cc |= clipping_code::off_top;
28
29 if (viewer_relative_position.x < -viewer_relative_position.z)
30 cc |= clipping_code::off_left;
31
32 if (viewer_relative_position.y < -viewer_relative_position.z)
33 cc |= clipping_code::off_bot;
34
35 if (viewer_relative_position.z < 0)
36 cc |= clipping_code::behind;
37 return cc;
38 }
39
40 clipping_code g3_code_point(g3s_point &p)
41 {
42 return p.p3_codes = build_g3_clipping_code_from_viewer_relative_position(p.p3_vec);
43 }
44
45 //rotates a point. returns codes. does not check if already rotated
46 clipping_code g3_rotate_point(g3s_point &dest,const vms_vector &src)
47 {
48 vm_vec_rotate(dest.p3_vec, vm_vec_build_sub(src, View_position), View_matrix);
49 dest.p3_flags = {}; //no projected
50 return g3_code_point(dest);
51 }
52
53 /* Multiply `a` and `b` into a 64-bit result. Check whether ((a * b) / c) will
54 * overflow when stored into a 32-bit signed integer. If the quotient does not
55 * overflow a 32-bit value, then return a std::optional that contains the
56 * quotient. If the quotient does overflow, then return a std::optional that
57 * does not contain a value.
58 */
59 std::optional<int32_t> checkmuldiv(const fix a, const fix b, const fix c)
60 {
61 /* product will be negative if and only if the sign bits of the input
62 * values require it. Storing the result in a 64-bit value ensures that
63 * overflow cannot occur, and so the sign bit cannot be incorrectly set as
64 * a side effect of overflow.
65 */
66 const int64_t product{int64_t{a} * int64_t{b}};
67 /* absolute_product will be positive, because the only negative number that
68 * remains negative after negation is too large to be produced by the
69 * multiplication of 2 32-bit signed inputs.
70 */
71 const auto absolute_product{(product < 0) ? -product : product};
72 if ((absolute_product >> 31) >= c)
73 /* If this branch is taken, then the division would produce a value
74 * that cannot be correctly represented in `int32_t`. Return a failure
75 * code, rather than returning an incorrect result. This case is
76 * tested explicitly, rather than the clearer construct of:
77
78 const auto result = q.q / static_cast<int64_t>(c);
79 if (static_cast<int32_t>(result) != result)
80 return std::nullopt;
81
82 * because that would always perform the division, before determining
83 * whether the division is valid.
84 */
85 return std::nullopt;
86 else {
87 /* According to language rules, this is a narrowing conversion.
88 * However, runtime logic above ensured that this path is only reached
89 * if the value is not changed by the narrowing conversion.
90 */
91 return static_cast<int32_t>(product / int64_t{c});
92 }
93 }
94
95 g3_projected_point g3_projected_point::build(const g3_rotated_point &rp)
96 {
97 g3_projected_point result;
98 result.p3_codes = build_g3_clipping_code_from_viewer_relative_position(rp.p3_vec);
99 if ((result.p3_codes & clipping_code::behind) != clipping_code::None)
100 /* If the point is behind the viewer, then skip computing any details
101 * about its position. It is invisible, so its exact position does not
102 * matter.
103 *
104 * This saves the time that would be spent initializing them, and has
105 * the useful side effect that the values are left uninitialized, which
106 * may cause debugging tools to report any inappropriate accesses.
107 */
108 return result;
109 const auto pz{rp.p3_vec.z};
110 if (const auto otx{checkmuldiv(rp.p3_vec.x, Canv_w2, pz)}, oty{checkmuldiv(rp.p3_vec.y, Canv_h2, pz)}; otx && oty)
111 {
112 result.p3_sx = Canv_w2 + *otx;
113 result.p3_sy = Canv_h2 - *oty;
114 result.p3_flags = projection_flag::projected;
115 }
116 else
117 result.p3_flags = projection_flag::overflow;
118 return result;
119 }
120
121 g3_rotated_point::g3_rotated_point(const g3_instance_context &viewer, const vms_vector &absolute_position) :
122 p3_vec{vm_vec_build_rotated(vm_vec_build_sub(absolute_position, viewer.position), viewer.matrix)}
123 {
124 }
125
126 g3_projected_point::g3_projected_point(const g3_instance_context &viewer, const vms_vector &absolute_position) :
127 g3_projected_point{g3_rotated_point{viewer, absolute_position}}
128 {
129 }
130
131 g3_projected_point::g3_projected_point(const g3_rotated_point &p) :
132 g3_projected_point{build(p)}
133 {
134 }
135
136 g3s_point::g3s_point(const g3_rotated_point &p) :
137 g3_rotated_point{p},
138 g3_projected_point{p}
139 {
140 }
141
142 g3s_point::g3s_point(const g3_instance_context &viewer, const vms_vector &absolute_position) :
143 g3_rotated_point{viewer, absolute_position},
144 g3_projected_point{static_cast<g3_rotated_point &>(*this)}
145 {
146 }
147
148 //projects a point
149 void g3_project_point(g3s_point &p)
150 {
151 #ifndef __powerc
152 if ((p.p3_flags & projection_flag::projected) || (p.p3_codes & clipping_code::behind) != clipping_code::None)
153 return;
154
155 const auto pz = p.p3_vec.z;
156 const auto otx = checkmuldiv(p.p3_vec.x, Canv_w2, pz);
157 std::optional<int32_t> oty;
158 if (otx && (oty = checkmuldiv(p.p3_vec.y, Canv_h2, pz)))
159 {
160 p.p3_sx = Canv_w2 + *otx;
161 p.p3_sy = Canv_h2 - *oty;
162 p.p3_flags |= projection_flag::projected;
163 }
164 else
165 p.p3_flags |= projection_flag::overflow;
166 #else
167 double fz;
168
169 if ((p.p3_flags & projection_flag::projected) || (p.p3_codes & clipping_code::behind) != clipping_code::None)
170 return;
171
172 if ( p.p3_vec.z <= 0 ) {
173 p.p3_flags |= projection_flag::overflow;
174 return;
175 }
176
177 fz = f2fl(p.p3_vec.z);
178 p.p3_sx = fl2f(fCanv_w2 + (f2fl(p.p3_vec.x)*fCanv_w2 / fz));
179 p.p3_sy = fl2f(fCanv_h2 - (f2fl(p.p3_vec.y)*fCanv_h2 / fz));
180
181 p.p3_flags |= projection_flag::projected;
182 #endif
183 }
184
185 #if DXX_USE_EDITOR
186 //from a 2d point, compute the vector through that point
187 vms_vector g3_point_2_vec(short sx,short sy)
188 {
189 return vm_vec_build_rotated(vm_vec_normalized(
190 vms_vector{
191 .x = fixmuldiv(fixdiv((sx << 16) - Canv_w2, Canv_w2), Matrix_scale.z, Matrix_scale.x),
192 .y = -fixmuldiv(fixdiv((sy << 16) - Canv_h2, Canv_h2), Matrix_scale.z, Matrix_scale.y),
193 .z = f1_0
194 }
195 ),
196 vm_transposed_matrix(Unscaled_matrix));
197 }
198 #endif
199
200 vms_vector g3_rotate_delta_vec(const vms_vector &src)
201 {
202 return vm_vec_build_rotated(src, View_matrix);
203 }
204
205 void g3_add_delta_vec(g3s_point &dest, const g3_rotated_point &src, const vms_vector &deltav)
206 {
207 vm_vec_add(dest.p3_vec,src.p3_vec,deltav);
208 dest.p3_flags = {}; //not projected
209 g3_code_point(dest);
210 }
211
212 //calculate the depth of a point - returns the z coord of the rotated point
213 fix g3_calc_point_depth(const vms_vector &pnt)
214 {
215 const auto q0{fixmulaccum({}, pnt.x - View_position.x, View_matrix.fvec.x)};
216 const auto q1{fixmulaccum(q0, pnt.y - View_position.y, View_matrix.fvec.y)};
217 const auto q2{fixmulaccum(q1, pnt.z - View_position.z, View_matrix.fvec.z)};
218 return fixquadadjust(q2);
219 }
220
221 }