Tuesday, May 31, 2022

More ray tracing in BASIC

More ray tracing

5  POKE65495,0:PMODE 4,1:PCLS:SCREEN1,1
10 SP = 2:O = 0:P = 0.5: DIM C(SP,3),R(SP),Q(SP)
20  FOR K = 1 TO SP: READ C(K,1),C(K,2),C(K,3),T:R(K) = T:Q(K) = T * T: NEXT K
30  DATA -0.3,-0.8,3,0.6
40  DATA 0.9,-1.1,2,0.2
50  FOR I = 0 TO 191: FOR J = 0 TO 255
70 X = 0.3:Y =  - 0.5:Z = 0:DX = J - 128:DY = I - 96
72 DZ = 300:DD = DX * DX + DY * DY + DZ * DZ
100 N =  - 1: IF Y <  = O AND DY > O THEN N = 0:S =  - Y / DY
110  FOR K = 1 TO SP:PX = C(K,1) - X:PY = C(K,2) - Y:PZ = C(K,3) - Z
140 SC = PX * DX + PY * DY + PZ * DZ
150  IF SC <  = O GOTO 200
155 PP = PX * PX + PY * PY + PZ * PZ
160 BB = SC * SC / DD:AA = Q(K) - PP + BB
180  IF AA <  = O GOTO 200
190 SC = ( SQR (BB) -  SQR (AA)) /  SQR (DD): IF SC < S OR N < O THEN N = K:S = SC
200  NEXT K: IF N < 0 GOTO 350
220 DX = DX * S:DY = DY * S:DZ = DZ * S:DD = DD * S * S:X = X + DX:Y = Y + DY:Z = Z + DZ
240  IF N = O GOTO 300
250 NX = X - C(N,1):NY = Y - C(N,2):NZ = Z - C(N,3)
270 L = 2 * (DX * NX + DY * NY + DZ * NZ) / Q(N)
280 DX = DX - NX * L:DY = DY - NY * L:DZ = DZ - NZ * L: GOTO 100
300  FOR K = 1 TO SP:U = C(K,1) - X:V = C(K,3) - Z: IF U * U + V * V <  = Q(K) GOTO 350
320  NEXT K
330  IF ((X -  INT (X)) > P) <  > ((Z -  INT (Z)) > P) THEN  PSET(J,I)
350  NEXT J,I
360  GOTO 360

 

 

 

1 comment:

  1. 5 POKE65497,0:PMODE 4,1:PCLS:SCREEN1,1
    10 SP = 2:O = 0:P = 0.5: DIM C(SP,3),R(SP),Q(SP)
    20 FOR K = 1 TO SP: READ C(K,1),C(K,2),C(K,3),T:R(K) = T:Q(K) = T * T: NEXT K
    30 DATA -0.3,-0.8,3,0.6
    40 DATA 0.9,-1.1,2,0.2
    45 GOSUB10000:REM MATH PATCH
    50 FOR I = 0 TO 191: FOR J = 0 TO 255
    70 X = 0.3:Y = - 0.5:Z = 0:DX = J - 128:DY = I - 96
    72 DZ = 300:DD = DX * DX + DY * DY + DZ * DZ
    100 N = - 1: IF Y < = O AND DY > O THEN N = 0:S = - Y / DY
    110 FOR K = 1 TO SP:PX = C(K,1) - X:PY = C(K,2) - Y:PZ = C(K,3) - Z
    140 SC = PX * DX + PY * DY + PZ * DZ
    150 IF SC < = O GOTO 200
    155 PP = PX * PX + PY * PY + PZ * PZ
    160 BB = SC * SC / DD:AA = Q(K) - PP + BB
    180 IF AA < = O GOTO 200
    190 SC = ( SQR (BB) - SQR (AA)) / SQR (DD): IF SC < S OR N < O THEN N = K:S = SC
    200 NEXT K: IF N < 0 GOTO 350
    220 DX = DX * S:DY = DY * S:DZ = DZ * S:DD = DD * S * S:X = X + DX:Y = Y + DY:Z = Z + DZ
    240 IF N = O GOTO 300
    250 NX = X - C(N,1):NY = Y - C(N,2):NZ = Z - C(N,3)
    270 L = 2 * (DX * NX + DY * NY + DZ * NZ) / Q(N)
    280 DX = DX - NX * L:DY = DY - NY * L:DZ = DZ - NZ * L: GOTO 100
    300 FOR K = 1 TO SP:U = C(K,1) - X:V = C(K,3) - Z: IF U * U + V * V < = Q(K) GOTO 350
    320 NEXT K
    330 IF ((X - INT (X)) > P) < > ((Z - INT (Z)) > P) THEN PSET(J,I)
    350 NEXT J,I
    360 GOTO 360

    REM COCO3 HARDWARE MULTIPLY PATCH.
    REM JUST ADD A GOSUB 10000 AT THE TOP OF YOUR CODE TO ADD IT
    REM ADDRESS WE ARE STORING THE MULTIPLY PATCH IN RAM
    10000 AD=VAL("&HFA0C")
    REM POKE THE MULTIPLY PATCH INTO RAM
    10001 FORI=0 TO 64:READ B$:A=VAL("&H"+B$):POKE AD+I,A:NEXT
    REM MAKE BASIC JUMP TO OUR MULTIPY INSTEAD OF USING IT'S CODE. $BB00 JMP $FA0C
    10002 POKE VAL("&HBB02"),VAL("&H7E"):POKE VAL("&HBB03"),VAL("&HFA"):POKE VAL("&HBB04"),VAL("&H0C")
    10003 RETURN
    10004 DATA 32,79,E7,60,96,60,3D,ED,63,E6,60,96,5E,3D,ED,61,E6,60,96,5D
    10005 DATA 3D,ED,65,E6,60,96,5F,3D,E3,62,ED,62,EC,65,E9,61,89,00,ED,60
    10006 DATA EC,63,D3,15,97,16,D7,63,EC,61,D9,14,99,13,DD,14,A6,60,89,00
    10007 DATA 97,13,32,67,39

    ReplyDelete