-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
220 lines (177 loc) · 4.32 KB
/
main.lua
File metadata and controls
220 lines (177 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
local Vector3 = require("classes.vector3")
local Vector2 = require("classes.vector2")
local Color = require("classes.color")
local Camera = require("classes.camera")
local Utility = require("utility")
local verts = {}
local edges = {}
local faces = {}
local colors = {}
local w,h,cx,cy
local cam
local radian = 0
function love.load()
-- Mouse stuff
love.mouse.setGrabbed(true)
love.mouse.setRelativeMode(true)
-- Other things
w = love.graphics.getWidth()
h = love.graphics.getHeight()
cx = w / 2
cy = h / 2
love.graphics.setBackgroundColor( 255, 255, 255 ) -- Set clear color
verts = {
Vector3.new(-1, -1, -1),
Vector3.new(1, -1, -1),
Vector3.new(1, 1, -1),
Vector3.new(-1, 1, -1),
Vector3.new(-1, -1, 1),
Vector3.new(1, -1, 1),
Vector3.new(1, 1, 1),
Vector3.new(-1, 1, 1)
}
edges = {
Vector2.new(1,2),
Vector2.new(2,3),
Vector2.new(3,4),
Vector2.new(4,1),
Vector2.new(5,6),
Vector2.new(6,7),
Vector2.new(7,8),
Vector2.new(8,5),
Vector2.new(1,5),
Vector2.new(2,6),
Vector2.new(3,7),
Vector2.new(4,8)
}
faces = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{1, 2, 6, 5},
{3, 4, 8, 7},
{1, 4, 8, 5},
{2, 3, 7, 6}
}
colors = {
Color.new(255, 0, 0),
Color.new(255, 128, 0),
Color.new(255, 255, 0),
Color.new(255, 255, 255),
Color.new(0, 0, 255),
Color.new(0, 255, 0)
}
cam = Camera.new(Vector3.new(0,0, -5))
end
function love.update(dt)
cam:update(dt)
radian = radian + dt
end
function love.draw()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setColor(255,255,255)
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10)
--[[
for i,e in ipairs(edges) do
local edgeX = e.x
local edgeY = e.y
local points = {}
for j,v in ipairs({verts[edgeX], verts[edgeY]}) do
local x = v.x
local y = v.y
local z = v.z
x = x - cam.position.x
y = y - cam.position.y
z = z - cam.position.z
local rotatedY = Utility.rotate2D(Vector2.new(x, z), cam.rotation.y)
x, z = rotatedY.x, rotatedY.y
local rotatedX = Utility.rotate2D(Vector2.new(y, z), cam.rotation.x)
y, z = rotatedX.x, rotatedX.y
f = (w/2) / z
x, y = x*f, y*f
table.insert(points, x + cx)
table.insert(points, y + cy)
end
if #points >= 2 then
love.graphics.line(points)
end
end
]]--
local vertList, screenCoords = {}, {}
for i,v in ipairs(verts) do
local x = v.x
local y = v.y
local z = v.z
x = x - cam.position.x
y = y - cam.position.y
z = z - cam.position.z
local rotatedY = Utility.rotate2D(Vector2.new(x, z), cam.rotation.y)
x, z = rotatedY.x, rotatedY.y
local rotatedX = Utility.rotate2D(Vector2.new(y, z), cam.rotation.x)
y, z = rotatedX.x, rotatedX.y
vertList[i] = Vector3.new(x, y, z)
f = (w/2) / z
x, y = x*f, y*f
screenCoords[i] = Vector2.new(x + cx, y + cy)
end
local faceList = {}
local faceColor = {}
local depth = {}
for f = 1, #faces do
local face = faces[f]
local onScreen = false
for j,vert in ipairs(face) do
local x,y = screenCoords[f].x, screenCoords[f].y
if vertList[vert].z > 0 and x > 0 and x < w and y > 0 and y < h then
onScreen = true
break
end
end
if onScreen then
local coords = {}
for j,v in ipairs(face) do
coords[j] = screenCoords[v]
end
faceList[f] = coords
faceColor[f] = colors[f]
local sum = 0
for i = 1, 3 do
for fi,j in ipairs(face) do
sum = sum + Utility.sum(vertList[j][i])
end
sum = sum ^ 2
end
depth[f] = sum
end
end
local faceListSorted = {}
pcall(function() table.sort(depth, function(a,b) return a<b end) end)
for i,v in ipairs(depth) do
table.insert(faceListSorted, faceList[#depth - i])
end
local printStr = "{ "
for i,v in ipairs(depth) do
printStr = printStr .. v
if i < #depth then
printStr = printStr .. ", "
end
end
printStr = printStr .. " }"
print(printStr)
for i,v in ipairs(faceListSorted) do
love.graphics.setColor(faceColor[i][1] or 255, faceColor[i][2] or 255, faceColor[i][3] or 255)
local points = {}
for j,p in pairs(v) do
table.insert(points, p.x)
table.insert(points, p.y)
end
love.graphics.polygon("fill", points)
end
end
function love.mousemoved(x, y, dx, dy, isTouch)
cam:mouse(dx, dy)
end
function love.keypressed(key, scancode, isRepeat)
if scancode == "escape" then
love.event.quit()
end
end