Debian Codegraph @

common/3d/draw.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 * Drawing routines
10 *
11 */
12
13
14 #include "dxxerror.h"
15
16 #include "3d.h"
17 #include "globvars.h"
18 #include "texmap.h"
19 #if !DXX_USE_OGL
20 #include "clipper.h"
21 #include "gr.h"
22 #endif
23 #include "d_enumerate.h"
24 #include "d_zip.h"
25 #include "partial_range.h"
26
27 namespace dcx {
28
29 #if DXX_USE_OGL
30 namespace {
31
32 const std::array<GLfloat, 8> build_color_array_from_color_palette_index(const color_palette_index color)
33 {
34 auto &&rgb{PAL2T(color)};
35 const GLfloat color_r{rgb.r / 63.0f};
36 const GLfloat color_g{rgb.g / 63.0f};
37 const GLfloat color_b{rgb.b / 63.0f};
38 return {{
39 color_r, color_g, color_b, 1.0,
40 color_r, color_g, color_b, 1.0,
41 }};
42 }
43
44 }
45
46 g3_draw_line_colors::g3_draw_line_colors(const color_palette_index color) :
47 color_array{build_color_array_from_color_palette_index(color)}
48 {
49 }
50 #else
51 namespace {
52
53 //deal with a clipped line
54 static void must_clip_line(const g3_draw_line_context &context, g3_draw_line_point *p0, g3_draw_line_point *p1, const clipping_code codes_or, temporary_points_t &tp)
55 {
56 if ((p0->p3_flags&projection_flag::temp_point) || (p1->p3_flags&projection_flag::temp_point))
57 ; //line has already been clipped, so give up
58 else {
59 clip_line(p0,p1,codes_or,tp);
60 g3_draw_line(context, *p0, *p1, tp);
61 }
62
63 //free temp points
64
65 if (p0->p3_flags & projection_flag::temp_point)
66 tp.free_temp_point(*p0);
67
68 if (p1->p3_flags & projection_flag::temp_point)
69 tp.free_temp_point(*p1);
70 }
71
72 }
73
74 //draws a line. takes two points. returns true if drew
75 void g3_draw_line(const g3_draw_line_context &context, g3_draw_line_point &p0, g3_draw_line_point &p1)
76 {
77 temporary_points_t tp;
78 g3_draw_line(context, p0, p1, tp);
79 }
80
81 void g3_draw_line(const g3_draw_line_context &context, g3_draw_line_point &p0, g3_draw_line_point &p1, temporary_points_t &tp)
82 {
83 if ((p0.p3_codes & p1.p3_codes) != clipping_code::None)
84 return;
85
86 const clipping_code codes_or = p0.p3_codes | p1.p3_codes;
87 if (
88 (codes_or & clipping_code::behind) != clipping_code::None ||
89 (static_cast<void>((p0.p3_flags & projection_flag::projected) || (g3_project_point(p0), 0)), p0.p3_flags & projection_flag::overflow) ||
90 (static_cast<void>((p1.p3_flags & projection_flag::projected) || (g3_project_point(p1), 0)), p1.p3_flags & projection_flag::overflow)
91 )
92 return must_clip_line(context, &p0,&p1, codes_or, tp);
93 gr_line(context.canvas, p0.p3_sx, p0.p3_sy, p1.p3_sx, p1.p3_sy, context.color);
94 }
95 #endif
96
97 //returns true if a plane is facing the viewer. takes the unrotated surface
98 //normal of the plane, and a point on it. The normal need not be normalized
99 bool g3_check_normal_facing(const vms_vector &v,const vms_vector &norm)
100 {
101 return (vm_vec_build_dot(vm_vec_build_sub(View_position, v), norm) > 0);
102 }
103
104 bool do_facing_check(const std::array<g3_draw_tmap_point *, 3> &vertlist)
105 {
106 //normal not specified, so must compute
107 //get three points (rotated) and compute normal
108 const auto tempv{vm_vec_perp(vertlist[0]->p3_vec, vertlist[1]->p3_vec, vertlist[2]->p3_vec)};
109 return (vm_vec_build_dot(tempv,vertlist[1]->p3_vec) < 0);
110 }
111
112 #if !DXX_USE_OGL
113 namespace {
114
115 static void must_clip_tmap_face(grs_canvas &, std::size_t nv, g3s_codes cc, grs_bitmap &bm, polygon_clip_points &Vbuf0, polygon_clip_points &Vbuf1, tmap_drawer_type tmap_drawer_ptr);
116
117 //deal with face that must be clipped
118 static void must_clip_flat_face(grs_canvas &canvas, std::size_t nv, g3s_codes cc, polygon_clip_points &Vbuf0, polygon_clip_points &Vbuf1, const uint8_t color)
119 {
120 temporary_points_t tp;
121 auto &bufptr{clip_polygon(Vbuf0, Vbuf1, &nv, &cc, tp)};
122
123 if (nv > 0 && (cc.uor & clipping_code::behind) == clipping_code::None && cc.uand == clipping_code::None)
124 {
125 std::array<fix, MAX_POINTS_IN_POLY*2> Vertex_list;
126 for (int i=0;i<nv;i++) {
127 g3s_point *p = bufptr[i];
128
129 if (!(p->p3_flags&projection_flag::projected))
130 g3_project_point(*p);
131
132 if (p->p3_flags&projection_flag::overflow) {
133 goto free_points;
134 }
135
136 Vertex_list[i*2] = p->p3_sx;
137 Vertex_list[i*2+1] = p->p3_sy;
138 }
139 gr_upoly_tmap(canvas, nv, Vertex_list, color);
140 }
141 //free temp points
142 free_points:
143 ;
144 }
145
146 }
147
148 //draw a flat-shaded face.
149 //returns 1 if off screen, 0 if drew
150 void _g3_draw_poly(grs_canvas &canvas, const std::span<g3_draw_tmap_point *const> pointlist, const uint8_t color)
151 {
152 g3s_codes cc;
153
154 polygon_clip_points Vbuf0, Vbuf1;
155 auto &bufptr{Vbuf0};
156
157 for (const auto &&[pl, bp] : zip(pointlist, bufptr))
158 {
159 bp = pl;
160 cc.uand &= bp->p3_codes;
161 cc.uor |= bp->p3_codes;
162 }
163
164 if (cc.uand != clipping_code::None)
165 return; //all points off screen
166
167 if (cc.uor != clipping_code::None)
168 {
169 must_clip_flat_face(canvas, pointlist.size(), cc, Vbuf0, Vbuf1, color);
170 return;
171 }
172
173 //now make list of 2d coords (& check for overflow)
174 std::array<fix, MAX_POINTS_IN_POLY*2> Vertex_list;
175 for (const auto &&[i, pl, p] : enumerate(zip(pointlist, bufptr)))
176 {
177 if (!(p->p3_flags&projection_flag::projected))
178 g3_project_point(*p);
179
180 if (p->p3_flags&projection_flag::overflow)
181 {
182 must_clip_flat_face(canvas, pointlist.size(), cc, Vbuf0, Vbuf1, color);
183 return;
184 }
185
186 Vertex_list[i*2] = p->p3_sx;
187 Vertex_list[i*2+1] = p->p3_sy;
188 }
189 gr_upoly_tmap(canvas, pointlist.size(), Vertex_list, color);
190 //say it drew
191 }
192
193 //draw a texture-mapped face.
194 void _g3_draw_tmap(grs_canvas &canvas, const std::span<g3_draw_tmap_point *const> pointlist, const g3s_uvl *const uvl_list, const g3s_lrgb *const light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr)
195 {
196 g3s_codes cc;
197
198 polygon_clip_points Vbuf0, Vbuf1;
199 auto &bufptr = Vbuf0;
200
201 for (auto &&[i, pl, bp] : enumerate(zip(pointlist, bufptr)))
202 {
203 const auto p = bp = pl;
204
205 cc.uand &= p->p3_codes;
206 cc.uor |= p->p3_codes;
207
208 if constexpr (!DXX_USE_OGL)
209 {
210 p->p3_u = uvl_list[i].u;
211 p->p3_v = uvl_list[i].v;
212 p->p3_l = (light_rgb[i].r+light_rgb[i].g+light_rgb[i].b)/3;
213 }
214
215 p->p3_flags |= projection_flag::uvs | projection_flag::ls;
216
217 }
218
219 if (cc.uand != clipping_code::None)
220 return; //all points off screen
221
222 if (cc.uor != clipping_code::None)
223 {
224 must_clip_tmap_face(canvas, pointlist.size(), cc, bm, Vbuf0, Vbuf1, tmap_drawer_ptr);
225 return;
226 }
227
228 //now make list of 2d coords (& check for overflow)
229
230 for (auto &&p : std::span(bufptr).first(pointlist.size()))
231 {
232 if (!(p->p3_flags&projection_flag::projected))
233 g3_project_point(*p);
234
235 if (p->p3_flags&projection_flag::overflow) {
236 Int3(); //should not overflow after clip
237 return;
238 }
239 }
240 (*tmap_drawer_ptr)(canvas, bm, std::span(bufptr).first(pointlist.size()));
241 }
242
243 namespace {
244
245 static void must_clip_tmap_face(grs_canvas &canvas, std::size_t nv, g3s_codes cc, grs_bitmap &bm, polygon_clip_points &Vbuf0, polygon_clip_points &Vbuf1, const tmap_drawer_type tmap_drawer_ptr)
246 {
247 temporary_points_t tp;
248 auto &bufptr{clip_polygon(Vbuf0, Vbuf1, &nv, &cc, tp)};
249 if (nv && (cc.uor & clipping_code::behind) == clipping_code::None && cc.uand == clipping_code::None)
250 {
251 for (int i=0;i<nv;i++) {
252 auto *const p{bufptr[i]};
253
254 if (!(p->p3_flags&projection_flag::projected))
255 g3_project_point(*p);
256 if (p->p3_flags&projection_flag::overflow) {
257 Int3(); //should not overflow after clip
258 goto free_points;
259 }
260 }
261
262 (*tmap_drawer_ptr)(canvas, bm, std::span(bufptr).first(nv));
263 }
264
265 free_points:
266 ;
267
268 // Assert(free_point_num==0);
269 }
270
271 }
272
273 //draw a sortof sphere - i.e., the 2d radius is proportional to the 3d
274 //radius, but not to the distance from the eye
275 void g3_draw_sphere(grs_canvas &canvas, g3_draw_sphere_point &pnt, const fix rad, const uint8_t color)
276 {
277 if ((pnt.p3_codes & clipping_code::behind) == clipping_code::None)
278 {
279 if (! (pnt.p3_flags & projection_flag::projected))
280 g3_project_point(pnt);
281
282 if (! (pnt.p3_flags & projection_flag::overflow)) {
283 const auto r2{fixmul(rad, Matrix_scale.x)};
284 #ifndef __powerc
285 const auto ot{checkmuldiv(r2, Canv_w2, pnt.p3_vec.z)};
286 if (!ot)
287 return;
288 const auto t{*ot};
289 #else
290 if (pnt.p3_vec.z == 0)
291 return;
292 const fix t = fl2f(((f2fl(r2) * fCanv_w2) / f2fl(pnt.p3_vec.z)));
293 #endif
294 gr_disk(canvas, pnt.p3_sx, pnt.p3_sy, t, color);
295 }
296 }
297 }
298 #endif
299
300 }