Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local math = require("math")
- local keyboard = require("keyboard")
- local os = require("os")
- -- Set screen resolution
- local screenWidth, screenHeight = gpu.getResolution()
- -- Define cube vertices
- local vertices = {
- {x = -0.5, y = -0.5, z = -0.5},
- {x = 0.5, y = -0.5, z = -0.5},
- {x = 0.5, y = 0.5, z = -0.5},
- {x = -0.5, y = 0.5, z = -0.5},
- {x = -0.5, y = -0.5, z = 0.5},
- {x = 0.5, y = -0.5, z = 0.5},
- {x = 0.5, y = 0.5, z = 0.5},
- {x = -0.5, y = 0.5, z = 0.5}
- }
- -- Define cube edges
- local edges = {
- {1, 2}, {2, 3}, {3, 4}, {4, 1},
- {5, 6}, {6, 7}, {7, 8}, {8, 5},
- {1, 5}, {2, 6}, {3, 7}, {4, 8}
- }
- -- Define cube faces (indices of vertices that form each face)
- local faces = {
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {1, 2, 6, 5},
- {2, 3, 7, 6},
- {3, 4, 8, 7},
- {4, 1, 5, 8}
- }
- -- Define cube colors
- local colors = {
- 0xFF0000, -- Red
- 0x00FF00, -- Green
- 0x0000FF, -- Blue
- 0xFFFF00, -- Yellow
- 0xFF00FF, -- Magenta
- 0x00FFFF -- Cyan
- }
- -- Camera position and rotation
- local camera = {x = 0, y = 0, z = -2, yaw = 0, pitch = 0}
- -- Function to project 3D coordinates to 2D screen with perspective
- local function projectVertex(vertex)
- local fov = 90
- local viewerDistance = 4
- local factor = fov / (viewerDistance + vertex.z - camera.z)
- local xProjected = (vertex.x - camera.x) * factor + screenWidth / 2
- local yProjected = (vertex.y - camera.y) * factor + screenHeight / 2
- return xProjected, yProjected
- end
- -- Function to rotate a vertex around the x-axis
- local function rotateX(vertex, angle)
- local y = vertex.y * math.cos(angle) - vertex.z * math.sin(angle)
- local z = vertex.y * math.sin(angle) + vertex.z * math.cos(angle)
- return {x = vertex.x, y = y, z = z}
- end
- -- Function to rotate a vertex around the y-axis
- local function rotateY(vertex, angle)
- local x = vertex.x * math.cos(angle) - vertex.z * math.sin(angle)
- local z = vertex.x * math.sin(angle) + vertex.z * math.cos(angle)
- return {x = x, y = vertex.y, z = z}
- end
- -- Function to rotate a vertex around the z-axis
- local function rotateZ(vertex, angle)
- local x = vertex.x * math.cos(angle) - vertex.y * math.sin(angle)
- local y = vertex.x * math.sin(angle) + vertex.y * math.cos(angle)
- return {x = x, y = y, z = vertex.z}
- end
- -- Function to draw a line between two points with a specified color
- local function drawLine(x1, y1, x2, y2, color)
- local dx = math.abs(x2 - x1)
- local dy = math.abs(y2 - y1)
- local sx = x1 < x2 and 1 or -1
- local sy = y1 < y2 and 1 or -1
- local err = dx - dy
- while true do
- gpu.setForeground(color)
- gpu.set(x1, y1, "█") -- Draw a solid block
- if x1 == x2 and y1 == y2 then break end
- local e2 = 2 * err
- if e2 > -dy then
- err = err - dy
- x1 = x1 + sx
- end
- if e2 < dx then
- err = err + dx
- y1 = y1 + sy
- end
- end
- end
- -- Function to draw filled faces of the cube with specified colors
- local function drawCube()
- -- Rotate vertices around the x, y, and z axes
- local rotatedVertices = {}
- for _, vertex in ipairs(vertices) do
- local rotatedVertex = rotateX(vertex, camera.pitch)
- rotatedVertex = rotateY(rotatedVertex, camera.yaw)
- rotatedVertex = rotateZ(rotatedVertex, 0)
- table.insert(rotatedVertices, rotatedVertex)
- end
- -- Draw each face
- for i, face in ipairs(faces) do
- local verticesToDraw = {}
- local color = colors[i]
- for _, vertexIndex in ipairs(face) do
- local x, y = projectVertex(rotatedVertices[vertexIndex])
- table.insert(verticesToDraw, {x = x, y = y})
- end
- local startVertex = verticesToDraw[#verticesToDraw]
- for i, vertex in ipairs(verticesToDraw) do
- local nextVertex = i == #verticesToDraw and startVertex or verticesToDraw[i + 1]
- drawLine(vertex.x, vertex.y, nextVertex.x, nextVertex.y, color)
- end
- end
- end
- -- Clear screen
- gpu.fill(1, 1, screenWidth, screenHeight, " ")
- -- Main loop
- local running = true
- local fps = 0
- local lastTime = os.clock()
- while running do
- drawCube()
- -- Check for keyboard input
- local keyPressed = keyboard.isKeyDown
- if keyPressed(keyboard.keys.q) then
- running = false -- Exit if 'q' key is pressed
- end
- if keyPressed(keyboard.keys.left) then
- camera.yaw = camera.yaw + 0.1 -- Rotate left
- end
- if keyPressed(keyboard.keys.right) then
- camera.yaw = camera.yaw - 0.1 -- Rotate right
- end
- if keyPressed(keyboard.keys.up) then
- camera.pitch = camera.pitch + 0.1 -- Rotate up
- end
- if keyPressed(keyboard.keys.down) then
- camera.pitch = camera.pitch - 0.1 -- Rotate down
- end
- if keyPressed(keyboard.keys.w) then
- camera.z = camera.z - 0.1 -- Move forward
- end
- if keyPressed(keyboard.keys.s) then
- camera.z = camera.z + 0.1 -- Move backward
- end
- if keyPressed(keyboard.keys.a) then
- camera.x = camera.x - 0.1 -- Move left
- end
- if keyPressed(keyboard.keys.d) then
- camera.x = camera.x + 0.1 -- Move right
- end
- -- Calculate FPS
- local currentTime = os.clock()
- local deltaTime = currentTime - lastTime
- fps = math.floor(1 / deltaTime)
- lastTime = currentTime
- -- Display FPS
- gpu.setForeground(0xFFFFFF)
- gpu.set(1, 1, "FPS: " .. fps)
- os.sleep(0.05)
- gpu.fill(1, 2, screenWidth, screenHeight - 1, " ") -- Clear screen before redraw (except FPS counter)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement