1#include "RationalReducerComponent.h"
2#include "MeshReductionManager.h"
4#include "Components/Core/MeshComponent.h"
5#include "Components/Core/MeshRenderComponent.h"
6#include "Components/Core/SceneComponent.h"
7#include "Components/Core/ModelComponent.h"
12#include "Resources/Mesh.h"
13#include "Resources/MaterialInstance.h"
14#include "Services/Services.h"
16#include "Foundation/ComponentModel/Component.h"
17#include "Foundation/Reflection/TypeDatabase.h"
24using namespace Cogs::RationalReducerExtension;
26void RationalReducerComponent::registerType()
37 Method(
Name(
"initialize"), &RationalReducerComponent::initialize),
38 Method(
Name(
"update"), &RationalReducerComponent::update),
40 DynamicComponent::registerDerivedType<RationalReducerComponent>().setFields(fields).setMethods(methods);
43void RationalReducerComponent::initialize(
Context * context)
45 this->context = context;
55 context->
engine->setDirty();
60 void updateMeshComps(
Context* context, std::vector<ComponentHandle> meshComps, std::vector<MeshHandle> handles)
62 for (
size_t i = 0; i < meshComps.size(); i++)
64 if (i >= handles.size())
return;
65 updateMeshComp(context, meshComps[i].resolveComponent<MeshComponent>(), handles[i]);
71 std::stack<Cogs::ComponentModel::Entity*> candidates;
72 candidates.push(entity);
74 std::vector<ComponentHandle> comps;
76 while (!candidates.empty()) {
77 auto candidate = candidates.top();
84 if (sceneComp !=
nullptr) {
85 for (
auto& child : sceneComp->children) {
86 candidates.push(child.get());
95void RationalReducerComponent::update()
98 if (meshComps.empty())
return;
100 if (this->meshComponents.size() > 0 && !std::equal(meshComps.begin(), meshComps.end(), this->meshComponents.begin())) {
102 originalMeshes.clear();
103 reducedMeshes.clear();
105 this->meshComponents = meshComps;
109 updateMeshComps(context, meshComps, originalMeshes);
117 updateMeshComps(context, meshComps, reducedMeshes);
123 if (originalMeshes.size() == 0) {
124 for (
auto & handle : meshComps) {
126 originalMeshes.push_back(comp->meshHandle);
128 materials.push_back(mat);
129 mat->setVariant(
"TangentSpaceNormalMap",
false);
135 std::vector<Mesh *> meshes;
136 for (
auto & handle : originalMeshes) meshes.push_back(handle.
resolve());
137 std::vector<MaterialInstance *> mats;
138 for (
auto & mat : materials) mats.push_back(mat.resolve());
144 updateMeshComps(context, meshComps, reducedMeshes);
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
ComponentType * getComponent() const
class Entity * getContainer() const
Get the container currently owning this component instance.
ComponentHandle getComponentHandle() const
Container for components, providing composition of dynamic entities.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< class Services > services
Services.
std::unique_ptr< class Engine > engine
Engine instance.
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
MeshHandle meshHandle
Handle to a Mesh resource to use when rendering.
Renders the contents of a MeshComponent using the given materials.
Contains information on how the entity behaves in the scene.
Interface to Rational Reducer for reducing meshes.
Field definition describing a single data member of a data structure.
Simple method definition.
Contains code for composing and managing entities built from components.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Contains reflection support.
ResourceType * resolve() const
Resolve the handle, returning a pointer to the actual resource.
float reductionPercent
The percentage of the triangles you want to remove.
int loglevel
Logging level.
bool enabled
Set to true to enable reduction.
bool addNormals
true if normals always to be generated, false = generate only if normals in input data.
float reductionEpsilon
A value telling RR to join all vertices where the distance between them are less than the given numbe...
Represents an unique name.