add stlloader

This commit is contained in:
Richard Ward
2025-12-19 16:22:19 +00:00
parent f383e8abd5
commit a9ad53ca8d
3 changed files with 119 additions and 22 deletions

View File

@@ -23,7 +23,6 @@ class SDL3CppConan(ConanFile):
self.requires("vulkan-validationlayers/1.3.243.0")
self.requires("ogg/1.3.5")
self.requires("theora/1.1.1")
self.requires("rapidjson/1.1.0")
self.requires("cli11/2.6.0")
self.requires("bullet3/3.25")
self.requires("box2d/3.1.1")

View File

@@ -1,23 +1,3 @@
local cube_vertices = {
{ position = {-1.0, -1.0, -1.0}, color = {1.0, 0.0, 0.0} },
{ position = {1.0, -1.0, -1.0}, color = {0.0, 1.0, 0.0} },
{ position = {1.0, 1.0, -1.0}, color = {0.0, 0.0, 1.0} },
{ position = {-1.0, 1.0, -1.0}, color = {1.0, 1.0, 0.0} },
{ position = {-1.0, -1.0, 1.0}, color = {1.0, 0.0, 1.0} },
{ position = {1.0, -1.0, 1.0}, color = {0.0, 1.0, 1.0} },
{ position = {1.0, 1.0, 1.0}, color = {1.0, 1.0, 1.0} },
{ position = {-1.0, 1.0, 1.0}, color = {0.2, 0.2, 0.2} },
}
local cube_indices = {
1, 2, 3, 3, 4, 1, -- back
5, 6, 7, 7, 8, 5, -- front
1, 5, 8, 8, 4, 1, -- left
2, 6, 7, 7, 3, 2, -- right
4, 3, 7, 7, 8, 4, -- top
1, 2, 6, 6, 5, 1, -- bottom
}
local pyramid_vertices = {
{ position = {0.0, 1.0, 0.0}, color = {1.0, 0.5, 0.0} },
{ position = {-1.0, -1.0, -1.0}, color = {0.0, 1.0, 1.0} },
@@ -35,6 +15,116 @@ local pyramid_indices = {
4, 5, 2,
}
local fallback_cube_vertices = {
{ position = {-1.0, -1.0, -1.0}, color = {1.0, 0.0, 0.0} },
{ position = {1.0, -1.0, -1.0}, color = {0.0, 1.0, 0.0} },
{ position = {1.0, 1.0, -1.0}, color = {0.0, 0.0, 1.0} },
{ position = {-1.0, 1.0, -1.0}, color = {1.0, 1.0, 0.0} },
{ position = {-1.0, -1.0, 1.0}, color = {1.0, 0.0, 1.0} },
{ position = {1.0, -1.0, 1.0}, color = {0.0, 1.0, 1.0} },
{ position = {1.0, 1.0, 1.0}, color = {1.0, 1.0, 1.0} },
{ position = {-1.0, 1.0, 1.0}, color = {0.2, 0.2, 0.2} },
}
local fallback_cube_indices = {
1, 2, 3, 3, 4, 1, -- back
5, 6, 7, 7, 8, 5, -- front
1, 5, 8, 8, 4, 1, -- left
2, 6, 7, 7, 3, 2, -- right
4, 3, 7, 7, 8, 4, -- top
1, 2, 6, 6, 5, 1, -- bottom
}
local function resolve_script_directory()
local info = debug.getinfo(1, "S")
local source = info and info.source
if not source then
return "."
end
if source:sub(1, 1) == "@" then
local path = source:sub(2)
return path:match("^(.*[\\/])") or "."
end
return "."
end
local function combine_paths(first, ...)
local parts = {first, ...}
local combined = parts[1] or ""
for i = 2, #parts do
local piece = parts[i]
if piece and piece ~= "" then
local last = combined:sub(-1)
if last ~= "/" and last ~= "\\" then
combined = combined .. "/"
end
combined = combined .. piece
end
end
return combined
end
local function load_stl_mesh(path)
local file, err = io.open(path, "r")
if not file then
return nil, nil, err
end
local vertices = {}
local indices = {}
local color = {0.6, 0.8, 1.0}
local vertex_count = 0
for line in file:lines() do
if line:lower():match("^%s*vertex") then
local coords = {}
for token in line:gmatch("%S+") do
if token:lower() ~= "vertex" then
local value = tonumber(token)
if value then
coords[#coords + 1] = value
end
end
end
if #coords == 3 then
vertex_count = vertex_count + 1
vertices[vertex_count] = {
position = {coords[1], coords[2], coords[3]},
color = {color[1], color[2], color[3]},
}
indices[#indices + 1] = vertex_count
end
end
end
file:close()
if vertex_count == 0 then
return nil, nil, "STL did not include any vertex lines"
end
return vertices, indices, nil
end
local script_directory = resolve_script_directory()
local stl_cube_path = combine_paths(script_directory, "models", "cube.stl")
local stl_vertices, stl_indices, stl_error = load_stl_mesh(stl_cube_path)
local cube_vertices = fallback_cube_vertices
local cube_indices = fallback_cube_indices
local stl_debug_info = {
path = stl_cube_path,
loaded = false,
vertex_count = 0,
index_count = 0,
error = stl_error,
}
if stl_vertices then
cube_vertices = stl_vertices
cube_indices = stl_indices
stl_debug_info.loaded = true
stl_debug_info.vertex_count = #stl_vertices
stl_debug_info.index_count = #stl_indices
else
stl_debug_info.error = stl_error or "STL file not available"
end
local math3d = require("math3d")
local string_format = string.format
local table_concat = table.concat
@@ -88,6 +178,14 @@ local function log_debug(fmt, ...)
print(string_format(fmt, ...))
end
if stl_debug_info.loaded then
log_debug("Loaded cube mesh from %s (%d vertices, %d indices)",
stl_debug_info.path, stl_debug_info.vertex_count, stl_debug_info.index_count)
else
log_debug("Failed to load cube STL (%s); using fallback cube",
stl_debug_info.error or "unknown")
end
local rotation_speeds = {x = 0.5, y = 0.7}
local shader_variants = {

View File

@@ -30,7 +30,7 @@ SPINNING_BINARY = os.path.join(
)
ALIASES = {
"conan_detect": "conan profile detect",
"conan_detect": "conan profile detect -f",
"conan_install": (
"conan install . -of build -b missing "
"-s compiler=msvc -s compiler.version=194 -s compiler.cppstd=17 "