Debian Codegraph @

renderer/HardwareClipper.cpp

raw symbols SCIP
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 "pserror.h"
20 #include "3d.h"
21 #include "HardwareInternal.h"
22
23 static int free_point_num = -1;
24 static g3Point temp_points[MAX_POINTS_IN_POLY];
25 static g3Point *free_points[MAX_POINTS_IN_POLY];
26 vector Clip_plane_point;
27
28 void InitFreePoints(void) {
29 int i;
30
31 for (i = 0; i < MAX_POINTS_IN_POLY; i++) {
32 free_points[i] = &temp_points[i];
33 }
34 free_point_num = 0;
35 }
36
37 g3Point *GetTempPoint(void) {
38 ASSERT(free_point_num < MAX_POINTS_IN_POLY);
39
40 g3Point *p = free_points[free_point_num++];
41 p->p3_flags = PF_TEMP_POINT;
42 return p;
43 }
44
45 void FreeTempPoint(g3Point *p) {
46 if (free_point_num < 1) {
47 mprintf(1, "clipper: no temp points to free\n");
48 free_point_num = 1;
49 Int3();
50 }
51 ASSERT(p->p3_flags & PF_TEMP_POINT);
52
53 free_points[--free_point_num] = p;
54
55 p->p3_flags &= ~PF_TEMP_POINT;
56 }
57
58 // Verify that all the temp points are free, and free them it they are not.
59 #ifdef _DEBUG
60 void CheckTempPoints(void) {
61 if (free_point_num) {
62 Int3();
63 InitFreePoints();
64 }
65 }
66 #endif
67
68 // Free up any temp points (created by the clipper) in the given pointlist
69 // Parameters: pointlist - pointer to list of pointers to points, returned by g3_ClipPolygon()
70 // nv - the number of points in pointlist
71 void g3_FreeTempPoints(g3Point **pointlist, int nv) {
72 // Go through list
73 int i;
74 for (i = 0; i < nv; i++) {
75 if (pointlist[i]->p3_flags & PF_TEMP_POINT) {
76 FreeTempPoint(pointlist[i]);
77 }
78 }
79
80 // Make sure all temp points are freed
81 CheckTempPoints();
82 }
83
84 // Clips an edge against the far plane
85 g3Point *ClipFarEdge(g3Point *on_pnt, g3Point *off_pnt) {
86 float z_on = on_pnt->p3_z;
87 float z_off = off_pnt->p3_z;
88
89 float k = 1.0f - ((z_off - Far_clip_z) / (z_off - z_on));
90
91 g3Point *tmp = GetTempPoint();
92 tmp->p3_z = on_pnt->p3_z + ((off_pnt->p3_z - on_pnt->p3_z) * k);
93 tmp->p3_x = on_pnt->p3_x + ((off_pnt->p3_x - on_pnt->p3_x) * k);
94 tmp->p3_y = on_pnt->p3_y + ((off_pnt->p3_y - on_pnt->p3_y) * k);
95
96 if (on_pnt->p3_flags & PF_UV) {
97 tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
98 tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);
99 tmp->p3_flags |= PF_UV;
100 }
101
102 if (on_pnt->p3_flags & PF_UV2) {
103 tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
104 tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);
105 tmp->p3_flags |= PF_UV2;
106 }
107
108 if (on_pnt->p3_flags & PF_L) {
109 tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
110 tmp->p3_flags |= PF_L;
111 }
112
113 if (on_pnt->p3_flags & PF_RGBA) {
114 tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
115 tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
116 tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
117 tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
118 tmp->p3_flags |= PF_RGBA;
119 }
120
121 g3_CodePoint(tmp);
122 return tmp;
123 }
124
125 // Clips an edge against the far plane
126 g3Point *ClipCustomEdge(g3Point *on_pnt, g3Point *off_pnt) {
127 g3Point *tmp = GetTempPoint();
128
129 vector ray_direction = off_pnt->p3_vec - on_pnt->p3_vec;
130 ray_direction.x /= Matrix_scale.x;
131 ray_direction.y /= Matrix_scale.y;
132 ray_direction.z /= Matrix_scale.z;
133
134 vector w = on_pnt->p3_vec - Clip_plane_point;
135 w.x /= Matrix_scale.x;
136 w.y /= Matrix_scale.y;
137 w.z /= Matrix_scale.z;
138
139 float k, den = -(Clip_plane * ray_direction);
140 if (den == 0.0f) {
141 k = 1.0f;
142 } else {
143 float num = Clip_plane * w;
144 k = num / den;
145 }
146
147 tmp->p3_vec = on_pnt->p3_vec + ((off_pnt->p3_vec - on_pnt->p3_vec) * k);
148
149 if (on_pnt->p3_flags & PF_UV) {
150 tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
151 tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);
152 tmp->p3_flags |= PF_UV;
153 }
154
155 if (on_pnt->p3_flags & PF_UV2) {
156 tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
157 tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);
158 tmp->p3_flags |= PF_UV2;
159 }
160
161 if (on_pnt->p3_flags & PF_L) {
162 tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
163 tmp->p3_flags |= PF_L;
164 }
165
166 if (on_pnt->p3_flags & PF_RGBA) {
167 tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
168 tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
169 tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
170 tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
171 tmp->p3_flags |= PF_RGBA;
172 }
173
174 g3_CodePoint(tmp);
175 return tmp;
176 }
177
178 // clips an edge against one plane.
179 g3Point *ClipEdge(int plane_flag, g3Point *on_pnt, g3Point *off_pnt) {
180 float a, b, k;
181 g3Point *tmp;
182
183 // compute clipping value k = (xs-zs) / (xs-xe-zs+ze)
184 // use x or y as appropriate, and negate x/y value as appropriate
185
186 if (plane_flag & CC_OFF_FAR) {
187 return ClipFarEdge(on_pnt, off_pnt);
188 }
189
190 if ((plane_flag & CC_OFF_CUSTOM) && Clip_custom) {
191 return ClipCustomEdge(on_pnt, off_pnt);
192 }
193
194 if (plane_flag & (CC_OFF_RIGHT | CC_OFF_LEFT)) {
195 a = on_pnt->p3_x;
196 b = off_pnt->p3_x;
197 } else {
198 a = on_pnt->p3_y;
199 b = off_pnt->p3_y;
200 }
201
202 if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
203 a = -a;
204 b = -b;
205 }
206
207 k = (a - on_pnt->p3_z) / ((a - on_pnt->p3_z) - b + off_pnt->p3_z); //(xs-zs) / (xs-zs-xe+ze)
208
209 tmp = GetTempPoint();
210
211 tmp->p3_x = on_pnt->p3_x + ((off_pnt->p3_x - on_pnt->p3_x) * k);
212 tmp->p3_y = on_pnt->p3_y + ((off_pnt->p3_y - on_pnt->p3_y) * k);
213
214 if (plane_flag & (CC_OFF_TOP | CC_OFF_BOT)) {
215 tmp->p3_z = tmp->p3_y;
216 } else {
217 tmp->p3_z = tmp->p3_x;
218 }
219
220 if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
221 tmp->p3_z = -tmp->p3_z;
222 }
223
224 if (on_pnt->p3_flags & PF_UV) {
225 tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
226 tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);
227
228 tmp->p3_flags |= PF_UV;
229 }
230
231 if (on_pnt->p3_flags & PF_UV2) {
232 tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
233 tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);
234
235 tmp->p3_flags |= PF_UV2;
236 }
237
238 if (on_pnt->p3_flags & PF_L) {
239 tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
240 tmp->p3_flags |= PF_L;
241 }
242
243 if (on_pnt->p3_flags & PF_RGBA) {
244 tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
245 tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
246 tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
247 tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
248 tmp->p3_flags |= PF_RGBA;
249 }
250
251 g3_CodePoint(tmp);
252
253 return tmp;
254 }
255
256 // clips a line to the viewing pyramid.
257 void ClipLine(g3Point **p0, g3Point **p1, uint8_t codes_or) {
258 int plane_flag;
259 g3Point *old_p1;
260
261 // might have these left over
262 //(*p0)->p3_flags &= ~(PF_UV|PF_L|PF_RGBA|PF_UV2);
263 //(*p1)->p3_flags &= ~(PF_UV|PF_L|PF_RGBA|PF_UV2);
264
265 for (plane_flag = 1; plane_flag <= 32; plane_flag <<= 1) {
266 if (codes_or & plane_flag) {
267 if ((*p0)->p3_codes & plane_flag) {
268 // swap!
269 g3Point *t = *p0;
270 *p0 = *p1;
271 *p1 = t;
272 }
273
274 old_p1 = *p1;
275
276 *p1 = ClipEdge(plane_flag, *p0, *p1);
277
278 codes_or = (*p0)->p3_codes | (*p1)->p3_codes; // get new codes
279
280 if (old_p1->p3_flags & PF_TEMP_POINT) {
281 FreeTempPoint(old_p1);
282 }
283 }
284 }
285 }
286
287 int ClipPlane(int plane_flag, g3Point **src, g3Point **dest, int *nv, g3Codes *cc) {
288 int i, prev, next;
289 g3Point **save_dest = dest;
290
291 // Init codes
292 cc->cc_and = 0xff;
293 cc->cc_or = 0;
294
295 for (i = 0, prev = *nv - 1, next = 1; i < *nv; i++) {
296 if (src[i]->p3_codes & plane_flag) {
297 // cur point off?
298 if (!(src[prev]->p3_codes & plane_flag)) {
299 // prev not off?
300 *dest = ClipEdge(plane_flag, src[prev], src[i]);
301 cc->cc_or |= (*dest)->p3_codes;
302 cc->cc_and &= (*dest)->p3_codes;
303 dest++;
304 }
305
306 if (!(src[next]->p3_codes & plane_flag)) {
307 *dest = ClipEdge(plane_flag, src[next], src[i]);
308 cc->cc_or |= (*dest)->p3_codes;
309 cc->cc_and &= (*dest)->p3_codes;
310 dest++;
311 }
312
313 // see if must free discarded point
314 if (src[i]->p3_flags & PF_TEMP_POINT) {
315 FreeTempPoint(src[i]);
316 }
317 } else {
318 // cur not off, copy to dest buffer
319 *dest++ = src[i];
320
321 cc->cc_or |= src[i]->p3_codes;
322 cc->cc_and &= src[i]->p3_codes;
323 }
324
325 prev = i;
326 if (++next == *nv) {
327 next = 0;
328 }
329 }
330
331 return (dest - save_dest);
332 }
333
334 // temp buffers for clipping
335 static g3Point *Vbuf0[MAX_POINTS_IN_POLY];
336 static g3Point *Vbuf1[MAX_POINTS_IN_POLY];
337
338 // Clips a polygon
339 // Parameters: pointlist - pointer to a list of pointers to points
340 // nv - the number of points in the polygon
341 // cc - the clip codes for this polygon
342 // Returns: a pointer to a list of pointer of points in the clipped polygon
343 // NOTE: You MUST call g3_FreeTempPoints() when you're done with the clipped polygon
344 g3Point **g3_ClipPolygon(g3Point **pointlist, int *nv, g3Codes *cc) {
345 int plane_flag;
346 g3Point **dest;
347
348 dest = Vbuf0;
349
350 //& ASSERT(free_point_num == 0); //DAJ UTB CMO
351 if (free_point_num != 0) {
352 mprintf(1, "clipper: leftover temp point\n");
353 free_point_num = 0;
354 Int3();
355 }
356
357 for (plane_flag = 1; plane_flag <= 32; plane_flag <<= 1) {
358 if (cc->cc_or & plane_flag) {
359
360 *nv = ClipPlane(plane_flag, pointlist, dest, nv, cc);
361
362 if (cc->cc_and) // clipped away
363 return dest;
364
365 pointlist = dest;
366 dest = (pointlist == Vbuf0) ? Vbuf1 : Vbuf0;
367 }
368 }
369 return pointlist; // we swapped after we copied
370 }
371
372 // sets the z distance of the far clipping plane
373 void g3_SetFarClipZ(float z) { Far_clip_z = z; }
374
375 // Sets up a custom clipping plane - g3_StartFrame must be called before this is called
376 void g3_SetCustomClipPlane(uint8_t state, vector *pnt, vector *normal) {
377 Clip_custom = state;
378 if (state) {
379 vector tempv;
380 vector norm = *normal;
381
382 tempv = *pnt - View_position;
383 Clip_plane_point = tempv * View_matrix;
384
385 Clip_plane = norm * Unscaled_matrix; // View_matrix;
386 vm_NormalizeVector(&Clip_plane);
387 }
388 }