mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 21:55:09 +00:00
code
This commit is contained in:
@@ -35,6 +35,8 @@ local pyramid_indices = {
|
||||
4, 5, 2,
|
||||
}
|
||||
|
||||
local math3d = require("math3d")
|
||||
|
||||
local rotation_speeds = {x = 0.5, y = 0.7}
|
||||
|
||||
local shader_variants = {
|
||||
@@ -52,62 +54,26 @@ local shader_variants = {
|
||||
},
|
||||
}
|
||||
|
||||
local function rotation_y(radians)
|
||||
local c = math.cos(radians)
|
||||
local s = math.sin(radians)
|
||||
return {
|
||||
c, 0.0, -s, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
s, 0.0, c, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
local function rotation_x(radians)
|
||||
local c = math.cos(radians)
|
||||
local s = math.sin(radians)
|
||||
return {
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, c, s, 0.0,
|
||||
0.0, -s, c, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
local function multiply_matrices(a, b)
|
||||
local result = {}
|
||||
for row = 1, 4 do
|
||||
for col = 1, 4 do
|
||||
local sum = 0.0
|
||||
for idx = 1, 4 do
|
||||
sum = sum + a[(idx - 1) * 4 + row] * b[(col - 1) * 4 + idx]
|
||||
end
|
||||
result[(col - 1) * 4 + row] = sum
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
local function translation(x, y, z)
|
||||
return {
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
x, y, z, 1.0,
|
||||
}
|
||||
end
|
||||
local camera = {
|
||||
eye = {2.0, 2.0, 2.5},
|
||||
center = {0.0, 0.0, 0.0},
|
||||
up = {0.0, 1.0, 0.0},
|
||||
fov = 0.78,
|
||||
near = 0.1,
|
||||
far = 10.0,
|
||||
}
|
||||
|
||||
local function build_model(time)
|
||||
local y = rotation_y(time * rotation_speeds.y)
|
||||
local x = rotation_x(time * rotation_speeds.x)
|
||||
return multiply_matrices(y, x)
|
||||
local y = math3d.rotation_y(time * rotation_speeds.y)
|
||||
local x = math3d.rotation_x(time * rotation_speeds.x)
|
||||
return math3d.multiply(y, x)
|
||||
end
|
||||
|
||||
local function create_cube(position, speed_scale, shader_key)
|
||||
local function compute_model_matrix(time)
|
||||
local base = build_model(time * speed_scale)
|
||||
local offset = translation(position[1], position[2], position[3])
|
||||
return multiply_matrices(offset, base)
|
||||
local offset = math3d.translation(position[1], position[2], position[3])
|
||||
return math3d.multiply(offset, base)
|
||||
end
|
||||
|
||||
return {
|
||||
@@ -121,8 +87,8 @@ end
|
||||
local function create_pyramid(position, shader_key)
|
||||
local function compute_model_matrix(time)
|
||||
local base = build_model(time * 0.6)
|
||||
local offset = translation(position[1], position[2], position[3])
|
||||
return multiply_matrices(offset, base)
|
||||
local offset = math3d.translation(position[1], position[2], position[3])
|
||||
return math3d.multiply(offset, base)
|
||||
end
|
||||
|
||||
return {
|
||||
@@ -145,3 +111,9 @@ end
|
||||
function get_shader_paths()
|
||||
return shader_variants
|
||||
end
|
||||
|
||||
function get_view_projection(aspect)
|
||||
local view = math3d.look_at(camera.eye, camera.center, camera.up)
|
||||
local projection = math3d.perspective(camera.fov, aspect, camera.near, camera.far)
|
||||
return math3d.multiply(projection, view)
|
||||
end
|
||||
|
||||
119
scripts/math3d.lua
Normal file
119
scripts/math3d.lua
Normal file
@@ -0,0 +1,119 @@
|
||||
local math3d = {}
|
||||
|
||||
local function normalize(vec)
|
||||
local x, y, z = vec[1], vec[2], vec[3]
|
||||
local len = math.sqrt(x * x + y * y + z * z)
|
||||
if len == 0.0 then
|
||||
return {x, y, z}
|
||||
end
|
||||
return {x / len, y / len, z / len}
|
||||
end
|
||||
|
||||
local function cross(a, b)
|
||||
return {
|
||||
a[2] * b[3] - a[3] * b[2],
|
||||
a[3] * b[1] - a[1] * b[3],
|
||||
a[1] * b[2] - a[2] * b[1],
|
||||
}
|
||||
end
|
||||
|
||||
local function dot(a, b)
|
||||
return a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
|
||||
end
|
||||
|
||||
local function identity_matrix()
|
||||
return {
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
function math3d.identity()
|
||||
return identity_matrix()
|
||||
end
|
||||
|
||||
function math3d.multiply(a, b)
|
||||
local result = {}
|
||||
for row = 1, 4 do
|
||||
for col = 1, 4 do
|
||||
local sum = 0.0
|
||||
for idx = 1, 4 do
|
||||
sum = sum + a[(idx - 1) * 4 + row] * b[(col - 1) * 4 + idx]
|
||||
end
|
||||
result[(col - 1) * 4 + row] = sum
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function math3d.translation(x, y, z)
|
||||
return {
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
x, y, z, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
function math3d.rotation_x(radians)
|
||||
local c = math.cos(radians)
|
||||
local s = math.sin(radians)
|
||||
return {
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, c, s, 0.0,
|
||||
0.0, -s, c, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
function math3d.rotation_y(radians)
|
||||
local c = math.cos(radians)
|
||||
local s = math.sin(radians)
|
||||
return {
|
||||
c, 0.0, -s, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
s, 0.0, c, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
}
|
||||
end
|
||||
|
||||
function math3d.look_at(eye, center, up)
|
||||
local f = normalize({center[1] - eye[1], center[2] - eye[2], center[3] - eye[3]})
|
||||
local s = normalize(cross(f, up))
|
||||
local u = cross(s, f)
|
||||
|
||||
local result = identity_matrix()
|
||||
result[1] = s[1]
|
||||
result[2] = u[1]
|
||||
result[3] = -f[1]
|
||||
result[5] = s[2]
|
||||
result[6] = u[2]
|
||||
result[7] = -f[2]
|
||||
result[9] = s[3]
|
||||
result[10] = u[3]
|
||||
result[11] = -f[3]
|
||||
result[13] = -dot(s, eye)
|
||||
result[14] = -dot(u, eye)
|
||||
result[15] = dot(f, eye)
|
||||
return result
|
||||
end
|
||||
|
||||
function math3d.perspective(fov, aspect, zNear, zFar)
|
||||
local tanHalf = math.tan(fov / 2.0)
|
||||
local result = {
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
}
|
||||
result[1] = 1.0 / (aspect * tanHalf)
|
||||
result[6] = -1.0 / tanHalf
|
||||
result[11] = zFar / (zNear - zFar)
|
||||
result[12] = -1.0
|
||||
result[15] = (zNear * zFar) / (zNear - zFar)
|
||||
return result
|
||||
end
|
||||
|
||||
return math3d
|
||||
Reference in New Issue
Block a user