Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Model.cpp
1/*
2Copyright (C) 2018,2019,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <mutex>
18#include "aeongames/ProtoBufHelpers.hpp"
19#include "aeongames/Node.hpp"
20#include "aeongames/Mesh.hpp"
21#include "aeongames/Pipeline.hpp"
22#include "aeongames/Material.hpp"
23#include "aeongames/Skeleton.hpp"
24#include "aeongames/Animation.hpp"
25#include "aeongames/Utilities.hpp"
26#include "aeongames/ProtoBufHelpers.hpp"
27#include "aeongames/ProtoBufUtils.hpp"
28#include "aeongames/CRC.hpp"
29#include "aeongames/AeonEngine.hpp"
30#include "aeongames/ResourceCache.hpp"
31#include "aeongames/Renderer.hpp"
32#include "aeongames/Model.hpp"
33#ifdef _MSC_VER
34#pragma warning( push )
35#pragma warning( disable : PROTOBUF_WARNINGS )
36#endif
37#include "model.pb.h"
38#ifdef _MSC_VER
39#pragma warning( pop )
40#endif
41
42namespace AeonGames
43{
44 Model::Model()
45 = default;
46 Model::~Model()
47 = default;
48
49 void Model::LoadFromMemory ( const void* aBuffer, size_t aBufferSize )
50 {
51 LoadFromProtoBufObject<Model, ModelMsg, "AEONMDL"_mgk> ( *this, aBuffer, aBufferSize );
52 }
53
54 void Model::LoadFromPBMsg ( const ModelMsg& aModelMsg )
55 {
56 ResourceId default_pipeline{};
57 ResourceId default_material{};
58
59 // Default Pipeline ---------------------------------------------------------------------
60 if ( aModelMsg.has_default_pipeline() )
61 {
62 default_pipeline = {"Pipeline"_crc32, GetReferenceMsgId ( aModelMsg.default_pipeline() ) } ;
63 default_pipeline.Store();
64 }
65
66 // Default Material ---------------------------------------------------------------------
67 if ( aModelMsg.has_default_material() )
68 {
69 default_material = {"Material"_crc32, GetReferenceMsgId ( aModelMsg.default_material() ) } ;
70 default_material.Store();
71 }
72
73 // Skeleton -----------------------------------------------------------------------------
74 if ( aModelMsg.has_skeleton() )
75 {
76 mSkeleton = { "Skeleton"_crc32, GetReferenceMsgId ( aModelMsg.skeleton() ) };
77 mSkeleton.Store();
78 }
79 // Meshes -----------------------------------------------------------------------------
80 mAssemblies.reserve ( aModelMsg.assembly_size() );
81 for ( auto& assembly : aModelMsg.assembly() )
82 {
83 ResourceId mesh{};
84 ResourceId pipeline{default_pipeline};
85 ResourceId material{default_material};
86
87 if ( assembly.has_mesh() )
88 {
89 mesh = {"Mesh"_crc32, GetReferenceMsgId ( assembly.mesh() ) } ;
90 mesh.Store();
91 }
92
93 if ( assembly.has_pipeline() )
94 {
95 pipeline = {"Pipeline"_crc32, GetReferenceMsgId ( assembly.pipeline() ) } ;
96 pipeline.Store();
97 }
98
99 if ( assembly.has_material() )
100 {
101 material = {"Material"_crc32, GetReferenceMsgId ( assembly.material() ) } ;
102 material.Store();
103 }
104 mAssemblies.emplace_back ( mesh, pipeline, material );
105 }
106 // Animations -----------------------------------------------------------------------------
107 mAnimations.reserve ( aModelMsg.animation_size() );
108 for ( auto& animation : aModelMsg.animation() )
109 {
110 mAnimations.emplace_back ( ResourceId{"Animation"_crc32, GetReferenceMsgId ( animation ) } ).Store();
111 }
112 }
113
115 {
117 mAssemblies.clear();
118 mAnimations.clear();
119 }
120
121 const std::vector<Model::Assembly>& Model::GetAssemblies() const
122 {
123 return mAssemblies;
124 }
125
127 {
128 return mSkeleton.Cast<Skeleton>();
129 }
130
131 const std::vector<ResourceId>& Model::GetAnimations() const
132 {
133 return mAnimations;
134 }
135
136 void Model::LoadRendererResources ( Renderer& aRenderer ) const
137 {
138 for ( const auto& i : mAssemblies )
139 {
140 aRenderer.LoadMesh ( *std::get<0> ( i ).Cast<Mesh>() );
141 aRenderer.LoadPipeline ( *std::get<1> ( i ).Cast<Pipeline>() );
142 aRenderer.LoadMaterial ( *std::get<2> ( i ).Cast<Material>() );
143 }
144 }
145
146 void Model::UnloadRendererResources ( Renderer& aRenderer ) const
147 {
148 for ( const auto& i : mAssemblies )
149 {
150 aRenderer.UnloadMesh ( *std::get<0> ( i ).Cast<Mesh>() );
151 aRenderer.UnloadPipeline ( *std::get<1> ( i ).Cast<Pipeline>() );
152 aRenderer.UnloadMaterial ( *std::get<2> ( i ).Cast<Material>() );
153 }
154 }
155}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
DLL const Skeleton * GetSkeleton() const
Get the skeleton associated with this model.
Definition Model.cpp:126
DLL void Unload() final
Unload model data and free resources.
Definition Model.cpp:114
DLL void LoadFromPBMsg(const ModelMsg &aModelMsg)
Load model data from a protobuf message.
Definition Model.cpp:54
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Load model data from a memory buffer.
Definition Model.cpp:49
DLL void LoadRendererResources(Renderer &aRenderer) const
Load renderer-specific resources for this model.
Definition Model.cpp:136
DLL const std::vector< Assembly > & GetAssemblies() const
Get the list of assemblies that compose this model.
Definition Model.cpp:121
DLL void UnloadRendererResources(Renderer &aRenderer) const
Unload renderer-specific resources for this model.
Definition Model.cpp:146
DLL const std::vector< ResourceId > & GetAnimations() const
Get the list of animation resource identifiers.
Definition Model.cpp:131
Abstract base class for rendering backends.
Definition Renderer.hpp:44
virtual void UnloadPipeline(const Pipeline &aPipeline)=0
Unloads a rendering pipeline from the renderer.
virtual void LoadPipeline(const Pipeline &aPipeline)=0
Loads a rendering pipeline (shaders and state) into the renderer.
virtual void UnloadMaterial(const Material &aMaterial)=0
Unloads material data from the renderer.
virtual void LoadMaterial(const Material &aMaterial)=0
Loads material data into the renderer.
virtual void LoadMesh(const Mesh &aMesh)=0
Loads mesh data into GPU memory.
virtual void UnloadMesh(const Mesh &aMesh)=0
Unloads mesh data from GPU memory.
Identifies a resource by its type and path CRC32 hashes.
void Store() const
Construct and store the resource in the cache if not already present.
Skeletal animation resource containing a hierarchy of joints.
Definition Skeleton.hpp:30
<- This is here just for the literals
Definition AABB.hpp:31
DLL uint32_t GetReferenceMsgId(const ReferenceMsg &reference_buffer)
Retrieve the identifier from a ReferenceMsg.
void LoadFromProtoBufObject(T &aTarget, const void *aBuffer, size_t aBufferSize)
Loads a Protocol Buffer message from a buffer and populates a target object.