Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SX = 800; SY = 500
- fov = 360
- minz = 0.1
- function rotateY(point, angle)
- local rad = angle*math.pi/180
- local cosa = math.cos(rad)
- local sina = math.sin(rad)
- local z = point.z*cosa - point.x*sina
- local x = point.z*sina + point.x*cosa
- return {x=x, y=point.y, z=z}
- end
- function project(point)
- local factor = fov/point.z
- local x = point.x*factor + SX/2
- local y = -point.y*factor + SY/2
- return {x=x, y=y, z=1}
- end
- function intersect(v1, v2)
- if v2.z == v1.z then return {x=0, y=0, z=0} end
- xs = (v2.x-v1.x)/(v2.z-v1.z)
- ys = (v2.y-v1.y)/(v2.z-v1.z)
- return {
- x = v1.x+xs*(minz-v1.z),
- y = v1.y+ys*(minz-v1.z),
- z = minz
- }
- end
- function love.load()
- i = 0
- love.graphics.setMode(SX, SY)
- pos = {x=0, y=-1.6, z=15}
- verticeso = {
- { -5, 0, 0}, { -5, 7, 0}, { 5, 7, 0}, { 5, 0, 0}, -- front wall
- { -5, 0, 10}, { -5, 7, 10}, { 5, 7, 10}, { 5, 0, 10}, -- back wall
- {-10, 0, -5}, {-10, 0, 15}, {10, 0, 15}, {10, 0, -5}, -- ground square
- {-.5, 0, 0}, {-.5, 2.5, 0}, {.5, 2.5, 0}, {.5, 0, 0}, -- door
- { -5, 3.5, 0}, { -5, 3.5, 10}, { 5, 3.5, 10}, { 5, 3.5, 0} -- first floor
- }
- bones = {
- { 1, 2}, { 2, 3}, { 3, 4}, { 4, 16}, {13, 1}, -- front wall (door)
- { 5, 6}, { 6, 7}, { 7, 8}, { 8, 5}, -- back wall
- { 1, 5}, { 2, 6}, { 3, 7}, { 4, 8}, -- front/back
- { 9, 10}, {10, 11}, {11, 12}, {12, 9}, -- ground square
- {13, 14}, {14, 15}, {15, 16}, -- door
- {17, 18}, {18, 19}, {19, 20}, {20, 17} -- first floor
- }
- surfaces = {
- {17, 18, 19, 20}, {1, 2, 6, 5}, {5, 6, 7, 8}, {3, 4, 8, 7},
- {2, 6, 7, 3}, {1, 2, 14, 13}, {14, 15, 3, 2}, {16, 15, 3, 4},
- {9, 10, 11, 12}
- }
- vertices = {}
- plots = {}
- for i = 1, #verticeso do
- v = verticeso[i]
- table.insert(vertices, {
- x = v[1]+pos.x,
- y = v[2]+pos.y,
- z = v[3]+pos.z
- })
- vertices[i] = rotateY(vertices[i], 10)
- table.insert(plots, project(vertices[i]))
- end
- --for k, v in pairs(plots) do print(k, v.x) end
- end
- function love.update()
- if love.keyboard.isDown("w") then
- for i = 1, #vertices do
- vertices[i].z = vertices[i].z-6*dt
- end
- end
- if love.keyboard.isDown("s") then
- for i = 1, #vertices do
- vertices[i].z = vertices[i].z+6*dt
- end
- end
- if love.keyboard.isDown("a") then
- for i = 1, #vertices do
- vertices[i] = rotateY(vertices[i], 50*dt)
- end
- end
- if love.keyboard.isDown("d") then
- for i = 1, #vertices do
- vertices[i] = rotateY(vertices[i], -50*dt)
- end
- end
- plots = {}
- for i = 1, #vertices do
- table.insert(plots, project(vertices[i]))
- end
- end
- function love.draw()
- love.graphics.setColor(255, 255, 255, 150)
- -- code for bones
- -- for i = 1, #bones do
- -- b = bones[i]
- -- v0 = vertices[b[1]]
- -- v1 = vertices[b[2]]
- -- if v0.z > minz or v1.z > minz then
- -- p0 = plots[b[1]]
- -- p1 = plots[b[2]]
- -- if v0.z < v1.z then v0, v1, p0, p1 = v1, v0, p1, p0 end
- -- if v0.z > minz and v1.z < minz then
- -- p1 = project(intersect(v0, v1))
- -- end
- -- love.graphics.line(p0.x, p0.y, p1.x, p1.y)
- -- end
- -- end
- for i = 1, #surfaces do
- s = surfaces[i]
- if #s > 2 then --minimum 3 sides
- vs = {}
- visible = false
- cut = false
- for iv = 1, #s do
- table.insert(vs, vertices[s[iv]])
- if vertices[s[iv]].z > minz
- then visible = true
- else cut = true end
- end
- if visible then
- if cut then
- vsc = {}
- for iv = 1, #vs do
- v1 = {x=vs[ iv].x, y=vs[ iv].y, z=vs[ iv].z}
- if iv == #vs then iv = 0 end
- v2 = {x=vs[iv+1].x, y=vs[iv+1].y, z=vs[iv+1].z}
- if v1.z > minz then table.insert(vsc, {x=v1.x, y=v1.y, z=v1.z}) end
- if v1.z < v2.z then v1, v2 = v2, v1 end
- if v1.z > minz and v2.z < minz then
- table.insert(vsc, intersect(v1, v2))
- end
- end
- vs = vsc
- end
- polygon = {}
- for iv = 1, #vs do
- v = project(vs[iv])
- table.insert(polygon, v.x)
- table.insert(polygon, v.y)
- end
- love.graphics.polygon("fill", polygon)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment