Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Mesh.cpp
1/*
2Copyright (C) 2016-2018,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*/
17#include "aeongames/ProtoBufHelpers.hpp"
18#ifdef _MSC_VER
19#pragma warning( push )
20#pragma warning( disable : PROTOBUF_WARNINGS )
21#endif
22#include "mesh.pb.h"
23#ifdef _MSC_VER
24#pragma warning( pop )
25#endif
26#include "aeongames/Mesh.hpp"
27#include "aeongames/Renderer.hpp"
28#include "aeongames/AeonEngine.hpp"
29
30namespace AeonGames
31{
32 Mesh::Mesh() = default;
34 {
35 Unload();
36 }
37
38 const std::vector<Mesh::AttributeTuple>& Mesh::GetAttributes () const
39 {
40 return mAttributes;
41 }
42
43 uint32_t Mesh::GetIndexSize () const
44 {
45 return mIndexSize;
46 }
47
48 uint32_t Mesh::GetIndexCount() const
49 {
50 return mIndexCount;
51 }
52
53 uint32_t Mesh::GetVertexCount() const
54 {
55 return mVertexCount;
56 }
57
58 const std::vector<uint8_t>& Mesh::GetVertexBuffer() const
59 {
60 return mVertexBuffer;
61 }
62
63 const std::vector<uint8_t>& Mesh::GetIndexBuffer() const
64 {
65 return mIndexBuffer;
66 }
67
68 const AABB& Mesh::GetAABB() const
69 {
70 return mAABB;
71 }
72
73 size_t GetAttributeTotalSize ( const Mesh::AttributeTuple& aAttributeTuple )
74 {
75 switch ( std::get<Mesh::AttributeType> ( aAttributeTuple ) )
76 {
77 case Mesh::BYTE:
79 return std::get<1> ( aAttributeTuple );
80 break;
81 case Mesh::SHORT:
84 return 2 * std::get<1> ( aAttributeTuple );
85 break;
86 case Mesh::INT:
88 case Mesh::FLOAT:
89 case Mesh::FIXED:
90 return 4 * std::get<1> ( aAttributeTuple );
91 case Mesh::DOUBLE:
92 return 8 * std::get<1> ( aAttributeTuple );
93 break;
94 }
95 return 0;
96 }
97
98 size_t Mesh::GetStride () const
99 {
100 size_t stride = 0;
101 for ( const auto& i : mAttributes )
102 {
103 stride += GetAttributeTotalSize ( i );
104 }
105 return stride;
106 }
107
108 void Mesh::LoadFromMemory ( const void* aBuffer, size_t aBufferSize )
109 {
110 LoadFromProtoBufObject<Mesh, MeshMsg, "AEONMSH"_mgk> ( *this, aBuffer, aBufferSize );
111 }
112
113 void Mesh::LoadFromPBMsg ( const MeshMsg& aMeshMsg )
114 {
115 mAABB = AABB
116 {
117 {
118 aMeshMsg.center().x(),
119 aMeshMsg.center().y(),
120 aMeshMsg.center().z()
121 },
122 {
123 aMeshMsg.radii().x(),
124 aMeshMsg.radii().y(),
125 aMeshMsg.radii().z()
126 }
127 };
128
129 mVertexCount = aMeshMsg.vertexcount();
130 mIndexCount = aMeshMsg.indexcount();
131 mIndexSize = aMeshMsg.indexsize();
132 mAttributes.reserve ( aMeshMsg.attribute().size() );
133 for ( const auto& i : aMeshMsg.attribute() )
134 {
135 mAttributes.emplace_back (
136 static_cast<AttributeSemantic> ( i.semantic() ),
137 static_cast<AttributeSize> ( i.size() ),
138 static_cast<AttributeType> ( i.type() ),
139 static_cast<AttributeFlags> ( i.flags() )
140 );
141 }
142
143 mVertexBuffer.clear();
144 mVertexBuffer.reserve ( aMeshMsg.vertexbuffer().size() );
145 std::copy ( aMeshMsg.vertexbuffer().begin(), aMeshMsg.vertexbuffer().end(), std::back_inserter ( mVertexBuffer ) );
146
147 mIndexBuffer.clear();
148 mIndexBuffer.reserve ( aMeshMsg.indexbuffer().size() );
149 std::copy ( aMeshMsg.indexbuffer().begin(), aMeshMsg.indexbuffer().end(), std::back_inserter ( mIndexBuffer ) );
150 }
152 {
153 mAABB = AABB{};
154
155 mVertexCount = 0;
156 mIndexCount = 0;
157 mIndexSize = 0;
158
159 mAttributes.clear();
160 mVertexBuffer.clear();
161 mIndexBuffer.clear();
162 }
163}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
Axis Aligned Bounding Box class.
Definition AABB.hpp:34
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Load mesh data from a raw memory buffer.
Definition Mesh.cpp:108
DLL void Unload() final
Unload mesh data and release resources.
Definition Mesh.cpp:151
DLL size_t GetStride() const
Get the stride (bytes per vertex) for the vertex buffer.
Definition Mesh.cpp:98
AttributeSemantic
Semantic meaning of a vertex attribute, identified by CRC32 of its name.
Definition Mesh.hpp:35
DLL uint32_t GetIndexSize() const
Get the size in bytes of a single index.
Definition Mesh.cpp:43
DLL ~Mesh() final
Destructor.
Definition Mesh.cpp:33
DLL void LoadFromPBMsg(const MeshMsg &aMeshMsg)
Load mesh data from a protobuf message.
Definition Mesh.cpp:113
AttributeType
Data type of a vertex attribute component.
Definition Mesh.hpp:48
@ UNSIGNED_SHORT
Unsigned 16-bit integer.
Definition Mesh.hpp:52
@ HALF_FLOAT
16-bit floating point.
Definition Mesh.hpp:53
@ BYTE
Signed 8-bit integer.
Definition Mesh.hpp:49
@ DOUBLE
64-bit floating point.
Definition Mesh.hpp:58
@ SHORT
Signed 16-bit integer.
Definition Mesh.hpp:51
@ UNSIGNED_BYTE
Unsigned 8-bit integer.
Definition Mesh.hpp:50
@ FLOAT
32-bit floating point.
Definition Mesh.hpp:56
@ FIXED
Fixed-point.
Definition Mesh.hpp:57
@ INT
Signed 32-bit integer.
Definition Mesh.hpp:54
@ UNSIGNED_INT
Unsigned 32-bit integer.
Definition Mesh.hpp:55
uint8_t AttributeSize
Type alias for the number of components in a vertex attribute.
Definition Mesh.hpp:78
std::tuple< AttributeSemantic, AttributeSize, AttributeType, AttributeFlags > AttributeTuple
Tuple describing a single vertex attribute (semantic, component count, type, flags).
Definition Mesh.hpp:82
DLL const AABB & GetAABB() const
Get the axis-aligned bounding box of the mesh.
Definition Mesh.cpp:68
DLL uint32_t GetIndexCount() const
Get the total number of indices.
Definition Mesh.cpp:48
uint8_t AttributeFlags
Type alias for vertex attribute flag bits.
Definition Mesh.hpp:80
DLL const std::vector< uint8_t > & GetIndexBuffer() const
Get the raw index data buffer.
Definition Mesh.cpp:63
DLL const std::vector< uint8_t > & GetVertexBuffer() const
Get the raw vertex data buffer.
Definition Mesh.cpp:58
DLL const std::vector< AttributeTuple > & GetAttributes() const
Get the list of vertex attributes.
Definition Mesh.cpp:38
DLL uint32_t GetVertexCount() const
Get the total number of vertices.
Definition Mesh.cpp:53
DLL Mesh()
Default constructor.
<- This is here just for the literals
Definition AABB.hpp:31
void LoadFromProtoBufObject(T &aTarget, const void *aBuffer, size_t aBufferSize)
Loads a Protocol Buffer message from a buffer and populates a target object.
DLL size_t GetAttributeTotalSize(const Mesh::AttributeTuple &aAttributeTuple)
Compute the total byte size of a single vertex attribute.
Definition Mesh.cpp:73