#include "3d/scene_loader.h" #include "generated/assets.h" #include "plane_data.h" #include "util/asset_manager.h" #include "util/mini_math.h" #include #include #include // For std::shared_ptr #include // For std::nothrow #include namespace SceneLoader { // Safe unaligned read helpers static inline uint32_t read_u32(const uint8_t* p) { uint32_t v; std::memcpy(&v, p, sizeof(v)); return v; } static inline float read_f32(const uint8_t* p) { float v; std::memcpy(&v, p, sizeof(v)); return v; } bool 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; } // Check Magic if (std::memcmp(data, "SCN1", 4) != 0) { printf("SceneLoader: Invalid magic (expected SCN1)\n"); return false; } size_t offset = 4; uint32_t num_objects = read_u32(data + offset); offset += 4; uint32_t num_cameras = read_u32(data + offset); offset += 4; uint32_t num_lights = read_u32(data + offset); offset += 4; // printf("SceneLoader: Loading %d objects, %d cameras, %d lights\n", // num_objects, num_cameras, num_lights); 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; if (offset + 4 > size) return false; uint32_t type_val = read_u32(data + offset); offset += 4; ObjectType type = (ObjectType)type_val; if (offset + 12 + 16 + 12 + 16 > size) return false; // Transforms + Color float px = read_f32(data + offset); offset += 4; float py = read_f32(data + offset); offset += 4; float pz = read_f32(data + offset); offset += 4; vec3 pos(px, py, pz); float rx = read_f32(data + offset); offset += 4; float ry = read_f32(data + offset); offset += 4; float rz = read_f32(data + offset); offset += 4; float rw = read_f32(data + offset); offset += 4; quat rot(rx, ry, rz, rw); float sx = read_f32(data + offset); offset += 4; float sy = read_f32(data + offset); offset += 4; float sz = read_f32(data + offset); offset += 4; vec3 scale(sx, sy, sz); float cr = read_f32(data + offset); offset += 4; float cg = read_f32(data + offset); offset += 4; float cb = read_f32(data + offset); offset += 4; float ca = read_f32(data + offset); offset += 4; vec4 color(cr, cg, cb, ca); // Plane Distance (if type == PLANE) float plane_distance = 0.0f; if (type == ObjectType::PLANE) { // Check bounds before reading plane_distance if (offset + 4 > size) return false; plane_distance = read_f32(data + offset); offset += 4; // Advance offset after reading plane_distance } // Mesh Asset Name Length // The offset is now correctly positioned for name_len, // either after ca (if not PLANE) or after plane_distance (if PLANE). if (offset + 4 > size) return false; uint32_t name_len = read_u32(data + offset); offset += 4; AssetId mesh_id = (AssetId)0; // Default or INVALID (if 0 is invalid) 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); } } // Physics properties if (offset + 4 + 4 + 4 > size) return false; float mass = read_f32(data + offset); offset += 4; float restitution = read_f32(data + offset); offset += 4; uint32_t is_static_u32 = read_u32(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 // Store plane distance in shared_user_data if it's a plane if (type == ObjectType::PLANE) { // Allocate PlaneData on the heap and manage with shared_ptr // Use std::make_shared for exception safety and efficiency obj.shared_user_data = std::make_shared(); // Assign the plane distance // Safely cast void* to PlaneData* using C-style cast on the shared_ptr's // get() ((PlaneData*)(obj.shared_user_data.get()))->distance = plane_distance; } // Add to scene scene.add_object(obj); } return true; } } // namespace SceneLoader