Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F123433
MeshManager.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
MeshManager.cpp
View Options
#include
<echo/Resource/3dsReader.h>
#include
<echo/Resource/MeshManager.h>
#include
<echo/Resource/MeshReader.h>
#include
<echo/Util/StringUtils.h>
#include
<echo/Graphics/SubMesh.h>
#include
<echo/Graphics/Mesh.h>
#include
<echo/FileSystem/FileSystem.h>
#include
<boost/bind.hpp>
namespace
Echo
{
MeshManager
::
MeshManager
(
FileSystem
&
fileSystem
,
MaterialManager
&
materialManager
,
SkeletonManager
&
skeletonManager
)
:
ResourceManager
<
Mesh
>
(
"mesh"
),
mFileSystem
(
fileSystem
),
mMaterialManager
(
materialManager
),
mSkeletonManager
(
skeletonManager
),
mMeshNames
(
0
)
{
RegisterLoader
(
"3ds"
,
boost
::
bind
(
&
Load3DS
,
_1
,
_2
));
RegisterLoader
(
"mesh"
,
boost
::
bind
(
&
LoadMesh
,
_1
,
_2
,
boost
::
ref
(
mMaterialManager
),
boost
::
ref
(
mSkeletonManager
)));
}
MeshManager
::~
MeshManager
()
{
}
bool
MeshManager
::
RegisterLoader
(
const
std
::
string
&
fileExtension
,
MeshLoaderFunc
loaderFunction
)
{
if
(
mMeshLoaders
.
find
(
fileExtension
)
!=
mMeshLoaders
.
end
())
{
std
::
cout
<<
"Error: MeshManager::RegisterLoader() loader for extension
\"
"
<<
fileExtension
<<
"
\"
already registered"
<<
std
::
endl
;
return
false
;
}
mMeshLoaders
[
fileExtension
]
=
loaderFunction
;
return
true
;
}
shared_ptr
<
Mesh
>
MeshManager
::
LoadResource
(
const
std
::
string
&
resourceFile
,
const
std
::
string
&
resourceName
)
{
//Determine the extension and load accordingly
u16
lastDot
=
0
;
u16
extensionLength
=
0
;
std
::
string
extension
;
lastDot
=
(
u16
)
resourceFile
.
find_last_of
(
"."
);
lastDot
++
;
//Want the first pos past the dot
extensionLength
=
(
u16
)
resourceFile
.
size
()
-
lastDot
;
extension
=
resourceFile
.
substr
(
lastDot
,
extensionLength
);
//Must load
std
::
map
<
std
::
string
,
MeshLoaderFunc
>::
iterator
eit
=
mMeshLoaders
.
find
(
extension
);
if
(
eit
==
mMeshLoaders
.
end
())
{
std
::
cout
<<
"Error: MeshManager::LoadResource() No Loader found for extension
\"
"
<<
extension
<<
"
\"
for file
\"
"
<<
resourceFile
<<
"
\"\n
"
;
return
shared_ptr
<
Mesh
>
();
}
else
{
Mesh
*
resource
=
(
eit
->
second
)(
resourceFile
,
mFileSystem
);
return
shared_ptr
<
Mesh
>
(
resource
);
}
return
shared_ptr
<
Mesh
>
();
}
shared_ptr
<
Mesh
>
MeshManager
::
CreateMesh
(
const
std
::
string
&
resourceName
,
const
std
::
string
&
resourceGroup
/*=EResourceGroups::DEFAULT_RESOURCE_GROUP*/
)
{
Mesh
*
mesh
=
new
Mesh
();
mesh
->
SetName
(
resourceName
);
ResourceManager
<
Mesh
>::
AddResource
(
resourceName
,
mesh
,
resourceGroup
);
return
ResourceManager
<
Mesh
>::
GetResource
(
resourceName
);
}
Mesh
*
MeshManager
::
Load3DS
(
const
std
::
string
&
resourceFile
,
FileSystem
&
fileSystem
)
{
Mesh
*
mesh
=
new
Mesh
();
mesh
->
SetName
(
resourceFile
);
shared_ptr
<
SubMesh
>
subMesh
=
mesh
->
CreateSubMesh
();
shared_ptr
<
std
::
vector
<
Vector3
>
>
normals
(
new
std
::
vector
<
Vector3
>
);
shared_ptr
<
std
::
vector
<
Vector3
>
>
vertices
(
new
std
::
vector
<
Vector3
>
);
shared_ptr
<
std
::
vector
<
VertexColour
>
>
colours
(
new
std
::
vector
<
VertexColour
>
);
shared_ptr
<
std
::
vector
<
u16
>
>
indices
(
new
std
::
vector
<
u16
>
);
std
::
vector
<
std
::
vector
<
TextureUV
>
>
textureCoordinates
;
File
file
=
fileSystem
.
Open
(
resourceFile
);
if
(
!
file
.
IsOpen
())
{
return
0
;
}
Read3DSFile
(
file
,
*
vertices
,
*
indices
,
textureCoordinates
);
shared_ptr
<
std
::
vector
<
shared_ptr
<
std
::
vector
<
TextureUV
>
>
>
>
textureCoordinatesConverted
(
new
std
::
vector
<
shared_ptr
<
std
::
vector
<
TextureUV
>
>
>
);
for
(
u32
t
=
0
;
t
<
textureCoordinates
.
size
();
++
t
)
{
shared_ptr
<
std
::
vector
<
TextureUV
>
>
texSet
(
new
std
::
vector
<
TextureUV
>
(
textureCoordinates
[
t
]));
textureCoordinatesConverted
->
push_back
(
texSet
);
}
colours
->
reserve
(
vertices
->
size
());
colours
->
clear
();
subMesh
->
SetVertices
(
vertices
);
subMesh
->
SetNormals
(
normals
);
subMesh
->
SetIndices
(
indices
);
subMesh
->
SetTextureCoordinateSets
(
textureCoordinatesConverted
);
for
(
u32
c
=
0
;
c
<
vertices
->
size
();
++
c
)
{
colours
->
push_back
(
Colour
());
++
c
;
}
subMesh
->
SetColours
(
colours
);
subMesh
->
CalculateAABB
();
subMesh
->
CalculateNormals
();
mesh
->
CalculateAABB
();
return
mesh
;
}
Mesh
*
MeshManager
::
LoadMesh
(
const
std
::
string
&
resourceFile
,
FileSystem
&
fileSystem
,
MaterialManager
&
materialManager
,
SkeletonManager
&
skeletonManager
)
{
File
file
=
fileSystem
.
Open
(
resourceFile
);
if
(
!
file
.
IsOpen
())
{
return
0
;
}
Mesh
*
mesh
=
ReadMeshFile
(
file
,
materialManager
,
skeletonManager
);
return
mesh
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Wed, Jan 15, 10:53 PM (5 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72079
Default Alt Text
MeshManager.cpp (4 KB)
Attached To
Mode
rEE Echo 3
Attached
Detach File
Event Timeline
Log In to Comment