Advertisement
nonogamer9

3D Cube Demo For OpenComputers

May 12th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local math = require("math")
  4. local keyboard = require("keyboard")
  5. local os = require("os")
  6.  
  7. -- Set screen resolution
  8. local screenWidth, screenHeight = gpu.getResolution()
  9.  
  10. -- Define cube vertices
  11. local vertices = {
  12. {x = -0.5, y = -0.5, z = -0.5},
  13. {x = 0.5, y = -0.5, z = -0.5},
  14. {x = 0.5, y = 0.5, z = -0.5},
  15. {x = -0.5, y = 0.5, z = -0.5},
  16. {x = -0.5, y = -0.5, z = 0.5},
  17. {x = 0.5, y = -0.5, z = 0.5},
  18. {x = 0.5, y = 0.5, z = 0.5},
  19. {x = -0.5, y = 0.5, z = 0.5}
  20. }
  21.  
  22. -- Define cube edges
  23. local edges = {
  24. {1, 2}, {2, 3}, {3, 4}, {4, 1},
  25. {5, 6}, {6, 7}, {7, 8}, {8, 5},
  26. {1, 5}, {2, 6}, {3, 7}, {4, 8}
  27. }
  28.  
  29. -- Define cube faces (indices of vertices that form each face)
  30. local faces = {
  31. {1, 2, 3, 4},
  32. {5, 6, 7, 8},
  33. {1, 2, 6, 5},
  34. {2, 3, 7, 6},
  35. {3, 4, 8, 7},
  36. {4, 1, 5, 8}
  37. }
  38.  
  39. -- Define cube colors
  40. local colors = {
  41. 0xFF0000, -- Red
  42. 0x00FF00, -- Green
  43. 0x0000FF, -- Blue
  44. 0xFFFF00, -- Yellow
  45. 0xFF00FF, -- Magenta
  46. 0x00FFFF -- Cyan
  47. }
  48.  
  49. -- Camera position and rotation
  50. local camera = {x = 0, y = 0, z = -2, yaw = 0, pitch = 0}
  51.  
  52. -- Function to project 3D coordinates to 2D screen with perspective
  53. local function projectVertex(vertex)
  54. local fov = 90
  55. local viewerDistance = 4
  56.  
  57. local factor = fov / (viewerDistance + vertex.z - camera.z)
  58. local xProjected = (vertex.x - camera.x) * factor + screenWidth / 2
  59. local yProjected = (vertex.y - camera.y) * factor + screenHeight / 2
  60.  
  61. return xProjected, yProjected
  62. end
  63.  
  64. -- Function to rotate a vertex around the x-axis
  65. local function rotateX(vertex, angle)
  66. local y = vertex.y * math.cos(angle) - vertex.z * math.sin(angle)
  67. local z = vertex.y * math.sin(angle) + vertex.z * math.cos(angle)
  68. return {x = vertex.x, y = y, z = z}
  69. end
  70.  
  71. -- Function to rotate a vertex around the y-axis
  72. local function rotateY(vertex, angle)
  73. local x = vertex.x * math.cos(angle) - vertex.z * math.sin(angle)
  74. local z = vertex.x * math.sin(angle) + vertex.z * math.cos(angle)
  75. return {x = x, y = vertex.y, z = z}
  76. end
  77.  
  78. -- Function to rotate a vertex around the z-axis
  79. local function rotateZ(vertex, angle)
  80. local x = vertex.x * math.cos(angle) - vertex.y * math.sin(angle)
  81. local y = vertex.x * math.sin(angle) + vertex.y * math.cos(angle)
  82. return {x = x, y = y, z = vertex.z}
  83. end
  84.  
  85. -- Function to draw a line between two points with a specified color
  86. local function drawLine(x1, y1, x2, y2, color)
  87. local dx = math.abs(x2 - x1)
  88. local dy = math.abs(y2 - y1)
  89. local sx = x1 < x2 and 1 or -1
  90. local sy = y1 < y2 and 1 or -1
  91. local err = dx - dy
  92.  
  93. while true do
  94. gpu.setForeground(color)
  95. gpu.set(x1, y1, "█") -- Draw a solid block
  96. if x1 == x2 and y1 == y2 then break end
  97. local e2 = 2 * err
  98. if e2 > -dy then
  99. err = err - dy
  100. x1 = x1 + sx
  101. end
  102. if e2 < dx then
  103. err = err + dx
  104. y1 = y1 + sy
  105. end
  106. end
  107. end
  108.  
  109. -- Function to draw filled faces of the cube with specified colors
  110. local function drawCube()
  111. -- Rotate vertices around the x, y, and z axes
  112. local rotatedVertices = {}
  113. for _, vertex in ipairs(vertices) do
  114. local rotatedVertex = rotateX(vertex, camera.pitch)
  115. rotatedVertex = rotateY(rotatedVertex, camera.yaw)
  116. rotatedVertex = rotateZ(rotatedVertex, 0)
  117. table.insert(rotatedVertices, rotatedVertex)
  118. end
  119.  
  120. -- Draw each face
  121. for i, face in ipairs(faces) do
  122. local verticesToDraw = {}
  123. local color = colors[i]
  124. for _, vertexIndex in ipairs(face) do
  125. local x, y = projectVertex(rotatedVertices[vertexIndex])
  126. table.insert(verticesToDraw, {x = x, y = y})
  127. end
  128. local startVertex = verticesToDraw[#verticesToDraw]
  129. for i, vertex in ipairs(verticesToDraw) do
  130. local nextVertex = i == #verticesToDraw and startVertex or verticesToDraw[i + 1]
  131. drawLine(vertex.x, vertex.y, nextVertex.x, nextVertex.y, color)
  132. end
  133. end
  134. end
  135.  
  136. -- Clear screen
  137. gpu.fill(1, 1, screenWidth, screenHeight, " ")
  138.  
  139. -- Main loop
  140. local running = true
  141. local fps = 0
  142. local lastTime = os.clock()
  143. while running do
  144. drawCube()
  145.  
  146. -- Check for keyboard input
  147. local keyPressed = keyboard.isKeyDown
  148. if keyPressed(keyboard.keys.q) then
  149. running = false -- Exit if 'q' key is pressed
  150. end
  151. if keyPressed(keyboard.keys.left) then
  152. camera.yaw = camera.yaw + 0.1 -- Rotate left
  153. end
  154. if keyPressed(keyboard.keys.right) then
  155. camera.yaw = camera.yaw - 0.1 -- Rotate right
  156. end
  157. if keyPressed(keyboard.keys.up) then
  158. camera.pitch = camera.pitch + 0.1 -- Rotate up
  159. end
  160. if keyPressed(keyboard.keys.down) then
  161. camera.pitch = camera.pitch - 0.1 -- Rotate down
  162. end
  163. if keyPressed(keyboard.keys.w) then
  164. camera.z = camera.z - 0.1 -- Move forward
  165. end
  166. if keyPressed(keyboard.keys.s) then
  167. camera.z = camera.z + 0.1 -- Move backward
  168. end
  169. if keyPressed(keyboard.keys.a) then
  170. camera.x = camera.x - 0.1 -- Move left
  171. end
  172. if keyPressed(keyboard.keys.d) then
  173. camera.x = camera.x + 0.1 -- Move right
  174. end
  175.  
  176. -- Calculate FPS
  177. local currentTime = os.clock()
  178. local deltaTime = currentTime - lastTime
  179. fps = math.floor(1 / deltaTime)
  180. lastTime = currentTime
  181.  
  182. -- Display FPS
  183. gpu.setForeground(0xFFFFFF)
  184. gpu.set(1, 1, "FPS: " .. fps)
  185.  
  186. os.sleep(0.05)
  187. gpu.fill(1, 2, screenWidth, screenHeight - 1, " ") -- Clear screen before redraw (except FPS counter)
  188. end
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement