Author Topic: Ray Trace a translation from SpecBAS  (Read 8535 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #15 on: February 27, 2022, 05:31:20 pm »
OMG! LOL yeah pretty nice!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #16 on: February 27, 2022, 06:42:38 pm »
he didn’t let me calm down to understand a few things.
It works. Walking in space. Look up, down, spin.
WASD + mouse.

Code: QB64: [Select]
  1. _TITLE "RayTrace" 'b+ trans from JB to QB64 2022-02-26
  2. scrw = 400
  3.  
  4.  
  5.  
  6.  
  7. SCREEN _NEWIMAGE(scrw, scrh, 32)
  8. READ spheres
  9. DIM c(spheres, 9), r(spheres), q(spheres), cl(4) AS _UNSIGNED LONG
  10. w = scrw / 2
  11. h = scrh / 4
  12. s = 0
  13. cl(1) = _RGB32(120, 65, 45) ' shaddow
  14. cl(2) = _RGB32(0, 0, 100)
  15. cl(3) = _RGB32(255, 255, 0)
  16. cl(4) = _RGB32(0, 0, 200)
  17. FOR k = 1 TO spheres
  18.     READ a, b, c, d
  19.     c(k, 5) = a
  20.     c(k, 2) = -(d + .3 + 2 * RND(1))
  21.     c(k, 6) = c
  22.     c(k, 4) = .1
  23.     r = d
  24.     r(k) = r
  25.     q(k) = r * r
  26.  
  27. me(1) = -0.2: me(2) = -3: me(0) = c(1, 1): me(3) = _PI
  28.  
  29.  
  30.  
  31.  
  32.     'bouncing
  33.     FOR k = 1 TO spheres
  34.         c(k, 4) = c(k, 4) + .01
  35.         ck = c(k, 2) + c(k, 4)
  36.         IF -ck < r(k) THEN c(k, 4) = -.2: ck = -r(k)
  37.         c(k, 2) = ck
  38.     NEXT k
  39.  
  40.     'control
  41.     mousex = 0: mousey = 0: WHILE _MOUSEINPUT: mousex = mousex + _MOUSEMOVEMENTX: mousey = mousey + _MOUSEMOVEMENTY: WEND
  42.     me(3) = me(3) + mousex * .01
  43.     h = h + mousey * .5
  44.     k_a = _KEYDOWN(ASC("a")): k_d = _KEYDOWN(ASC("d")): k_w = _KEYDOWN(ASC("w")): k_s = _KEYDOWN(ASC("s"))
  45.     go_ang = -(k_a * 90 + k_d * -90 + k_s * 180 - 180) * (_PI / 180) - me(3)
  46.     go = (k_a + k_d + k_w + k_s = -1) * .2
  47.     me(0) = me(0) + SIN(go_ang) * go
  48.     me(2) = me(2) - COS(go_ang) * go
  49.     me(1) = me(1) + (_KEYDOWN(ASC("+")) - _KEYDOWN(ASC("-"))) * .02
  50.  
  51.     'spheres rotating
  52.     FOR k = 1 TO spheres
  53.         c(k, 1) = c(k, 5) - me(0)
  54.         c(k, 3) = c(k, 6) - me(2)
  55.         rotate_2d c(k, 1), c(k, 3), me(3)
  56.         c(k, 1) = c(k, 1) + me(0)
  57.         c(k, 3) = c(k, 3) + me(2)
  58.     NEXT k
  59.  
  60.  
  61.     FOR i = 1 TO scrh STEP 1
  62.         FOR j = 0 TO scrw - 1
  63.             x = me(0): y = me(1): z = me(2): ba = 3
  64.             dx = j - w: dy = h - i: dz = scrw
  65.             dd = dx * dx + dy * dy + dz * dz
  66.             neverend_cycle_agent = 0
  67.             DO: neverend_cycle_agent = neverend_cycle_agent + 1: IF neverend_cycle_agent > 2000 THEN EXIT DO
  68.                 n = (y >= 0 OR dy <= 0) '* -1   <<< Makes $1000 for knowing where to tap the hammer
  69.                 IF n = 0 THEN s = (y / dy) * -1
  70.                 dd_temp = 1 / SQR(dd)
  71.                 FOR k = 1 TO spheres
  72.                     px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  73.                     pp = px * px + py * py + pz * pz
  74.                     sc = px * dx + py * dy + pz * dz
  75.                     IF sc > 0 THEN
  76.                         bb = sc * sc / dd
  77.                         aa = q(k) - pp + bb
  78.                         IF aa > 0 THEN
  79.                             sc = (SQR(bb) - SQR(aa)) * dd_temp
  80.                             IF sc < s OR n < 0 THEN n = k: s = sc
  81.                         END IF
  82.                     END IF
  83.                 NEXT k
  84.                 IF n < 0 THEN
  85.                     PSET (j, scrh - i), _RGB32(128 * (scrh - i) / scrh + 128 * (dy * dy / dd), 128 * (scrh - i) / scrh + 128 * (dy * dy / dd), 200 + 55 * (dy * dy / dd))
  86.                     EXIT DO
  87.                 ELSE
  88.                     dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  89.                     x = x + dx: y = y + dy: z = z + dz
  90.                     IF n <> 0 THEN
  91.                         nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  92.                         nn = nx * nx + ny * ny + nz * nz
  93.                         l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  94.                         dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  95.                     ELSE
  96.                         FOR k = 1 TO spheres
  97.                             u = c(k, 1) - x
  98.                             v = c(k, 3) - z
  99.                             IF u * u + v * v <= q(k) THEN ba = 1: EXIT FOR
  100.                         NEXT k
  101.  
  102.                         x2 = x - me(0): z2 = z - me(2): rotate_2d x2, z2, -me(3): x2 = x2 + me(0): z2 = z2 + me(2) 'field rotating
  103.  
  104.                         IF (x2 - INT(x2) > .5) = (z2 - INT(z2) > .5) THEN 'x,y pepita size
  105.                             PSET (j, scrh - i), cl(ba)
  106.                         ELSE
  107.                             PSET (j, scrh - i), cl(ba + 1)
  108.                         END IF
  109.                         EXIT DO
  110.                     END IF
  111.                 END IF
  112.             LOOP
  113.         NEXT j
  114.     NEXT i
  115.     _DISPLAY: CLS
  116.  
  117.  
  118. DATA -0.3,-0.8,3,0.6
  119. DATA 0.9,-1.4,3.5,0.35
  120.  
  121. DATA 0.7,-0.45,2.5,0.4
  122. DATA -0.5,-0.3,1.5,0.25
  123. DATA 1.0,-0.2,1.5,0.2
  124. DATA -0.1,-0.2,1.25,0.2
  125.  
  126.  
  127. SUB rotate_2d (x, y, ang): x1 = -(x * COS(ang) - y * SIN(ang)): y1 = -(x * SIN(ang) + y * COS(ang)): x = x1: y = y1: END SUB
  128.  
  129.  

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #17 on: February 28, 2022, 08:51:49 am »
I tried something else. If we calculate an image as soil, it still works very well. If we expose the dog to the ground, the dog cannot be recognized on the ground because we are close to the ground and cannot see it at the same time. But if we approach the spheres, the dog can be recognized in the sphere due to the reflection! We can raise the "SCRW" high, it makes a very nice picture, it will only be difficult to navigate because it will be very slow.

Do you see the dog in the sphere?
 
forum.jpg
* ray2.zip (Filesize: 12.26 KB, Downloads: 251)

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #18 on: March 01, 2022, 01:53:28 pm »
Nothing as wickedly wild as what Master Gy has done.

It is still a bunch of fun mindlessly playing with colors.

 
spheres.png

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #19 on: March 01, 2022, 04:00:35 pm »
You guys are doing great things with this, bravo!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ray Trace a translation from SpecBAS
« Reply #20 on: March 02, 2022, 07:00:02 pm »
Ah! I have tsh73 translation with the Rainbow Sub solved. Now the colors are as they were from the original code ie the sky is no long flat blue PLUS I added some Mods that move the balls and Move your mouse for surprise! (Well not now I guess).

Code: QB64: [Select]
  1. 'https://retrobasic.allbasic.info/index.php?topic=721.0
  2. 'converting to JB. Tsh73 Feb 2022
  3. 'converting to QB64 b+ 2022-03-02 and Mods >>>>   moving balls, move your mouse!
  4. _Title "Move your mouse!"
  5. Const scrw = 320, scrh = 200
  6. Screen _NewImage(scrw, scrh, 32)
  7.  
  8. qb(0) = &HFF000000
  9. qb(1) = &HFF000088
  10. qb(2) = &HFF008800
  11. qb(3) = &HFF008888
  12. qb(4) = &HFF880000
  13. qb(5) = &HFF880088
  14. qb(6) = &HFF888800
  15. qb(7) = &HFFCCCCCC
  16. qb(8) = &HFF888888
  17. qb(9) = &HFF0000FF
  18. qb(10) = &HFF00FF00
  19. qb(11) = &HFF00FFFF
  20. qb(12) = &HFFFF0000
  21. qb(13) = &HFFFF00FF
  22. qb(14) = &HFFFFFF00
  23. qb(15) = &HFFFFFFFF
  24.  
  25.  
  26. For i = 0 To 15: pal(i) = qb(i): Next
  27. ' pal 16,255,255,255:
  28. pal(16) = &HFFFFFFFF
  29. ' pal 32,0,192,255:
  30. pal(32) = &HFF00CCFF
  31. ' pal 255,0,0,192:
  32. pal(255) = &HFF0000CC
  33. ' rainbow 16 to 32:
  34. rainbow 16, 32
  35. ' rainbow 32 to 255
  36. rainbow 32, 255
  37.  
  38. Read spheres
  39. Dim c(spheres, 3), r(spheres), q(spheres), cl(4), d(spheres, 3)
  40. w = scrw / 2: h = scrh / 2: s = 0
  41. cl(1) = 6: cl(2) = 1
  42. cl(3) = cl(1) + 8: cl(4) = cl(2) + 8
  43. For k = 1 To spheres
  44.     Read c1, c2, c3, r
  45.     c(k, 1) = c1: c(k, 2) = c2: c(k, 3) = c3
  46.     d(k, 1) = .01 * (Rnd - .5): d(k, 2) = .01 * (Rnd - .5): d(k, 3) = .01 * (Rnd - .5)
  47.     r(k) = r: q(k) = r * r
  48.  
  49. Data -0.3,-0.8,3,0.6
  50.  
  51. Data 0.9,-1.4,3.5,0.35
  52. Data 0.7,-0.45,2.5,0.4
  53. Data -0.5,-0.3,1.5,0.15
  54. Data 1.0,-0.2,1.5,0.1
  55. Data -0.1,-0.2,1.25,0.2
  56.  
  57.     lc = lc + 1
  58.     If lc = 50 Then toggle = 1 - toggle: lc = 0
  59.     For k = 1 To spheres
  60.         If toggle Then
  61.             c(k, 1) = c(k, 1) + d(k, 1): c(k, 2) = c(k, 2) + d(k, 2): c(k, 3) = c(k, 3) + d(k, 3)
  62.         Else
  63.             c(k, 1) = c(k, 1) - d(k, 1): c(k, 2) = c(k, 2) - d(k, 2): c(k, 3) = c(k, 3) - d(k, 3)
  64.         End If
  65.     Next
  66.     c(spheres, 1) = -2.5 * (.4 - _MouseX / _Width): c(spheres, 2) = -2. * (.75 * _Height - _MouseY) / _Height: c(spheres, 3) = 2
  67.     For i = 1 To scrh
  68.         For j = 0 To scrw - 1
  69.  
  70.             x = 0.3: y = -0.5: z = 0: ba = 3
  71.             dx = j - w: dy = h - i: dz = (scrh / 480) * 600
  72.             dd = dx * dx + dy * dy + dz * dz
  73.             recurs:
  74.             b1 = (y >= 0): b2 = (dy <= 0)
  75.             b3 = -(b1 Or b2)
  76.             n = n - b3
  77.             n = 0 - -(y >= 0 Or dy <= 0)
  78.  
  79.             If n = 0 Then s = 0 - y / dy
  80.             For k = 1 To spheres
  81.                 px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  82.                 pp = px * px + py * py + pz * pz
  83.                 sc = px * dx + py * dy + pz * dz
  84.                 If sc <= 0 Then GoTo continueK
  85.                 bb = sc * sc / dd
  86.                 aa = q(k) - pp + bb
  87.                 If aa <= 0 Then GoTo continueK
  88.                 sc = (Sqr(bb) - Sqr(aa)) / Sqr(dd)
  89.                 If sc < s Or n < 0 Then n = k: s = sc
  90.                 continueK:
  91.             Next k
  92.  
  93.             If n < 0 Then
  94.                 'plot ink 16+(dy*dy/dd)*240;j,scrh-i
  95.                 c = Int(16 + (dy * dy / dd) * 240)
  96.                 '#gr "color ";c;" ";c;" ";c
  97.                 If lastCol <> c Then 'prevent extra color switching - JB speed-up
  98.                     Color pal(c)
  99.                     lastCol = c
  100.                 End If
  101.                 PSet (j, scrh - i)
  102.                 GoTo continueJ
  103.             End If
  104.             dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  105.             x = x + dx: y = y + dy: z = z + dz
  106.             If n <> 0 Then
  107.                 nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  108.                 nn = nx * nx + ny * ny + nz * nz
  109.                 l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  110.                 dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  111.                 GoTo recurs 'really only GOTO
  112.             End If
  113.             For k = 1 To spheres
  114.                 u = c(k, 1) - x
  115.                 v = c(k, 3) - z
  116.                 If u * u + v * v <= q(k) Then
  117.                     ba = 1
  118.                     Exit For
  119.                 End If
  120.             Next k
  121.             If (x - Int(x) > .5) = (z - Int(z) > .5) Then
  122.                 'needs "int to lower" vs "int to 0"
  123.                 'If (x - (Int(x) - (x <> Int(x)) * (x < 0)) > .5) = (z - (Int(z) - (z <> Int(z)) * (z < 0)) > .5) Then
  124.                 'If (x Mod 1 + (x < 0) > .5) = (z Mod 1 + (z < 0) > .5) Then
  125.                 ik = cl(ba)
  126.             Else
  127.                 ik = cl(ba + 1)
  128.             End If
  129.             'plot ink ik;j,scrh-i
  130.             '#gr "color ";ik;" ";ik;" ";ik
  131.             If lastCol <> ik Then 'prevent extra color switching
  132.                 Color pal(ik)
  133.                 lastCol = ik
  134.             End If
  135.             PSet (j, scrh - i)
  136.             continueJ:
  137.         Next j
  138.     Next i
  139.     _Display
  140.  
  141. Sub rainbow (startIdx, stopIdx)
  142.     r0 = _Red32(pal(startIdx))
  143.     r1 = _Red32(pal(stopIdx))
  144.     g0 = _Green32(pal(startIdx))
  145.     g1 = _Green32(pal(stopIdx))
  146.     b0 = _Blue32(pal(startIdx))
  147.     b1 = _Blue32(pal(stopIdx))
  148.     For i = startIdx + 1 To stopIdx - 1
  149.         a = 1 - (stopIdx - i) / (stopIdx - startIdx) 'startIdx..stopIdx -> 0..1
  150.         R = Int(r0 * (1 - a) + r1 * a)
  151.         G = Int(g0 * (1 - a) + g1 * a)
  152.         B = Int(b0 * (1 - a) + b1 * a)
  153.         pal(i) = _RGB32(R, G, B)
  154.     Next
  155.  
  156.  

Colors as they should be:
 
RayTrace 2 JB trans.PNG