Close

Some 3D

A project log for BASIC microcomputer, based on Arduino

A microcomputer with PS/2 keyboard, SD card storage and composite video output, running Terminal-BASIC

Andrey SkvortsovAndrey Skvortsov 02/22/2020 at 19:280 Comments

I try to do some 3D wireframe graphics with Terminal-BASIC matrix operations:

The source is in the Terminal-BASIC team repository:

10 REM 3D rotations using matrix operations
20 REM FILL TABLES OF SIN AND COS
30 DIM SC(1,255)
40 LET D = 2.*PI()/256.
50 LET A = 0
60 FOR I%=0 TO 255
65   A = A + D
70   SC(0,I%) = SIN(A)
80   SC(1,I%) = COS(A)
90 NEXT I%
100 REM Octahedron
110 DIM V1(2,0), V1W(2,0)
120 V1(0,0) = 0 : V1(1,0) = 0 : V1(2,0) = 40
125 MAT V1W = V1
130 DIM V2(2,0), V2W(2,0)
140 V2(0,0) = -25 : V2(1,0) = 25 : V2(2,0) = 0
145 MAT V2W = V2
150 DIM V3(2,0), V3W(2,0)
160 V3(0,0) = -25 : V3(1,0) = -25 : V3(2,0) = 0
165 MAT V3W = V3
170 DIM V4(2,0), V4W(2,0)
180 V4(0,0) = 25 : V4(1,0) = -25 : V4(2,0) = 0
185 MAT V4W = V4
190 DIM V5(2,0), V5W(2,0)
200 V5(0,0) = 25 : V5(1,0) = 25 : V5(2,0) = 0
205 MAT V5W = V5
210 DIM V6(2,0), V6W(2,0)
220 V6(0,0) = 0 : V6(1,0) = 0 : V6(2,0) = -40
230 MAT V6W = V6
300 REM screen coordinates
320 DIM VS1(1), VS2(1), VS3(1), VS4(1), VS5(2), VS6(2)
330 LET A% = 0 : LET B%=0 : LET C%=0
340 DIM RMX(2,2) : MAT RMX = IDN
350 DIM RMY(2,2) : MAT RMY = IDN
360 DIM RMZ(2,2) : MAT RMZ = IDN
370 DIM RM(2,2)
380 GOSUB 1100 : GOSUB 1200 : GOSUB 1300
1000 REM main loop
1010 K$ = INKEY$()
1020 IF K$ = "d" THEN A% = A% + 1 : GOSUB 1100
1030 IF K$ = "a" THEN A% = A% - 1 : GOSUB 1100
1040 IF K$ = "w" THEN B% = B% + 1 : GOSUB 1200
1050 IF K$ = "s" THEN B% = B% - 1 : GOSUB 1200
1060 IF K$ = "e" THEN C% = C% + 1 : GOSUB 1300
1070 IF K$ = "q" THEN C% = C% - 1 : GOSUB 1300
1080 GOTO 1000
1100 REM Update rot matrix z
1110 IF A%<0 THEN A%=255
1120 IF A%>255 THEN A%=0
1130 RMZ(0,0) = SC(1,A%) : RMZ(0,1) = -SC(0,A%)
1140 RMZ(1,0) = SC(0,A%) : RMZ(1,1) = SC(1,A%)
1150 GOSUB 1400
1160 GOSUB 9000 : GOSUB 10000
1170 RETURN
1200 REM Update rot matrix x
1210 IF B%<0 THEN B%=255
1220 IF B%>255 THEN B%=0
1230 RMX(1,1) = SC(1,B%) : RMX(1,2) = -SC(0,B%)
1240 RMX(2,1) = SC(0,B%) : RMX(2,2) = SC(1,B%)
1250 GOSUB 1400
1260 GOSUB 9000 : GOSUB 10000
1270 RETURN
1300 REM Update rot matrix y
1310 IF C%<0 THEN C%=255
1320 IF C%>255 THEN C%=0
1330 RMY(0,0) = SC(1,C%) : RMY(0,2) = SC(0,C%)
1340 RMY(2,0) = -SC(0,C%) : RMY(2,2) = SC(1,C%)
1350 GOSUB 1400
1360 GOSUB 9000 : GOSUB 10000
1370 RETURN
1400 REM Rotate points
1410 MAT RM = RMX * RMY
1420 MAT RM = RM * RMZ
1440 MAT V1W = RM*V1 : MAT V2W = RM*V2
1450 MAT V3W = RM*V3 : MAT V4W = RM*V4
1460 MAT V5W = RM*V5 : MAT V6W = RM*V6
1470 RETURN
1990 END
9000 REM procedure to project
9010 VS1(0) = V1W(0,0)+100 : VS1(1) = -V1W(2,0)+90
9020 VS2(0) = V2W(0,0)+100 : VS2(1) = -V2W(2,0)+90
9030 VS3(0) = V3W(0,0)+100 : VS3(1) = -V3W(2,0)+90
9040 VS4(0) = V4W(0,0)+100 : VS4(1) = -V4W(2,0)+90
9050 VS5(0) = V5W(0,0)+100 : VS5(1) = -V5W(2,0)+90
9060 VS6(0) = V6W(0,0)+100 : VS6(1) = -V6W(2,0)+90
10000 REM procedure to draw figure
10005 CLS
10010 LINE VS1(0),VS1(1),VS2(0),VS2(1)
10020 LINE VS1(0),VS1(1),VS3(0),VS3(1)
10030 LINE VS1(0),VS1(1),VS4(0),VS4(1)
10040 LINE VS1(0),VS1(1),VS5(0),VS5(1)
10050 LINE VS2(0),VS2(1),VS3(0),VS3(1)
10060 LINE VS3(0),VS3(1),VS4(0),VS4(1)
10070 LINE VS4(0),VS4(1),VS5(0),VS5(1)
10080 LINE VS5(0),VS5(1),VS2(0),VS2(1)
10090 LINE VS6(0),VS6(1),VS2(0),VS2(1)
10100 LINE VS6(0),VS6(1),VS3(0),VS3(1)
10110 LINE VS6(0),VS6(1),VS4(0),VS4(1)
10120 LINE VS6(0),VS6(1),VS5(0),VS5(1)
10190 RETURN

Discussions