Debian Codegraph @

common/3d/rod.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 * Rod routines
10 *
11 */
12
13
14 #include "3d.h"
15 #include "globvars.h"
16 #include "maths.h"
17 #if !DXX_USE_OGL
18 #include "gr.h"
19 #endif
20
21 namespace dcx {
22
23 #if !DXX_USE_OGL
24 g3_projected_point::g3_projected_point(const vms_vector &relative_position, g3_rotated_point::from_relative_position) :
25 p3_codes{build_g3_clipping_code_from_viewer_relative_position(relative_position)},
26 p3_flags{}
27 {
28 }
29
30 g3s_point::g3s_point(const vms_vector &relative_position, from_relative_position rel) :
31 g3_rotated_point{relative_position, rel},
32 g3_projected_point{relative_position, rel}
33 {
34 }
35 #endif
36
37 namespace {
38
39 struct rod_corners_result
40 {
41 #if DXX_USE_OGL
42 using g3_rod_corner_point = g3_rotated_point;
43 #else
44 using g3_rod_corner_point = g3s_point;
45 #endif
46 std::array<g3_rod_corner_point, 4> points;
47 clipping_code cc;
48 static clipping_code build_clipping_code(const std::array<g3_rod_corner_point, 4> &points);
49 rod_corners_result(const g3_rotated_point &top_point, const vms_vector &scaled_rod_top_width, const g3_rotated_point &bot_point, const vms_vector &scaled_rod_bot_width) :
50 points{{
51 g3_rod_corner_point{vm_vec_build_add(top_point.p3_vec, scaled_rod_top_width), g3_rotated_point::from_relative_position{}},
52 g3_rod_corner_point{vm_vec_build_sub(top_point.p3_vec, scaled_rod_top_width), g3_rotated_point::from_relative_position{}},
53 g3_rod_corner_point{vm_vec_build_sub(bot_point.p3_vec, scaled_rod_bot_width), g3_rotated_point::from_relative_position{}},
54 g3_rod_corner_point{vm_vec_build_add(bot_point.p3_vec, scaled_rod_bot_width), g3_rotated_point::from_relative_position{}}
55 }},
56 cc{build_clipping_code(points)}
57 {
58 }
59 };
60
61 clipping_code rod_corners_result::build_clipping_code(const std::array<g3_rod_corner_point, 4> &points)
62 {
63 clipping_code codes_and{0xff};
64 for (auto &i : points)
65 codes_and &= (
66 #if DXX_USE_OGL
67 /* In the OpenGL build, the per-point code is not computed above
68 * because `g3_rotated_point` does not need it and has nowhere to
69 * store it. Compute it now, and use it immediately.
70 */
71 build_g3_clipping_code_from_viewer_relative_position(i.p3_vec)
72 #else
73 /* In the SDL-only build, the per-point clipping_code is computed
74 * as part of initializing the individual `g3s_point` instances.
75 * Read it here and use it to update the composite clipping code.
76 */
77 i.p3_codes
78 #endif
79 );
80 return codes_and;
81 }
82
83 //compute the corners of a rod. fills in vertbuf.
84 static rod_corners_result calc_rod_corners(const g3_rotated_point &bot_point, const fix bot_width, const g3_rotated_point &top_point, const fix top_width)
85 {
86 //compute vector from one point to other, do cross product with vector
87 //from eye to get perpendiclar
88
89 auto delta_vec{vm_vec_build_sub(bot_point.p3_vec, top_point.p3_vec)};
90
91 //unscale for aspect
92
93 delta_vec.x = fixdiv(delta_vec.x,Matrix_scale.x);
94 delta_vec.y = fixdiv(delta_vec.y,Matrix_scale.y);
95
96 //calc perp vector
97
98 //do lots of normalizing to prevent overflowing. When this code works,
99 //it should be optimized
100
101 vm_vec_normalize(delta_vec);
102
103 const auto top = vm_vec_normalized(top_point.p3_vec);
104
105 auto rod_norm{vm_vec_cross(delta_vec, top)};
106
107 vm_vec_normalize(rod_norm);
108
109 //scale for aspect
110
111 rod_norm.x = fixmul(rod_norm.x,Matrix_scale.x);
112 rod_norm.y = fixmul(rod_norm.y,Matrix_scale.y);
113 rod_norm.z = {};
114 return rod_corners_result{top_point, vm_vec_copy_scale(rod_norm, top_width), bot_point, vm_vec_copy_scale(rod_norm, bot_width)};
115 }
116
117 }
118
119 //draw a bitmap object that is always facing you
120 void g3_draw_rod_tmap(grs_canvas &canvas, grs_bitmap &bitmap, const g3_rotated_point &bot_point, fix bot_width, const g3_rotated_point &top_point, fix top_width, g3s_lrgb light, const tmap_drawer_type tmap_drawer_ptr)
121 {
122 rod_corners_result rod{calc_rod_corners(bot_point, bot_width, top_point, top_width)};
123 if (rod.cc != clipping_code::None)
124 return;
125
126 const fix average_light = static_cast<unsigned>(light.r+light.g+light.b)/3;
127 const std::array<g3s_uvl, 4> uvl_list{{
128 { 0x0200, 0x0200, average_light },
129 { 0xfe00, 0x0200, average_light },
130 { 0xfe00, 0xfe00, average_light },
131 { 0x0200, 0xfe00, average_light }
132 }};
133 const std::array<g3s_lrgb, 4> lrgb_list{{
134 light,
135 light,
136 light,
137 light,
138 }};
139
140 g3_draw_tmap(canvas, std::array<g3_draw_tmap_point *, 4>{{
141 &rod.points[0],
142 &rod.points[1],
143 &rod.points[2],
144 &rod.points[3],
145 }}, uvl_list, lrgb_list, bitmap, tmap_drawer_ptr);
146 }
147
148 #if !DXX_USE_OGL
149 //draws a bitmap with the specified 3d width & height
150 void g3_draw_bitmap(grs_canvas &canvas, const vms_vector &pos, fix width, fix height, grs_bitmap &bm)
151 {
152 g3s_point pnt;
153 fix w,h;
154 if ((g3_rotate_point(pnt, pos) & clipping_code::behind) != clipping_code::None)
155 return;
156 g3_project_point(pnt);
157 if (pnt.p3_flags & projection_flag::overflow)
158 return;
159 #ifndef __powerc
160 const auto pz = pnt.p3_vec.z;
161 if (const auto ox = checkmuldiv(width, Canv_w2, pz))
162 w = fixmul(*ox, Matrix_scale.x);
163 else
164 return;
165
166 if (const auto oy = checkmuldiv(height, Canv_h2, pz))
167 h = fixmul(*oy, Matrix_scale.y);
168 else
169 return;
170 #else
171 if (pnt.p3_vec.z == 0)
172 return;
173 double fz = f2fl(pnt.p3_vec.z);
174 w = fixmul(fl2f(((f2fl(width)*fCanv_w2) / fz)), Matrix_scale.x);
175 h = fixmul(fl2f(((f2fl(height)*fCanv_h2) / fz)), Matrix_scale.y);
176 #endif
177 const fix blob0y = pnt.p3_sy - h, blob1x = pnt.p3_sx + w;
178 const std::array<grs_point, 3> blob_vertices{{
179 {pnt.p3_sx - w, blob0y},
180 {blob1x, blob0y},
181 {blob1x, pnt.p3_sy + h},
182 }};
183 scale_bitmap(bm, blob_vertices, 0, canvas.cv_bitmap);
184 }
185 #endif
186
187 }