File: HardwareClipper.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (388 lines) | stat: -rw-r--r-- 10,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pserror.h"
#include "3d.h"
#include "HardwareInternal.h"

static int free_point_num = -1;
static g3Point temp_points[MAX_POINTS_IN_POLY];
static g3Point *free_points[MAX_POINTS_IN_POLY];
vector Clip_plane_point;

void InitFreePoints(void) {
  int i;

  for (i = 0; i < MAX_POINTS_IN_POLY; i++) {
    free_points[i] = &temp_points[i];
  }
  free_point_num = 0;
}

g3Point *GetTempPoint(void) {
  ASSERT(free_point_num < MAX_POINTS_IN_POLY);

  g3Point *p = free_points[free_point_num++];
  p->p3_flags = PF_TEMP_POINT;
  return p;
}

void FreeTempPoint(g3Point *p) {
  if (free_point_num < 1) {
    mprintf(1, "clipper: no temp points to free\n");
    free_point_num = 1;
    Int3();
  }
  ASSERT(p->p3_flags & PF_TEMP_POINT);

  free_points[--free_point_num] = p;

  p->p3_flags &= ~PF_TEMP_POINT;
}

// Verify that all the temp points are free, and free them it they are not.
#ifdef _DEBUG
void CheckTempPoints(void) {
  if (free_point_num) {
    Int3();
    InitFreePoints();
  }
}
#endif

// Free up any temp points (created by the clipper) in the given pointlist
// Parameters:	pointlist - pointer to list of pointers to points, returned by g3_ClipPolygon()
//				nv - the number of points in pointlist
void g3_FreeTempPoints(g3Point **pointlist, int nv) {
  // Go through list
  int i;
  for (i = 0; i < nv; i++) {
    if (pointlist[i]->p3_flags & PF_TEMP_POINT) {
      FreeTempPoint(pointlist[i]);
    }
  }

  // Make sure all temp points are freed
  CheckTempPoints();
}

// Clips an edge against the far plane
g3Point *ClipFarEdge(g3Point *on_pnt, g3Point *off_pnt) {
  float z_on = on_pnt->p3_z;
  float z_off = off_pnt->p3_z;

  float k = 1.0f - ((z_off - Far_clip_z) / (z_off - z_on));

  g3Point *tmp = GetTempPoint();
  tmp->p3_z = on_pnt->p3_z + ((off_pnt->p3_z - on_pnt->p3_z) * k);
  tmp->p3_x = on_pnt->p3_x + ((off_pnt->p3_x - on_pnt->p3_x) * k);
  tmp->p3_y = on_pnt->p3_y + ((off_pnt->p3_y - on_pnt->p3_y) * k);

  if (on_pnt->p3_flags & PF_UV) {
    tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
    tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);
    tmp->p3_flags |= PF_UV;
  }

  if (on_pnt->p3_flags & PF_UV2) {
    tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
    tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);
    tmp->p3_flags |= PF_UV2;
  }

  if (on_pnt->p3_flags & PF_L) {
    tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
    tmp->p3_flags |= PF_L;
  }

  if (on_pnt->p3_flags & PF_RGBA) {
    tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
    tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
    tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
    tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
    tmp->p3_flags |= PF_RGBA;
  }

  g3_CodePoint(tmp);
  return tmp;
}

// Clips an edge against the far plane
g3Point *ClipCustomEdge(g3Point *on_pnt, g3Point *off_pnt) {
  g3Point *tmp = GetTempPoint();

  vector ray_direction = off_pnt->p3_vec - on_pnt->p3_vec;
  ray_direction.x /= Matrix_scale.x;
  ray_direction.y /= Matrix_scale.y;
  ray_direction.z /= Matrix_scale.z;

  vector w = on_pnt->p3_vec - Clip_plane_point;
  w.x /= Matrix_scale.x;
  w.y /= Matrix_scale.y;
  w.z /= Matrix_scale.z;

  float k, den = -(Clip_plane * ray_direction);
  if (den == 0.0f) {
    k = 1.0f;
  } else {
    float num = Clip_plane * w;
    k = num / den;
  }

  tmp->p3_vec = on_pnt->p3_vec + ((off_pnt->p3_vec - on_pnt->p3_vec) * k);

  if (on_pnt->p3_flags & PF_UV) {
    tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
    tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);
    tmp->p3_flags |= PF_UV;
  }

  if (on_pnt->p3_flags & PF_UV2) {
    tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
    tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);
    tmp->p3_flags |= PF_UV2;
  }

  if (on_pnt->p3_flags & PF_L) {
    tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
    tmp->p3_flags |= PF_L;
  }

  if (on_pnt->p3_flags & PF_RGBA) {
    tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
    tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
    tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
    tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
    tmp->p3_flags |= PF_RGBA;
  }

  g3_CodePoint(tmp);
  return tmp;
}

// clips an edge against one plane.
g3Point *ClipEdge(int plane_flag, g3Point *on_pnt, g3Point *off_pnt) {
  float a, b, k;
  g3Point *tmp;

  // compute clipping value k = (xs-zs) / (xs-xe-zs+ze)
  // use x or y as appropriate, and negate x/y value as appropriate

  if (plane_flag & CC_OFF_FAR) {
    return ClipFarEdge(on_pnt, off_pnt);
  }

  if ((plane_flag & CC_OFF_CUSTOM) && Clip_custom) {
    return ClipCustomEdge(on_pnt, off_pnt);
  }

  if (plane_flag & (CC_OFF_RIGHT | CC_OFF_LEFT)) {
    a = on_pnt->p3_x;
    b = off_pnt->p3_x;
  } else {
    a = on_pnt->p3_y;
    b = off_pnt->p3_y;
  }

  if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
    a = -a;
    b = -b;
  }

  k = (a - on_pnt->p3_z) / ((a - on_pnt->p3_z) - b + off_pnt->p3_z); //(xs-zs) / (xs-zs-xe+ze)

  tmp = GetTempPoint();

  tmp->p3_x = on_pnt->p3_x + ((off_pnt->p3_x - on_pnt->p3_x) * k);
  tmp->p3_y = on_pnt->p3_y + ((off_pnt->p3_y - on_pnt->p3_y) * k);

  if (plane_flag & (CC_OFF_TOP | CC_OFF_BOT)) {
    tmp->p3_z = tmp->p3_y;
  } else {
    tmp->p3_z = tmp->p3_x;
  }

  if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
    tmp->p3_z = -tmp->p3_z;
  }

  if (on_pnt->p3_flags & PF_UV) {
    tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u - on_pnt->p3_u) * k);
    tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v - on_pnt->p3_v) * k);

    tmp->p3_flags |= PF_UV;
  }

  if (on_pnt->p3_flags & PF_UV2) {
    tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2 - on_pnt->p3_u2) * k);
    tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2 - on_pnt->p3_v2) * k);

    tmp->p3_flags |= PF_UV2;
  }

  if (on_pnt->p3_flags & PF_L) {
    tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l - on_pnt->p3_l) * k);
    tmp->p3_flags |= PF_L;
  }

  if (on_pnt->p3_flags & PF_RGBA) {
    tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r - on_pnt->p3_r) * k);
    tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g - on_pnt->p3_g) * k);
    tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b - on_pnt->p3_b) * k);
    tmp->p3_a = on_pnt->p3_a + ((off_pnt->p3_a - on_pnt->p3_a) * k);
    tmp->p3_flags |= PF_RGBA;
  }

  g3_CodePoint(tmp);

  return tmp;
}

// clips a line to the viewing pyramid.
void ClipLine(g3Point **p0, g3Point **p1, uint8_t codes_or) {
  int plane_flag;
  g3Point *old_p1;

  // might have these left over
  //(*p0)->p3_flags &= ~(PF_UV|PF_L|PF_RGBA|PF_UV2);
  //(*p1)->p3_flags &= ~(PF_UV|PF_L|PF_RGBA|PF_UV2);

  for (plane_flag = 1; plane_flag <= 32; plane_flag <<= 1) {
    if (codes_or & plane_flag) {
      if ((*p0)->p3_codes & plane_flag) {
        // swap!
        g3Point *t = *p0;
        *p0 = *p1;
        *p1 = t;
      }

      old_p1 = *p1;

      *p1 = ClipEdge(plane_flag, *p0, *p1);

      codes_or = (*p0)->p3_codes | (*p1)->p3_codes; // get new codes

      if (old_p1->p3_flags & PF_TEMP_POINT) {
        FreeTempPoint(old_p1);
      }
    }
  }
}

int ClipPlane(int plane_flag, g3Point **src, g3Point **dest, int *nv, g3Codes *cc) {
  int i, prev, next;
  g3Point **save_dest = dest;

  // Init codes
  cc->cc_and = 0xff;
  cc->cc_or = 0;

  for (i = 0, prev = *nv - 1, next = 1; i < *nv; i++) {
    if (src[i]->p3_codes & plane_flag) {
      // cur point off?
      if (!(src[prev]->p3_codes & plane_flag)) {
        // prev not off?
        *dest = ClipEdge(plane_flag, src[prev], src[i]);
        cc->cc_or |= (*dest)->p3_codes;
        cc->cc_and &= (*dest)->p3_codes;
        dest++;
      }

      if (!(src[next]->p3_codes & plane_flag)) {
        *dest = ClipEdge(plane_flag, src[next], src[i]);
        cc->cc_or |= (*dest)->p3_codes;
        cc->cc_and &= (*dest)->p3_codes;
        dest++;
      }

      // see if must free discarded point
      if (src[i]->p3_flags & PF_TEMP_POINT) {
        FreeTempPoint(src[i]);
      }
    } else {
      // cur not off, copy to dest buffer
      *dest++ = src[i];

      cc->cc_or |= src[i]->p3_codes;
      cc->cc_and &= src[i]->p3_codes;
    }

    prev = i;
    if (++next == *nv) {
      next = 0;
    }
  }

  return (dest - save_dest);
}

// temp buffers for clipping
static g3Point *Vbuf0[MAX_POINTS_IN_POLY];
static g3Point *Vbuf1[MAX_POINTS_IN_POLY];

// Clips a polygon
// Parameters:	pointlist - pointer to a list of pointers to points
//					nv - the number of points in the polygon
//					cc - the clip codes for this polygon
// Returns:	a pointer to a list of pointer of points in the clipped polygon
// NOTE: You MUST call g3_FreeTempPoints() when you're done with the clipped polygon
g3Point **g3_ClipPolygon(g3Point **pointlist, int *nv, g3Codes *cc) {
  int plane_flag;
  g3Point **dest;

  dest = Vbuf0;

  //&	ASSERT(free_point_num == 0);	//DAJ UTB CMO
  if (free_point_num != 0) {
    mprintf(1, "clipper: leftover temp point\n");
    free_point_num = 0;
    Int3();
  }

  for (plane_flag = 1; plane_flag <= 32; plane_flag <<= 1) {
    if (cc->cc_or & plane_flag) {

      *nv = ClipPlane(plane_flag, pointlist, dest, nv, cc);

      if (cc->cc_and) // clipped away
        return dest;

      pointlist = dest;
      dest = (pointlist == Vbuf0) ? Vbuf1 : Vbuf0;
    }
  }
  return pointlist; // we swapped after we copied
}

// sets the z distance of the far clipping plane
void g3_SetFarClipZ(float z) { Far_clip_z = z; }

// Sets up a custom clipping plane - g3_StartFrame must be called before this is called
void g3_SetCustomClipPlane(uint8_t state, vector *pnt, vector *normal) {
  Clip_custom = state;
  if (state) {
    vector tempv;
    vector norm = *normal;

    tempv = *pnt - View_position;
    Clip_plane_point = tempv * View_matrix;

    Clip_plane = norm * Unscaled_matrix; // View_matrix;
    vm_NormalizeVector(&Clip_plane);
  }
}