2024-12-27 01:34:34 +00:00
|
|
|
|
|
|
|
#include "Mesh.h"
|
2024-12-27 18:19:20 +00:00
|
|
|
#include "Engine/AssetManager.h"
|
|
|
|
#include "gcml.h"
|
|
|
|
|
|
|
|
#include "../Engine/AssetManager.h"
|
|
|
|
|
2024-12-27 18:47:48 +00:00
|
|
|
extern AssetManager g_AssetManager;
|
2024-12-27 01:34:34 +00:00
|
|
|
|
|
|
|
const std::string MeshComponent::name = "Mesh";
|
|
|
|
|
|
|
|
MeshComponent::MeshComponent()
|
2024-12-30 04:25:16 +00:00
|
|
|
: vao(0), indexCount(0), MeshPath("assets/models/DefaultMesh.obj")
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-12-27 18:59:05 +00:00
|
|
|
const std::string &MeshComponent::GetName() const
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2024-12-27 18:59:05 +00:00
|
|
|
const std::string &MeshComponent::GetStaticName()
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2024-12-30 04:25:16 +00:00
|
|
|
void MeshComponent::Update(float deltaTime)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-12-27 01:34:34 +00:00
|
|
|
YAML::Node MeshComponent::Serialize()
|
|
|
|
{
|
|
|
|
YAML::Node node;
|
2024-12-27 18:59:05 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
node["vao"] = static_cast<int>(vao);
|
|
|
|
node["indexCount"] = static_cast<int>(indexCount);
|
|
|
|
|
2024-12-30 04:25:16 +00:00
|
|
|
// Serialize Textures
|
|
|
|
YAML::Node texturesNode;
|
|
|
|
for (const auto &texture : textures)
|
|
|
|
{
|
|
|
|
YAML::Node texNode;
|
|
|
|
texNode["id"] = static_cast<int>(texture.id);
|
|
|
|
texNode["type"] = texture.type;
|
|
|
|
texNode["path"] = texture.path;
|
|
|
|
texturesNode.push_back(texNode);
|
|
|
|
}
|
|
|
|
node["textures"] = texturesNode;
|
|
|
|
|
|
|
|
node["MeshPath"] = MeshPath;
|
2024-12-27 18:19:20 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2024-12-27 18:59:05 +00:00
|
|
|
void MeshComponent::Deserialize(const YAML::Node &node)
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
2024-12-27 18:19:20 +00:00
|
|
|
if (node["MeshPath"])
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
MeshPath = node["MeshPath"].as<std::string>();
|
2024-12-27 18:59:05 +00:00
|
|
|
|
|
|
|
DEBUG_PRINT("Loading Mesh: %s", MeshPath.c_str());
|
2024-12-27 18:19:20 +00:00
|
|
|
|
2024-12-27 18:59:05 +00:00
|
|
|
Model *model = g_AssetManager.loadAsset<Model *>(AssetType::MODEL, MeshPath.c_str());
|
2024-12-27 18:19:20 +00:00
|
|
|
|
2024-12-30 04:25:16 +00:00
|
|
|
if (!model)
|
|
|
|
{
|
|
|
|
DEBUG_PRINT("Failed to load model: %s", MeshPath.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_PRINT("Model loaded successfully with %zu vertices and %zu indices.",
|
|
|
|
model->vertices.size(), model->indices.size());
|
|
|
|
|
|
|
|
// Assign VAO and index count
|
2024-12-27 18:59:05 +00:00
|
|
|
if (model->vao != 0)
|
|
|
|
{
|
|
|
|
vao = model->vao;
|
|
|
|
}
|
2024-12-27 19:17:33 +00:00
|
|
|
else if (node["vao"])
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
vao = node["vao"].as<int>();
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
2024-12-30 04:25:16 +00:00
|
|
|
|
2024-12-27 18:59:05 +00:00
|
|
|
if (model->indices.size() != 0)
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
indexCount = static_cast<GLuint>(model->indices.size());
|
2024-12-27 18:59:05 +00:00
|
|
|
}
|
2024-12-27 19:17:33 +00:00
|
|
|
else if (node["indexCount"])
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
indexCount = node["indexCount"].as<int>();
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-30 04:25:16 +00:00
|
|
|
// Assign Textures
|
|
|
|
if (!model->textures.empty())
|
|
|
|
{
|
|
|
|
textures = model->textures;
|
2024-12-27 18:59:05 +00:00
|
|
|
}
|
2024-12-30 04:25:16 +00:00
|
|
|
else if (node["textures"])
|
2024-12-27 19:17:33 +00:00
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
const YAML::Node &texturesNode = node["textures"];
|
|
|
|
for (const auto &texNode : texturesNode)
|
|
|
|
{
|
|
|
|
Texture texture;
|
|
|
|
texture.id = texNode["id"].as<int>();
|
|
|
|
texture.type = texNode["type"].as<std::string>();
|
|
|
|
texture.path = texNode["path"].as<std::string>();
|
|
|
|
textures.push_back(texture);
|
|
|
|
}
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (node["vao"])
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
vao = node["vao"].as<int>();
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
|
|
|
if (node["indexCount"])
|
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
indexCount = node["indexCount"].as<int>();
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
2024-12-30 04:25:16 +00:00
|
|
|
if (node["textures"])
|
2024-12-27 19:17:33 +00:00
|
|
|
{
|
2024-12-30 04:25:16 +00:00
|
|
|
const YAML::Node &texturesNode = node["textures"];
|
|
|
|
for (const auto &texNode : texturesNode)
|
|
|
|
{
|
|
|
|
Texture texture;
|
|
|
|
texture.id = texNode["id"].as<int>();
|
|
|
|
texture.type = texNode["type"].as<std::string>();
|
|
|
|
texture.path = texNode["path"].as<std::string>();
|
|
|
|
textures.push_back(texture);
|
|
|
|
}
|
2024-12-27 19:17:33 +00:00
|
|
|
}
|
2024-12-27 18:19:20 +00:00
|
|
|
}
|
2024-12-30 04:25:16 +00:00
|
|
|
}
|