Author Topic: Texel Raytracer by Antoni Gual  (Read 2160 times)

Offline The Librarian

  • Moderator
Texel Raytracer by Antoni Gual
« on: March 17, 2018, 09:39:44 AM »
Texel Raytracer

Author: Antoni Gual
Source: petesqbsite
URL: http://www.petesqbsite.com/downloads/graphics.shtml
Version: 2004
Tags: [3d], [raytrace], [poke]

Description:
Pure QB Realtime Raytracer Demo. Translated to/optimized for QB by Antoni Gual agual@eic.ictnet.es. The original was written in C by Texel, a Spanish demo coder [...]

Source Code:
Code: QB64 [Select]
  1. 'Pure QB Realtime Raytracer Demo
  2. 'Translated to/optimized for QB by Antoni Gual agual@eic.ictnet.es
  3. 'The original was written in C by Texel, a Spanish demo coder.
  4. 'It will not work in the IDE due to integer overflow errors.
  5. 'Compile with QB 4.0 or QB4.5 + ffix. It does 12.5 fps in my P4 1,4.
  6. 'The C version (DOS protected mode, DJGPP) does 50 fps :(
  7.  
  8. 'ffix
  9. CONST objnum = 4
  10.  
  11. DIM n AS INTEGER, K AS INTEGER, OBJMIN AS INTEGER, OBJMIN2 AS INTEGER
  12. DIM OBJ(objnum) AS INTEGER, l AS INTEGER, posi AS INTEGER, POS2 AS INTEGER
  13. DIM s AS INTEGER, t(8200) AS INTEGER, XX AS INTEGER, YY AS INTEGER, XQ AS INTEGER
  14. DIM YQ AS INTEGER, mmmm AS INTEGER, xx1 AS INTEGER, yy1 AS INTEGER
  15. DIM t2(8200) AS INTEGER, ipos AS INTEGER
  16.  
  17. DIM A(objnum) AS SINGLE, B(objnum) AS SINGLE, C(objnum) AS SINGLE
  18. DIM R(objnum) AS SINGLE
  19.  
  20. DEF SEG = &HA000
  21. 'Cambiar la paleta a tonos de azul
  22. OUT &H3C8, 0 '
  23. FOR n = 0 TO 127
  24.     OUT &H3C9, 0
  25.     OUT &H3C9, INT(n / 4)
  26.     OUT &H3C9, INT(n / 2)
  27. FOR n = 0 TO 127
  28.     OUT &H3C9, INT(n / 2)
  29.     OUT &H3C9, INT(31 + n / 4)
  30.     OUT &H3C9, 63
  31. D = 230
  32. l = 0
  33.    
  34. 'four objects
  35. OBJ(0) = 0: A(0) = -50 + l: B(0) = 0: C(0) = -100: R(0) = -55 * 55
  36. OBJ(1) = 0: A(1) = 50 - l: B(1) = -25: C(1) = -120: R(1) = -55 * 55
  37. OBJ(2) = 0: A(2) = 0: B(2) = 500: C(2) = -220: R(2) = -500! * 500
  38. OBJ(3) = 1: A(3) = 60: B(3) = -35: C(3) = -30
  39.  
  40. tt! = TIMER
  41. FOR l = 0 TO 199
  42.        
  43.     A(0) = -50 + l
  44.     A(1) = 50 - l
  45.     posi = 400
  46.     mmmm = -1
  47.     'calculamos uno de cada 4 pixels a buffer t()
  48.     FOR Y = -40 TO 39 STEP 2
  49.         FOR X = -80 TO 79 STEP 2
  50.             X0 = X
  51.             Y0 = Y
  52.             GOSUB raytrace
  53.             t(posi) = COL
  54.             posi = posi + 1
  55.         NEXT
  56.     NEXT
  57.     posi = 482
  58.     POS2 = 0
  59.     'calculamos pixels restantes, interpolando si podemos
  60.     FOR YQ = 6 TO 43
  61.         FOR XQ = 2 TO 77
  62.             'interpolar
  63.             IF t2(posi) = t2(posi + 1) AND t2(posi) = t2(posi + 80) AND t2(posi) = t2(posi + 81) THEN
  64.                 ipos = (YQ * 1280 + (XQ * 4))
  65.                 FOR YY = 0 TO 3
  66.                     FOR XX = 0 TO 3
  67.                         POKE ipos, (YY * (t(posi + 80) * (4 - XX) + t(posi + 81) * XX) + (t(posi) * (4 - XX) + t(posi + 1) * XX) * (4 - YY)) \ 16
  68.                         ipos = ipos + 1
  69.                     NEXT
  70.                     ipos = ipos + 316
  71.                 NEXT
  72.                 'no interpolar
  73.             ELSE
  74.                 mmmm = 0
  75.                 FOR yy1 = 0 TO 3
  76.                     FOR xx1 = 0 TO 3
  77.                         IF xx1 OR yy1 THEN
  78.                             X0 = (-160 + XQ * 4 + xx1) / 2
  79.                             Y0 = (-100 + YQ * 4 + yy1) / 2
  80.                             GOSUB raytrace
  81.                             POKE (YQ * 4 + yy1) * 320 + XQ * 4 + xx1, COL
  82.                         ELSE
  83.                             POKE YQ * 1280 + XQ * 4, t(posi)
  84.                         END IF
  85.                     NEXT
  86.                 NEXT
  87.             END IF
  88.             posi = posi + 1
  89.         NEXT
  90.         posi = posi + 4
  91.     NEXT
  92. COLOR 255: PRINT l / (TIMER - tt!)
  93. KK$ = INPUT$(1)
  94.  
  95. raytrace:
  96. Z0 = 0
  97. MD = 1 / SQR(X0 * X0 + Y0 * Y0 + D * D)
  98. X1 = X0 * MD
  99. Y1 = Y0 * MD
  100. Z1 = -(D + Z0) * MD
  101. K = 0
  102. COL = 0
  103. OBJMIN = objnum
  104. IF mmmm THEN t2(posi) = objnum
  105.     TMIN = 327680
  106.     FOR n = 0 TO 2
  107.         IF OBJ(n) = 0 AND (OBJ(n) <> OBJMIN) THEN
  108.             A0 = A(n) - X0
  109.             B0 = B(n) - Y0
  110.             C0 = C(n) - Z0
  111.             TB = A0 * X1 + B0 * Y1 + C0 * Z1
  112.             RZ = TB * TB - A0 * A0 - B0 * B0 - C0 * C0
  113.             IF RZ >= R(n) THEN
  114.                 TN = TB - SQR(RZ - R(n))
  115.                 IF TN < TMIN AND TN > 0 THEN TMIN = TN: OBJMIN2 = n
  116.             END IF
  117.         END IF
  118.     NEXT
  119.     OBJMIN = OBJMIN2
  120.     IF TMIN < 327680 AND (OBJ(OBJMIN) = 0) THEN
  121.         IF mmmm THEN t2(posi) = t2(posi) * K * objnum * 3 + OBJMIN
  122.         X0 = X0 + X1 * TMIN
  123.         Y0 = Y0 + Y1 * TMIN
  124.         Z0 = Z0 + Z1 * TMIN
  125.         NX = X0 - A(OBJMIN)
  126.         NY = Y0 - B(OBJMIN)
  127.         NZ = Z0 - C(OBJMIN)
  128.         CA = 2 * (NX * X1 + NY * Y1 + NZ * Z1) / (NX * NX + NY * NY + NZ * NZ + 1)
  129.         X1 = X1 - NX * CA
  130.         Y1 = Y1 - NY * CA
  131.         Z1 = Z1 - NZ * CA
  132.         A2 = A(3) - X0
  133.         B2 = B(3) - Y0
  134.         C2 = C(3) - Z0
  135.         MV = 1 / SQR(A2 * A2 + B2 * B2 + C2 * C2)
  136.         A2 = A2 * MV
  137.         B2 = B2 * MV
  138.         C2 = C2 * MV
  139.         s = 0
  140.         FOR n = 0 TO 2
  141.             IF OBJ(n) = 0 AND NOT s THEN
  142.                 A0 = X0 - A(n)
  143.                 B0 = Y0 - B(n)
  144.                 C0 = Z0 - C(n)
  145.                 TB = A2 * A0 + B2 * B0 + C2 * C0
  146.                 RZ = TB * TB - A0 * A0 - B0 * B0 - C0 * C0
  147.                 IF RZ >= R(n) AND TB < 0 THEN s = -1: IF mmmm THEN t2(posi) = t2(posi) * 32
  148.             END IF
  149.         NEXT
  150.         IF NOT s THEN
  151.             IF mmmm THEN t2(posi) = t2(posi) + 1
  152.             col2 = X1 * A2 + Y1 * B2 + Z1 * C2
  153.             IF col2 < 0 THEN col2 = 0
  154.             cc = col2 * col2
  155.             col2 = cc * cc
  156.             MV = SQR(NX * NX + NY * NY + NZ * NZ)
  157.             'IF COL2 < 0 THEN COL2 = 0
  158.             col2 = col2 + (NX * A2 + NY * B2 + NZ * C2) / MV
  159.             IF col2 < 0 THEN col2 = 0
  160.             COL = COL + col2 / ((K + 1) * (K + 1) * 2)
  161.             IF COL > 1 THEN COL = 1
  162.         END IF
  163.         K = K + 1
  164.     END IF
  165. LOOP WHILE TMIN < 327680 AND K <= 2
  166. IF K = 0 THEN COL = 50 ELSE COL = COL * 255
  167.  

« Last Edit: August 08, 2018, 11:17:06 PM by odin »