-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3dc.lua
238 lines (202 loc) · 4.93 KB
/
s3dc.lua
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
local s3dc = {}
local shader_code = [[
extern mat4 projection, inv_translate, inv_rotate_y, inv_rotate_x;
extern bool is_canvas;
vec4 position(mat4 transform_projection, vec4 vertex_position)
{
vec4 pos = TransformMatrix * vertex_position * inv_translate * inv_rotate_y * inv_rotate_x * projection;
if (is_canvas)
{
pos.y *= -1;
}
return pos;
}
]]
---@return table
local function identity()
return {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}
end
---@param out table
---@param a table
---@param b table
local function cross(out, a, b)
local x = a[2] * b[3] - a[3] * b[2]
local y = a[3] * b[1] - a[1] * b[3]
local z = a[1] * b[2] - a[2] * b[1]
out[1] = x
out[2] = y
out[3] = z
end
---@param out table
---@param a table
---@param b number
local function mul(out, a, b)
out[1] = a[1] * b
out[2] = a[2] * b
out[3] = a[3] * b
end
---@param out table
---@param a table
---@param b table
local function add(out, a, b)
out[1] = a[1] + b[1]
out[2] = a[2] + b[2]
out[3] = a[3] + b[3]
end
---@param out table
---@param a table
local function norm(out, a)
local k = math.sqrt(a[1] ^ 2 + a[2] ^ 2 + a[3] ^ 2)
mul(out, a, k == 0 and 0 or 1 / k)
end
local shader
local projection, inv_translate, inv_rotate_y, inv_rotate_x
function s3dc.load()
s3dc.pos = {0, 0, 0}
s3dc.angle = {pitch = 0, yaw = 0}
s3dc.top = {0, -1, 0}
s3dc.front = {0, 0, 1}
s3dc.fov = math.rad(70)
s3dc.near = 10
s3dc.far = 10000
shader = shader or love.graphics.newShader(shader_code)
projection = identity()
inv_translate = identity()
inv_rotate_y = identity()
inv_rotate_x = identity()
s3dc.rotate(0, 0)
end
---@param x number
---@param y number
---@param w number
---@param h number
function s3dc.show(x, y, w, h)
local pos = s3dc.pos
pos[1] = x + w / 2
pos[2] = y + h / 2
pos[3] = -h / 2 / math.tan(s3dc.fov / 2)
local angle = s3dc.angle
angle.pitch = 0
angle.yaw = 0
end
local function inv_translate_mat4()
inv_translate[13] = -s3dc.pos[1]
inv_translate[14] = -s3dc.pos[2]
inv_translate[15] = -s3dc.pos[3]
end
local function inv_rotate_y_mat4()
local c = math.cos(-s3dc.angle.yaw)
local s = math.sin(-s3dc.angle.yaw)
inv_rotate_y[1] = c
inv_rotate_y[3] = -s
inv_rotate_y[9] = s
inv_rotate_y[11] = c
end
local function inv_rotate_x_mat4()
local c = math.cos(-s3dc.angle.pitch)
local s = math.sin(-s3dc.angle.pitch)
inv_rotate_x[6] = c
inv_rotate_x[7] = s
inv_rotate_x[10] = -s
inv_rotate_x[11] = c
end
---@param fovy number
---@param aspect number
---@param near number
---@param far number
local function from_perspective(fovy, aspect, near, far)
assert(aspect ~= 0)
assert(near ~= far)
local t = math.tan(fovy / 2)
projection[1] = 1 / (t * aspect)
projection[6] = -1 / t
projection[11] = -(far + near) / (far - near)
projection[12] = 1
projection[15] = (2 * far * near) / (far - near)
projection[16] = 0
end
local drawing = false
local width, height
function s3dc.draw_start()
assert(not drawing, "Calling s3dc.draw_start() twice")
drawing = true
width, height = love.graphics.getDimensions()
s3dc.draw_update()
love.graphics.setShader(shader)
shader:send("is_canvas", love.graphics.getCanvas() ~= nil)
end
function s3dc.draw_end()
assert(drawing, "Calling s3dc.draw_end() twice")
drawing = false
love.graphics.setShader()
end
function s3dc.draw_update()
from_perspective(s3dc.fov, width / height, s3dc.near, s3dc.far)
inv_translate_mat4()
inv_rotate_y_mat4()
inv_rotate_x_mat4()
shader:send("projection", projection)
shader:send("inv_translate", inv_translate)
shader:send("inv_rotate_y", inv_rotate_y)
shader:send("inv_rotate_x", inv_rotate_x)
end
---@param dx number
---@param dy number
---@param dz number
function s3dc.translate(dx, dy, dz)
local pos = s3dc.pos
pos[1] = pos[1] + dx
pos[2] = pos[2] + dy
pos[3] = pos[3] + dz
end
---@param dx number
---@param dy number
function s3dc.rotate(dx, dy)
local angle = s3dc.angle
angle.pitch = angle.pitch + dx -- rotation about the X axis
angle.yaw = angle.yaw + dy -- rotation about the Y axis
local front = s3dc.front
front[1] = math.sin(angle.yaw) * math.cos(angle.pitch)
front[2] = -math.sin(angle.pitch)
front[3] = math.cos(angle.yaw) * math.cos(angle.pitch)
norm(front, front)
end
local tmp_vec3 = {}
---@param delta number
function s3dc.forward(delta)
mul(tmp_vec3, s3dc.front, delta)
add(s3dc.pos, s3dc.pos, tmp_vec3)
end
---@param delta number
function s3dc.right(delta)
cross(tmp_vec3, s3dc.front, s3dc.top)
norm(tmp_vec3, tmp_vec3)
mul(tmp_vec3, tmp_vec3, delta)
add(s3dc.pos, s3dc.pos, tmp_vec3)
end
---@param delta number
function s3dc.up(delta)
cross(tmp_vec3, s3dc.front, s3dc.top)
cross(tmp_vec3, tmp_vec3, s3dc.front)
norm(tmp_vec3, tmp_vec3)
mul(tmp_vec3, tmp_vec3, delta)
add(s3dc.pos, s3dc.pos, tmp_vec3)
end
---@param delta number
function s3dc.backward(delta)
s3dc.forward(-delta)
end
---@param delta number
function s3dc.left(delta)
s3dc.right(-delta)
end
---@param delta number
function s3dc.down(delta)
s3dc.up(-delta)
end
return s3dc