retroman

Untitled

Aug 4th, 2025
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Include "fbgfx.bi"
  2. Using FB
  3.  
  4. ScreenRes 640, 480, 32
  5. Const PI = 3.14159
  6.  
  7. Type Vec3D
  8.     As Single x, y, z
  9. End Type
  10.  
  11. ' === Basic 3D rotation ===
  12. Function RotateY(v As Vec3D, angle As Single) As Vec3D
  13.     Dim As Vec3D o
  14.     o.x = v.x * Cos(angle) + v.z * Sin(angle)
  15.     o.z = -v.x * Sin(angle) + v.z * Cos(angle)
  16.     o.y = v.y
  17.     Return o
  18. End Function
  19.  
  20. Function RotateX(v As Vec3D, angle As Single) As Vec3D
  21.     Dim As Vec3D o
  22.     o.y = v.y * Cos(angle) - v.z * Sin(angle)
  23.     o.z = v.y * Sin(angle) + v.z * Cos(angle)
  24.     o.x = v.x
  25.     Return o
  26. End Function
  27.  
  28. Function Project(v As Vec3D) As Vec3D
  29.     Dim As Single scale = 400 / (v.z + 4)
  30.     Dim As Vec3D o
  31.     o.x = v.x * scale + 320
  32.     o.y = v.y * scale + 240
  33.     Return o
  34. End Function
  35.  
  36. ' Cube vertices
  37. Dim Shared As Vec3D cube(0 To 7) = { _
  38.     (-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), _
  39.     (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1) }
  40.  
  41. Dim As Integer face(0 To 5, 0 To 3) = { _
  42.     {0,1,2,3}, {4,5,6,7}, {0,1,5,4}, _
  43.     {2,3,7,6}, {0,3,7,4}, {1,2,6,5} }
  44.  
  45. ' Create striped background
  46. For y As Integer = 0 To 479
  47.     For x As Integer = 0 To 639
  48.         If (y \ 10) Mod 2 = 0 Then
  49.             PSet (x, y), RGB(50, 50, 50)
  50.         Else
  51.             PSet (x, y), RGB(120, 120, 120)
  52.         End If
  53.     Next
  54. Next
  55.  
  56. ' Store background
  57. Dim As Any Ptr bg = ImageCreate(640, 480)
  58. Get (0,0)-(639,479), bg
  59.  
  60. Dim As Single angle = 0
  61.  
  62. Do
  63.     ScreenLock
  64.     Put (0,0), bg, PSet
  65.  
  66.     ' Rotate + project cube
  67.     Dim As Vec3D p(7)
  68.     For i As Integer = 0 To 7
  69.         Dim As Vec3D r = RotateX(cube(i), angle)
  70.         r = RotateY(r, angle * 1.3)
  71.         p(i) = Project(r)
  72.     Next
  73.  
  74.     ' Draw cube faces with stealth effect
  75.     For f As Integer = 0 To 5
  76.         ' Backface culling
  77.         Dim As Single ax = p(face(f,1)).x - p(face(f,0)).x
  78.         Dim As Single ay = p(face(f,1)).y - p(face(f,0)).y
  79.         Dim As Single bx = p(face(f,2)).x - p(face(f,1)).x
  80.         Dim As Single by = p(face(f,2)).y - p(face(f,1)).y
  81.         If (ax*by - ay*bx) < 0 Then Continue For
  82.  
  83.         ' Bounding box
  84.         Dim As Integer minX = 640, minY = 480, maxX = 0, maxY = 0
  85.         For i As Integer = 0 To 3
  86.             Dim px as integer = CInt(p(face(f,i)).x)
  87.             Dim py as integer = CInt(p(face(f,i)).y)
  88.             If px < minX Then minX = px
  89.             If px > maxX Then maxX = px
  90.             If py < minY Then minY = py
  91.             If py > maxY Then maxY = py
  92.         Next
  93.  
  94.         ' Clip
  95.         minX = IIf(minX < 0, 0, minX)
  96.         minY = IIf(minY < 0, 0, minY)
  97.         maxX = IIf(maxX > 639, 639, maxX)
  98.         maxY = IIf(maxY > 479, 479, maxY)
  99.  
  100.         ' Draw distorted face
  101.         For y As Integer = minY To maxY
  102.             For x As Integer = minX To maxX
  103.                 ' Simple point-in-quad via winding number or barycentric test
  104.                 ' We cheat here for demo: just draw inside bbox with distortion
  105.                 Dim col as long = Point(x, y)
  106.                 Dim r as ubyte = (col Shr 16 And 255) + 30 : If r > 255 Then r = 255
  107.                 Dim g as ubyte = (col Shr 8 And 255) + 30 : If g > 255 Then g = 255
  108.                 Dim b as ubyte = (col And 255) + 30 : If b > 255 Then b = 255
  109.                 PSet (x, y), RGB(r, g, b)
  110.             Next
  111.         Next
  112.     Next
  113.  
  114.     ScreenUnLock
  115.  
  116.     angle += 0.01
  117.     Sleep 16
  118. Loop Until InKey = Chr(27)
  119. ImageDestroy bg
  120.  
Advertisement
Add Comment
Please, Sign In to add comment