diff options
Diffstat (limited to 'src/3d/scene_loader.cc')
| -rw-r--r-- | src/3d/scene_loader.cc | 196 |
1 files changed, 114 insertions, 82 deletions
diff --git a/src/3d/scene_loader.cc b/src/3d/scene_loader.cc index 69079ef..669fac8 100644 --- a/src/3d/scene_loader.cc +++ b/src/3d/scene_loader.cc @@ -1,108 +1,140 @@ #include "3d/scene_loader.h" -#include "util/asset_manager.h" #include "generated/assets.h" +#include "util/asset_manager.h" #include "util/mini_math.h" -#include <cstring> #include <cstdio> +#include <cstring> #include <vector> bool SceneLoader::LoadScene(Scene& scene, const uint8_t* data, size_t size) { - if (!data || size < 16) { // Header size check - printf("SceneLoader: Data too small\n"); - return false; - } + if (!data || size < 16) { // Header size check + printf("SceneLoader: Data too small\n"); + return false; + } - // Check Magic - if (std::memcmp(data, "SCN1", 4) != 0) { - printf("SceneLoader: Invalid magic (expected SCN1)\n"); - return false; - } - - size_t offset = 4; + // Check Magic + if (std::memcmp(data, "SCN1", 4) != 0) { + printf("SceneLoader: Invalid magic (expected SCN1)\n"); + return false; + } - uint32_t num_objects = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; - uint32_t num_cameras = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; - uint32_t num_lights = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; + size_t offset = 4; - // printf("SceneLoader: Loading %d objects, %d cameras, %d lights\n", num_objects, num_cameras, num_lights); + uint32_t num_objects = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; + uint32_t num_cameras = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; + uint32_t num_lights = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; - for (uint32_t i = 0; i < num_objects; ++i) { - if (offset + 64 > size) return false; // Name check - - char name[65] = {0}; - std::memcpy(name, data + offset, 64); offset += 64; + // printf("SceneLoader: Loading %d objects, %d cameras, %d lights\n", + // num_objects, num_cameras, num_lights); - if (offset + 4 > size) return false; - uint32_t type_val = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; - ObjectType type = (ObjectType)type_val; + for (uint32_t i = 0; i < num_objects; ++i) { + if (offset + 64 > size) + return false; // Name check - if (offset + 12 + 16 + 12 + 16 > size) return false; // Transforms + Color + char name[65] = {0}; + std::memcpy(name, data + offset, 64); + offset += 64; - float px = *reinterpret_cast<const float*>(data + offset); offset += 4; - float py = *reinterpret_cast<const float*>(data + offset); offset += 4; - float pz = *reinterpret_cast<const float*>(data + offset); offset += 4; - vec3 pos(px, py, pz); + if (offset + 4 > size) + return false; + uint32_t type_val = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; + ObjectType type = (ObjectType)type_val; - float rx = *reinterpret_cast<const float*>(data + offset); offset += 4; - float ry = *reinterpret_cast<const float*>(data + offset); offset += 4; - float rz = *reinterpret_cast<const float*>(data + offset); offset += 4; - float rw = *reinterpret_cast<const float*>(data + offset); offset += 4; - quat rot(rx, ry, rz, rw); + if (offset + 12 + 16 + 12 + 16 > size) + return false; // Transforms + Color - float sx = *reinterpret_cast<const float*>(data + offset); offset += 4; - float sy = *reinterpret_cast<const float*>(data + offset); offset += 4; - float sz = *reinterpret_cast<const float*>(data + offset); offset += 4; - vec3 scale(sx, sy, sz); + float px = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float py = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float pz = *reinterpret_cast<const float*>(data + offset); + offset += 4; + vec3 pos(px, py, pz); - float cr = *reinterpret_cast<const float*>(data + offset); offset += 4; - float cg = *reinterpret_cast<const float*>(data + offset); offset += 4; - float cb = *reinterpret_cast<const float*>(data + offset); offset += 4; - float ca = *reinterpret_cast<const float*>(data + offset); offset += 4; - vec4 color(cr, cg, cb, ca); + float rx = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float ry = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float rz = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float rw = *reinterpret_cast<const float*>(data + offset); + offset += 4; + quat rot(rx, ry, rz, rw); - // Mesh Asset Name Length - if (offset + 4 > size) return false; - uint32_t name_len = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; + float sx = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float sy = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float sz = *reinterpret_cast<const float*>(data + offset); + offset += 4; + vec3 scale(sx, sy, sz); - AssetId mesh_id = (AssetId)0; // Default or INVALID (if 0 is invalid) + float cr = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float cg = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float cb = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float ca = *reinterpret_cast<const float*>(data + offset); + offset += 4; + vec4 color(cr, cg, cb, ca); - if (name_len > 0) { - if (offset + name_len > size) return false; - char mesh_name[128] = {0}; - if (name_len < 128) { - std::memcpy(mesh_name, data + offset, name_len); - } - offset += name_len; - - // Resolve Asset ID - mesh_id = GetAssetIdByName(mesh_name); - if (mesh_id == AssetId::ASSET_LAST_ID) { - printf("SceneLoader: Warning: Mesh asset '%s' not found for object '%s'\n", mesh_name, name); - } - } + // Mesh Asset Name Length + if (offset + 4 > size) + return false; + uint32_t name_len = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; - // Physics properties - if (offset + 4 + 4 + 4 > size) return false; - float mass = *reinterpret_cast<const float*>(data + offset); offset += 4; - float restitution = *reinterpret_cast<const float*>(data + offset); offset += 4; - uint32_t is_static_u32 = *reinterpret_cast<const uint32_t*>(data + offset); offset += 4; - bool is_static = (is_static_u32 != 0); + AssetId mesh_id = (AssetId)0; // Default or INVALID (if 0 is invalid) - // Create Object3D - Object3D obj(type); - obj.position = pos; - obj.rotation = rot; - obj.scale = scale; - obj.color = color; - obj.mesh_asset_id = mesh_id; - obj.mass = mass; - obj.restitution = restitution; - obj.is_static = is_static; - // user_data is nullptr by default + if (name_len > 0) { + if (offset + name_len > size) + return false; + char mesh_name[128] = {0}; + if (name_len < 128) { + std::memcpy(mesh_name, data + offset, name_len); + } + offset += name_len; - // Add to scene - scene.add_object(obj); + // Resolve Asset ID + mesh_id = GetAssetIdByName(mesh_name); + if (mesh_id == AssetId::ASSET_LAST_ID) { + printf( + "SceneLoader: Warning: Mesh asset '%s' not found for object '%s'\n", + mesh_name, name); + } } - return true; + // Physics properties + if (offset + 4 + 4 + 4 > size) + return false; + float mass = *reinterpret_cast<const float*>(data + offset); + offset += 4; + float restitution = *reinterpret_cast<const float*>(data + offset); + offset += 4; + uint32_t is_static_u32 = *reinterpret_cast<const uint32_t*>(data + offset); + offset += 4; + bool is_static = (is_static_u32 != 0); + + // Create Object3D + Object3D obj(type); + obj.position = pos; + obj.rotation = rot; + obj.scale = scale; + obj.color = color; + obj.mesh_asset_id = mesh_id; + obj.mass = mass; + obj.restitution = restitution; + obj.is_static = is_static; + // user_data is nullptr by default + + // Add to scene + scene.add_object(obj); + } + + return true; }
\ No newline at end of file |
