3D Dot Sphere

Started by SToS, November 19, 2024, 17:56:39

Previous topic - Next topic

SToS

Just for fun! :P

SuperStrict

Const WINWIDTH:UInt = 800
Const WINHEIGHT:UInt = 600

Type TColour
    Field r:Int
    Field g:Int
    Field b:Int
    Field a:Int

    Method New(r:Int, g:Int, b:Int, a:Int = 255)
        Self.r = r
        Self.g = g
        Self.b = b
        Self.a = a
    End Method
End Type

Type TSphere
    Const camera_pos:Int = 300

    Field distance:Int
    Field distance_dir:Int
    Field colours:TColour[256]
    Field count:Float
    Field half_width:Int
    Field half_height:Int
    Field spin:Float

    Method New()
        Self.distance = -500
        Self.distance_dir = 4
        Self.count = 0
        Self.half_width = WINWIDTH / 2
        Self.half_height = WINHEIGHT / 2
        For Local i:Int = 0 To 255
            Local r:Float = 1.0 + CosF(Rad2Deg(Float( i       * Pi) / 128.0))
            Local g:Float = 1.0 + CosF(Rad2Deg(Float((i - 85) * Pi) / 128.0))
            Local b:Float = 1.0 + CosF(Rad2Deg(Float((i + 85) * Pi) / 128.0))
            Self.colours[i] = New TColour(Int((r * 127.0)) Mod 256, Int((g * 127.0)) Mod 256, Int((b * 127.0)) Mod 256)
        Next i
        Self.spin = -6.3
    End Method

    Method Draw()
        Self.distance :- Self.distance_dir
        If Self.distance < -1500 Or Self.distance > -200 Then
            Self.distance_dir = -Self.distance_dir
        EndIf
        Self.count :+ 0.5

        Self.spin :+ 0.5
        Local spin_cos:Float = CosF(Self.spin)
        Local spin_sin:Float = SinF(Self.spin)
        For Local i:Int = -90 To 90 Step 5
            Local i_cos_x50:Float = CosF(Float(i)) * 50
            Local i_sin_x50:Float = SinF(Float(i)) * 50
            Local dot_spacing:Int = 10
            If i Mod 10 = 0 Then
                dot_spacing = 20
            EndIf
            Local index:Int = (Abs(i) + Int(Self.count)) Mod 256
            Local c:TColour = Self.colours[index]
            If dot_spacing = 10 Then
                For Local j:Int = 0 To 360 Step 10
                    Local x:Int
                    Local y:Int
                    Local cos1:Float = (100.0 * spin_cos) + (CosF(Float(j)) * i_cos_x50)
                    Local sin1:Float =                       SinF(Float(j)) * i_cos_x50
                    Calc_3D(Int(cos1), Int(sin1), Int(i_sin_x50), Self.half_width, Self.half_height, ..
                    spin_cos, spin_sin, x, y, Self.camera_pos, Self.distance)
                    If x > 0 And x < WINWIDTH And y > 0 And y < WINHEIGHT Then
                        SetColor c.r, c.g, c.b
                        SetAlpha c.a / 255.0
                        Plot x, y
                    EndIf
                Next j
            Else
                For Local j:Int = 0 To 360 Step 20
                    Local x:Int
                    Local y:Int
                    Local cos1:Float = (100.0 * spin_cos) + (CosF(Float(j)) * i_cos_x50)
                    Local sin1:Float =                       SinF(Float(j)) * i_cos_x50
                    Calc_3D(Int(cos1), Int(sin1), Int(i_sin_x50), Self.half_width, Self.half_height, ..
                    spin_cos, spin_sin, x, y, Self.camera_pos, Self.distance)
                    If x > 0 And x < WINWIDTH And y > 0 And y < WINHEIGHT Then
                        SetColor c.r, c.g, c.b
                        SetAlpha c.a / 255.0
                        Plot x, y
                    EndIf
                Next j
            EndIf
        Next i
    End Method

    Function Calc_3D(x:Int, y:Int, z:Int, ..
        cx:Int, cy:Int, ..
        cr1:Float, sr1:Float, ..
        sx:Int Var, sy:Int Var, ..
        cp:Int, d:Int)
        Local v1:Float = (cr1 *  x) - (sr1 *  z)
        Local v2:Float = (sr1 *  x) + (cr1 *  z)
        Local v3:Float = (cr1 *  y) - (sr1 * v1)
        Local v4:Float = (cr1 * v1) + (sr1 *  y)
        Local v5:Float = (sr1 * v2) + (cr1 * v3)
        Local v6:Float = (cr1 * v2) - (sr1 * v3)

        v6 :- Float(cp)
        sx = Int(((Float(d) * v4) / v6) + Float(cx))
        sy = Int(((Float(d) * v5) / v6) + Float(cy))
    End Function

    Function Rad2Deg:Float(radians:Float)
        Return radians * 180.0 / Pi
    End Function
End Type

Global sphere:TSphere = New TSphere

Graphics WINWIDTH, WINHEIGHT, 0

Repeat
Cls
sphere.Draw()
Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()

Baggey

@SToS Are you some sort off Professional Programmer?

Lovely Demo! In BlitzMaxNG ;)
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!