added load from json game objects

This commit is contained in:
Vlad committed 2026-04-07 20:17:04 +06:00
1 parent 178f69967b
commit e1b491be73
6 files changed
+242 -3

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
#include "GameObjectLoader.h"
#include <fstream>
#include <iostream>
#include <cmath>
#include "render/Renderer.h"
#include "render/TextureManager.h"
#include "utils/Utils.h"
#include <Eigen/Geometry>
namespace ZL {
using json = nlohmann::json;
std::vector<GameObjectData> GameObjectLoader::loadFromJson(const std::string& jsonPath, const std::string& zipPath)
{
std::vector<GameObjectData> objects;
json j;
try {
std::ifstream file(jsonPath);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + jsonPath);
}
if (file.peek() == std::ifstream::traits_type::eof()) {
throw std::runtime_error("JSON file is empty: " + jsonPath);
}
file >> j;
if (!j.contains("objects") || !j["objects"].is_array()) {
std::cerr << "Warning: 'objects' array not found in " << jsonPath << std::endl;
return objects;
}
for (const auto& item : j["objects"]) {
GameObjectData data;
data.name = item.value("name", "Unknown");
data.texturePath = item.value("texturePath", "");
data.meshPath = item.value("meshPath", "");
data.rotationX = item.value("rotationX", 0.0f);
data.rotationY = item.value("rotationY", 0.0f);
data.rotationZ = item.value("rotationZ", 0.0f);
data.positionX = item.value("positionX", 0.0f);
data.positionY = item.value("positionY", 0.0f);
data.positionZ = item.value("positionZ", 0.0f);
data.scale = item.value("scale", 1.0f);
if (!data.meshPath.empty()) {
objects.push_back(data);
}
}
std::cout << "Successfully loaded " << objects.size() << " game objects from " << jsonPath << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error loading JSON: " << e.what() << std::endl;
}
return objects;
}
std::unordered_map<std::string, LoadedGameObject> GameObjectLoader::loadAndCreateGameObjects(
const std::string& jsonPath,
Renderer& renderer,
const std::string& zipPath)
{
std::unordered_map<std::string, LoadedGameObject> gameObjects;
std::vector<GameObjectData> objectsData = loadFromJson(jsonPath, zipPath);
for (const auto& objData : objectsData) {
std::cout << "Loading game object: " << objData.name << std::endl;
LoadedGameObject gameObj;
gameObj.name = objData.name;
// Load texture
try {
gameObj.texture = std::make_shared<Texture>(CreateTextureDataFromPng(objData.texturePath, zipPath.c_str()));
}
catch (const std::exception& e) {
std::cerr << "GameObjectLoader: Failed to load texture for '" << objData.name << "': " << e.what() << std::endl;
continue;
}
// Load mesh
try {
gameObj.mesh.data = LoadFromTextFile02(objData.meshPath, zipPath);
}
catch (const std::exception& e) {
std::cerr << "GameObjectLoader: Failed to load mesh for '" << objData.name << "': " << e.what() << std::endl;
continue;
}
// Apply rotation
Eigen::Quaternionf rotationQuat = Eigen::Quaternionf::Identity();
if (objData.rotationX != 0.0f) {
rotationQuat = Eigen::Quaternionf(Eigen::AngleAxisf(objData.rotationX, Eigen::Vector3f::UnitX())) * rotationQuat;
}
if (objData.rotationY != 0.0f) {
rotationQuat = Eigen::Quaternionf(Eigen::AngleAxisf(objData.rotationY, Eigen::Vector3f::UnitY())) * rotationQuat;
}
if (objData.rotationZ != 0.0f) {
rotationQuat = Eigen::Quaternionf(Eigen::AngleAxisf(objData.rotationZ, Eigen::Vector3f::UnitZ())) * rotationQuat;
}
gameObj.mesh.data.RotateByMatrix(rotationQuat.toRotationMatrix());
// Apply scale
if (objData.scale != 1.0f) {
gameObj.mesh.data.Scale(objData.scale);
}
// Apply position
if (objData.positionX != 0.0f || objData.positionY != 0.0f || objData.positionZ != 0.0f) {
gameObj.mesh.data.Move({ objData.positionX, objData.positionY, objData.positionZ });
}
gameObj.mesh.RefreshVBO();
gameObjects[objData.name] = gameObj;
std::cout << "Successfully loaded: " << objData.name << std::endl;
}
std::cout << "Total game objects loaded: " << gameObjects.size() << std::endl;
return gameObjects;
}
} // namespace ZL