-
Notifications
You must be signed in to change notification settings - Fork 5
/
Graf3D.p
executable file
·477 lines (391 loc) · 12 KB
/
Graf3D.p
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{$S Graf }
UNIT Graf3D;
{ three-dimensional graphics routines layered on top of QuickDraw }
INTERFACE
USES {$U obj:QuickDraw } QuickDraw;
CONST radConst=57.29578;
TYPE Point3D=RECORD
x: REAL;
y: REAL;
z: REAL;
END;
Point2D=RECORD
x: REAL;
y: REAL;
END;
XfMatrix = ARRAY[0..3,0..3] OF REAL;
Port3DPtr = ^Port3D;
Port3D = RECORD
GPort: GrafPtr;
viewRect: Rect;
xLeft,yTop,xRight,yBottom: REAL;
pen,penPrime,eye: Point3D;
hSize,vSize: REAL;
hCenter,vCenter: REAL;
xCotan,yCotan: REAL;
ident: BOOLEAN;
xForm: XfMatrix;
END;
VAR thePort3D: Port3DPtr;
PROCEDURE Open3DPort (port: Port3DPtr);
PROCEDURE SetPort3D (port: Port3DPtr);
PROCEDURE GetPort3D (VAR port: Port3DPtr);
PROCEDURE MoveTo2D(x,y: REAL); PROCEDURE MoveTo3D(x,y,z: REAL);
PROCEDURE LineTo2D(x,y: REAL); PROCEDURE LineTo3D(x,y,z: REAL);
PROCEDURE Move2D(dx,dy: REAL); PROCEDURE Move3D(dx,dy,dz: REAL);
PROCEDURE Line2D(dx,dy: REAL); PROCEDURE Line3D(dx,dy,dz: REAL);
PROCEDURE ViewPort (r: Rect);
PROCEDURE LookAt (left,top,right,bottom: REAL);
PROCEDURE ViewAngle (angle: REAL);
PROCEDURE Identity;
PROCEDURE Scale (xFactor,yFactor,zFactor: REAL);
PROCEDURE Translate (dx,dy,dz: REAL);
PROCEDURE Pitch (xAngle: REAL);
PROCEDURE Yaw (yAngle: REAL);
PROCEDURE Roll (zAngle: REAL);
PROCEDURE Skew (zAngle: REAL);
PROCEDURE TransForm (src: Point3D; VAR dst: Point3D);
FUNCTION Clip3D (src1,src2: Point3D; VAR dst1,dst2: POINT): BOOLEAN;
PROCEDURE SetPt3D (VAR pt3D: Point3D; x,y,z: REAL);
PROCEDURE SetPt2D (VAR pt2D: Point2D; x,y: REAL);
IMPLEMENTATION
PROCEDURE Open3DPort(* port: Port3DPtr *);
{ initialize all values in port^ to their defaults }
BEGIN
thePort3D:=port;
port^.GPort:=thePort;
ViewPort(thePort^.portRect);
WITH thePort^.portRect DO LookAt(left,top,right,bottom);
ViewAngle(0);
Identity;
MoveTo3D(0,0,0);
END;
PROCEDURE SetPort3D(* port: Port3DPtr *);
{ change to another Port3D }
BEGIN
thePort3D:=port;
SetPort(port^.GPort);
END;
PROCEDURE GetPort3D(* VAR port: Port3DPtr *);
{ inquire the current Port3D }
BEGIN
port:=thePort3D;
END;
PROCEDURE MoveTo3D(* x,y,z: REAL *);
{ Move from current position to x,y,z without drawing. }
VAR pt1,pt2: POINT;
oldPrime: Point3D;
BEGIN
WITH thePort3D^ DO
BEGIN
oldPrime:=penPrime;
pen.x:=x;
pen.y:=y;
pen.z:=z;
TransForm(pen,penPrime);
IF Clip3D(oldPrime,penPrime,pt1,pt2) THEN MoveTo(pt2.H,pt2.V);
END;
END;
PROCEDURE LineTo3D(* x,y,z: REAL *);
{ draw a 3-D line from current position to x,y,z. }
VAR oldPrime: Point3D;
pt1,pt2: POINT;
BEGIN
WITH thePort3D^ DO
BEGIN
oldPrime:=penPrime;
pen.x:=x;
pen.y:=y;
pen.z:=z;
TransForm(pen,penPrime);
IF Clip3D(oldPrime,penPrime,pt1,pt2) THEN
BEGIN
MoveTo(pt1.h,pt1.v);
LineTo(pt2.H,pt2.V);
END;
END;
END;
PROCEDURE Move3D(* dx,dy,dz: REAL *);
BEGIN
WITH thePort3D^ DO MoveTo3D(pen.x+dx,pen.y+dy,pen.z+dz);
END;
PROCEDURE Line3D(* dx,dy,dz: REAL *);
BEGIN
WITH thePort3D^ DO LineTo3D(pen.x+dx,pen.y+dy,pen.z+dz);
END;
PROCEDURE MoveTo2D(* x,y: REAL *);
BEGIN
MoveTo3D(x,y,thePort3D^.pen.z);
END;
PROCEDURE Move2D(* dx,dy: REAL *);
BEGIN
Move3D(dx,dy,0.0);
END;
PROCEDURE LineTo2D(* x,y: REAL *);
BEGIN
LineTo3D(x,y,thePort3D^.pen.z);
END;
PROCEDURE Line2D(* dx,dy: REAL *);
BEGIN
Line3D(dx,dy,0.0);
END;
PROCEDURE ViewLook;
{ re-calculate offsets and scales after LookAt or ViewPort }
BEGIN
WITH thePort3D^ DO
WITH viewRect DO
BEGIN
hSize:=(right-left)/2.0;
vSize:=(bottom-top)/(-2.0); { vert pos down, y pos up }
hCenter:=left + hSize;
vCenter:=top - vSize;
END;
END;
PROCEDURE ViewPort(* r: Rect *);
{ specify what portion of the folder to map onto }
BEGIN
thePort3D^.viewRect:=r;
ViewLook; { re-calculate scales and offsets }
END;
PROCEDURE LookAt(* left,top,right,bottom: REAL *);
{ specify the real number coordinates of the portRect }
BEGIN
WITH thePort3D^ DO
BEGIN
xLeft:=left;
xRight:=right;
yBottom:=bottom;
yTop:=top;
eye.x:=(left+right)/2.0;
eye.y:=(top+bottom)/2.0;
END;
ViewLook; { re-calculate scales and offsets }
END;
PROCEDURE ViewAngle(* angle: REAL *);
{ specify the horizontal angle subtended by the viewing pyramid }
BEGIN
WITH thePort3D^ DO
BEGIN
IF angle < 0.1 THEN angle:=0.1;
angle:=angle/(2.0*radConst); { halve angle & convert to rad }
xCotan:=COS(angle)/SIN(angle); { remember for perspective calc }
yCotan:=xCotan * (xRight-xLeft)/(yTop-yBottom);
eye.z:=xCotan * (xRight-xLeft)/2;
END;
END;
PROCEDURE TransForm(* src: Point3D; VAR dst: Point3D *);
{ use the current xForm matrix to transform }
{ a 3D source point into a 3D destination point. }
BEGIN
IF thePort3D^.ident THEN dst:=src
ELSE WITH thePort3D^ DO
BEGIN
dst.x:=src.x * xForm[0,0] + src.y * xForm[1,0]
+ src.z * xForm[2,0] + xForm[3,0];
dst.y:=src.x * xForm[0,1] + src.y * xForm[1,1]
+ src.z * xForm[2,1] + xForm[3,1];
dst.z:=src.x * xForm[0,2] + src.y * xForm[1,2]
+ src.z * xForm[2,2] + xForm[3,2];
END;
END;
FUNCTION Clip3D(* src1,src2: Point3D; VAR dst1,dst2: POINT *);
{ do full 3D clipping to viewing pyramid and return 2D }
{ screen coords in dst. Function value true if visible. }
LABEL 0;
TYPE Edge=(left,top,right,bottom);
OutCode=SET OF Edge;
VAR c,c1,c2: OutCode;
pt3D: Point3D;
t: REAL;
pt1,pt2: POINT;
PROCEDURE Code(pt3D: Point3D; VAR c: OutCode);
BEGIN
c:=[];
IF pt3D.x < -pt3D.z THEN c:=[left] ELSE IF pt3D.x > pt3D.z THEN c:=[right];
IF pt3D.y < -pt3D.z THEN c:=c+[bottom] ELSE IF pt3D.y > pt3D.z THEN c:=c+[top];
END;
BEGIN
Clip3D:=FALSE;
WITH thePort3D^ DO
BEGIN { convert both points into clipping coord system }
src1.x:=(src1.x - eye.x) * xCotan;
src1.y:=(src1.y - eye.y) * yCotan;
src1.z:=eye.z - src1.z;
src2.x:=(src2.x - eye.x) * xCotan;
src2.y:=(src2.y - eye.y) * yCotan;
src2.z:=eye.z - src2.z;
END;
Code(src1,c1); Code(src2,c2);
WHILE c1+c2 <> [] DO
BEGIN
IF c1*c2 <> [] THEN GOTO 0; { both out on same side }
c:=c1; IF c=[] THEN c:=c2;
IF left IN c THEN { calc intersect with left edge }
BEGIN
t:=(src1.z+src1.x) / ((src1.x-src2.x) - (src2.z-src1.z));
pt3D.z:=t*(src2.z-src1.z) + src1.z;
pt3D.x:=-pt3D.z;
pt3D.y:=t*(src2.y-src1.y) + src1.y;
END
ELSE IF right IN c THEN { calc intersect with right edge }
BEGIN
t:=(src1.z-src1.x) / ((src2.x-src1.x) - (src2.z-src1.z));
pt3D.z:=t*(src2.z-src1.z) + src1.z;
pt3D.x:=pt3D.z;
pt3D.y:=t*(src2.y-src1.y) + src1.y;
END
ELSE IF bottom IN c THEN { calc intersect with bottom edge }
BEGIN
t:=(src1.z+src1.y) / ((src1.y-src2.y) - (src2.z-src1.z));
pt3D.z:=t*(src2.z-src1.z) + src1.z;
pt3D.x:=t*(src2.x-src1.x) + src1.x;
pt3D.y:=-pt3D.z;
END
ELSE IF top IN c THEN { calc intersect with top edge }
BEGIN
t:=(src1.z-src1.y) / ((src2.y-src1.y) - (src2.z-src1.z));
pt3D.z:=t*(src2.z-src1.z) + src1.z;
pt3D.x:=t*(src2.x-src1.x) + src1.x;
pt3D.y:=pt3D.z;
END;
IF c=c1 THEN BEGIN src1:=pt3D; Code(src1,c1); END
ELSE BEGIN src2:=pt3D; Code(src2,c2); END;
END;
{ if we reach here, the line from src1 to src2 is visible }
Clip3D:=TRUE;
WITH thePort3D^ DO
WITH GPort^ DO
BEGIN { convert clip coords to screen coords }
dst1.H:=ROUND(hCenter + hSize * src1.x / src1.z);
dst1.V:=ROUND(vCenter + vSize * src1.y / src1.z);
dst2.H:=ROUND(hCenter + hSize * src2.x / src2.z);
dst2.V:=ROUND(vCenter + vSize * src2.y / src2.z);
END;
0: END;
PROCEDURE Identity;
{ reset the transform matrix to identity }
VAR ROW,COL: INTEGER;
BEGIN;
WITH thePort3D^ DO
BEGIN
FOR ROW:=0 TO 3 DO
FOR COL:=0 TO 3 DO
IF ROW=COL THEN xForm[ROW,COL]:=1.0
ELSE xForm[ROW,COL]:=0.0;
ident:=TRUE; { SET FLAG SO xForm CAN BE SKIPPED }
END;
END;
PROCEDURE Scale(* xFactor,yFactor,zFactor: REAL *);
{ change xForm matrix to provide scaling }
VAR ROW: INTEGER;
BEGIN
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
FOR ROW:=0 TO 3 DO
BEGIN
xForm[ROW,0]:=xForm[ROW,0]*xFactor;
xForm[ROW,1]:=xForm[ROW,1]*yFactor;
xForm[ROW,2]:=xForm[ROW,2]*zFactor;
END;
END;
END;
PROCEDURE Translate(* dx,dy,dz: REAL *);
{ change xForm matrix to translate }
BEGIN
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
xForm[3,0]:=xForm[3,0]+dx;
xForm[3,1]:=xForm[3,1]+dy;
xForm[3,2]:=xForm[3,2]+dz;
END;
END;
PROCEDURE Pitch(* xAngle: REAL *);
{ change xForm matrix to rotate xAngle degrees around x-Axis }
VAR si,co,TEMP: REAL;
BEGIN
xAngle:=xAngle/radConst; { convert degrees to rads }
si:=SIN(xAngle); co:=COS(xAngle);
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
TEMP:=xForm[0,1]*co+xForm[0,2]*si;
xForm[0,2]:=xForm[0,2]*co-xForm[0,1]*si; xForm[0,1]:=TEMP;
TEMP:=xForm[1,1]*co+xForm[1,2]*si;
xForm[1,2]:=xForm[1,2]*co-xForm[1,1]*si; xForm[1,1]:=TEMP;
TEMP:=xForm[2,1]*co+xForm[2,2]*si;
xForm[2,2]:=xForm[2,2]*co-xForm[2,1]*si; xForm[2,1]:=TEMP;
TEMP:=xForm[3,1]*co+xForm[3,2]*si;
xForm[3,2]:=xForm[3,2]*co-xForm[3,1]*si; xForm[3,1]:=TEMP;
END;
END;
PROCEDURE Yaw(* yAngle: REAL *);
{ change xForm matrix to rotate yAngle degrees around y-Axis }
VAR si,co,TEMP: REAL;
BEGIN
yAngle:=yAngle/radConst; { convert degrees to rads }
si:=SIN(yAngle); co:=COS(yAngle);
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
TEMP:=xForm[0,0]*co-xForm[0,2]*si;
xForm[0,2]:=xForm[0,0]*si+xForm[0,2]*co; xForm[0,0]:=TEMP;
TEMP:=xForm[1,0]*co-xForm[1,2]*si;
xForm[1,2]:=xForm[1,0]*si+xForm[1,2]*co; xForm[1,0]:=TEMP;
TEMP:=xForm[2,0]*co-xForm[2,2]*si;
xForm[2,2]:=xForm[2,0]*si+xForm[2,2]*co; xForm[2,0]:=TEMP;
TEMP:=xForm[3,0]*co-xForm[3,2]*si;
xForm[3,2]:=xForm[3,0]*si+xForm[3,2]*co; xForm[3,0]:=TEMP;
END;
END;
PROCEDURE Roll(* zAngle: REAL *);
{ change xForm matrix to rotate zAngle degrees around z-Axis }
VAR si,co,TEMP: REAL;
BEGIN
zAngle:=zAngle/radConst; { convert degrees to rads }
si:=SIN(zAngle); co:=COS(zAngle);
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
TEMP:=xForm[0,0]*co+xForm[0,1]*si;
xForm[0,1]:=xForm[0,1]*co-xForm[0,0]*si; xForm[0,0]:=TEMP;
TEMP:=xForm[1,0]*co+xForm[1,1]*si;
xForm[1,1]:=xForm[1,1]*co-xForm[1,0]*si; xForm[1,0]:=TEMP;
TEMP:=xForm[2,0]*co+xForm[2,1]*si;
xForm[2,1]:=xForm[2,1]*co-xForm[2,0]*si; xForm[2,0]:=TEMP;
TEMP:=xForm[3,0]*co+xForm[3,1]*si;
xForm[3,1]:=xForm[3,1]*co-xForm[3,0]*si; xForm[3,0]:=TEMP;
END;
END;
PROCEDURE Skew(* zAngle: REAL *);
{ change xForm matrix to skew zAngle degrees around z-Axis }
{ x := (x + y*TAN(zAngle)) zAngle limited to +-90 degrees }
VAR co,TA: REAL;
COL: INTEGER;
BEGIN
zAngle:=zAngle/radConst; { convert degrees to rads }
co:= COS(zAngle);
IF ABS(co) > 1.0E-5 THEN
BEGIN
TA:= SIN(zAngle)/co;
WITH thePort3D^ DO
BEGIN
ident:=FALSE;
FOR COL:=0 TO 2 DO
xForm[1,COL]:=xForm[1,COL]+xForm[0,COL]*TA;
END;
END;
END;
PROCEDURE SetPt3D(* VAR pt3D: Point3D; x,y,z: REAL *);
BEGIN
pt3D.x:=x;
pt3D.y:=y;
pt3D.z:=z;
END;
PROCEDURE SetPt2D(* VAR pt2D: Point2D; x,y: REAL *);
BEGIN
pt2D.x:=x;
pt2D.y:=y;
END;
END. { of Unit }