-
Notifications
You must be signed in to change notification settings - Fork 23
/
m2dlib.c
384 lines (302 loc) · 10.3 KB
/
m2dlib.c
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
/*
* 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* With Grateful Acknowledgements to the books writen by Andre LaMothe:
* <Tricks of the 3D Game Programming Gurus-Advanced 3D Graphics and Rasterization>
* <Tricks of the Windows Game Programming Gurus>
*/
#include "m2dlib.h"
#include "osdvar.h"
//Reset 2d object, just copy local verts to transfer verts
void Reset_Polygon2D(POLYGON2D_PTR poly) {
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++) {
VECTOR2D_COPY(&(poly->vlist_trans[curr_vert]),
&(poly->vlist_local[curr_vert]));
}
}
int Transform_Polygon2D(POLYGON2D_PTR poly, float roate, float tx, float ty) {
// test for valid pointer
if (!poly)
return (0);
// test for negative rotation angle
if (roate < 0)
roate += 360;
// loop and rotate each point, very crude, no lookup!!!
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++) {
poly->vlist_trans[curr_vert].x += tx;
poly->vlist_trans[curr_vert].y += ty;
// perform rotation
float xr = (float) poly->vlist_trans[curr_vert].x * Fast_Cos(roate)
- (float) poly->vlist_trans[curr_vert].y * Fast_Sin(roate);
float yr = (float) poly->vlist_trans[curr_vert].x * Fast_Sin(roate)
+ (float) poly->vlist_trans[curr_vert].y * Fast_Cos(roate);
// store result back
poly->vlist_trans[curr_vert].x = xr;
poly->vlist_trans[curr_vert].y = yr;
} // end for curr_vert
// return success
return (1);
}
int Translate_Polygon2D(POLYGON2D_PTR poly, float dx, float dy) {
// this function translates the center of a polygon
// test for valid pointer
if (!poly)
return (0);
// translate
poly->x0 += dx;
poly->y0 += dy;
// return success
return (1);
} // end Translate_Polygon2D
int Rotate_Polygon2D(POLYGON2D_PTR poly, float theta) {
// this function rotates the local coordinates of the polygon
// test for valid pointer
if (!poly)
return (0);
// test for negative rotation angle
if (theta < 0)
theta += 360;
// loop and rotate each point, very crude, no lookup!!!
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++) {
// perform rotation
float xr = (float) poly->vlist_trans[curr_vert].x * Fast_Cos(theta)
- (float) poly->vlist_trans[curr_vert].y * Fast_Sin(theta);
float yr = (float) poly->vlist_trans[curr_vert].x * Fast_Sin(theta)
+ (float) poly->vlist_trans[curr_vert].y * Fast_Cos(theta);
// store result back
poly->vlist_trans[curr_vert].x = xr;
poly->vlist_trans[curr_vert].y = yr;
} // end for curr_vert
// return success
return (1);
} // end Rotate_Polygon2D
int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy) {
// this function scalesthe local coordinates of the polygon
// test for valid pointer
if (!poly)
return (0);
// loop and scale each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++) {
// scale and store result back
poly->vlist_local[curr_vert].x *= sx;
poly->vlist_local[curr_vert].y *= sy;
} // end for curr_vert
// return success
return (1);
} // end Scale_Polygon2D
//return 1: visible
// 0: invisible
int Clip_Line(VECTOR4D_PTR v) {
// this function clips the sent line using the globally defined clipping
// region
// internal clipping codes
#define CLIP_CODE_C 0x0000
#define CLIP_CODE_N 0x0008
#define CLIP_CODE_S 0x0004
#define CLIP_CODE_E 0x0002
#define CLIP_CODE_W 0x0001
#define CLIP_CODE_NE 0x000a
#define CLIP_CODE_SE 0x0006
#define CLIP_CODE_NW 0x0009
#define CLIP_CODE_SW 0x0005
int x1 = v->x, y1 = v->y, x2 = v->z, y2 = v->w;
int xc1 = x1, yc1 = y1, xc2 = x2, yc2 = y2;
int p1_code = 0, p2_code = 0;
// determine codes for p1 and p2
if (y1 < atti_3d_min_clipY)
p1_code |= CLIP_CODE_N;
else if (y1 > atti_3d_max_clipY)
p1_code |= CLIP_CODE_S;
if (x1 < atti_3d_min_clipX)
p1_code |= CLIP_CODE_W;
else if (x1 > atti_3d_max_clipX)
p1_code |= CLIP_CODE_E;
if (y2 < atti_3d_min_clipY)
p2_code |= CLIP_CODE_N;
else if (y2 > atti_3d_max_clipY)
p2_code |= CLIP_CODE_S;
if (x2 < atti_3d_min_clipX)
p2_code |= CLIP_CODE_W;
else if (x2 > atti_3d_max_clipX)
p2_code |= CLIP_CODE_E;
// try and trivially reject
if ((p1_code & p2_code))
return (0);
// test for totally visible, if so leave points untouched
if (p1_code == 0 && p2_code == 0)
return (1);
// determine end clip point for p1
switch (p1_code) {
case CLIP_CODE_C:
break;
case CLIP_CODE_N: {
yc1 = atti_3d_min_clipY;
xc1 = x1 + 0.5 + (atti_3d_min_clipY - y1) * (x2 - x1) / (y2 - y1);
}
break;
case CLIP_CODE_S: {
yc1 = atti_3d_max_clipY;
xc1 = x1 + 0.5 + (atti_3d_max_clipY - y1) * (x2 - x1) / (y2 - y1);
}
break;
case CLIP_CODE_W: {
xc1 = atti_3d_min_clipX;
yc1 = y1 + 0.5 + (atti_3d_min_clipX - x1) * (y2 - y1) / (x2 - x1);
}
break;
case CLIP_CODE_E: {
xc1 = atti_3d_max_clipX;
yc1 = y1 + 0.5 + (atti_3d_max_clipX - x1) * (y2 - y1) / (x2 - x1);
}
break;
// these cases are more complex, must compute 2 intersections
case CLIP_CODE_NE: {
// north hline intersection
yc1 = atti_3d_min_clipY;
xc1 = x1 + 0.5 + (atti_3d_min_clipY - y1) * (x2 - x1) / (y2 - y1);
// test if intersection is valid, of so then done, else compute next
if (xc1 < atti_3d_min_clipX || xc1 > atti_3d_max_clipX) {
// east vline intersection
xc1 = atti_3d_max_clipX;
yc1 = y1 + 0.5 + (atti_3d_max_clipX - x1) * (y2 - y1) / (x2 - x1);
} // end if
}
break;
case CLIP_CODE_SE: {
// south hline intersection
yc1 = atti_3d_max_clipY;
xc1 = x1 + 0.5 + (atti_3d_max_clipY - y1) * (x2 - x1) / (y2 - y1);
// test if intersection is valid, of so then done, else compute next
if (xc1 < atti_3d_min_clipX || xc1 > atti_3d_max_clipX) {
// east vline intersection
xc1 = atti_3d_max_clipX;
yc1 = y1 + 0.5 + (atti_3d_max_clipX - x1) * (y2 - y1) / (x2 - x1);
} // end if
}
break;
case CLIP_CODE_NW: {
// north hline intersection
yc1 = atti_3d_min_clipY;
xc1 = x1 + 0.5 + (atti_3d_min_clipY - y1) * (x2 - x1) / (y2 - y1);
// test if intersection is valid, of so then done, else compute next
if (xc1 < atti_3d_min_clipX || xc1 > atti_3d_max_clipX) {
xc1 = atti_3d_min_clipX;
yc1 = y1 + 0.5 + (atti_3d_min_clipX - x1) * (y2 - y1) / (x2 - x1);
} // end if
}
break;
case CLIP_CODE_SW: {
// south hline intersection
yc1 = atti_3d_max_clipY;
xc1 = x1 + 0.5 + (atti_3d_max_clipY - y1) * (x2 - x1) / (y2 - y1);
// test if intersection is valid, of so then done, else compute next
if (xc1 < atti_3d_min_clipX || xc1 > atti_3d_max_clipX) {
xc1 = atti_3d_min_clipX;
yc1 = y1 + 0.5 + (atti_3d_min_clipX - x1) * (y2 - y1) / (x2 - x1);
} // end if
}
break;
default:
break;
} // end switch
// determine clip point for p2
switch (p2_code) {
case CLIP_CODE_C:
break;
case CLIP_CODE_N: {
yc2 = atti_3d_min_clipY;
xc2 = x2 + (atti_3d_min_clipY - y2) * (x1 - x2) / (y1 - y2);
}
break;
case CLIP_CODE_S: {
yc2 = atti_3d_max_clipY;
xc2 = x2 + (atti_3d_max_clipY - y2) * (x1 - x2) / (y1 - y2);
}
break;
case CLIP_CODE_W: {
xc2 = atti_3d_min_clipX;
yc2 = y2 + (atti_3d_min_clipX - x2) * (y1 - y2) / (x1 - x2);
}
break;
case CLIP_CODE_E: {
xc2 = atti_3d_max_clipX;
yc2 = y2 + (atti_3d_max_clipX - x2) * (y1 - y2) / (x1 - x2);
}
break;
// these cases are more complex, must compute 2 intersections
case CLIP_CODE_NE: {
// north hline intersection
yc2 = atti_3d_min_clipY;
xc2 = x2 + 0.5 + (atti_3d_min_clipY - y2) * (x1 - x2) / (y1 - y2);
// test if intersection is valid, of so then done, else compute next
if (xc2 < atti_3d_min_clipX || xc2 > atti_3d_max_clipX) {
// east vline intersection
xc2 = atti_3d_max_clipX;
yc2 = y2 + 0.5 + (atti_3d_max_clipX - x2) * (y1 - y2) / (x1 - x2);
} // end if
}
break;
case CLIP_CODE_SE: {
// south hline intersection
yc2 = atti_3d_max_clipY;
xc2 = x2 + 0.5 + (atti_3d_max_clipY - y2) * (x1 - x2) / (y1 - y2);
// test if intersection is valid, of so then done, else compute next
if (xc2 < atti_3d_min_clipX || xc2 > atti_3d_max_clipX) {
// east vline intersection
xc2 = atti_3d_max_clipX;
yc2 = y2 + 0.5 + (atti_3d_max_clipX - x2) * (y1 - y2) / (x1 - x2);
} // end if
}
break;
case CLIP_CODE_NW: {
// north hline intersection
yc2 = atti_3d_min_clipY;
xc2 = x2 + 0.5 + (atti_3d_min_clipY - y2) * (x1 - x2) / (y1 - y2);
// test if intersection is valid, of so then done, else compute next
if (xc2 < atti_3d_min_clipX || xc2 > atti_3d_max_clipX) {
xc2 = atti_3d_min_clipX;
yc2 = y2 + 0.5 + (atti_3d_min_clipX - x2) * (y1 - y2) / (x1 - x2);
} // end if
}
break;
case CLIP_CODE_SW: {
// south hline intersection
yc2 = atti_3d_max_clipY;
xc2 = x2 + 0.5 + (atti_3d_max_clipY - y2) * (x1 - x2) / (y1 - y2);
// test if intersection is valid, of so then done, else compute next
if (xc2 < atti_3d_min_clipX || xc2 > atti_3d_max_clipX) {
xc2 = atti_3d_min_clipX;
yc2 = y2 + 0.5 + (atti_3d_min_clipX - x2) * (y1 - y2) / (x1 - x2);
} // end if
}
break;
default:
break;
} // end switch
// do bounds check
if ((xc1 < atti_3d_min_clipX) || (xc1 > atti_3d_max_clipX)
|| (yc1 < atti_3d_min_clipY) || (yc1 > atti_3d_max_clipY)
|| (xc2 < atti_3d_min_clipX) || (xc2 > atti_3d_max_clipX)
|| (yc2 < atti_3d_min_clipY) || (yc2 > atti_3d_max_clipY)) {
return (0);
} // end if
// store vars back
v->x = xc1;
v->y = yc1;
v->z = xc2;
v->w = yc2;
return (1);
} // end Clip_Line