Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raytracing (started by Bplus)
#1
Hello!

It's not qb64, but it started from here. A few years ago, Forum member Bplus shared a raytracing code with us. I will paste this here. I really liked this and tried to find the points in the program where the camera view can be adjusted and a real-time 3d world can be walked around. I managed to do it here in qb64, but unfortunately the processing is slow and I stopped. For a few months, I have been studying the b4a developer, in which you can write for Android in the basic language.

I only want to show this now because it started with these few lines of code. If I didn't come across the code then, I might not have written this program either.
The program runs beautifully on an Android device, and you can perceive the depths in 3D with cheap VR glasses available for phones.

Thank you Bplus in retrospect! Smile


Code: (Select All)
_TITLE "RayTrace" 'b+ trans from JB to QB64 2022-02-26
CONST scrw = 1024, scrh = 680
SCREEN _NEWIMAGE(scrw, scrh, 32)
_SCREENMOVE 150, 40
READ spheres
DIM c(spheres, 3), r(spheres), q(spheres), cl(4) AS _UNSIGNED LONG
w = scrw / 2
h = scrh / 2
s = 0
cl(1) = _RGB32(120, 65, 45) ' shaddow
cl(2) = _RGB32(0, 0, 100)
cl(3) = _RGB32(255, 255, 0)
cl(4) = _RGB32(0, 0, 200)
FOR k = 1 TO spheres
    READ a, b, c, d
    c(k, 1) = a
    c(k, 2) = b
    c(k, 3) = c
    r = d
    r(k) = r
    q(k) = r * r
NEXT k

FOR i = 1 TO scrh
    FOR j = 0 TO scrw - 1
        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
        DO
            n = (y >= 0 OR dy <= 0) '* -1  <<< Makes $1000 for knowing where to tap the hammer
            IF n = 0 THEN s = (y / dy) * -1
            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
                    bb = sc * sc / dd
                    aa = q(k) - pp + bb
                    IF aa > 0 THEN
                        sc = (SQR(bb) - SQR(aa)) / SQR(dd)
                        IF sc < s OR n < 0 THEN n = k: s = sc
                    END IF
                END IF
            NEXT k
            IF n < 0 THEN
                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))
                EXIT DO
            ELSE
                dx = dx * s: dy = dy * s: dz = dz * s: dd = dd * s * s
                x = x + dx: y = y + dy: z = 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
                ELSE
                    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: EXIT FOR
                    NEXT k
                    IF (x - INT(x) > .5) = (z - INT(z) > .5) THEN
                        PSET (j, scrh - i), cl(ba)
                    ELSE
                        PSET (j, scrh - i), cl(ba + 1)
                    END IF
                    EXIT DO
                END IF
            END IF
        LOOP
    NEXT j
NEXT i
SLEEP

DATA 6
DATA -0.3,-0.8,3,0.6
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





APK (installer to android)
https://drive.google.com/file/d/15qqP9Y_...sp=sharing
Reply
#2
@MasterGy wow! nice even have music that goes with it!

That code came from Paul Dunn of SpecBas and he might have gotten it from someone else too!

Update:
Paul Dunn sure like the mods!
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)