QB64.org Forum

Active Forums => Programs => Topic started by: bplus on February 26, 2022, 02:39:54 pm

Title: Ray Trace a translation from SpecBAS
Post by: bplus on February 26, 2022, 02:39:54 pm
by Paul Dunn here was the original SpecBAS code:
Code: [Select]
10 PAPER 0: INK 15: CLS:
 palette 16,255,255,255:
 palette 32,0,192,255:
 palette 255,0,0,192:
 rainbow 16 to 32:
 rainbow 32 to 255
20 read spheres:t=msecs:
 DIM c(spheres,3),r(spheres),q(spheres),cl(4):
 w=scrw/2,h=scrh/2,s=0:
 cl(1)=6,cl(2)=1,
 cl(3)=cl(1)+8,cl(4)=cl(2)+8
30 FOR k=1 TO spheres:
    READ c(k,1),c(k,2),c(k,3),r:
    r(k)=r,q(k)=r*r:
 NEXT k
40 data 6:
 DATA -0.3,-0.8,3,0.6
50 DATA 0.9,-1.4,3.5,0.35:
 data 0.7,-0.45,2.5,0.4:
 data -0.5,-0.3,1.5,0.15:
 DATA 1.0,-0.2,1.5,0.1:
 DATA -0.1,-0.2,1.25,0.2
60 FOR i=1 TO scrh:
    FOR j=0 TO scrw-1
70       x=0.3,y=-0.5,z=0,ba=3:
       dx=j-w,dy=h-i,dz=(scrh/480)*600,
       dd=dx*dx+dy*dy+dz*dz
80       n=-(y>=0 OR dy<=0):
       IF n=0 THEN
          s=-y/dy
90       FOR k=1 TO spheres:
          px=c(k,1)-x,py=c(k,2)-y,pz=c(k,3)-z,
          pp=px*px+py*py+pz*pz,
          sc=px*dx+py*dy+pz*dz:
          IF sc<=0 THEN
             GO TO 120
100          bb=sc*sc/dd,
          aa=q(k)-pp+bb:
          IF aa<=0 THEN
             GO TO 120
110          sc=(SQR bb-SQR aa)/SQR dd:
          IF sc<s OR n<0 THEN
             n=k,s=sc
120       NEXT k
130       IF n<0 THEN
          plot ink 16+(dy*dy/dd)*240;j,scrh-i:
          go to 200
140       dx=dx*s,dy=dy*s,dz=dz*s,dd=dd*s*s,
       x+=dx,y+=dy,z+=dz:
       IF n<>0 THEN
           nx=x-c(n,1),ny=y-c(n,2),nz=z-c(n,3),
          nn=nx*nx+ny*ny+nz*nz,
          l=2*(dx*nx+dy*ny+dz*nz)/nn,
          dx=dx-nx*l,dy=dy-ny*l,dz=dz-nz*l:
          GO TO 80
160       FOR k=1 TO spheres:
          u=c(k,1)-x,
          v=c(k,3)-z:
          IF u*u+v*v<=q(k) THEN
             ba=1:
             go to 180
170       NEXT k
180       IF (x-INT x>.5)=(z-INT z>.5) THEN
          ik=cl(ba)
       else
          ik=cl(ba+1)
190       plot ink ik;j,scrh-i       
200    NEXT j:
 NEXT i
210 print at 0,0;transparent 1;ink 0;"Time: ";(msecs-t)/1000;" secs":
 pause 0:

Thanks to Rod at Just Basic for getting ball rolling for JB version, pretty easy from there to QB64
Code: QB64: [Select]
  1. _Title "RayTrace" 'b+ trans from JB to QB64 2022-02-26
  2. Const scrw = 1024, scrh = 680
  3. Screen _NewImage(scrw, scrh, 32)
  4. _ScreenMove 150, 40
  5. Read spheres
  6. Dim c(spheres, 3), r(spheres), q(spheres), cl(4) As _Unsigned Long
  7. w = scrw / 2
  8. h = scrh / 2
  9. s = 0
  10. cl(1) = _RGB32(120, 65, 45) ' shaddow
  11. cl(2) = _RGB32(0, 0, 100)
  12. cl(3) = _RGB32(255, 255, 0)
  13. cl(4) = _RGB32(0, 0, 200)
  14. For k = 1 To spheres
  15.     Read a, b, c, d
  16.     c(k, 1) = a
  17.     c(k, 2) = b
  18.     c(k, 3) = c
  19.     r = d
  20.     r(k) = r
  21.     q(k) = r * r
  22.  
  23. For i = 1 To scrh
  24.     For j = 0 To scrw - 1
  25.         x = 0.3: y = -0.5: z = 0: ba = 3
  26.         dx = j - w: dy = h - i: dz = (scrh / 480) * 600
  27.         dd = dx * dx + dy * dy + dz * dz
  28.         recursion:
  29.         n = (y >= 0 Or dy <= 0) '* -1   <<< Makes $1000 for knowing where to tap the hammer
  30.         If n = 0 Then s = (y / dy) * -1
  31.         For k = 1 To spheres
  32.             px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  33.             pp = px * px + py * py + pz * pz
  34.             sc = px * dx + py * dy + pz * dz
  35.             If sc <= 0 Then GoTo donek
  36.             bb = sc * sc / dd
  37.             aa = q(k) - pp + bb
  38.             If aa <= 0 Then GoTo donek
  39.             sc = (Sqr(bb) - Sqr(aa)) / Sqr(dd)
  40.             If sc < s Or n < 0 Then n = k: s = sc
  41.             donek:
  42.         Next k
  43.         If n < 0 Then
  44.             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))
  45.             GoTo donej
  46.         End If
  47.         dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  48.         x = x + dx: y = y + dy: z = z + dz
  49.         If n <> 0 Then
  50.             nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  51.             nn = nx * nx + ny * ny + nz * nz
  52.             l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  53.             dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  54.             GoTo recursion
  55.         End If
  56.         For k = 1 To spheres
  57.             u = c(k, 1) - x
  58.             v = c(k, 3) - z
  59.             If u * u + v * v <= q(k) Then ba = 1: Exit For
  60.         Next k
  61.         If (x - Int(x) > .5) = (z - Int(z) > .5) Then
  62.             PSet (j, scrh - i), cl(ba)
  63.         Else
  64.             PSet (j, scrh - i), cl(ba + 1)
  65.         End If
  66.         donej:
  67.     Next j
  68.  
  69. Data -0.3,-0.8,3,0.6
  70. Data 0.9,-1.4,3.5,0.35
  71. Data 0.7,-0.45,2.5,0.4
  72. Data -0.5,-0.3,1.5,0.15
  73. Data 1.0,-0.2,1.5,0.1
  74. Data -0.1,-0.2,1.25,0.2
  75.  
  76.  

 
Title: Re: Ray Trace a translation from SpecBAS
Post by: Pete on February 26, 2022, 03:19:44 pm
Oh balls!

or... Oh balls, there's a bunch of GOTO statements in the translation. Hmmm, let's see. How about...

Code: QB64: [Select]
  1. _TITLE "RayTrace" 'b+ trans from JB to QB64 2022-02-26
  2. CONST scrw = 1024, scrh = 680
  3. SCREEN _NEWIMAGE(scrw, scrh, 32)
  4. _SCREENMOVE 150, 40
  5. READ spheres
  6. DIM c(spheres, 3), r(spheres), q(spheres), cl(4) AS _UNSIGNED LONG
  7. w = scrw / 2
  8. h = scrh / 2
  9. s = 0
  10. cl(1) = _RGB32(120, 65, 45) ' shaddow
  11. cl(2) = _RGB32(0, 0, 100)
  12. cl(3) = _RGB32(255, 255, 0)
  13. cl(4) = _RGB32(0, 0, 200)
  14. FOR k = 1 TO spheres
  15.     READ a, b, c, d
  16.     c(k, 1) = a
  17.     c(k, 2) = b
  18.     c(k, 3) = c
  19.     r = d
  20.     r(k) = r
  21.     q(k) = r * r
  22.  
  23. FOR i = 1 TO scrh
  24.     FOR j = 0 TO scrw - 1
  25.         x = 0.3: y = -0.5: z = 0: ba = 3
  26.         dx = j - w: dy = h - i: dz = (scrh / 480) * 600
  27.         dd = dx * dx + dy * dy + dz * dz
  28.         DO
  29.             n = (y >= 0 OR dy <= 0) '* -1   <<< Makes $1000 for knowing where to tap the hammer
  30.             IF n = 0 THEN s = (y / dy) * -1
  31.             FOR k = 1 TO spheres
  32.                 px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  33.                 pp = px * px + py * py + pz * pz
  34.                 sc = px * dx + py * dy + pz * dz
  35.                 IF sc > 0 THEN
  36.                     bb = sc * sc / dd
  37.                     aa = q(k) - pp + bb
  38.                     IF aa > 0 THEN
  39.                         sc = (SQR(bb) - SQR(aa)) / SQR(dd)
  40.                         IF sc < s OR n < 0 THEN n = k: s = sc
  41.                     END IF
  42.                 END IF
  43.             NEXT k
  44.             IF n < 0 THEN
  45.                 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))
  46.                 EXIT DO
  47.             ELSE
  48.                 dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  49.                 x = x + dx: y = y + dy: z = z + dz
  50.                 IF n <> 0 THEN
  51.                     nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  52.                     nn = nx * nx + ny * ny + nz * nz
  53.                     l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  54.                     dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  55.                 ELSE
  56.                     FOR k = 1 TO spheres
  57.                         u = c(k, 1) - x
  58.                         v = c(k, 3) - z
  59.                         IF u * u + v * v <= q(k) THEN ba = 1: EXIT FOR
  60.                     NEXT k
  61.                     IF (x - INT(x) > .5) = (z - INT(z) > .5) THEN
  62.                         PSET (j, scrh - i), cl(ba)
  63.                     ELSE
  64.                         PSET (j, scrh - i), cl(ba + 1)
  65.                     END IF
  66.                     EXIT DO
  67.                 END IF
  68.             END IF
  69.         LOOP
  70.     NEXT j
  71.  
  72. DATA -0.3,-0.8,3,0.6
  73. DATA 0.9,-1.4,3.5,0.35
  74. DATA 0.7,-0.45,2.5,0.4
  75. DATA -0.5,-0.3,1.5,0.15
  76. DATA 1.0,-0.2,1.5,0.1
  77. DATA -0.1,-0.2,1.25,0.2
  78.  

Looks cool though. Thanks for the heavy lifting!

Pete
Title: Re: Ray Trace a translation from SpecBAS
Post by: SierraKen on February 26, 2022, 05:43:41 pm
Awesome stuff guys! You got me thinking of a rotating Earth globe, so I found B+'s and saved that one too. :D

Here is the link to that: https://qb64forum.alephc.xyz/index.php?topic=4547.0 (https://qb64forum.alephc.xyz/index.php?topic=4547.0)
Title: Re: Ray Trace a translation from SpecBAS
Post by: Phlashlite on February 27, 2022, 04:19:38 am
AWESOME!
Title: Re: Ray Trace a translation from SpecBAS
Post by: FellippeHeitor on February 27, 2022, 07:35:53 am
It's mind-blowing. I can't wrap my head around it. Real cool.
Title: Re: Ray Trace a translation from SpecBAS
Post by: OldMoses on February 27, 2022, 08:12:34 am
I "sort of" recognize certain equations in this, from using a simple, single ray algorithm to check for line of sight occlusions in a 3D environment. Of course rendering these sorts of light and reflective views is what it's really good for. It's stuff that I wish I understood better.

It's amazing how much this is doing, for such a small amount of code. Nicely done.
Title: Re: Ray Trace a translation from SpecBAS
Post by: Dav on February 27, 2022, 10:05:18 am
Very nice code.  I don’t understand it, but it is amazing.  I wonder if it’s possible to modify it to render several pages in advance, with the spheres moving a little, them play back the pages like an animation.

- Dav
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on February 27, 2022, 11:56:17 am
Yeah, Paul Dunn is like Master Gy both genius and both hard as heck to follow ;-))

Also they both prefer to write code horizontally : allot : tersely: for module length blocks : or at least sub length : )
(I am sure the code I showed from original SpecBAS was edited for public consumption who like me tend to prefer vertical script.)

@Pete is better without GoTo's
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on February 27, 2022, 12:13:12 pm
Very nice code.  I don’t understand it, but it is amazing.  I wonder if it’s possible to modify it to render several pages in advance, with the spheres moving a little, them play back the pages like an animation.

- Dav

Dang! Dave that's an idea but can't be pre-drawn because when they move the reflections will be different but an awesome idea! QB64 might be able to render fast enough, guess we will have to check ;-))
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on February 27, 2022, 01:44:26 pm
Update tsh73 had breakthrough with the coloring "Rainbow" part, now the thumbnail matches original SpecBas image.
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Translation later when I have time to study it.
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy on February 27, 2022, 02:46:43 pm
This is huge! I don’t understand how it works, but it’s unbelievable that there are a few lines throughout. Fantastic! Congratulations !!!!
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy on February 27, 2022, 02:48:16 pm
It would be interesting if you could speed up and create a simple animation!
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy on February 27, 2022, 03:28:47 pm
I added. WASD can be used to move forward, backward, left, right, and "-" and "+" keys are used for height.

One sphere circles around the other.
This makes the animation easy to understand.
I still don't understand how it works.
Congratulations to the creator!

Code: QB64: [Select]
  1. _TITLE "RayTrace" 'b+ trans from JB to QB64 2022-02-26
  2. scrw = 400
  3.  
  4.  
  5. SCREEN _NEWIMAGE(scrw, scrh, 32)
  6. READ spheres
  7. DIM c(spheres, 3), r(spheres), q(spheres), cl(4) AS _UNSIGNED LONG
  8. w = scrw / 2
  9. h = scrh / 4
  10. s = 0
  11. cl(1) = _RGB32(120, 65, 45) ' shaddow
  12. cl(2) = _RGB32(0, 0, 100)
  13. cl(3) = _RGB32(255, 255, 0)
  14. cl(4) = _RGB32(0, 0, 200)
  15. FOR k = 1 TO spheres
  16.     READ a, b, c, d
  17.     c(k, 1) = a
  18.     c(k, 2) = b
  19.     c(k, 3) = c
  20.     r = d
  21.     r(k) = r
  22.     q(k) = r * r
  23.  
  24. start_x = 0.3: start_y = -0.2: start_z = -3: start_x = c(1, 1)
  25. dz_temp = (scrh / 480) * 600
  26.  
  27.  
  28.     ang2 = ang2 + .1: rad = 1
  29.     c(2, 1) = c(1, 1) + SIN(ang2) * rad
  30.     c(2, 3) = c(1, 3) + COS(ang2) * rad
  31.  
  32.  
  33.     start_x = start_x + (_KEYDOWN(ASC("a")) - _KEYDOWN(ASC("d"))) * .1
  34.     start_z = start_z + (_KEYDOWN(ASC("s")) - _KEYDOWN(ASC("w"))) * .1
  35.     start_y = start_y + (_KEYDOWN(ASC("+")) - _KEYDOWN(ASC("-"))) * .02
  36.  
  37.  
  38.  
  39.  
  40.     FOR i = 1 TO scrh STEP 1
  41.         FOR j = 0 TO scrw - 1
  42.             x = start_x: y = start_y: z = start_z: ba = 3
  43.             dx = j - w: dy = h - i: dz = dz_temp
  44.             dd = dx * dx + dy * dy + dz * dz
  45.             DO
  46.                 n = (y >= 0 OR dy <= 0) '* -1   <<< Makes $1000 for knowing where to tap the hammer
  47.                 IF n = 0 THEN s = (y / dy) * -1
  48.                 dd_temp = 1 / SQR(dd)
  49.                 FOR k = 1 TO spheres
  50.                     px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  51.                     pp = px * px + py * py + pz * pz
  52.                     sc = px * dx + py * dy + pz * dz
  53.                     IF sc > 0 THEN
  54.                         bb = sc * sc / dd
  55.                         aa = q(k) - pp + bb
  56.                         IF aa > 0 THEN
  57.                             sc = (SQR(bb) - SQR(aa)) * dd_temp
  58.                             IF sc < s OR n < 0 THEN n = k: s = sc
  59.                         END IF
  60.                     END IF
  61.                 NEXT k
  62.                 IF n < 0 THEN
  63.                     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))
  64.                     EXIT DO
  65.                 ELSE
  66.                     dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  67.                     x = x + dx: y = y + dy: z = z + dz
  68.                     IF n <> 0 THEN
  69.                         nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  70.                         nn = nx * nx + ny * ny + nz * nz
  71.                         l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  72.                         dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  73.                     ELSE
  74.                         FOR k = 1 TO spheres
  75.                             u = c(k, 1) - x
  76.                             v = c(k, 3) - z
  77.                             IF u * u + v * v <= q(k) THEN ba = 1: EXIT FOR
  78.                         NEXT k
  79.                         IF (x - INT(x) > .5) = (z - INT(z) > .5) THEN 'x,y pepita size
  80.                             PSET (j, scrh - i), cl(ba)
  81.                         ELSE
  82.                             PSET (j, scrh - i), cl(ba + 1)
  83.                         END IF
  84.                         EXIT DO
  85.                     END IF
  86.                 END IF
  87.             LOOP
  88.         NEXT j
  89.     NEXT i
  90.     _DISPLAY: CLS
  91.  
  92.  
  93. DATA -0.3,-0.8,3,0.6
  94. DATA 0.9,-1.4,3.5,0.35
  95.  
  96. DATA 0.7,-0.45,2.5,0.4
  97. DATA -0.5,-0.3,1.5,0.15
  98. DATA 1.0,-0.2,1.5,0.1
  99. DATA -0.1,-0.2,1.25,0.2
  100.  
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on February 27, 2022, 04:12:22 pm
Well dang! @MasterGy you understood good enough I'd say, nice! Wow!

Now make the spheres bounce around like inside a box, LOL
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy on February 27, 2022, 04:52:40 pm
so you think that ? :)

Code: QB64: [Select]
  1. _TITLE "RayTrace" 'b+ trans from JB to QB64 2022-02-26
  2. scrw = 400
  3.  
  4.  
  5. SCREEN _NEWIMAGE(scrw, scrh, 32)
  6. READ spheres
  7. DIM c(spheres, 9), r(spheres), q(spheres), cl(4) AS _UNSIGNED LONG
  8. w = scrw / 2
  9. h = scrh / 4
  10. s = 0
  11. cl(1) = _RGB32(120, 65, 45) ' shaddow
  12. cl(2) = _RGB32(0, 0, 100)
  13. cl(3) = _RGB32(255, 255, 0)
  14. cl(4) = _RGB32(0, 0, 200)
  15. FOR k = 1 TO spheres
  16.     READ a, b, c, d
  17.     c(k, 1) = a
  18.     c(k, 2) = -(d + .3 + 2 * RND(1))
  19.     c(k, 3) = c
  20.     c(k, 4) = .1
  21.     r = d
  22.     r(k) = r
  23.     q(k) = r * r
  24.  
  25. start_x = 0.3: start_y = -0.2: start_z = -3: start_x = c(1, 1)
  26. dz_temp = (scrh / 480) * 600
  27.  
  28.     'bouncing
  29.     FOR k = 1 TO spheres
  30.         c(k, 4) = c(k, 4) + .01
  31.         ck = c(k, 2) + c(k, 4)
  32.         IF -ck < r(k) THEN c(k, 4) = -.2: ck = -r(k)
  33.         c(k, 2) = ck
  34.     NEXT k
  35.  
  36.     start_x = start_x + (_KEYDOWN(ASC("a")) - _KEYDOWN(ASC("d"))) * .1
  37.     start_z = start_z + (_KEYDOWN(ASC("s")) - _KEYDOWN(ASC("w"))) * .1
  38.     start_y = start_y + (_KEYDOWN(ASC("+")) - _KEYDOWN(ASC("-"))) * .02
  39.  
  40.  
  41.  
  42.  
  43.     FOR i = 1 TO scrh STEP 1
  44.         FOR j = 0 TO scrw - 1
  45.             x = start_x: y = start_y: z = start_z: ba = 3
  46.             dx = j - w: dy = h - i: dz = dz_temp
  47.             dd = dx * dx + dy * dy + dz * dz
  48.             DO
  49.                 n = (y >= 0 OR dy <= 0) '* -1   <<< Makes $1000 for knowing where to tap the hammer
  50.                 IF n = 0 THEN s = (y / dy) * -1
  51.                 dd_temp = 1 / SQR(dd)
  52.                 FOR k = 1 TO spheres
  53.                     px = c(k, 1) - x: py = c(k, 2) - y: pz = c(k, 3) - z
  54.                     pp = px * px + py * py + pz * pz
  55.                     sc = px * dx + py * dy + pz * dz
  56.                     IF sc > 0 THEN
  57.                         bb = sc * sc / dd
  58.                         aa = q(k) - pp + bb
  59.                         IF aa > 0 THEN
  60.                             sc = (SQR(bb) - SQR(aa)) * dd_temp
  61.                             IF sc < s OR n < 0 THEN n = k: s = sc
  62.                         END IF
  63.                     END IF
  64.                 NEXT k
  65.                 IF n < 0 THEN
  66.                     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))
  67.                     EXIT DO
  68.                 ELSE
  69.                     dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
  70.                     x = x + dx: y = y + dy: z = z + dz
  71.                     IF n <> 0 THEN
  72.                         nx = x - c(n, 1): ny = y - c(n, 2): nz = z - c(n, 3)
  73.                         nn = nx * nx + ny * ny + nz * nz
  74.                         l = 2 * (dx * nx + dy * ny + dz * nz) / nn
  75.                         dx = dx - nx * l: dy = dy - ny * l: dz = dz - nz * l
  76.                     ELSE
  77.                         FOR k = 1 TO spheres
  78.                             u = c(k, 1) - x
  79.                             v = c(k, 3) - z
  80.                             IF u * u + v * v <= q(k) THEN ba = 1: EXIT FOR
  81.                         NEXT k
  82.                         IF (x - INT(x) > .5) = (z - INT(z) > .5) THEN 'x,y pepita size
  83.                             PSET (j, scrh - i), cl(ba)
  84.                         ELSE
  85.                             PSET (j, scrh - i), cl(ba + 1)
  86.                         END IF
  87.                         EXIT DO
  88.                     END IF
  89.                 END IF
  90.             LOOP
  91.         NEXT j
  92.     NEXT i
  93.     _DISPLAY: CLS
  94.  
  95.  
  96. DATA -0.3,-0.8,3,0.6
  97. DATA 0.9,-1.4,3.5,0.35
  98.  
  99. DATA 0.7,-0.45,2.5,0.4
  100. DATA -0.5,-0.3,1.5,0.25
  101. DATA 1.0,-0.2,1.5,0.2
  102. DATA -0.1,-0.2,1.25,0.2
  103.  
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on February 27, 2022, 05:31:20 pm
OMG! LOL yeah pretty nice!
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy 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.  
Title: Re: Ray Trace a translation from SpecBAS
Post by: MasterGy 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?
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Ray Trace a translation from SpecBAS
Post by: CharlieJV 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.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus on March 01, 2022, 04:00:35 pm
You guys are doing great things with this, bravo!
Title: Re: Ray Trace a translation from SpecBAS
Post by: bplus 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:
  [ This attachment cannot be displayed inline in 'Print Page' view ]