Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D rendering of Game of Life by ubi44
#1
This code is far too good to NOT share here, but to be clear I did not write this, it comes from avatar name ubi44.

Code: (Select All)
Screen _NewImage(640, 488, 32)
_AllowFullScreen _SquarePixels
_FullScreen _SquarePixels , _Smooth
Dim As Integer MAX, MAX2

start:
MAX = 25 + Int(Rnd * 25): MAX2 = MAX / 2
ReDim U(MAX, MAX, MAX) As _Byte
ReDim U2(MAX, MAX, MAX) As _Byte

MoveZ = -MAX2 / 2
rand = (Rnd - Rnd * .25) * .2
Randomize Timer
t = 0
For x = 1 To MAX - 1
    For y = 1 To MAX - 1
        For z = 1 To MAX - 1
            If Rnd < .5 + rand Then U(x, y, z) = 1: t = t + 1 Else U(x, y, z) = -1
Next z, y, x
Dim Shade(255) As Long
For c = 0 To 255
    i& = _NewImage(1, 1, 32)
    _Dest i&
    Line (0, 0)-(_Width(i&), _Height(i&)), _RGB(c, c, c), BF
    Shade(c) = _CopyImage(i&, 33)
    _FreeImage i&
Next c
Do
    TEX = .4 - (MoveZ + 10.5) * .001
    If _FullScreen Then _MouseHide Else _MouseShow
    If _KeyDown(18432) Then MoveZ = MoveZ - 1
    If _KeyDown(20480) Then MoveZ = MoveZ + 1
    Cls
    frames% = frames% + 1
    If oldtime$ <> Time$ Then
        fps = frames%
        frames% = 1
        oldtime$ = Time$
    End If
    _Limit 10
    Color _RGB(0, 0, 0), _RGB(200, 200, 200)
    Print t; " first gen cell alive "; b; "live cell at now!"
    Color _RGB(0, 0, 0), _RGB(227, 227, 227)
    Print "up down arrow key to move forward or backward"
    Color _RGB(0, 0, 0), _RGB(255, 255, 255)
    Print "space to restart || current grid:"; MAX; "^3"
    Color _RGB(255, 255, 255), _RGB(0, 0, 0)
    a = a + (fps) / 160
    If a > 360 Then a = 0
    cos1 = Cos(a)
    sin1 = Sin(a)

    For x = 1 To MAX - 1
        For y = 1 To MAX - 1
            For z = 1 To MAX - 1
                If U(x, y, z) = 1 Then
                    'set up cube
                    ix = (x - MAX2) - TEX: iy = (y - MAX2) - TEX: iz = (z - MAX2) - TEX
                    jx = (x - MAX2) + TEX: jy = (y - MAX2) - TEX: jz = (z - MAX2) - TEX
                    kx = (x - MAX2) + TEX: ky = (y - MAX2) + TEX: kz = (z - MAX2) - TEX
                    lx = (x - MAX2) - TEX: ly = (y - MAX2) + TEX: lz = (z - MAX2) - TEX

                    mx = (x - MAX2) - TEX: my = (y - MAX2) - TEX: mz = (z - MAX2) + TEX
                    nx = (x - MAX2) + TEX: ny = (y - MAX2) - TEX: nz = (z - MAX2) + TEX
                    ox = (x - MAX2) + TEX: oy = (y - MAX2) + TEX: oz = (z - MAX2) + TEX
                    px = (x - MAX2) - TEX: py = (y - MAX2) + TEX: pz = (z - MAX2) + TEX
                    'rotation x/z
                    ax = (ix) * cos1 - (iz) * sin1 '
                    az = (ix) * sin1 + (iz) * cos1 '
                    ay = iy
                    bx = (jx) * cos1 - (jz) * sin1
                    bz = (jx) * sin1 + (jz) * cos1 '
                    by = jy
                    cx = (kx) * cos1 - (kz) * sin1
                    cz = (kx) * sin1 + (kz) * cos1 '
                    cy = ky
                    dx = (lx) * cos1 - (lz) * sin1
                    dz = (lx) * sin1 + (lz) * cos1 '
                    dy = ly

                    ex = (mx) * cos1 - (mz) * sin1
                    ez = (mx) * sin1 + (mz) * cos1 '
                    ey = my
                    fx = (nx) * cos1 - (nz) * sin1
                    fz = (nx) * sin1 + (nz) * cos1 '
                    fy = ny
                    gx = (ox) * cos1 - (oz) * sin1 '
                    gz = (ox) * sin1 + (oz) * cos1 '
                    gy = oy
                    hx = (px) * cos1 - (pz) * sin1
                    hz = (px) * sin1 + (pz) * cos1 '
                    hy = py

                    'front
                    push = -(MAX + MAX2 + MoveZ)
                    u = checkV(ax, ay, az - push, bx, by, bz - push, dx, dy, dz - push)
                    If u > 0 Then
                        col% = maxi((205 - (((dz + MAX2)) / MAX) * 205) * u + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(ax, ay, -az + push)-(bx, by, -bz + push)-(dx, dy, -dz + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(bx, by, -bz + push)-(cx, cy, -cz + push)-(dx, dy, -dz + push)
                    End If
                    'back
                    u = checkV(hx, hy, hz - push, fx, fy, fz - push, ex, ey, ez - push)
                    If u > 0 Then
                        col% = maxi((205 - (((fz + MAX2)) / MAX) * 205) * u + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(ex, ey, -ez + push)-(fx, fy, -fz + push)-(hx, hy, -hz + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(fx, fy, -fz + push)-(gx, gy, -gz + push)-(hx, hy, -hz + push)
                    End If
                    'left
                    u = checkV(dx, dy, dz - push, ex, ey, ez - push, ax, ay, az - push)
                    If u > 0 Then
                        col% = maxi((205 - (((ez + MAX2)) / MAX) * 205) * (u) + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(ax, ay, -az + push)-(ex, ey, -ez + push)-(dx, dy, -dz + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(ex, ey, -ez + push)-(hx, hy, -hz + push)-(dx, dy, -dz + push)
                    End If
                    ''right
                    u = checkV(fx, fy, fz - push, cx, cy, cz - push, bx, by, bz - push)
                    If u > 0 Then
                        col% = maxi((205 - (((fz + MAX2)) / MAX) * 205) * (u) + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(bx, by, -bz + push)-(cx, cy, -cz + push)-(fx, fy, -fz + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(cx, cy, -cz + push)-(gx, gy, -gz + push)-(fx, fy, -fz + push)
                    End If
                    'up
                    u = checkV(bx, by, bz - push, ax, ay, az - push, ex, ey, ez - push)
                    If u > 0 Then
                        col% = maxi((205 - (((ez + MAX2)) / MAX) * 205) * (u) + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(ax, ay, -az + push)-(bx, by, -bz + push)-(ex, ey, -ez + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(bx, by, -bz + push)-(ex, ey, -ez + push)-(fx, fy, -fz + push)
                    End If
                    'down
                    u = checkV(hx, hy, hz - push, dx, dy, dz - push, cx, cy, cz - push)
                    If u > 0 Then
                        col% = maxi((205 - (((hz + MAX2)) / MAX) * 205) * (u) + 25)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(cx, cy, -cz + push)-(dx, dy, -dz + push)-(hx, hy, -hz + push)
                        _MapTriangle (0, 0)-(_Width(Shade(col%)), 0)-(0, _Height(Shade(col%))), Shade(col%) To(cx, cy, -cz + push)-(hx, hy, -hz + push)-(gx, gy, -gz + push)
                    End If
                End If
    Next z, y, x
    For x = 1 To MAX - 1
        For y = 1 To MAX - 1
            For z = 1 To MAX - 1
                mm = 0
                If U(x, y, z) = 1 Then
                    For xx = x - 1 To x + 1
                        For yy = y - 1 To y + 1
                            For zz = z - 1 To z + 1
                                If x = xx And y = yy And z = zz Then _Continue
                                If U(xx, yy, zz) = 1 Then mm = mm + 1
                    Next zz, yy, xx
                    If mm < 9 Or mm > 18 Then U2(x, y, z) = -1 Else U2(x, y, z) = 1
                Else
                    For xx = x - 1 To x + 1
                        For yy = y - 1 To y + 1
                            For zz = z - 1 To z + 1
                                If x = xx And y = yy And z = zz Then _Continue
                                If U(xx, yy, zz) = 1 Then mm = mm + 1
                    Next zz, yy, xx
                    If (mm > 12 And mm < 18) Then U2(x, y, z) = 1 Else U2(x, y, z) = -1
                End If
    Next z, y, x
    _Display
    b = 0
    For x = 1 To MAX - 1
        For y = 1 To MAX - 1
            For z = 1 To MAX - 1
                U(x, y, z) = U2(x, y, z)
                If U(x, y, z) = 1 Then b = b + 1
    Next z, y, x
    If _KeyDown(32) Then GoTo start
Loop Until _KeyDown(27)
Function checkV (x1, y1, z1, x2, y2, z2, x3, y3, z3)
    Xo = (y2 - y1) * (z3 - z1) - (z2 - z1) * (y3 - y1)
    Yo = (z2 - z1) * (x3 - x1) - (x2 - x1) * (z3 - z1)
    Zo = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
    D = Sqr(Xo * Xo + Yo * Yo + Zo * Zo)
    nx = Xo / D: ny = Yo / D: nz = Zo / D
    px = x1: py = y1: pz = z1
    D = Sqr(px * px + py * py + pz * pz)
    dx = px / D: dy = py / D: dz = pz / D
    uv = (nz * dz + ny * dy + nx * dx)
    If uv < 0 Or uv > 1 Then checkV = -1 Else checkV = uv
End Function
Function maxi (x)
    If x > 255 Then maxi = 255: Exit Function
    If x < 0 Then maxi = 0: Exit Function
    maxi = x
End Function

What does _SquarePixels do?
b = b + ...
Reply


Messages In This Thread
3D rendering of Game of Life by ubi44 - by bplus - 02-13-2024, 07:35 PM



Users browsing this thread: 1 Guest(s)