Cogs.Core
ExportGltfCommand.cpp
1#include "ExportGltfCommand.h"
2#include "Serialization/GltfWriter.h"
3#include "Components/Core/ModelComponent.h"
4#include "Components/Core/MeshComponent.h"
5#include "Components/Core/SceneComponent.h"
6#include "Components/Core/StaticModelComponent.h"
7
8#include "Foundation/Logging/Logger.h"
9
10namespace
11{
12 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("ExportGltfCommand");
13
14 void getEntity(Cogs::Core::Context* /*context*/, std::vector<Cogs::ComponentModel::Entity*>& entities, Cogs::ComponentModel::Entity* entity)
15 {
16 if (entity == nullptr) return;
17
18 if (auto result = std::find_if(entities.begin(), entities.end(), [e = entity](Cogs::ComponentModel::Entity*& x) { return x->getId() == e->getId(); });
19 result != entities.end()) {
20 return;
21 }
22
24 if (!sceneComp) return;
25
28 if (!meshComp && !staticModelComp) return;
29
30 LOG_DEBUG(logger, "export entity id: %zu", entity->getId());
31 entities.push_back(entity);
32 }
33}
34
35#ifndef EMSCRIPTEN
37{
38
39 std::vector<ComponentModel::Entity*> entities;
40 if (exportScene) {
41 const std::unordered_map<EntityId, EntityPtr>& entityMap = context->store->getEntities();
42
43 for (const auto& e : entityMap) {
44 Cogs::ComponentModel::Entity* entity = context->store->getEntityPtr(static_cast<EntityId>(e.first));
45 getEntity(context, entities, entity);
46 }
47 }
48 else {
49 for (auto id : state->selected) {
50 ComponentModel::Entity* entity = context->store->getEntityPtr(id);
51 if (entity) {
52 entities.push_back(context->store->getEntityPtr(id));
53 }
54 }
55 }
56
57 bool ok;
58 if (exportAsGlb == true) {
59 ok = GltfWriter::writeGlb(entities, filePath, fileName);
60 }
61 else {
62 ok = GltfWriter::writeGltf(entities, filePath, fileName);
63 }
64
65 if (!ok) {
66 LOG_ERROR(logger, "Save %s failed. Path: %s", (exportAsGlb ? "GLB" : "GLTF"), filePath.c_str());
67 }
68}
69#else
71#endif
72
73void Cogs::Core::ExportGltfCommand::undo()
74{
75}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
constexpr size_t getId() const noexcept
Get the unique identifier of this entity.
Definition: Entity.h:113
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class EntityStore * store
Entity store.
Definition: Context.h:231
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
const std::unordered_map< EntityId, EntityPtr > & getEntities() const
Return map of entities with global ownership.
Definition: EntityStore.h:268
virtual void apply() override
Run the command.
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
Definition: MeshComponent.h:15
Contains information on how the entity behaves in the scene.
Log implementation class.
Definition: LogManager.h:139
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180